zbxapi 0.1.398 → 0.2.415

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,308 @@
1
+ # Title:: Zabbix API Ruby Library
2
+ # License:: LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
3
+ # Copyright:: Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ #--
20
+ ##########################################
21
+ # Subversion information
22
+ # $Id: api_dsl.rb 409 2012-09-11 23:39:58Z nelsonab $
23
+ # $Revision: 409 $
24
+ ##########################################
25
+ #++
26
+
27
+ require "zbxapi/zdebug"
28
+ require "zbxapi/exceptions"
29
+
30
+ class ZabbixAPI_Method
31
+ include ZDebug
32
+
33
+ class InvalidArity<RuntimeError
34
+ end
35
+
36
+ class AlreadyUsedError<RuntimeError
37
+ end
38
+
39
+ class InvalidMethodError<ZError
40
+ end
41
+
42
+ def initialize(apiclass,apimethod)
43
+ @apiclass=apiclass
44
+ @apimethod=apimethod
45
+ @login_required=true
46
+ @arg_processors={} # {version=>block}
47
+ @validparams={} # {version=>[valid parameters]}
48
+ @requiredparams={} # {version=>[required parameters]}
49
+ @default=:default # Which is the default version to use
50
+ @method_names={}
51
+ @method_names["0"]="#{@apiclass}.#{@apimethod}".downcase
52
+ @deprecated=nil
53
+ @invalidated=nil
54
+ end
55
+
56
+ def login_not_required
57
+ @login_required=false
58
+ end
59
+
60
+ #Deprecate this function starting at API version ver
61
+ #If msg is nil the following will be printed:
62
+ #"#{method} is deprecated for API versions #{ver} and higher"
63
+ # Where method is the current method name for the API
64
+ def deprecate(ver,msg=nil)
65
+ #Deprecate can only be called once
66
+ raise AlreadyUsedError.new("Deprecate can only be used once per method") if @deprecated
67
+ @deprecated={ver=>(msg || "") }
68
+ end
69
+
70
+ #Invalidate this function starting at API version ver
71
+ #This will raise the ZabbixAPI_Method::InvalidMethod exception
72
+ #If msg is nil the following string will be used in the exception:
73
+ #"#{method} is invalid for API versions #{ver} and higher"
74
+ # Where method is the current method name for the API
75
+ def invalidate(ver,msg=nil)
76
+ #Invalidate can only be called once
77
+ raise AlreadyUsedError.new("Invalidate can only be used once per method") if @deprecated
78
+ @invalidated={ver=>(msg || "") }
79
+ end
80
+
81
+ #Allows for the override of the API method to be called
82
+ #The default method is version 0 which can be overridden
83
+ def method_override(ver,name)
84
+ @method_names[ver]=name
85
+ end
86
+
87
+ #add_arg_processor
88
+ #Creates an argument processor suitable for versions starting at ver
89
+ #ver is a string denoting the starting version number this argument processor should be used on
90
+ #This method also needs a block to be passed. The block
91
+ def add_arg_processor(ver,&block)
92
+ raise InvalidArity.new("Argument processor must accept one parameter") if block.arity !=1
93
+ @arg_processors[ver]=block
94
+ @default=ver
95
+ end
96
+
97
+ def get_arg_processor(ver)
98
+ ver=get_version(ver,@arg_processors)
99
+ return nil if ver.nil?
100
+ @arg_processors[ver]
101
+ end
102
+
103
+ #calls the argument processor appropriate for ver
104
+ #If no argument processor found, params are returned unchanged
105
+ def call_arg_processor(ver,params={})
106
+ processor=get_arg_processor(ver)
107
+ if processor.nil?
108
+ params
109
+ else
110
+ processor.call(params)
111
+ end
112
+ end
113
+
114
+ def add_valid_params(ver,params)
115
+ @validparams[ver]=params
116
+ end
117
+
118
+ def add_required_params(ver,params)
119
+ @requiredparams[ver]=params
120
+ end
121
+
122
+ def get_valid_params(ver)
123
+ ver=get_version(ver,@validparams)
124
+ return nil if ver.nil?
125
+ @validparams[ver]
126
+ end
127
+
128
+ def get_required_params(ver)
129
+ ver=get_version(ver,@requiredparams)
130
+ return nil if ver.nil?
131
+ @requiredparams[ver]
132
+ end
133
+
134
+ def params_good?(server_version, params)
135
+ debug(8,:msg=>"Server Version", :var=>server_version)
136
+ var=params.is_a?(Hash) ? params.keys : params
137
+ debug(8,:msg=>"Param keys", :var=>var)
138
+
139
+ valid_params=get_valid_params(server_version)
140
+
141
+ if valid_params #continue to see if there's a required param
142
+ raise ArgumentError.new("Named Arguments (Hash) expected for #{@apiclass}.#{@apimethod},"\
143
+ " '#{params.class}' received: #{params.inspect}") if !params.is_a?(Hash)
144
+ args=params.keys
145
+
146
+ invalid_args=args-valid_params
147
+ debug(9,:msg=>"Invalid args",:var=>invalid_args)
148
+ raise ZbxAPI_ParameterError.new("Invalid parameters #{invalid_args.inspect}") if !invalid_args.empty?
149
+ end
150
+
151
+ required_params=get_required_params(server_version)
152
+ # ver=get_version(server_version,@requiredparams)
153
+
154
+ return true if required_params.nil?
155
+ required_args=required_params.reject { |i| i.class==Array }
156
+ required_or_args=required_params.reject { |i| i.class!=Array }
157
+
158
+ missing_args=[]
159
+ missing_args=required_args-args
160
+
161
+ required_or_args.delete_if do |i|
162
+ count=i.length
163
+ missing_args<<i if (i-args).count==count
164
+ end
165
+
166
+ debug(9,:msg=>"Required Params",:var=>required_params)
167
+ debug(9,:msg=>"Missing params",:var=>missing_args)
168
+
169
+ if !missing_args.empty?
170
+ msg=missing_args.map do |i|
171
+ if i.class==Array
172
+ "(#{i.join(" | ")})"
173
+ else
174
+ i
175
+ end
176
+ end.join(", ")
177
+ raise ZbxAPI_ParameterError.new("Missing required arguments: #{msg}")
178
+ end
179
+ true
180
+ end
181
+
182
+ #This is the function that does the actual APIcall.
183
+ def do(server,params={})
184
+ debug(7,:msg=>"Method Name",:var=>@method_names[@method_names.keys.sort.first])
185
+ debug(7,:msg=>"Server",:var=>server)
186
+ debug(7,:msg=>"Params",:var=>params)
187
+
188
+ ver=get_version(server.API_version,@method_names)
189
+ name=@method_names[ver]
190
+
191
+ debug(8,:msg=>"Method Name used",:var=>name)
192
+
193
+ if @invalidated
194
+ ver=get_version(server.API_version,@invalidated)
195
+ if ver
196
+ msg=@invalidated.values.first.empty? ?
197
+ "#{name} is invalid for api versions #{@invalidated.keys.first} and higher" : @invalidated.values.first
198
+ raise InvalidMethodError.new(msg)
199
+ end
200
+ end
201
+
202
+ if @deprecated
203
+ ver=get_version(server.API_version,@deprecated)
204
+ if ver
205
+ msg=@deprecated.values.first.empty? ?
206
+ "#{name} is deprecated for api versions #{@deprecated.keys.first} and higher" : @deprecated.values.first
207
+ warn("Warning: #{msg}")
208
+ end
209
+ end
210
+
211
+ debug(8,:msg=>"Params before arg processor",:var=>params)
212
+ params=call_arg_processor(server.API_version,params)
213
+ debug(7,:msg=>"Params after arg processor",:var=>params)
214
+ params_good?(server.API_version,params)
215
+ server.api_call(name,params)
216
+ end
217
+
218
+ #returns the version number closest to server which is less than
219
+ #or equal to server
220
+ #If no versions exist in hash, nil is returned
221
+ def get_version(server,hash)
222
+ 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} }
225
+
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?
233
+ end
234
+
235
+
236
+ end
237
+
238
+ class ZabbixAPI_Base
239
+ def initialize(server)
240
+ @server=server
241
+ end
242
+
243
+ def api_methods
244
+ self.class.api_methods.keys
245
+ end
246
+
247
+ def api_aliases
248
+ self.class.api_aliases
249
+ end
250
+
251
+ def valid_params(sym,ver=nil)
252
+ api_method=self.class.api_methods[sym]
253
+ return nil if api_method.nil?
254
+ api_method.valid_params(ver)
255
+ end
256
+
257
+ def self.method_missing(sym,&block)
258
+ add(sym,&block)
259
+ end
260
+
261
+ def self.action(sym, &block)
262
+ add(sym,&block)
263
+ end
264
+
265
+ def self.actions(*sym)
266
+ sym.each {|s| add(s)}
267
+ end
268
+
269
+ def self.api_methods
270
+ @api_methods
271
+ end
272
+
273
+ def self.api_aliases
274
+ @api_aliases={} if @api_aliases.nil?
275
+ @api_aliases
276
+ end
277
+
278
+ def self.alias(from,to)
279
+ @api_aliases={} if @api_aliases.nil?
280
+ @api_aliases[from]=to
281
+
282
+ @api_methods[to]=@api_methods[from]
283
+
284
+ define_method(to) do |params|
285
+ self.class.api_methods[to].do(@server,params)
286
+ end
287
+ end
288
+
289
+ def self.add(sym,&block)
290
+ @api_methods={} if @api_methods.nil?
291
+ @api_methods[sym]=ZabbixAPI_Method.new(self.to_s,sym.to_s)
292
+ @api_methods[sym].instance_eval(&block) if !block.nil?
293
+
294
+ #Create a method definition for the parameter in question.
295
+ #params is not treated as an array, the splat operator
296
+ #is there to allow for no parameters to be passed
297
+ define_method(sym) do |*params|
298
+ #The splat operator ensures we get an array, but we need
299
+ #to test to ensure we receive one hash parameter
300
+ if (params.length>1)
301
+ raise ArgumentError.new("Hash or one argument expected for #{self.class}.#{sym.to_s}, received: #{params.inspect}")
302
+ end
303
+ self.class.api_methods[sym].do(@server,params.first)
304
+ end
305
+ end
306
+ end
307
+
308
+
@@ -19,41 +19,16 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: zbxapi.rb 281 2011-04-06 18:10:16Z nelsonab $
23
- # $Revision: 281 $
22
+ # $Id: dsl_event.rb 340 2011-10-14 23:22:06Z nelsonab $
23
+ # $Revision: 340 $
24
24
  ##########################################
25
25
  #++
26
26
 
27
- require "api_classes/subclass_base"
28
-
29
-
30
- #******************************************************************************
31
- #
32
- # Class ZbxAPI_History
33
- #
34
- # Class encapsulating history functions
35
- #
36
- # get
37
- #
38
- #******************************************************************************
39
-
40
- class ZbxAPI_History
41
-
42
- def initialize(server)
43
- @server=server
44
- end
45
-
46
- #Get the history for an item.
47
- # itemids is a required option
48
- # example: get({"itemids"=>12345})
49
- def get(options)
50
- @server.checkauth
51
- @server.checkversion(1,3)
52
-
53
- raise ZbxAPI_ParameterError, "Missing 'itemid'", "History.get" if options["itemids"].nil?
54
-
55
- p obj=@server.raw_api("history.get",options)
56
- return obj['result']
57
- end
27
+ require "api_classes/api_dsl"
58
28
 
29
+ class Event < ZabbixAPI_Base
59
30
  end
31
+
32
+ Event.get
33
+ Event.acknowledge
34
+ Event.delete
@@ -0,0 +1,31 @@
1
+ # Title:: Zabbix API Ruby Library
2
+ # License:: LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
3
+ # Copyright:: Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ #--
20
+ ##########################################
21
+ # Subversion information
22
+ # $Id: dsl_graph.rb 409 2012-09-11 23:39:58Z nelsonab $
23
+ # $Revision: 409 $
24
+ ##########################################
25
+ #++
26
+
27
+ require "api_classes/api_dsl"
28
+
29
+ class Graph < ZabbixAPI_Base
30
+ actions :get, :create, :update, :delete
31
+ end
@@ -0,0 +1,33 @@
1
+ # Title:: Zabbix API Ruby Library
2
+ # License:: LGPL 2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
3
+ # Copyright:: Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ #--
20
+ ##########################################
21
+ # Subversion information
22
+ # $Id: dsl_history.rb 337 2011-10-14 16:11:39Z nelsonab $
23
+ # $Revision: 337 $
24
+ ##########################################
25
+ #++
26
+
27
+ require "api_classes/api_dsl"
28
+
29
+ class History < ZabbixAPI_Base
30
+ end
31
+
32
+ History.get
33
+ History.delete
@@ -0,0 +1,49 @@
1
+ require "api_classes/api_dsl"
2
+
3
+ class Host < ZabbixAPI_Base
4
+ actions :update, :massAdd, :massUpdate, :massRemove
5
+
6
+ action :get do
7
+ #arg_processor "1.3" do |params|
8
+ # params["output"]="extend"
9
+ # params
10
+ #end
11
+
12
+ add_valid_params "1.3", ["nodeids","groupids","hostids","templateids",
13
+ "itemids","triggerids","graphids","proxyids","maintenanceids",
14
+ "dhostids","dserviceids","monitored_hosts","templated_hosts",
15
+ "proxy_hosts","with_items","with_monitored_items",
16
+ "with_historical_items","with_triggers","with_monitored_triggers",
17
+ "with_httptests","with_monitored_httptests","with_graphs",
18
+ "editable","filter","search","startSearch","excludeSearch",
19
+ "searchWildcardsEnabled","output","select_groups","selectParentTemplates",
20
+ "select_items","select_triggers","select_graphs","select_applications",
21
+ "select_macros","select_profile","countOutput","groupOutput",
22
+ "preservekeys","sortfield","sortorder","limit","extendoutput"]
23
+ end
24
+
25
+ action :exists do
26
+ add_valid_params "1.3", ["nodeids","hostid","host"]
27
+ end
28
+
29
+ action :create do
30
+ add_valid_params "1.3", ["host","port","status","useip",
31
+ "dns","ip","proxy_hostid","useipmi","ipmi_ip","ipmi_port",
32
+ "ipmi_authtype","ipmi_privilege","ipmi_username",
33
+ "ipmi_password","groups","templates"]
34
+ end
35
+
36
+ action :delete do
37
+ add_arg_processor "0" do |params|
38
+ retval=nil
39
+ if params.is_a?(Fixnum)
40
+ retval=[{"hostid"=>params}]
41
+ else
42
+ retval=params
43
+ end
44
+
45
+ retval
46
+ end
47
+ end
48
+
49
+ end