nagira 0.2.6 → 0.2.7

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.
data/History.md CHANGED
@@ -1,7 +1,17 @@
1
+ ### v.0.2.7
2
+
3
+ * Mon Apr 15 2013 -- Dmytro Kovalov
4
+ - Extend ActiveResource support
5
+ - add prefix /ar
6
+ - AR_PREFIX constant for the same
7
+ - covert all output ot Array if it's ActiveResource
8
+ - ActiveResource supportewd now in /_objects, /_status
9
+
1
10
  ### v.0.2.6
2
- * Tue Mar 26 2013 -- Ivan Gusev
3
11
 
12
+ * Tue Mar 26 2013 -- Ivan Gusev
4
13
  - Fix for long time parsing https://github.com/dmytro/nagira/issues/20
14
+
5
15
  ### v.0.2.5
6
16
 
7
17
  * Fri Mar 15 2013 -- Dmytro Kovalov
data/lib/app.rb CHANGED
@@ -19,16 +19,16 @@
19
19
  # @!macro [new] service_name
20
20
  # @param [String] :service_name Configured Nagios service for the host
21
21
  #
22
- # @!macro [new] accepted
22
+ # @!macro [new] accepted
23
23
  #
24
24
  # <b>Accepted output type modifiers:</b>
25
25
  #
26
- # @!macro [new] list
26
+ # @!macro [new] list
27
27
  #
28
28
  # - +/_list+ : Short list of available objects, depending on the
29
29
  # current request context: hosts, services, etc.
30
30
  #
31
- # @!macro [new] state
31
+ # @!macro [new] state
32
32
  #
33
33
  # - +/_state+ - Instead of full status information send only
34
34
  # current state. For hosts up/down, for services OK, Warn,
@@ -36,10 +36,10 @@
36
36
  #
37
37
  # @!macro [new] full
38
38
  #
39
- # - +/_full+ - Show full status information. When used in
40
- # /_status/_full call will display full hoststaus and
39
+ # - +/_full+ - Show full status information. When used in
40
+ # /_status/_full call will display full hoststaus and
41
41
  # servicestatus information for each host.
42
- #
42
+ #
43
43
  #
44
44
 
45
45
 
@@ -59,29 +59,29 @@ class Nagira < Sinatra::Base
59
59
  #
60
60
  # @method startup_configuration
61
61
  # @overload before("Initial Config")
62
- configure do
63
-
62
+ configure do
63
+
64
64
  $nagios = { }
65
65
  $nagios[:config] = Nagios::Config.new Nagira.settings.nagios_cfg
66
66
  $nagios[:config].parse
67
67
 
68
- $nagios.merge!({
69
- status: Nagios::Status.new( Nagira.settings.status_cfg ||
68
+ $nagios.merge!({
69
+ status: Nagios::Status.new( Nagira.settings.status_cfg ||
70
70
  $nagios[:config].status_file
71
71
  ),
72
- objects: Nagios::Objects.new( Nagira.settings.objects_cfg ||
72
+ objects: Nagios::Objects.new( Nagira.settings.objects_cfg ||
73
73
  $nagios[:config].object_cache_file
74
74
  ),
75
- commands: Nagios::ExternalCommands.new( Nagira.settings.command_file ||
75
+ commands: Nagios::ExternalCommands.new( Nagira.settings.command_file ||
76
76
  $nagios[:config].command_file
77
77
  )
78
78
  })
79
79
 
80
- $nagios.merge!({
80
+ $nagios.merge!({
81
81
  status_inflight: Nagios::Status.new( Nagira.settings.status_cfg ||
82
82
  $nagios[:config].status_file
83
83
  ),
84
- objects_inflight: Nagios::Objects.new( Nagira.settings.objects_cfg ||
84
+ objects_inflight: Nagios::Objects.new( Nagira.settings.objects_cfg ||
85
85
  $nagios[:config].object_cache_file
86
86
  )
87
87
  }) if ::DEFAULT[:start_background_parser]
@@ -123,7 +123,7 @@ class Nagira < Sinatra::Base
123
123
  # @method parse_nagios_files
124
124
  # @overload before("Parse Nagios files")
125
125
 
126
- before do
126
+ before do
127
127
 
128
128
  if Nagira.settings.start_background_parser
129
129
  unless $bg.alive?
@@ -132,9 +132,9 @@ class Nagira < Sinatra::Base
132
132
  $nagios[:status].parse
133
133
  $nagios[:objects].parse
134
134
  end
135
- $use_inflight_status ? @status = $nagios[:status_inflight].status['hosts']
135
+ $use_inflight_status ? @status = $nagios[:status_inflight].status['hosts']
136
136
  : @status = $nagios[:status].status['hosts']
137
- $use_inflight_objects ? @objects = $nagios[:objects_inflight].objects
137
+ $use_inflight_objects ? @objects = $nagios[:objects_inflight].objects
138
138
  : @objects = $nagios[:objects].objects
139
139
  else
140
140
  $nagios[:config].parse
@@ -147,7 +147,7 @@ class Nagira < Sinatra::Base
147
147
 
148
148
  ##
149
149
  # TODO: This stuff breaks XML valid. Will have to wait.
150
- #
150
+ #
151
151
  # idx = 0
152
152
  # @status.keys.uniq.each do |hostname|
153
153
  # @status[idx] = @status[hostname]
@@ -168,13 +168,13 @@ class Nagira < Sinatra::Base
168
168
  #
169
169
  # Clear values onf instance variables before start.
170
170
  #
171
- before do
171
+ before do
172
172
  @data = []
173
173
  @format = @output = nil
174
174
  end
175
175
 
176
176
  ##
177
- # @method strip_extensions
177
+ # @method strip_extensions
178
178
  # @overload before("detect format")
179
179
  #
180
180
  # Detect and strip output format extension
@@ -187,19 +187,25 @@ class Nagira < Sinatra::Base
187
187
  # \@format can be assigned one of the symbols: :xml, :json, :yaml.
188
188
  #
189
189
  # = Examples
190
- #
190
+ #
191
191
  # GET /_objects # => default format
192
192
  # GET /_objects.json # => :json
193
193
  # GET /_status/_list.yaml # => :yaml
194
- #
195
- before do
194
+ #
195
+ before do
196
196
  request.path_info.sub!(/#{settings.format_extensions}/, '')
197
197
  @format = ($1 || settings.format).to_sym
198
198
  content_type "application/#{@format.to_s}"
199
199
  end
200
200
 
201
+ before do
202
+ request.path_info.sub!(/^(#{Nagira::AR_PREFIX})\//, '/')
203
+ @active_resource = $1 ? true : false
204
+ end
205
+
206
+
201
207
  ##
202
- # @method strip_output_type
208
+ # @method strip_output_type
203
209
  # @overload before('detect output mode')
204
210
  #
205
211
  # Detect output mode modifier
@@ -228,14 +234,14 @@ class Nagira < Sinatra::Base
228
234
  ##
229
235
  # @method find_jsonp_callback
230
236
  # @overload before('find callback name')
231
- #
237
+ #
232
238
  # Detects if request is using jQuery JSON-P and sets @callback
233
239
  # variable. @callback variable is used if after method and prepends
234
240
  # JSON data with callback function name.
235
241
  #
236
242
  # = Example
237
243
  #
238
- # GET /_api?callback=jQuery12313123123 # @callback == jQuery12313123123
244
+ # GET /_api?callback=jQuery12313123123 # @callback == jQuery12313123123
239
245
  #
240
246
  # JSONP support is based on the code from +sinatra/jsonp+ Gem
241
247
  # https://github.com/shtirlic/sinatra-jsonp.
@@ -261,13 +267,23 @@ class Nagira < Sinatra::Base
261
267
  # return unless request["REQUEST_METHOD"] == 'PUT'
262
268
  if ! @data || @data.empty?
263
269
  halt [404, {
264
- :message => "Object not found or bad request",
270
+ :message => "Object not found or bad request",
265
271
  :error => "HTTP::Notfound"
266
272
  }.send("to_#{@format}")
267
273
  ]
268
274
  end
269
275
  end
270
276
 
277
+
278
+ ##
279
+ # @method convert_to_active_resource
280
+ # @overload after("Return Array for ActiveResouce routes")
281
+ #
282
+ #
283
+ after do
284
+ @data = @data.values if @active_resource && @data.is_a?(Hash)
285
+ end
286
+
271
287
  ##
272
288
  # @method return_jsonp_data
273
289
  # @overload after("Return formatted data")
@@ -289,13 +305,14 @@ class Nagira < Sinatra::Base
289
305
 
290
306
 
291
307
 
308
+
292
309
  ##
293
310
  # @method get_api
294
311
  # @overload get(/_api)
295
312
  #
296
- # Provide information about API routes
313
+ # Provide information about API routes
297
314
  #
298
- get "/_api" do
315
+ get "/_api" do
299
316
  @data = self.api
300
317
  nil
301
318
  end
@@ -305,12 +322,12 @@ class Nagira < Sinatra::Base
305
322
  # @method get_runtime_config
306
323
  # @overload get(/_runtime)
307
324
  #
308
- # Print out nagira runtime configuration
309
- get "/_runtime" do
325
+ # Print out nagira runtime configuration
326
+ get "/_runtime" do
310
327
  @data = {
311
328
  application: self.class,
312
329
  version: VERSION,
313
- runtime: {
330
+ runtime: {
314
331
  environment: Nagira.settings.environment,
315
332
  home: ENV['HOME'],
316
333
  user: ENV['LOGNAME'],
@@ -323,7 +340,7 @@ class Nagira < Sinatra::Base
323
340
  # @method get_slash
324
341
  # @overload get(/)
325
342
  #
326
- # Returns application information: name, version, github repository.
343
+ # Returns application information: name, version, github repository.
327
344
  get "/" do
328
345
  @data = {
329
346
  :application => self.class,
@@ -11,7 +11,9 @@ class Nagira < Sinatra::Base
11
11
  # requested output type(XML, YAML or JSON).
12
12
  #
13
13
  #
14
+
14
15
  # @method get_config
16
+ # @overload get("/_config")
15
17
  #
16
18
  # Get Nagios configuration hash form parsing main Nagios
17
19
  # configuration file nagios.cfg
@@ -14,10 +14,10 @@ class Nagira < Sinatra::Base
14
14
  # @macro list
15
15
  #
16
16
  get "/_objects" do
17
-
17
+
18
18
  @data = begin
19
19
  @output == :list ? @objects.keys : @objects
20
- rescue NoMethodError
20
+ rescue NoMethodError
21
21
  nil
22
22
  end
23
23
  nil
@@ -31,16 +31,13 @@ class Nagira < Sinatra::Base
31
31
  #
32
32
  # @!macro accepted
33
33
  # @!macro list
34
- #
34
+ #
35
35
  #
36
36
  get "/_objects/:type" do |type|
37
37
  begin
38
- if type.singularize == type
39
- @data = @objects[type.to_sym]
40
- else
41
- @data = Array[@objects[type.to_sym]]
42
- end
38
+ @data = @objects[type.to_sym]
43
39
  @data = @data.keys if @output == :list
40
+
44
41
  rescue NoMethodError
45
42
  nil
46
43
  end
@@ -94,36 +94,17 @@ class Nagira < Sinatra::Base
94
94
 
95
95
  nil
96
96
  end
97
-
98
- # Hoststatus for single host
99
- #
100
- # @method get_status_hostname
101
- #
102
- # @!macro hostname
103
- #
104
- # @!macro accepted
105
- # @!macro state
106
- #
107
- # Support for:
108
- # - plural resources: N/A
109
- # - object access by ID: NO (TODO)
110
-
111
- get "/_status/:hostname" do |hostname|
112
-
113
- hostname = hostname.to_i if hostname =~ /^\d+$/
114
- @data = @status[hostname]['hoststatus'].dup if @status.has_key? hostname
115
-
116
- if @output == :state
117
- @data = @data.slice("host_name", "current_state")
118
- end
119
-
120
- nil
121
- end
122
97
 
123
98
  ##
124
99
  # @method get_status
125
100
  #
126
- # All hosts status. If no output modifier provided, outputs full hosttatus information for each host. Not including services information.
101
+ # Return all hosts status.
102
+ #
103
+ # If no output modifier provided, outputs full hosttatus information for each host. Not including services information.
104
+ #
105
+ # Alias: get /_status is the same thing as get /_status/_hosts with
106
+ # ActiveResource compatibility, i.e. for */_hosts request Nagira
107
+ # returns array instead of hash.
127
108
  #
128
109
  # @!macro accepted
129
110
  # @!macro state
@@ -134,7 +115,8 @@ class Nagira < Sinatra::Base
134
115
  # - plural resources: N/A
135
116
  # - object access by ID: N/A
136
117
 
137
- get "/_status" do
118
+ get /^\/_status(\/_hosts)?/ do
119
+
138
120
  @data = @status.dup
139
121
 
140
122
  case @output
@@ -150,4 +132,31 @@ class Nagira < Sinatra::Base
150
132
 
151
133
  nil
152
134
  end
135
+
136
+ # Hoststatus for single host
137
+ #
138
+ # @method get_status_hostname
139
+ #
140
+ # @!macro hostname
141
+ #
142
+ # @!macro accepted
143
+ # @!macro state
144
+ #
145
+ # Support for:
146
+ # - plural resources: N/A
147
+ # - object access by ID: NO (TODO)
148
+
149
+ get "/_status/:hostname" do |hostname|
150
+
151
+
152
+ hostname = hostname.to_i if hostname =~ /^\d+$/
153
+ @data = @status[hostname]['hoststatus'].dup if @status.has_key? hostname
154
+
155
+ if @output == :state
156
+ @data = @data.slice("host_name", "current_state")
157
+ end
158
+
159
+ nil
160
+ end
161
+
153
162
  end
@@ -33,6 +33,12 @@ class Nagira < Sinatra::Base
33
33
 
34
34
  VERSION = File.read(File.expand_path(File.dirname(__FILE__)) + '/../version.txt').strip
35
35
  GITHUB = "http://dmytro.github.com/nagira/"
36
+
37
+ ##
38
+ # When this prefix added to routes convert output to ActiveResource
39
+ # compatible format (basically Array instead of Hash).
40
+ #
41
+ AR_PREFIX = "/ar"
36
42
 
37
43
  # Get all routes that Nagira provides.
38
44
  def api
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.2.7
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagira
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-27 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel