nagira 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b32464392f51f1065c6ee7bd2ac934e8d7c40a64
|
4
|
+
data.tar.gz: b3c93110e478dc8faf50b4bc1df85bd1ee47bb0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 649894c5163069cda283b7cf89233fff1a2d8cdb92c4edd95621837a535d9b24af7e040d78fb415e6f11b3f0682940926fbb9281f4c6a69f965261b321060f22
|
7
|
+
data.tar.gz: 570a91ef223e5b5a688dbbddcbc3a10cd027f74ba105b8dd2612eb6152e856d7b1cceda62ffdcaf80b71798ba68b10b4db26e8cb87f4580bd34b614f5b9161ac
|
data/History.md
CHANGED
@@ -1,19 +1,27 @@
|
|
1
|
+
### v0.5.2
|
2
|
+
* Tue Jan 19 2016 -- Dmytro Kovalov
|
3
|
+
- More refactoring
|
4
|
+
|
1
5
|
### v0.5.1
|
2
6
|
* Thr Dec 17 2015 -- Dmytro Kovalov
|
3
7
|
- Refactored parsing of Nagios files, cleaner code. No functionality changes.
|
8
|
+
|
4
9
|
### v0.5.0
|
5
10
|
* Fri Nov 6 2015 -- Dmytro Kovalov
|
6
11
|
- Add support for Service groups, new route `GET /_status/_servicegroup/`
|
7
12
|
- Vagrantfile for Ubuntu development
|
13
|
+
|
8
14
|
### v0.4.2
|
9
15
|
* Fri Oct 9 2015 -- Dmytro Kovalov
|
10
16
|
- init.d script fixes
|
11
17
|
- Fix for not detecting PID of the running Nagira, when ruby bibnary
|
12
18
|
is like ruby1.9.1
|
13
19
|
- Sanity checks for nagios user and log directory
|
20
|
+
|
14
21
|
### v0.4.1
|
15
22
|
* Wed Dec 26 2014 -- Dmytro Kovalov (birthday release, NAgira tuns 4 years today :)
|
16
23
|
- Added puma support, puma is default now
|
24
|
+
|
17
25
|
### v0.4.0
|
18
26
|
* Wed Dec 24 2014 -- Dmytro Kovalov
|
19
27
|
- new API endpoints
|
@@ -45,6 +53,7 @@
|
|
45
53
|
* July, Aug 2013
|
46
54
|
- RSpec tests
|
47
55
|
- GET endpoints checks
|
56
|
+
|
48
57
|
### v.0.2.10, v.0.2.11
|
49
58
|
* Fri Jul 19 2013 -- Dmytro Kovalov
|
50
59
|
- Gem versions fixes in gemspec, use gemspec in Gemfile.
|
@@ -56,8 +65,10 @@
|
|
56
65
|
- JSON structure specs for PUT methods
|
57
66
|
- return 400 if PUT not success
|
58
67
|
- specs for nagios.cmd writes
|
68
|
+
|
59
69
|
### v.0.2.9
|
60
70
|
- Bugfix - library loading order change, for @format processing in PUT routes.
|
71
|
+
|
61
72
|
### v.0.2.8
|
62
73
|
* Wed Apr 17 2013 -- Dmytro Kovalov
|
63
74
|
- Bugfix - prevent tracedumps on nonexisting hostnames
|
data/Rakefile
CHANGED
data/app/app.rb
CHANGED
@@ -1,326 +1,72 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# @overload get "$1"
|
4
|
-
#
|
5
|
-
# @return HTTP response. Hash formatted in the format defined by
|
6
|
-
# requested output type(XML, YAML or JSON).
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# @!macro [new] type
|
11
|
-
# @param [String] :type Type is one of Nagios objects like hosts, hostgroupsroups, etc.
|
12
|
-
#
|
13
|
-
# @!macro [new] name
|
14
|
-
# @param [String] :name
|
15
|
-
#
|
16
|
-
# @!macro [new] hostname
|
17
|
-
# @param [String] :hostname Configured Nagios hostname
|
18
|
-
#
|
19
|
-
# @!macro [new] service_name
|
20
|
-
# @param [String] :service_name Configured Nagios service for the host
|
21
|
-
#
|
22
|
-
# @!macro [new] accepted
|
23
|
-
#
|
24
|
-
# <b>Accepted output type modifiers:</b>
|
25
|
-
#
|
26
|
-
# @!macro [new] list
|
27
|
-
#
|
28
|
-
# - +/_list+ : Short list of available objects, depending on the
|
29
|
-
# current request context: hosts, services, etc.
|
30
|
-
#
|
31
|
-
# @!macro [new] state
|
32
|
-
#
|
33
|
-
# - +/_state+ - Instead of full status information send only
|
34
|
-
# current state. For hosts up/down, for services OK, Warn,
|
35
|
-
# Critical, Unknown (0,1,2-1)
|
36
|
-
#
|
37
|
-
# @!macro [new] full
|
38
|
-
#
|
39
|
-
# - +/_full+ - Show full status information. When used in
|
40
|
-
# /_status/_full call will display full hoststaus and
|
41
|
-
# servicestatus information for each host.
|
42
|
-
#
|
43
|
-
#
|
1
|
+
require 'active_model/serialization'
|
2
|
+
require 'active_model/serializers/xml' # for Hash.to_xml
|
44
3
|
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'active_support/inflector/inflections'
|
6
|
+
require 'active_support/core_ext/hash/slice' # for Hash.slice
|
45
7
|
|
8
|
+
require 'json'
|
9
|
+
require 'yaml'
|
10
|
+
require 'fileutils'
|
11
|
+
require 'sinatra/base'
|
12
|
+
require 'sinatra/reloader'
|
13
|
+
require 'singleton'
|
14
|
+
|
15
|
+
require_relative "../config/defaults"
|
16
|
+
require_relative "../config/environment"
|
17
|
+
|
18
|
+
Dir.glob("#{Nagira::BASE}/lib/**/*.rb").each { |file| require file }
|
19
|
+
Dir.glob("#{Nagira::BASE}/app/{loggers,parsers,concerns,helpers}/*.rb").each { |file| require file }
|
20
|
+
|
21
|
+
Dir.glob("#{Nagira::BASE}/app/**/*.rb").each { |file| require file }
|
46
22
|
|
47
|
-
require 'nagira'
|
48
23
|
|
49
24
|
##
|
50
25
|
# Main class of Nagira application implementing RESTful API for
|
51
26
|
# Nagios.
|
52
27
|
#
|
53
28
|
class Nagira < Sinatra::Base
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
##
|
58
|
-
# Do some necessary tasks at start and then run Sinatra app.
|
59
|
-
#
|
60
|
-
# @method startup_configuration
|
61
|
-
# @overload before("Initial Config")
|
62
|
-
configure do
|
63
|
-
|
64
|
-
Parser.config = settings.nagios_cfg
|
65
|
-
Parser.status = settings.status_cfg || Parser.config.status_file
|
66
|
-
Parser.objects = settings.objects_cfg || Parser.config.object_cache_file
|
67
|
-
Parser.commands = settings.command_file || Parser.config.command_file
|
68
|
-
|
69
|
-
BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
|
70
|
-
BackgroundParser.start = ::DEFAULT[:start_background_parser]
|
71
|
-
|
72
|
-
Logger.log "Starting Nagira application"
|
73
|
-
Logger.log "Version #{Nagira::VERSION}"
|
74
|
-
Logger.log "Running in #{Nagira.settings.environment} environment"
|
75
|
-
|
76
|
-
Parser.state.to_h.keys.each do |x|
|
77
|
-
Logger.log "Using nagios #{x} file: #{Parser.state[x].path}"
|
78
|
-
end
|
79
|
-
|
80
|
-
BackgroundParser.run if BackgroundParser.configured?
|
81
|
-
end
|
82
|
-
|
83
|
-
|
84
|
-
##
|
85
|
-
# Parse nagios files.
|
86
|
-
#
|
87
|
-
# Note: *.parse methods are monkey-patched here (if you have required
|
88
|
-
# 'lib/nagira' above) to set min parsing interval to avoid file paring
|
89
|
-
# on each HTTP request. File is parsed only if it was changed and if
|
90
|
-
# it was parsed more then 60 (default) seconds ago. See
|
91
|
-
# +lib/nagira/timed_parse.rb+ for mor more info.
|
92
|
-
#
|
93
|
-
# In development mode use files located under +./test/data+
|
94
|
-
# directory. This allows to do development on the host where Nagios is
|
95
|
-
# notinstalled. If you want to change this edit configuration in
|
96
|
-
# config/environment.rb file.
|
97
|
-
#
|
98
|
-
# See also comments in config/default.rb file regarding nagios_cfg,
|
99
|
-
# status_cfg, objects_cfg.
|
100
|
-
#
|
101
|
-
# @method parse_nagios_files
|
102
|
-
# @overload before("Parse Nagios files")
|
103
|
-
before do
|
104
|
-
Logger.log("BackgroundParser is not running", :warning) if
|
105
|
-
BackgroundParser.configured? && BackgroundParser.dead?
|
106
|
-
|
107
|
-
Parser.parse
|
108
|
-
|
109
|
-
@status = Parser.status['hosts']
|
110
|
-
@objects = Parser.objects
|
111
|
-
|
112
|
-
#
|
113
|
-
# Add plural keys to use ActiveResource with Nagira
|
114
|
-
#
|
115
|
-
@objects.keys.each do |singular|
|
116
|
-
@objects[singular.to_s.pluralize.to_sym] = @objects[singular]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
##
|
121
|
-
# @method clear_instance_data
|
122
|
-
# @overload before("clear data")
|
123
|
-
#
|
124
|
-
# Clear values onf instance variables before start.
|
125
|
-
#
|
126
|
-
before do
|
127
|
-
@data = []
|
128
|
-
@format = @output = nil
|
129
|
-
end
|
130
|
-
|
131
|
-
##
|
132
|
-
# @method strip_extensions
|
133
|
-
# @overload before("detect format")
|
134
|
-
#
|
135
|
-
# Detect and strip output format extension
|
136
|
-
#
|
137
|
-
# Strip extension (@format) from HTTP route and set it as instance
|
138
|
-
# variable @format. Valid formats are .xml, .json, .yaml. If format
|
139
|
-
# is not specified, it is set to default format
|
140
|
-
# (Nagira.settings.format).
|
29
|
+
# @!macro [attach] sinatra.get
|
141
30
|
#
|
142
|
-
#
|
31
|
+
# @overload get "$1"
|
143
32
|
#
|
144
|
-
#
|
33
|
+
# @return HTTP response. Hash formatted in the format defined by
|
34
|
+
# requested output type(XML, YAML or JSON).
|
145
35
|
#
|
146
|
-
# GET /_objects # => default format
|
147
|
-
# GET /_objects.json # => :json
|
148
|
-
# GET /_status/_list.yaml # => :yaml
|
149
36
|
#
|
150
|
-
before do
|
151
|
-
request.path_info.sub!(/#{settings.format_extensions}/, '')
|
152
|
-
@format = ($1 || settings.format).to_sym
|
153
|
-
content_type "application/#{@format.to_s}"
|
154
|
-
end
|
155
|
-
|
156
|
-
##
|
157
|
-
# @method detect_ar_type
|
158
|
-
# @overload before('detect ActiveResource mode')
|
159
37
|
#
|
160
|
-
#
|
38
|
+
# @!macro [new] type
|
39
|
+
# @param [String] :type Type is one of Nagios objects like hosts, hostgroupsroups, etc.
|
161
40
|
#
|
162
|
-
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
##
|
167
|
-
# @method strip_output_type
|
168
|
-
# @overload before('detect output mode')
|
41
|
+
# @!macro [new] name
|
42
|
+
# @param [String] :name
|
169
43
|
#
|
170
|
-
#
|
44
|
+
# @!macro [new] hostname
|
45
|
+
# @param [String] :hostname Configured Nagios hostname
|
171
46
|
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
# (+/list, +/state+, +/full+ routes).
|
47
|
+
# @!macro [new] service_name
|
48
|
+
# @param [String] :service_name Configured Nagios service for the host
|
175
49
|
#
|
176
|
-
#
|
177
|
-
# route. If no output type specfied it is set to +:full+. Output
|
178
|
-
# mode can be followed by format extension (+.json+, +.xml+ or
|
179
|
-
# +.yaml+).
|
50
|
+
# @!macro [new] accepted
|
180
51
|
#
|
181
|
-
#
|
52
|
+
# <b>Accepted output type modifiers:</b>
|
182
53
|
#
|
183
|
-
#
|
184
|
-
# GET /_status/_state # => :state
|
185
|
-
# GET /_status/:hostname # => :full
|
186
|
-
# GET /_status # => :normal
|
54
|
+
# @!macro [new] list
|
187
55
|
#
|
188
|
-
|
189
|
-
|
190
|
-
@output = ($1 || :normal).to_sym
|
191
|
-
end
|
192
|
-
|
193
|
-
##
|
194
|
-
# @method find_jsonp_callback
|
195
|
-
# @overload before('find callback name')
|
56
|
+
# - +/_list+ : Short list of available objects, depending on the
|
57
|
+
# current request context: hosts, services, etc.
|
196
58
|
#
|
197
|
-
#
|
198
|
-
# variable. @callback variable is used if after method and prepends
|
199
|
-
# JSON data with callback function name.
|
59
|
+
# @!macro [new] state
|
200
60
|
#
|
201
|
-
#
|
61
|
+
# - +/_state+ - Instead of full status information send only
|
62
|
+
# current state. For hosts up/down, for services OK, Warn,
|
63
|
+
# Critical, Unknown (0,1,2-1)
|
202
64
|
#
|
203
|
-
#
|
65
|
+
# @!macro [new] full
|
204
66
|
#
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
before do
|
209
|
-
if @format == :json
|
210
|
-
['callback','jscallback','jsonp','jsoncallback'].each do |x|
|
211
|
-
@callback = params.delete(x) unless @callback
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
##
|
217
|
-
# @method object_not_found
|
218
|
-
# @overload after("Object not found or bad request")
|
219
|
-
#
|
220
|
-
# If result-set of object/status search is empty return HTTP 404 .
|
221
|
-
# This can happen when you are requesting status for not existing
|
222
|
-
# host and/or service.
|
223
|
-
#
|
224
|
-
#
|
225
|
-
after do
|
226
|
-
if ! @data || @data.empty?
|
227
|
-
halt [404, {
|
228
|
-
:message => "Object not found or bad request",
|
229
|
-
:error => "HTTP::Notfound"
|
230
|
-
}.send("to_#{@format}")
|
231
|
-
]
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
##
|
236
|
-
# @method argument_error
|
237
|
-
# @overload after("ArgumentError")
|
67
|
+
# - +/_full+ - Show full status information. When used in
|
68
|
+
# /_status/_full call will display full hoststaus and
|
69
|
+
# servicestatus information for each host.
|
238
70
|
#
|
239
|
-
# Return 400 if result of PUT operation is not success.
|
240
71
|
#
|
241
|
-
after do
|
242
|
-
return unless request.put?
|
243
|
-
halt [400, @data.send("to_#{@format}") ] if ! @data[:result]
|
244
|
-
end
|
245
|
-
|
246
|
-
##
|
247
|
-
# @method convert_to_active_resource
|
248
|
-
# @overload after("Return Array for ActiveResouce routes")
|
249
|
-
#
|
250
|
-
#
|
251
|
-
after do
|
252
|
-
@data = @data.values if @active_resource && @data.is_a?(Hash)
|
253
|
-
end
|
254
|
-
|
255
|
-
##
|
256
|
-
# @method return_jsonp_data
|
257
|
-
# @overload after("Return formatted data")
|
258
|
-
#
|
259
|
-
# If it's a JSON-P request, return its data with prepended @callback
|
260
|
-
# function name. JSONP request is detected by +before+ method.
|
261
|
-
#
|
262
|
-
# If no callback paramete given, then simply return formatted data
|
263
|
-
# as XML, JSON, or YAML in response body.
|
264
|
-
#
|
265
|
-
# = Example
|
266
|
-
#
|
267
|
-
# $ curl 'http://localhost:4567/?callback=test'
|
268
|
-
# test(["{\"application\":\"Nagira\",\"version\":\"0.1.3\",\"url\":\"http://dmytro.github.com/nagira/\"}"])
|
269
|
-
#
|
270
|
-
after do
|
271
|
-
body( @callback ? "#{@callback.to_s} (#{@data.to_json})" : @data.send("to_#{@format}") )
|
272
|
-
end
|
273
|
-
|
274
|
-
##
|
275
|
-
# @method get_api
|
276
|
-
# @overload get(/_api)
|
277
|
-
#
|
278
|
-
# Provide information about API routes
|
279
|
-
#
|
280
|
-
get "/_api" do
|
281
|
-
@data = self.api
|
282
|
-
nil
|
283
|
-
end
|
284
|
-
|
285
|
-
##
|
286
|
-
# @method get_runtime_config
|
287
|
-
# @overload get(/_runtime)
|
288
|
-
#
|
289
|
-
# Print out nagira runtime configuration
|
290
|
-
get "/_runtime" do
|
291
|
-
@data = {
|
292
|
-
application: self.class,
|
293
|
-
version: VERSION,
|
294
|
-
runtime: {
|
295
|
-
environment: Nagira.settings.environment,
|
296
|
-
home: ENV['HOME'],
|
297
|
-
user: ENV['LOGNAME'],
|
298
|
-
nagiosFiles: $nagios.to_h.keys.map { |x| { x => $nagios[x].path }}
|
299
|
-
}
|
300
|
-
}
|
301
|
-
nil
|
302
|
-
end
|
303
|
-
|
304
|
-
# @method get_slash
|
305
|
-
# @overload get(/)
|
306
|
-
#
|
307
|
-
# Returns application information: name, version, github repository.
|
308
|
-
get "/" do
|
309
|
-
@data = {
|
310
|
-
:application => self.class,
|
311
|
-
:version => VERSION,
|
312
|
-
:source => GITHUB,
|
313
|
-
:apiUrl => request.url.sub(/\/$/,'') + "/_api",
|
314
|
-
}
|
315
|
-
nil
|
316
|
-
end
|
317
|
-
# Other resources in parsed status file. Supported are => ["hosts",
|
318
|
-
# "info", "process", "contacts"]
|
319
|
-
# get "/:resource" do |resource|
|
320
|
-
# respond_with $nagios.status[resource], @format
|
321
|
-
# end
|
322
72
|
end
|
323
|
-
|
324
|
-
require "app/put/status"
|
325
|
-
require "app/put/host"
|
326
|
-
require "app/put"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module HostStatusNameConcerneable
|
2
|
+
|
3
|
+
attr_accessor :hostname, :service_name, :resource
|
4
|
+
|
5
|
+
def status
|
6
|
+
@status ||=
|
7
|
+
Nagira::HostStatusController.new(@nagios_status, hostname: hostname, output: :full)
|
8
|
+
.status[hostname][resource]
|
9
|
+
end
|
10
|
+
|
11
|
+
def with_service_name
|
12
|
+
if service_name
|
13
|
+
{ service_name => status[service_name] }
|
14
|
+
else
|
15
|
+
status
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|