zbxapi 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,43 @@
26
26
 
27
27
  require "zbxapi/zdebug"
28
28
  require "zbxapi/exceptions"
29
+ require "pp"
30
+
31
+ class ZabbixAPI_ParametersDSL
32
+ attr_reader :valid_params, :required_params
33
+
34
+ def initialize(other_validparams,other_requiredparams)
35
+ @other_valid_parameters=other_validparams
36
+ @other_required_parameters=other_requiredparams
37
+ @valid_params=[]
38
+ @required_params=[]
39
+ end
40
+
41
+ def inherit(ver)
42
+ @valid_params=@other_valid_parameters[ver] || []
43
+ @required_params=@other_required_parameters[ver] || []
44
+ end
45
+
46
+ def add(*params)
47
+ @valid_params+=params
48
+ @valid_params.flatten!
49
+ end
50
+
51
+ def remove(*params)
52
+ #TODO Add sanity checking to ensure that a removed parameter is also removed from the required list
53
+ @valid_params-=params
54
+ end
55
+
56
+ def from(var)
57
+ var
58
+ end
59
+
60
+ def requires(*params)
61
+ #TODO Add sanity checking to ensure that required arguments are also valid arguments
62
+ @required_params+=params
63
+ @required_params.flatten!
64
+ end
65
+ end
29
66
 
30
67
  class ZabbixAPI_Method
31
68
  include ZDebug
@@ -67,6 +104,12 @@ class ZabbixAPI_Method
67
104
  @deprecated={ver=>(msg || "") }
68
105
  end
69
106
 
107
+ def deprecate_dsl(msg)
108
+ warn msg
109
+ caller[1]=~/(.*:\d+):.*`(.*?)'/
110
+ warn "from: #{$1}"
111
+ end
112
+
70
113
  #Invalidate this function starting at API version ver
71
114
  #This will raise the ZabbixAPI_Method::InvalidMethod exception
72
115
  #If msg is nil the following string will be used in the exception:
@@ -112,13 +155,21 @@ class ZabbixAPI_Method
112
155
  end
113
156
 
114
157
  def add_valid_params(ver,params)
158
+ deprecate_dsl("DSL statement add_valid_params is deprecated, use the parameters statement")
115
159
  @validparams[ver]=params
116
160
  end
117
161
 
118
162
  def add_required_params(ver,params)
163
+ deprecate_dsl("DSL statement add_required_params is deprecated, use the parameters statement")
119
164
  @requiredparams[ver]=params
120
165
  end
121
166
 
167
+ # Return the valid parameters for the method given version.
168
+ # If version is nil, the highest version number available in the valid
169
+ # parameters hash is used.
170
+ # If ver is a version number, the closest version number in the valid
171
+ # parameters hash which is less than or equal to is returned.
172
+ # nil is returned is no valid parameters are found
122
173
  def get_valid_params(ver)
123
174
  ver=get_version(ver,@validparams)
124
175
  return nil if ver.nil?
@@ -131,6 +182,15 @@ class ZabbixAPI_Method
131
182
  @requiredparams[ver]
132
183
  end
133
184
 
185
+ def parameters(ver,*params,&block)
186
+ parameters=ZabbixAPI_ParametersDSL.new(@validparams,@requiredparams)
187
+
188
+ parameters.add(params) if !params.nil?
189
+ parameters.instance_eval(&block) if !block.nil?
190
+ @validparams[ver]=parameters.valid_params
191
+ @requiredparams[ver]=parameters.required_params
192
+ end
193
+
134
194
  def params_good?(server_version, params)
135
195
  debug(8,:msg=>"Server Version", :var=>server_version)
136
196
  var=params.is_a?(Hash) ? params.keys : params
@@ -220,16 +280,51 @@ class ZabbixAPI_Method
220
280
  #If no versions exist in hash, nil is returned
221
281
  def get_version(server,hash)
222
282
  return nil if hash.nil?
223
- server=server.split(".").map{|i| i.to_i }
224
- hash=hash.keys.map{|i| i.split(".").map {|a| a.to_i} }
283
+ if server
284
+ #server=server.split(".").map{|i| i.to_i }
285
+ keys=hash.keys.sort do |a,b|
286
+ aa=a.split(".")
287
+ bb=b.split(".")
288
+ last_pos=((aa.length > bb.length) ? aa.length : bb.length)-1
289
+ pos=0
290
+ while aa[pos].to_i==bb[pos].to_i
291
+ break if pos>=last_pos
292
+ pos+=1
293
+ end
294
+ (aa[pos].to_i<=>bb[pos].to_i)
295
+ end
225
296
 
226
- equality=0
227
- hash.sort! {|x,y| x<=>y }
228
- version=nil
229
- hash.each {|i|
230
- version=i if (server<=>i)>=0
231
- }
232
- version.join(".") if !version.nil?
297
+ keys.delete_if do |k|
298
+ kk=k.split(".")
299
+ ss=server.split(".")
300
+ last_pos=((kk.length > ss.length) ? ss.length : ss.length)-1
301
+ pos=0
302
+ while kk[pos].to_i<=ss[pos].to_i
303
+ break if pos>=last_pos
304
+ pos+=1
305
+ end
306
+ kk[pos].to_i>ss[pos].to_i
307
+ end
308
+
309
+ if keys.empty?
310
+ return nil
311
+ else
312
+ return keys.last
313
+ end
314
+ else
315
+ sorted=hash.keys.sort do |a,b|
316
+ aa=a.split(".")
317
+ bb=b.split(".")
318
+ last_pos=((aa.length > bb.length) ? aa.length : bb.length)-1
319
+ pos=0
320
+ while aa[pos].to_i==bb[pos].to_i
321
+ break if pos>=last_pos
322
+ pos+=1
323
+ end
324
+ (aa[pos].to_i<=>bb[pos].to_i)
325
+ end
326
+ sorted.last
327
+ end
233
328
  end
234
329
 
235
330
 
@@ -251,7 +346,13 @@ class ZabbixAPI_Base
251
346
  def valid_params(sym,ver=nil)
252
347
  api_method=self.class.api_methods[sym]
253
348
  return nil if api_method.nil?
254
- api_method.valid_params(ver)
349
+ api_method.get_valid_params(ver)
350
+ end
351
+
352
+ def required_params(sym,ver=nil)
353
+ api_method=self.class.api_methods[sym]
354
+ return nil if api_method.nil?
355
+ api_method.get_required_params(ver)
255
356
  end
256
357
 
257
358
  def self.method_missing(sym,&block)
@@ -27,44 +27,50 @@ class Host < ZabbixAPI_Base
27
27
  # params
28
28
  #end
29
29
 
30
- add_valid_params "1.3", ["nodeids","groupids","hostids","templateids",
31
- "itemids","triggerids","graphids","proxyids","maintenanceids",
32
- "dhostids","dserviceids","monitored_hosts","templated_hosts",
33
- "proxy_hosts","with_items","with_monitored_items",
34
- "with_historical_items","with_triggers","with_monitored_triggers",
35
- "with_httptests","with_monitored_httptests","with_graphs",
36
- "editable","filter","search","startSearch","excludeSearch",
37
- "searchWildcardsEnabled","output","select_groups","selectParentTemplates",
38
- "select_items","select_triggers","select_graphs","select_applications",
39
- "selectInterfaces","select_macros","select_profile","countOutput","groupOutput",
40
- "preservekeys","sortfield","sortorder","limit","extendoutput"]
41
- add_valid_params "2.0", ["nodeids","groupids","hostids","templateids", "itemids",
42
- "triggerids","graphids","proxyids","maintenanceids", "dhostids",
43
- "dserviceids","monitored_hosts","templated_hosts", "proxy_hosts",
44
- "with_items","with_monitored_items", "with_historical_items",
45
- "with_triggers","with_monitored_triggers", "with_httptests",
46
- "with_monitored_httptests","with_graphs", "editable","filter",
47
- "search","startSearch","excludeSearch", "searchWildcardsEnabled",
48
- "output","select_groups","selectParentTemplates", "select_items",
49
- "select_triggers","select_graphs","select_applications",
50
- "selectInterfaces","select_macros","select_profile","countOutput",
51
- "groupOutput", "preservekeys","sortfield","sortorder","limit",
52
- "extendoutput"]
30
+ parameters "1.3",
31
+ "nodeids","groupids","hostids","templateids","itemids","triggerids",
32
+ "graphids","proxyids","maintenanceids","dhostids","dserviceids",
33
+ "monitored_hosts","templated_hosts","proxy_hosts","with_items",
34
+ "with_monitored_items","with_historical_items","with_triggers",
35
+ "with_monitored_triggers","with_httptests",
36
+ "with_monitored_httptests","with_graphs","editable","filter",
37
+ "search","startSearch","excludeSearch","searchWildcardsEnabled",
38
+ "output","select_groups","selectParentTemplates","select_items",
39
+ "select_triggers","select_graphs","select_applications",
40
+ "selectInterfaces","select_macros","select_profile","countOutput",
41
+ "groupOutput","preservekeys","sortfield","sortorder","limit",
42
+ "extendoutput"
43
+
44
+ parameters "2.0" do
45
+ inherit from "1.3"
46
+ end
47
+
48
+ #parameters "3.0" do
49
+ # inherit from "2.0"
50
+ # remove "select_macros","preserve_keys","extendoutput"
51
+ # add "test_param"
52
+ # requires "test_param"
53
+ #end
53
54
  end
54
55
 
55
56
  action :exists do
56
- add_valid_params "1.3", ["nodeids","hostid","host"]
57
+ parameters "1.3" do
58
+ add "nodeids","hostid","host"
59
+ end
57
60
  end
58
61
 
59
62
  action :create do
60
- add_valid_params "1.3", ["host","name","port","status","useip",
61
- "dns","ip","proxy_hostid","useipmi","ipmi_ip","ipmi_port",
62
- "ipmi_authtype","ipmi_privilege","ipmi_username",
63
- "ipmi_password","groups","templates"]
64
- add_valid_params "2.0", ["host","name","port","status","useip",
65
- "dns","ip","proxy_hostid","useipmi","ipmi_ip","ipmi_port",
66
- "ipmi_authtype","ipmi_privilege","ipmi_username",
67
- "ipmi_password","groups","templates","interfaces"]
63
+ parameters "1.3" do
64
+ add "host","name","port","status","useip","dns","ip","proxy_hostid",
65
+ "useipmi","ipmi_ip","ipmi_port","ipmi_authtype","ipmi_privilege",
66
+ "ipmi_username","ipmi_password","groups","templates"
67
+ requires "groups","interfaces"
68
+ end
69
+
70
+ parameters "2.0" do
71
+ inherit from "1.3"
72
+ add "interfaces"
73
+ end
68
74
  end
69
75
 
70
76
  action :delete do
@@ -35,14 +35,15 @@ Trigger.delete
35
35
  Trigger.deleteDependencies
36
36
  Trigger.exists
37
37
  Trigger.get do
38
- add_valid_params "1.3", ['triggerids', "select_functions", "nodeids", "groupids", "templateids",
38
+ parameters "1.3",
39
+ 'triggerids', "select_functions", "nodeids", "groupids", "templateids",
39
40
  "hostids", "itemids", "applicationids", "functions", "inherited", "templated", "monitored",
40
41
  "active", "maintenance", "withUnacknowledgedEvents", "withAcknowledgedEvents",
41
42
  "withLastEventUnacknowledged", "skipDependent", "editable", "lastChangeSince",
42
43
  "lastChangeTill", "filter", "group", "host", "only_true", "min_severity", "search",
43
44
  "startSearch", "excludeSearch", "searchWildcardsEnabled", "output", "expandData",
44
45
  "expandDescription", "select_groups", "select_hosts", "select_items", "select_dependencies",
45
- "countOutput", "groupOutput", "preservekeys", "sortfield", "sortorder", "limit"]
46
+ "countOutput", "groupOutput", "preservekeys", "sortfield", "sortorder", "limit"
46
47
  end
47
48
  Trigger.update
48
49
 
data/zbxapi.rb CHANGED
@@ -340,6 +340,29 @@ class ZabbixAPI
340
340
  end
341
341
  end
342
342
 
343
+ def api_info(request=:objects,*options)
344
+ options = options[0] || {}
345
+ version=options[:version] || nil
346
+ request_type=options[:params] || :valid
347
+
348
+ case request
349
+ when :objects then
350
+ @objects.keys.map { |k| k.to_s }
351
+ else
352
+ obj,meth=request.split(".")
353
+ if meth then
354
+ case request_type
355
+ when :valid
356
+ @objects[obj.intern].valid_params(meth.intern,version)
357
+ when :required
358
+ @objects[obj.intern].required_params(meth.intern,version)
359
+ end
360
+ else
361
+ @objects[obj.intern].api_methods.map{ |m| m.to_s }
362
+ end
363
+ end
364
+ end
365
+
343
366
  private
344
367
 
345
368
  #Select the http object to be used.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zbxapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
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-09-13 00:00:00.000000000 Z
12
+ date: 2013-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json