nagira 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +11 -0
  3. data/Rakefile +1 -1
  4. data/app/app.rb +44 -298
  5. data/app/concerns/host_status_name_concerneable.rb +18 -0
  6. data/app/concerns/output_typeable.rb +22 -0
  7. data/app/controllers/api_help_controller.rb +25 -0
  8. data/app/controllers/host_status_controller.rb +82 -0
  9. data/{lib/nagira/hostgroup.rb → app/controllers/hostgroup_controller.rb} +1 -1
  10. data/app/controllers/resource_status_controler.rb +65 -0
  11. data/app/controllers/service_status_controler.rb +50 -0
  12. data/{lib/nagira/servicegroup.rb → app/controllers/servicegroups_controller.rb} +1 -1
  13. data/app/filters/after.rb +76 -0
  14. data/app/filters/before.rb +156 -0
  15. data/app/filters/configure.rb +34 -0
  16. data/app/helpers/put_helpers.rb +11 -0
  17. data/{lib/nagira → app/loggers}/simple_logger.rb +0 -0
  18. data/{lib/nagira → app/parsers}/background_parse.rb +0 -0
  19. data/{lib/nagira → app/parsers}/parser.rb +1 -9
  20. data/app/routes/get.rb +50 -0
  21. data/app/routes/get/config.rb +11 -0
  22. data/app/{get → routes/get}/objects.rb +4 -24
  23. data/app/routes/get/status.rb +98 -0
  24. data/app/{get → routes/get}/status/hostgroups.rb +3 -14
  25. data/app/routes/get/status/servicegroups.rb +16 -0
  26. data/app/{put → routes/put}/host.rb +1 -4
  27. data/app/{put → routes/put}/status.rb +13 -20
  28. data/app/writers/external_command_writer.rb +52 -0
  29. data/bin/nagira +1 -1
  30. data/config/defaults.rb +20 -12
  31. data/config/environment.rb +2 -1
  32. data/lib/nagira/timed_parse.rb +1 -1
  33. data/spec/01_background_parser.rb +5 -5
  34. data/spec/01_data_format/01_nagira_response_spec.rb +6 -6
  35. data/spec/01_data_format/02_0_status_spec.rb +1 -1
  36. data/spec/01_data_format/02_nagira_data_spec.rb +8 -8
  37. data/spec/01_data_format/03_api_spec.rb +3 -3
  38. data/spec/get/status/comments_spec.rb +2 -2
  39. data/spec/get/status/endpoints_spec.rb +11 -11
  40. data/spec/get/status/hostgroup.rb +3 -3
  41. data/spec/get/status/hosts_spec.rb +1 -1
  42. data/spec/get/status/servicegroup.rb +4 -4
  43. data/spec/get/status/services_spec.rb +1 -1
  44. data/spec/put/host_spec.rb +4 -4
  45. data/spec/put/status_spec.rb +5 -5
  46. data/spec/put/support.rb +3 -3
  47. data/spec/spec_helper.rb +3 -3
  48. data/version.txt +1 -1
  49. metadata +70 -60
  50. data/app/get/config.rb +0 -24
  51. data/app/get/status.rb +0 -189
  52. data/app/get/status/servicegroups.rb +0 -28
  53. data/app/put.rb +0 -53
  54. data/lib/nagira.rb +0 -67
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ba8c40871546f2f94e517df75a4041fc2e439c0
4
- data.tar.gz: 609ac720f528e5d0a5d6a74eaf9d85ff9d215c4d
3
+ metadata.gz: b32464392f51f1065c6ee7bd2ac934e8d7c40a64
4
+ data.tar.gz: b3c93110e478dc8faf50b4bc1df85bd1ee47bb0f
5
5
  SHA512:
6
- metadata.gz: e34c0161e542e366e1a3d0f83f53938c2a4d914c3a8aa8b81ac1503e21930374bee6f20901dafe59ea4cb20a017cd8865b7d7734a2f152135ef88899f544aaf8
7
- data.tar.gz: c90991ee5002abc1d42625385765fc3acadb521027f1bf6cabe976cf1f9422f344eeeb2389159a5c5e183625564ab3a8485dd372b449bc31be5f3c88df06e7e0
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
@@ -1,5 +1,5 @@
1
1
  require 'fileutils'
2
- require_relative 'lib/nagira'
2
+ require_relative 'app/app'
3
3
 
4
4
  Dir.glob(File.join(Nagira.root, 'lib', 'tasks','*.rake')).each do |rake|
5
5
  load rake
data/app/app.rb CHANGED
@@ -1,326 +1,72 @@
1
- # @!macro [attach] sinatra.get
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
- set :app_file, __FILE__
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
- # \@format can be assigned one of the symbols: :xml, :json, :yaml.
31
+ # @overload get "$1"
143
32
  #
144
- # = Examples
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
- # Detect if this a request for ActiveResource PATH
38
+ # @!macro [new] type
39
+ # @param [String] :type Type is one of Nagios objects like hosts, hostgroupsroups, etc.
161
40
  #
162
- before do
163
- @active_resource = request.path_info =~ %r{^#{Nagira::AR_PREFIX}/}
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
- # Detect output mode modifier
44
+ # @!macro [new] hostname
45
+ # @param [String] :hostname Configured Nagios hostname
171
46
  #
172
- # Detect and strip output type from HTTP route. Full list of
173
- # output types is +:list+, +:state+ or +:full+, corresponding to
174
- # (+/list, +/state+, +/full+ routes).
47
+ # @!macro [new] service_name
48
+ # @param [String] :service_name Configured Nagios service for the host
175
49
  #
176
- # Output type defined by route modifier appended to the end of HTTP
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
- # = Examples
52
+ # <b>Accepted output type modifiers:</b>
182
53
  #
183
- # GET /_objects/_list # => :list
184
- # GET /_status/_state # => :state
185
- # GET /_status/:hostname # => :full
186
- # GET /_status # => :normal
54
+ # @!macro [new] list
187
55
  #
188
- before do
189
- request.path_info.sub!(/\/_(list|state|full)$/, '')
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
- # Detects if request is using jQuery JSON-P and sets @callback
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
- # = Example
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
- # GET /_api?callback=jQuery12313123123 # @callback == jQuery12313123123
65
+ # @!macro [new] full
204
66
  #
205
- # JSONP support is based on the code from +sinatra/jsonp+ Gem
206
- # https://github.com/shtirlic/sinatra-jsonp.
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