nagira 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.md +11 -0
- data/Rakefile +1 -1
- data/app/app.rb +44 -298
- data/app/concerns/host_status_name_concerneable.rb +18 -0
- data/app/concerns/output_typeable.rb +22 -0
- data/app/controllers/api_help_controller.rb +25 -0
- data/app/controllers/host_status_controller.rb +82 -0
- data/{lib/nagira/hostgroup.rb → app/controllers/hostgroup_controller.rb} +1 -1
- data/app/controllers/resource_status_controler.rb +65 -0
- data/app/controllers/service_status_controler.rb +50 -0
- data/{lib/nagira/servicegroup.rb → app/controllers/servicegroups_controller.rb} +1 -1
- data/app/filters/after.rb +76 -0
- data/app/filters/before.rb +156 -0
- data/app/filters/configure.rb +34 -0
- data/app/helpers/put_helpers.rb +11 -0
- data/{lib/nagira → app/loggers}/simple_logger.rb +0 -0
- data/{lib/nagira → app/parsers}/background_parse.rb +0 -0
- data/{lib/nagira → app/parsers}/parser.rb +1 -9
- data/app/routes/get.rb +50 -0
- data/app/routes/get/config.rb +11 -0
- data/app/{get → routes/get}/objects.rb +4 -24
- data/app/routes/get/status.rb +98 -0
- data/app/{get → routes/get}/status/hostgroups.rb +3 -14
- data/app/routes/get/status/servicegroups.rb +16 -0
- data/app/{put → routes/put}/host.rb +1 -4
- data/app/{put → routes/put}/status.rb +13 -20
- data/app/writers/external_command_writer.rb +52 -0
- data/bin/nagira +1 -1
- data/config/defaults.rb +20 -12
- data/config/environment.rb +2 -1
- data/lib/nagira/timed_parse.rb +1 -1
- data/spec/01_background_parser.rb +5 -5
- data/spec/01_data_format/01_nagira_response_spec.rb +6 -6
- data/spec/01_data_format/02_0_status_spec.rb +1 -1
- data/spec/01_data_format/02_nagira_data_spec.rb +8 -8
- data/spec/01_data_format/03_api_spec.rb +3 -3
- data/spec/get/status/comments_spec.rb +2 -2
- data/spec/get/status/endpoints_spec.rb +11 -11
- data/spec/get/status/hostgroup.rb +3 -3
- data/spec/get/status/hosts_spec.rb +1 -1
- data/spec/get/status/servicegroup.rb +4 -4
- data/spec/get/status/services_spec.rb +1 -1
- data/spec/put/host_spec.rb +4 -4
- data/spec/put/status_spec.rb +5 -5
- data/spec/put/support.rb +3 -3
- data/spec/spec_helper.rb +3 -3
- data/version.txt +1 -1
- metadata +70 -60
- data/app/get/config.rb +0 -24
- data/app/get/status.rb +0 -189
- data/app/get/status/servicegroups.rb +0 -28
- data/app/put.rb +0 -53
- data/lib/nagira.rb +0 -67
@@ -0,0 +1,34 @@
|
|
1
|
+
class Nagira < Sinatra::Base
|
2
|
+
set :app_file, __FILE__
|
3
|
+
|
4
|
+
Dir["#{Nagira::BASE}/app/{parsers,writers,loggers}/*.rb"].each { |file| require file }
|
5
|
+
|
6
|
+
##
|
7
|
+
# Do some necessary tasks at start and then run Sinatra app.
|
8
|
+
#
|
9
|
+
# @method startup_configuration
|
10
|
+
# @overload before("Initial Config")
|
11
|
+
configure do
|
12
|
+
|
13
|
+
Parser.config = settings.nagios_cfg
|
14
|
+
Parser.status = settings.status_cfg || Parser.config.status_file
|
15
|
+
Parser.objects = settings.objects_cfg || Parser.config.object_cache_file
|
16
|
+
|
17
|
+
Writer.commands = settings.command_file || Parser.config.command_file
|
18
|
+
|
19
|
+
BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
|
20
|
+
BackgroundParser.start = ::DEFAULT[:start_background_parser]
|
21
|
+
|
22
|
+
Logger.log "Starting Nagira application"
|
23
|
+
Logger.log "Version #{Nagira::VERSION}"
|
24
|
+
Logger.log "Running in #{Nagira.settings.environment} environment"
|
25
|
+
|
26
|
+
Parser.state.to_h.keys.each do |x|
|
27
|
+
Logger.log "Using nagios #{x} file: #{Parser.state[x].path}"
|
28
|
+
end
|
29
|
+
Logger.log "Using nagios command file: #{Writer.commands.path}"
|
30
|
+
|
31
|
+
BackgroundParser.run if BackgroundParser.configured?
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Nagira < Sinatra::Base
|
2
|
+
|
3
|
+
|
4
|
+
# Small helper to submit data to ::Nagios::ExternalCommands
|
5
|
+
# object. For status updates sends external coond via
|
6
|
+
# ::Nagios::ExternalCommands.send method.
|
7
|
+
def update_service_status params
|
8
|
+
Writer.new(:PROCESS_SERVICE_CHECK_RESULT).put params
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
File without changes
|
File without changes
|
@@ -8,7 +8,6 @@ class Nagira < Sinatra::Base
|
|
8
8
|
# Parser.config = < path to nagios.cfg file>
|
9
9
|
# Parser.status = < path to status.cfg file>
|
10
10
|
# Parser.objects = < path to object_cache file>
|
11
|
-
# Parser.commands = < path to the external command execution file >
|
12
11
|
#
|
13
12
|
class Parser
|
14
13
|
include Singleton
|
@@ -95,16 +94,9 @@ class Nagira < Sinatra::Base
|
|
95
94
|
def objects
|
96
95
|
state
|
97
96
|
.send(with_inflight?(:objects))
|
98
|
-
.objects
|
97
|
+
.objects || []
|
99
98
|
end
|
100
99
|
|
101
|
-
def commands=(commands_file)
|
102
|
-
state.commands = Nagios::ExternalCommands.new(commands_file)
|
103
|
-
end
|
104
|
-
|
105
|
-
def commands
|
106
|
-
state.commands
|
107
|
-
end
|
108
100
|
end
|
109
101
|
end
|
110
102
|
end
|
data/app/routes/get.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Nagira < Sinatra::Base
|
2
|
+
|
3
|
+
##
|
4
|
+
# @method get_api
|
5
|
+
# @overload get(/_api)
|
6
|
+
#
|
7
|
+
# Provide information about API routes
|
8
|
+
#
|
9
|
+
get "/_api" do
|
10
|
+
ApiHelpController.get
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# @method get_runtime_config
|
15
|
+
# @overload get(/_runtime)
|
16
|
+
#
|
17
|
+
# Print out nagira runtime configuration
|
18
|
+
get "/_runtime" do
|
19
|
+
{
|
20
|
+
application: self.class,
|
21
|
+
version: VERSION,
|
22
|
+
runtime: {
|
23
|
+
environment: Nagira.settings.environment,
|
24
|
+
home: ENV['HOME'],
|
25
|
+
user: ENV['LOGNAME'],
|
26
|
+
nagiosFiles: Parser.state.to_h.keys.map { |x| { x: Parser.state.to_h[x].path }},
|
27
|
+
commandFile: Writer.commands.path
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
# @method get_slash
|
33
|
+
# @overload get(/)
|
34
|
+
#
|
35
|
+
# Returns application information: name, version, github repository.
|
36
|
+
get "/" do
|
37
|
+
{
|
38
|
+
:application => self.class,
|
39
|
+
:version => VERSION,
|
40
|
+
:source => GITHUB,
|
41
|
+
:apiUrl => request.url.sub(/\/$/,'') + "/_api",
|
42
|
+
}
|
43
|
+
end
|
44
|
+
# Other resources in parsed status file. Supported are => ["hosts",
|
45
|
+
# "info", "process", "contacts"]
|
46
|
+
# get "/:resource" do |resource|
|
47
|
+
# respond_with $nagios.status[resource], @format
|
48
|
+
# end
|
49
|
+
|
50
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class Nagira < Sinatra::Base
|
2
|
+
include OutputTypeable
|
2
3
|
#
|
3
4
|
# Objects routes
|
4
5
|
# ============================================================
|
5
|
-
|
6
6
|
##
|
7
7
|
# @method get_objects
|
8
8
|
#
|
@@ -14,13 +14,7 @@ class Nagira < Sinatra::Base
|
|
14
14
|
# @macro list
|
15
15
|
#
|
16
16
|
get "/_objects" do
|
17
|
-
|
18
|
-
@data = begin
|
19
|
-
@output == :list ? @objects.keys : @objects
|
20
|
-
rescue NoMethodError
|
21
|
-
nil
|
22
|
-
end
|
23
|
-
nil
|
17
|
+
body_with_list @objects
|
24
18
|
end
|
25
19
|
|
26
20
|
##
|
@@ -34,15 +28,7 @@ class Nagira < Sinatra::Base
|
|
34
28
|
#
|
35
29
|
#
|
36
30
|
get "/_objects/:type" do |type|
|
37
|
-
|
38
|
-
@data = @objects[type.to_sym]
|
39
|
-
@data = @data.keys if @output == :list
|
40
|
-
|
41
|
-
rescue NoMethodError
|
42
|
-
nil
|
43
|
-
end
|
44
|
-
|
45
|
-
nil
|
31
|
+
body_with_list @objects[type.to_sym]
|
46
32
|
end
|
47
33
|
|
48
34
|
##
|
@@ -57,12 +43,6 @@ class Nagira < Sinatra::Base
|
|
57
43
|
# * none
|
58
44
|
#
|
59
45
|
get "/_objects/:type/:name" do |type,name|
|
60
|
-
|
61
|
-
@data = @objects[type.to_sym][name]
|
62
|
-
rescue NoMethodError
|
63
|
-
nil
|
64
|
-
end
|
65
|
-
|
66
|
-
nil
|
46
|
+
@objects[type.to_sym][name]
|
67
47
|
end
|
68
48
|
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require_relative 'status/hostgroups'
|
2
|
+
require_relative 'status/servicegroups'
|
3
|
+
|
4
|
+
class Nagira < Sinatra::Base
|
5
|
+
|
6
|
+
##
|
7
|
+
# @method get_status_hostname_services_service_name
|
8
|
+
# @overload get("/_status/:hostname/_services/:service_name")
|
9
|
+
#
|
10
|
+
# @!macro hostname
|
11
|
+
# @!macro service_name
|
12
|
+
#
|
13
|
+
# Full or short status information for single service on single
|
14
|
+
# host.
|
15
|
+
#
|
16
|
+
# @!macro accepted
|
17
|
+
# @!macro state
|
18
|
+
#
|
19
|
+
get "/_status/:hostname/_services/:service_name" do |hostname,service|
|
20
|
+
ServiceStatusController.new(
|
21
|
+
@status, hostname: hostname, service_name: service, output: @output
|
22
|
+
).get
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# @method get_status_hostname_services
|
27
|
+
# @!macro hostname
|
28
|
+
#
|
29
|
+
# Endpoints:
|
30
|
+
# - GET /_status/:hostname/_services
|
31
|
+
# - GET /_status/:hostname/_hostcomments
|
32
|
+
# - GET /_status/:hostname/_servicecomments
|
33
|
+
#
|
34
|
+
# Read +services+, +hostcomments+ or +servicecomments+ for single
|
35
|
+
# host.
|
36
|
+
#
|
37
|
+
# @!macro accepted
|
38
|
+
# @!macro state
|
39
|
+
# @!macro list
|
40
|
+
# @!macro full
|
41
|
+
get %r{^/_status/(?<hostname>#{hostname_regex})/_(?<resource>(services|hostcomments|servicecomments))$} do |hostname,resource|
|
42
|
+
|
43
|
+
# hostname = hostname.to_i if hostname =~ /^\d+$/
|
44
|
+
ResourceStatusController.new(
|
45
|
+
@status, hostname: hostname, output: @output, resource: resource
|
46
|
+
).get
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# @method get_status
|
52
|
+
#
|
53
|
+
# Return all hosts status.
|
54
|
+
#
|
55
|
+
# If no output modifier provided, outputs full hosttatus information
|
56
|
+
# for each host. Not including services information. When +_full+
|
57
|
+
# modifier is provided data include hoststatus, servicestatus and
|
58
|
+
# all comments (servicecomments and hostcomments) for hosts.
|
59
|
+
#
|
60
|
+
# Alias: get /_status is the same thing as get /_status/_hosts with
|
61
|
+
# ActiveResource compatibility, i.e. for */_hosts request Nagira
|
62
|
+
# returns array instead of hash.
|
63
|
+
#
|
64
|
+
# @!macro accepted
|
65
|
+
# @!macro state
|
66
|
+
# @!macro list
|
67
|
+
# @!macro full
|
68
|
+
#
|
69
|
+
# Support for (see API):
|
70
|
+
# - plural resources: N/A
|
71
|
+
# - object access by ID: N/A
|
72
|
+
|
73
|
+
get %r{^/_status(/_hosts)?$} do
|
74
|
+
HostStatusController.new(@status, output: @output).get
|
75
|
+
end
|
76
|
+
|
77
|
+
# Hoststatus for single host or all services.
|
78
|
+
#
|
79
|
+
# @method get_status_hostname
|
80
|
+
#
|
81
|
+
# Endpoint
|
82
|
+
#
|
83
|
+
# - get "/_status/:hostname"
|
84
|
+
#
|
85
|
+
# @!macro hostname
|
86
|
+
#
|
87
|
+
# @!macro accepted
|
88
|
+
# @!macro state
|
89
|
+
#
|
90
|
+
# Support for:
|
91
|
+
# - plural resources: N/A
|
92
|
+
# - object access by ID: NO (TODO)
|
93
|
+
|
94
|
+
get %r{^/_status/(?<hostname>#{hostname_regex})$} do |hostname|
|
95
|
+
HostStatusController.new(@status, output: @output, hostname: hostname).get
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -1,12 +1,4 @@
|
|
1
1
|
class Nagira < Sinatra::Base
|
2
|
-
# @!macro [attach] sinatra.get
|
3
|
-
#
|
4
|
-
# @overload get "$1"
|
5
|
-
#
|
6
|
-
# @return HTTP response. Hash formatted in the format defined by
|
7
|
-
# requested output type(XML, YAML or JSON).
|
8
|
-
#
|
9
|
-
|
10
2
|
|
11
3
|
##
|
12
4
|
# @method get_status_hostgroup
|
@@ -15,8 +7,7 @@ class Nagira < Sinatra::Base
|
|
15
7
|
# service status.
|
16
8
|
#
|
17
9
|
get "/_status/_hostgroup/:hostgroup" do |hostgroup|
|
18
|
-
|
19
|
-
nil
|
10
|
+
HostgroupController.new(hostgroup).full
|
20
11
|
end
|
21
12
|
|
22
13
|
##
|
@@ -26,8 +17,7 @@ class Nagira < Sinatra::Base
|
|
26
17
|
# - GET /_status/_hostgroup/:hostgroup/_service
|
27
18
|
#
|
28
19
|
get "/_status/_hostgroup/:hostgroup/_service" do |hostgroup|
|
29
|
-
|
30
|
-
nil
|
20
|
+
HostgroupController.new(hostgroup).service_status
|
31
21
|
end
|
32
22
|
|
33
23
|
##
|
@@ -37,7 +27,6 @@ class Nagira < Sinatra::Base
|
|
37
27
|
# Endpoint:
|
38
28
|
# - GET /_status/_hostgroup/:host
|
39
29
|
get "/_status/_hostgroup/:hostgroup/_host" do |hostgroup|
|
40
|
-
|
41
|
-
nil
|
30
|
+
HostgroupController.new(hostgroup).host_status
|
42
31
|
end
|
43
32
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Nagira < Sinatra::Base
|
2
|
+
|
3
|
+
##
|
4
|
+
# @method get_status_servicegroup
|
5
|
+
#
|
6
|
+
# Endpoint:
|
7
|
+
# - GET /_status/_servicegroup/:servicegroup
|
8
|
+
#
|
9
|
+
# Supported extensions: _state, _list
|
10
|
+
#
|
11
|
+
|
12
|
+
get "/_status/_servicegroup/:servicegroup" do |group_name|
|
13
|
+
ServicegroupController.new(group_name).send(@output)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -33,10 +33,7 @@ class Nagira < Sinatra::Base
|
|
33
33
|
# "plugin_output" : "ping OK"
|
34
34
|
# }
|
35
35
|
put "/_status/:host_name" do
|
36
|
-
|
37
|
-
'host_name' => params['host_name']
|
38
|
-
})
|
39
|
-
nil
|
36
|
+
HostStatusController.new({}, hostname: params['host_name']).put(@input.first)
|
40
37
|
end
|
41
38
|
|
42
39
|
# Same as /_status/:host_name (Not implemented)
|
@@ -51,38 +51,32 @@ class Nagira < Sinatra::Base
|
|
51
51
|
# ]
|
52
52
|
#
|
53
53
|
#
|
54
|
-
put "/_status/:host_name/_services" do
|
54
|
+
put "/_status/:host_name/_services" do |hostname|
|
55
55
|
|
56
56
|
data, result = [], true
|
57
57
|
|
58
58
|
@input.each do |datum|
|
59
59
|
# FIXME - this calls update for each service. Should be batching them together
|
60
60
|
update = update_service_status(
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
datum.merge({
|
62
|
+
'host_name' => hostname
|
63
|
+
})
|
64
64
|
)
|
65
65
|
data << update[:object].first
|
66
66
|
result &&= update[:result]
|
67
67
|
end
|
68
|
-
|
69
|
-
nil
|
70
|
-
|
68
|
+
{ result: result, object: data }
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
# Update single service on a single host by JSON data.
|
74
|
-
put "/_status/:host_name/_services/:service_description" do
|
75
|
-
|
76
|
-
|
77
|
-
'service_description' => params['service_description'],
|
78
|
-
'host_name' => params['host_name']
|
79
|
-
})
|
80
|
-
nil
|
72
|
+
put "/_status/:host_name/_services/:service_description" do |hostname,service|
|
73
|
+
ServiceStatusController.new({ }, hostname: hostname, service_name: service)
|
74
|
+
.put @input.first
|
81
75
|
end
|
82
76
|
|
83
77
|
# @method put_status_as_http_parms
|
84
78
|
# @overload put(/_status/:host_name/_services/:service_description/_return_code/:return_code/_plugin_output/:plugin_output)
|
85
|
-
#
|
79
|
+
#
|
86
80
|
# Update single service status on a single host. Use data provided
|
87
81
|
# in RESTful path as parameters.
|
88
82
|
#
|
@@ -90,9 +84,8 @@ class Nagira < Sinatra::Base
|
|
90
84
|
# curl -d "test data" \
|
91
85
|
# -X PUT http://localhost:4567/_status/viy/_services/PING/_return_code/0/_plugin_output/OK
|
92
86
|
# # => ok
|
93
|
-
put "/_status/:host_name/_services/:service_description/_return_code/:return_code/_plugin_output/:plugin_output" do
|
94
|
-
|
95
|
-
nil
|
87
|
+
put "/_status/:host_name/_services/:service_description/_return_code/:return_code/_plugin_output/:plugin_output" do
|
88
|
+
update_service_status params
|
96
89
|
end
|
97
|
-
|
90
|
+
|
98
91
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Nagira < Sinatra::Base
|
2
|
+
# Singleton class. Handles updates to the of the Nagios external commands file.
|
3
|
+
#
|
4
|
+
# Example usage:
|
5
|
+
#
|
6
|
+
# Writer.commands = <path to the external command execution file>
|
7
|
+
#
|
8
|
+
class Writer
|
9
|
+
|
10
|
+
@@commands = nil
|
11
|
+
|
12
|
+
def initialize(action)
|
13
|
+
@action = action
|
14
|
+
end
|
15
|
+
|
16
|
+
# Send PUT update to Nagios::ExternalCommands
|
17
|
+
#
|
18
|
+
# @param action [Symbol] Nagios external command name
|
19
|
+
# @param params [Hash] Parsed Hash from PUT request input.
|
20
|
+
#
|
21
|
+
# FIXME: This only accepts single service. Modify to use Arrays too
|
22
|
+
def put(params)
|
23
|
+
|
24
|
+
res = @@commands.write(
|
25
|
+
params.merge({ :action => @action })
|
26
|
+
)
|
27
|
+
{ :result => res[:result], :object => res[:data]}
|
28
|
+
end
|
29
|
+
|
30
|
+
def write
|
31
|
+
end
|
32
|
+
|
33
|
+
# State structure keep all the Nagios parsed state information for
|
34
|
+
# :objects, :status, :config, etc. as well as "inflight" data.
|
35
|
+
attr_accessor :state
|
36
|
+
|
37
|
+
class << self
|
38
|
+
|
39
|
+
def commands=(commands_file)
|
40
|
+
@@commands = Nagios::ExternalCommands.new(commands_file)
|
41
|
+
end
|
42
|
+
|
43
|
+
def commands
|
44
|
+
@@commands
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|