zbxapi 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/libs/api_exceptions.rb +125 -0
- data/libs/exceptions.rb +122 -0
- data/libs/zdebug.rb +163 -0
- data/zbxapi.rb +951 -0
- metadata +82 -0
data/zbxapi.rb
ADDED
@@ -0,0 +1,951 @@
|
|
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: zbxapi.rb 254 2010-12-27 19:45:42Z nelsonab $
|
23
|
+
# $Revision: 254 $
|
24
|
+
##########################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
#setup our search path or libraries
|
28
|
+
path=File.expand_path(File.dirname(__FILE__) + "/./")+"/"
|
29
|
+
|
30
|
+
|
31
|
+
require path+'libs/zdebug'
|
32
|
+
require path+'libs/api_exceptions.rb'
|
33
|
+
require 'uri'
|
34
|
+
#require 'net/http'
|
35
|
+
require 'net/https'
|
36
|
+
require 'rubygems'
|
37
|
+
require 'json'
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
#------------------------------------------------------------------------------
|
42
|
+
#
|
43
|
+
# Class ZabbixAPI
|
44
|
+
#++
|
45
|
+
# Main Zabbix API class.
|
46
|
+
#
|
47
|
+
#------------------------------------------------------------------------------
|
48
|
+
|
49
|
+
class ZabbixAPI
|
50
|
+
|
51
|
+
include ZDebug
|
52
|
+
|
53
|
+
attr_accessor :method, :params, :debug_level, :auth
|
54
|
+
|
55
|
+
#subordinate class
|
56
|
+
attr_accessor :user # [User#new]
|
57
|
+
#subordinate class
|
58
|
+
attr_accessor :usergroup, :host, :item, :hostgroup, :application, :trigger, :sysmap, :history
|
59
|
+
|
60
|
+
@id=0
|
61
|
+
@auth=''
|
62
|
+
@url=nil
|
63
|
+
|
64
|
+
private
|
65
|
+
@user_name=''
|
66
|
+
@password=''
|
67
|
+
|
68
|
+
|
69
|
+
class Redirect < Exception #:nodoc: all
|
70
|
+
end
|
71
|
+
|
72
|
+
public
|
73
|
+
|
74
|
+
# The initialization routine for the Zabbix API class
|
75
|
+
# * url is a string defining the url to connect to, it should only point to the base url for Zabbix, not the api directory
|
76
|
+
# * debug_level is the default level to be used for debug messages
|
77
|
+
# Upon successful initialization the class will be set up to allow a connection to the Zabbix server
|
78
|
+
# A connection however will not have been made, to actually connect to the Zabbix server use the login method
|
79
|
+
def initialize(url,debug_level=0)
|
80
|
+
set_debug_level(debug_level)
|
81
|
+
@orig_url=url #save the original url
|
82
|
+
@url=URI.parse(url+'/api_jsonrpc.php')
|
83
|
+
@user = ZbxAPI_User.new(self)
|
84
|
+
@usergroup = ZbxAPI_UserGroup.new(self)
|
85
|
+
@host = ZbxAPI_Host.new(self)
|
86
|
+
@item = ZbxAPI_Item.new(self)
|
87
|
+
@hostgroup = ZbxAPI_HostGroup.new(self)
|
88
|
+
@application = ZbxAPI_Application.new(self)
|
89
|
+
@trigger = ZbxAPI_Trigger.new(self)
|
90
|
+
@sysmap = ZbxAPI_Sysmap.new(self)
|
91
|
+
@history = ZbxAPI_History.new(self)
|
92
|
+
@id=0
|
93
|
+
|
94
|
+
debug(6,"protocol: #{@url.scheme}, host: #{@url.host}")
|
95
|
+
debug(6,"port: #{@url.port}, path: #{@url.path}")
|
96
|
+
debug(6,"query: #{@url.query}, fragment: #{@url.fragment}")
|
97
|
+
end
|
98
|
+
|
99
|
+
#wraps the given information into the appropriate JSON object
|
100
|
+
#* method is a string
|
101
|
+
#* params is a hash of the parameters for the method to be called
|
102
|
+
#Returns a hash representing a Zabbix API JSON call
|
103
|
+
def json_obj(method,params={})
|
104
|
+
obj =
|
105
|
+
{
|
106
|
+
'jsonrpc'=>'2.0',
|
107
|
+
'method'=>method,
|
108
|
+
'params'=>params,
|
109
|
+
'auth'=>@auth,
|
110
|
+
'id'=>@id
|
111
|
+
}
|
112
|
+
debug(10, "json_obj: #{obj}")
|
113
|
+
return obj.to_json
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
#Performs a log in to the Zabbix server
|
118
|
+
#* _user_ is a string with the username
|
119
|
+
#* _password_ is a string with the user''s password
|
120
|
+
#* _save_ tells the method to save the login details in internal class variables or not
|
121
|
+
#
|
122
|
+
#Raises:
|
123
|
+
#
|
124
|
+
#* Zbx_API_ExeeptionBadAuth is raised when one of the following conditions is met
|
125
|
+
# 1. no-string variables were passed in
|
126
|
+
# 1. no username or password was passed in or saved from a previous login
|
127
|
+
# 1. login details were rejected by the server
|
128
|
+
#* ZbxAPI_ExceptionBadServerUrl
|
129
|
+
# 1. There was a socket error
|
130
|
+
# 1. The url used to create the class was bad
|
131
|
+
# 1. The connection to the server was refused
|
132
|
+
def login(user='',password='',save=true)
|
133
|
+
# p user.class
|
134
|
+
# p password.class
|
135
|
+
if user.class!=String or password.class!=String
|
136
|
+
raise ZbxAPI_ExceptionBadAuth.new,'Login called with non-string values'
|
137
|
+
end
|
138
|
+
if (user!='' and password!='') then
|
139
|
+
l_user = user
|
140
|
+
l_password = password
|
141
|
+
if save then
|
142
|
+
@user_name=user
|
143
|
+
@password=password
|
144
|
+
end
|
145
|
+
elsif (@user_name!='' and @password!='') then
|
146
|
+
l_user = @user_name
|
147
|
+
l_password = @password
|
148
|
+
else
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
begin
|
153
|
+
result = do_request(json_obj('user.authenticate',{'user'=>l_user,'password'=>l_password}))
|
154
|
+
@auth=result['result']
|
155
|
+
|
156
|
+
#setup the version variables
|
157
|
+
@major,@minor=do_request(json_obj('APIInfo.version',{}))['result'].split('.')
|
158
|
+
@major=@major.to_i
|
159
|
+
@minor=@minor.to_i
|
160
|
+
|
161
|
+
rescue SocketError
|
162
|
+
raise ZbxAPI_ExceptionBadServerUrl
|
163
|
+
rescue JSON::ParserError
|
164
|
+
raise ZbxAPI_ExceptionBadServerUrl
|
165
|
+
rescue Errno::ECONNREFUSED
|
166
|
+
raise ZbxAPI_ExceptionBadServerUrl
|
167
|
+
rescue ZbxAPI_GeneralError => e
|
168
|
+
if e.message["code"]==-32602
|
169
|
+
raise ZbxAPI_ExceptionBadAuth,'Bad username and/or password'
|
170
|
+
else
|
171
|
+
raise e
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
# Tests to determine if the login information is still valid
|
178
|
+
# returns: true if it is valid or false if it is not.
|
179
|
+
def test_login
|
180
|
+
if @auth!='' then
|
181
|
+
result = do_request(json_obj('user.checkauth',
|
182
|
+
{'sessionid'=>@auth}))
|
183
|
+
if !result['result'] then
|
184
|
+
@auth=''
|
185
|
+
return false #auth hash bad
|
186
|
+
end
|
187
|
+
return true #auth hash good
|
188
|
+
else
|
189
|
+
return false
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
#Returns true if a login was performed
|
194
|
+
def loggedin?
|
195
|
+
!(@auth=='' or @auth.nil?)
|
196
|
+
end
|
197
|
+
|
198
|
+
#wrapper to loggedin?
|
199
|
+
#returns nothing, raises the exception ZbxAPI_ExceptionBadAuth if loggedin? returns false
|
200
|
+
def checkauth
|
201
|
+
raise ZbxAPI_ExceptionBadAuth, 'Not logged in' if !loggedin?
|
202
|
+
end
|
203
|
+
|
204
|
+
#returns the version number for the API from the server
|
205
|
+
def API_version(options={})
|
206
|
+
return "#{@major}.#{@minor}"
|
207
|
+
end
|
208
|
+
|
209
|
+
# Provides raw access to the API via a small wrapper.
|
210
|
+
# _method_ is the method to be called
|
211
|
+
# _params_ are the parameters to be passed to the call
|
212
|
+
# returns a hash of the results from the server
|
213
|
+
def raw_api(method,params=nil)
|
214
|
+
debug(6,method,"method")
|
215
|
+
debug(6,params,"Parameters")
|
216
|
+
|
217
|
+
checkauth
|
218
|
+
checkversion(1,1)
|
219
|
+
params={} if params==nil
|
220
|
+
|
221
|
+
obj=do_request(json_obj(method,params))
|
222
|
+
return obj['result']
|
223
|
+
end
|
224
|
+
|
225
|
+
# Function to test whether or not a function will work with the current API version of the server
|
226
|
+
# If no options are presented the major and minor are assumed to be the minimum version
|
227
|
+
# number suitable to run the function
|
228
|
+
# Does not explicitly return anything, but raises ZbxAPI_ExceptionVersion if there is a problem
|
229
|
+
def checkversion(major,minor,options=nil)
|
230
|
+
caller[0]=~/`(.*?)'/
|
231
|
+
caller_func=$1
|
232
|
+
|
233
|
+
raise ZbxAPI_ExceptionVersion, "#{caller_func} requires API version #{major}.#{minor} or higher" if major>@major
|
234
|
+
raise ZbxAPI_ExceptionVersion, "#{caller_func} requires API version #{major}.#{minor} or higher" if minor>@minor
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
#Sends JSON encoded string to server
|
239
|
+
def do_request(json_obj)
|
240
|
+
#puts json_obj
|
241
|
+
redirects=0
|
242
|
+
begin # This is here for redirects
|
243
|
+
http = Net::HTTP.new(@url.host, @url.port)
|
244
|
+
http.use_ssl=true if @url.class==URI::HTTPS
|
245
|
+
response = nil
|
246
|
+
# http.set_debug_output($stderr) #Uncomment to see low level HTTP debug
|
247
|
+
# http.use_ssl = @url.scheme=='https' ? true : false
|
248
|
+
# http.start do |http|
|
249
|
+
headers={'Content-Type'=>'application/json-rpc',
|
250
|
+
'User-Agent'=>'Zbx Ruby CLI'}
|
251
|
+
debug(4,"Sending: #{json_obj}")
|
252
|
+
response = http.post(@url.path, json_obj,headers)
|
253
|
+
if response.code=="301"
|
254
|
+
puts "Redirecting to #{response['location']}"
|
255
|
+
@url=URI.parse(response['location'])
|
256
|
+
raise Redirect
|
257
|
+
end
|
258
|
+
debug(4,"Response Code: #{response.code}")
|
259
|
+
debug(4,response.body,"Response Body",5000)
|
260
|
+
# end
|
261
|
+
|
262
|
+
@id+=1 # increment the ID value for the API call
|
263
|
+
|
264
|
+
# check return code and throw exception for error checking
|
265
|
+
resp = JSON.parse(response.body) #parse the JSON Object so we can use it
|
266
|
+
if !resp["error"].nil?
|
267
|
+
errcode=resp["error"]["code"].to_i
|
268
|
+
case errcode
|
269
|
+
when -32602 then
|
270
|
+
raise ZbxAPI_ExceptionLoginPermission.new(resp["error"],:retry=>true)
|
271
|
+
when -32500 then
|
272
|
+
raise ZbxAPI_ExceptionPermissionError.new(resp["error"],:retry=>true)
|
273
|
+
else
|
274
|
+
puts "other error"
|
275
|
+
raise ZbxAPI_GeneralError, resp["error"]
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
return resp
|
280
|
+
|
281
|
+
rescue Redirect
|
282
|
+
redirects+=1
|
283
|
+
retry if redirects<=5
|
284
|
+
raise ZbxAPI_GeneralError, "Too many redirects"
|
285
|
+
rescue NoMethodError
|
286
|
+
raise ZbxAPI_GeneralError.new("Unable to connect to #{@url.host}", :retry=>false)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
private
|
291
|
+
|
292
|
+
def setup_connection
|
293
|
+
@http=Net::HTTP.new(@url.host, @url.port)
|
294
|
+
http.use_ssl=true if @url.class==URI::HTTPS
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Class: Zbx_API_Sub
|
299
|
+
# Wrapper class to ensure all class calls goes to the parent object not the
|
300
|
+
# currently instantiated object.
|
301
|
+
# Also ensures class specific variable sanity for global functions
|
302
|
+
class ZbxAPI_Sub < ZabbixAPI #:nodoc: all
|
303
|
+
attr_accessor :parent
|
304
|
+
|
305
|
+
def initialize(parent)
|
306
|
+
@parent=parent
|
307
|
+
end
|
308
|
+
|
309
|
+
def checkauth
|
310
|
+
@parent.checkauth
|
311
|
+
end
|
312
|
+
|
313
|
+
def checkversion(major,minor,options=nil)
|
314
|
+
@parent.checkversion(major,minor,options)
|
315
|
+
end
|
316
|
+
|
317
|
+
def do_request(req)
|
318
|
+
return @parent.do_request(req)
|
319
|
+
end
|
320
|
+
|
321
|
+
def json_obj(method, param)
|
322
|
+
return @parent.json_obj(method, param)
|
323
|
+
end
|
324
|
+
|
325
|
+
def debug(level,param="",message=nil)
|
326
|
+
@parent.debug(level,param,message)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
# Class ZbxAPI_User
|
331
|
+
#
|
332
|
+
# Class encapsulating User functions
|
333
|
+
#
|
334
|
+
# API Function Status
|
335
|
+
# [get] Implemented, need error checking
|
336
|
+
# [authenticate] Will not implement here, belongs in ZabbixAPI main class
|
337
|
+
# [checkauth] Will not implement here, belongs in ZabbixAPI main class
|
338
|
+
# [getid] Implemented
|
339
|
+
# [create] Implemented, need to test more to find fewest items
|
340
|
+
# needed, input value testing needed
|
341
|
+
# [update]
|
342
|
+
#
|
343
|
+
# [addmedia]
|
344
|
+
#
|
345
|
+
# [deletemedia]
|
346
|
+
#
|
347
|
+
# [updatemedia]
|
348
|
+
# [delete] Implemented, checking of input values needed
|
349
|
+
#
|
350
|
+
# All functions expect a hash of options to add.
|
351
|
+
# If multiple users need to be manipulated it must be broken out into different calls
|
352
|
+
|
353
|
+
class ZbxAPI_User < ZbxAPI_Sub
|
354
|
+
def get(options={})
|
355
|
+
checkauth
|
356
|
+
checkversion(1,1)
|
357
|
+
|
358
|
+
obj=do_request(json_obj('user.get',options))
|
359
|
+
return obj['result']
|
360
|
+
end
|
361
|
+
|
362
|
+
def getid(username)
|
363
|
+
raise ZbxAPI_ExceptionArgumentError, "String argument expected" if username.class != String
|
364
|
+
|
365
|
+
checkauth
|
366
|
+
checkversion(1,1)
|
367
|
+
|
368
|
+
obj=do_request(json_obj('user.getid',{'alias'=>username}))
|
369
|
+
return obj['result']
|
370
|
+
end
|
371
|
+
|
372
|
+
def create(options)
|
373
|
+
checkauth
|
374
|
+
checkversion(1,1)
|
375
|
+
|
376
|
+
#Check input parameters
|
377
|
+
|
378
|
+
raise ZbxAPI_ParameterError, "Missing 'name' argument", "User.create" if options["name"].nil?
|
379
|
+
raise ZbxAPI_ParameterError, "Missing 'alias' argument", "User.create" if options["alias"].nil?
|
380
|
+
raise ZbxAPI_ParameterError, "Missing 'passwd' argument", "User.create" if options["passwd"].nil?
|
381
|
+
|
382
|
+
obj=do_request(json_obj('user.create',options))
|
383
|
+
return obj['result']
|
384
|
+
end
|
385
|
+
|
386
|
+
# Alias function name for code written to work against 1.0 API
|
387
|
+
# may be removed in future versions
|
388
|
+
|
389
|
+
def add(options)
|
390
|
+
puts "WARNING API Function User.add is deprecated and will be removed in the future without further warning"
|
391
|
+
create(options)
|
392
|
+
end
|
393
|
+
|
394
|
+
def delete(userid)
|
395
|
+
checkauth
|
396
|
+
checkversion(1,1)
|
397
|
+
|
398
|
+
obj=do_request(json_obj('user.delete',[userid]))
|
399
|
+
return obj['result']
|
400
|
+
end
|
401
|
+
|
402
|
+
def update(options)
|
403
|
+
checkauth
|
404
|
+
checkversion(1,1)
|
405
|
+
|
406
|
+
obj=do_request(json_obj('user.update',options))
|
407
|
+
return obj['result']
|
408
|
+
end
|
409
|
+
|
410
|
+
# addmedia expects a hash of the following variables
|
411
|
+
# userid, mediatypeid, sendto, severity, active, period
|
412
|
+
def addmedia(options)
|
413
|
+
debug(8, "User.addmedia Start")
|
414
|
+
checkauth
|
415
|
+
checkversion(1,1)
|
416
|
+
|
417
|
+
# p options
|
418
|
+
|
419
|
+
raise ZbxAPI_ParameterError, "Missing 'userid' argument", "User.addmedia" if options["userid"].nil?
|
420
|
+
raise ZbxAPI_ParameterError, "Missing 'mediatypeid' argument", "User.addmedia" if options["mediatypeid"].nil?
|
421
|
+
raise ZbxAPI_ParameterError, "Missing 'severity' argument", "User.addmedia" if options["severity"].nil?
|
422
|
+
raise ZbxAPI_ParameterError, "Missing 'active' argument", "User.addmedia" if options["active"].nil?
|
423
|
+
raise ZbxAPI_ParameterError, "Missing 'period' argument", "User.addmedia" if options["period"].nil?
|
424
|
+
|
425
|
+
args = {}
|
426
|
+
args["userid"]=options["userid"]
|
427
|
+
args["medias"]={}
|
428
|
+
args["medias"]["mediatypeid"]=options["mediatypeid"]
|
429
|
+
args["medias"]["sendto"]=options["sendto"]
|
430
|
+
args["medias"]["severity"]=options["severity"]
|
431
|
+
args["medias"]["active"]=options["active"]
|
432
|
+
args["medias"]["period"]=options["period"]
|
433
|
+
|
434
|
+
# p args
|
435
|
+
|
436
|
+
obj=do_request(json_obj('user.addMedia',args))
|
437
|
+
return obj['result']
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
#******************************************************************************
|
442
|
+
#
|
443
|
+
# Class ZbxAPI_Host
|
444
|
+
#
|
445
|
+
# Class encapsulating Host and template functions
|
446
|
+
#
|
447
|
+
# API Function Status
|
448
|
+
# get Basic function implemented
|
449
|
+
# getid
|
450
|
+
# create Basic function implemented 20091020
|
451
|
+
# update
|
452
|
+
# massupdate
|
453
|
+
# delete Implimented
|
454
|
+
#
|
455
|
+
# template.create implemented as host.create_template
|
456
|
+
# template.get implemented as host.get_template
|
457
|
+
# template.delete implemented as host.delete_template
|
458
|
+
#
|
459
|
+
#******************************************************************************
|
460
|
+
|
461
|
+
class ZbxAPI_Host < ZbxAPI_Sub
|
462
|
+
def get(options={})
|
463
|
+
checkauth
|
464
|
+
checkversion(1,1)
|
465
|
+
|
466
|
+
obj=do_request(json_obj('host.get',options))
|
467
|
+
obj['result']
|
468
|
+
end
|
469
|
+
|
470
|
+
def get_template(options={})
|
471
|
+
checkauth
|
472
|
+
checkversion(1,3)
|
473
|
+
|
474
|
+
obj=do_request(json_obj('template.get',options))
|
475
|
+
obj['result']
|
476
|
+
end
|
477
|
+
|
478
|
+
def create(options={})
|
479
|
+
checkauth
|
480
|
+
checkversion(1,1)
|
481
|
+
|
482
|
+
obj=do_request(json_obj('host.create',options))
|
483
|
+
obj['result']
|
484
|
+
end
|
485
|
+
|
486
|
+
def create_template(options={})
|
487
|
+
checkauth
|
488
|
+
checkversion(1,3)
|
489
|
+
|
490
|
+
obj=do_request(json_obj('template.create',options))
|
491
|
+
obj['result']
|
492
|
+
end
|
493
|
+
|
494
|
+
# http://www.zabbix.com/documentation/1.8/api/objects/host#hostdelete
|
495
|
+
#Accepts a single host id or an array of host id's to be deleted
|
496
|
+
def delete(ids)
|
497
|
+
checkauth
|
498
|
+
checkversion(1,3)
|
499
|
+
|
500
|
+
obj=do_request(json_obj('host.delete',delete_helper("hostid",ids)))
|
501
|
+
obj['result']
|
502
|
+
end
|
503
|
+
|
504
|
+
def delete_template(ids)
|
505
|
+
checkauth
|
506
|
+
checkversion(1,3)
|
507
|
+
|
508
|
+
obj=do_request(json_obj('template.delete',delete_helper("templateid",ids)))
|
509
|
+
obj['result']
|
510
|
+
end
|
511
|
+
|
512
|
+
private
|
513
|
+
|
514
|
+
def delete_helper(id_type,ids)
|
515
|
+
if ids.class==Fixnum
|
516
|
+
ids=[ids]
|
517
|
+
elsif ids.class==Array
|
518
|
+
ids=ids
|
519
|
+
else
|
520
|
+
raise ZbxAPI_ParameterError, "ids parameter must be number or array"
|
521
|
+
end
|
522
|
+
|
523
|
+
ids.map do |id|
|
524
|
+
{id_type=>id}
|
525
|
+
end
|
526
|
+
end
|
527
|
+
|
528
|
+
end
|
529
|
+
|
530
|
+
#******************************************************************************
|
531
|
+
#
|
532
|
+
# Class ZbxAPI_Item
|
533
|
+
#
|
534
|
+
# Class encapsulating Item functions
|
535
|
+
#
|
536
|
+
# API Function Status
|
537
|
+
# get Basic Function working
|
538
|
+
# getid Function implemented
|
539
|
+
# create Function implemented
|
540
|
+
# update
|
541
|
+
# delete Function implemented - need to add type checking to input
|
542
|
+
#
|
543
|
+
#******************************************************************************
|
544
|
+
|
545
|
+
class ZbxAPI_Item < ZbxAPI_Sub
|
546
|
+
def get(options={})
|
547
|
+
checkauth
|
548
|
+
checkversion(1,1)
|
549
|
+
|
550
|
+
obj=do_request(json_obj('item.get',options))
|
551
|
+
return obj['result']
|
552
|
+
end
|
553
|
+
|
554
|
+
def getid(options)
|
555
|
+
checkauth
|
556
|
+
checkversion(1,1)
|
557
|
+
|
558
|
+
obj=do_request(json_obj('item.getid', options))
|
559
|
+
return obj['result']
|
560
|
+
end
|
561
|
+
|
562
|
+
def create(options)
|
563
|
+
debug(8,options)
|
564
|
+
checkauth
|
565
|
+
checkversion(1,1)
|
566
|
+
|
567
|
+
obj=do_request(json_obj('item.create', options))
|
568
|
+
return obj['result']
|
569
|
+
end
|
570
|
+
|
571
|
+
# Alias function for code written against 1.0 API
|
572
|
+
def add(options)
|
573
|
+
puts "WARNING API Function Item.add is deprecated and will be removed in the future without further warning"
|
574
|
+
create(options)
|
575
|
+
end
|
576
|
+
|
577
|
+
def delete(ids)
|
578
|
+
checkauth
|
579
|
+
checkversion(1,1)
|
580
|
+
|
581
|
+
obj=do_request(json_obj('item.delete', ids))
|
582
|
+
return obj['result']
|
583
|
+
end
|
584
|
+
end
|
585
|
+
|
586
|
+
#******************************************************************************
|
587
|
+
#
|
588
|
+
# Class ZbxAPI_UserGroup
|
589
|
+
#
|
590
|
+
# Class encapsulating User Group functions
|
591
|
+
#
|
592
|
+
# API Function Status
|
593
|
+
# get Basic function implemented
|
594
|
+
# getid
|
595
|
+
# create
|
596
|
+
# update
|
597
|
+
# updaterights
|
598
|
+
# addrights
|
599
|
+
# addusers
|
600
|
+
# removeusers
|
601
|
+
# delete
|
602
|
+
#
|
603
|
+
#******************************************************************************
|
604
|
+
|
605
|
+
class ZbxAPI_UserGroup < ZbxAPI_Sub
|
606
|
+
def get(options={})
|
607
|
+
checkauth
|
608
|
+
checkversion(1,1)
|
609
|
+
|
610
|
+
obj=do_request(json_obj('usergroup.get',options))
|
611
|
+
return obj['result']
|
612
|
+
end
|
613
|
+
end
|
614
|
+
|
615
|
+
#******************************************************************************
|
616
|
+
#
|
617
|
+
# Class ZbxAPI_HostGroup
|
618
|
+
#
|
619
|
+
# Class encapsulating User Group functions
|
620
|
+
#
|
621
|
+
# API Function Status
|
622
|
+
# get Basic function implemented
|
623
|
+
# getid
|
624
|
+
# create
|
625
|
+
# update
|
626
|
+
# delete
|
627
|
+
# addhosts
|
628
|
+
# removehost
|
629
|
+
# addgroupstohost
|
630
|
+
# updategroupstohost
|
631
|
+
#
|
632
|
+
#******************************************************************************
|
633
|
+
|
634
|
+
class ZbxAPI_HostGroup < ZbxAPI_Sub
|
635
|
+
def create(options={})
|
636
|
+
debug(8, "HostGroup.create Start")
|
637
|
+
checkauth
|
638
|
+
checkversion(1,1)
|
639
|
+
|
640
|
+
obj=do_request(json_obj('hostgroup.create',options))
|
641
|
+
return obj['result']
|
642
|
+
end
|
643
|
+
|
644
|
+
# alias function for code written against 1.0 API
|
645
|
+
def add(options={})
|
646
|
+
puts "WARNING API Function HostGroup.add is deprecated and will be removed in the future without further warning"
|
647
|
+
create(options)
|
648
|
+
end
|
649
|
+
|
650
|
+
def get(options={})
|
651
|
+
debug(8, "HostGroup.get Start")
|
652
|
+
checkauth
|
653
|
+
checkversion(1,1)
|
654
|
+
|
655
|
+
obj=do_request(json_obj('hostgroup.get',options))
|
656
|
+
return obj['result']
|
657
|
+
end
|
658
|
+
|
659
|
+
def getId(name)
|
660
|
+
puts "WARNING API Function HostGroup.getId is deprecated and will be removed in the future without further warning"
|
661
|
+
getObjects(name)
|
662
|
+
end
|
663
|
+
|
664
|
+
def getObjects(name)
|
665
|
+
debug(8, "HostGroup.getId Start")
|
666
|
+
checkauth
|
667
|
+
checkversion(1,1)
|
668
|
+
|
669
|
+
begin
|
670
|
+
if name.class==String
|
671
|
+
do_request(json_obj('hostgroup.getObjects',{"name"=>name}))['result']
|
672
|
+
elsif name.class==Array
|
673
|
+
valid = name.map {|item| item.class==String ? nil : false} # create a validation array of nils or false
|
674
|
+
valid.compact! # remove nils
|
675
|
+
raise ZbxAPI_ParameterError, "Expected a string or an array of strings" if !valid.empty?
|
676
|
+
|
677
|
+
results=[]
|
678
|
+
name.each do |item|
|
679
|
+
response=do_request(json_obj('hostgroup.getObjects',{"name"=>item}))
|
680
|
+
response['result'].each {|result| results << result } # Just in case the server returns an array
|
681
|
+
end
|
682
|
+
results
|
683
|
+
else
|
684
|
+
raise ZbxAPI_ParameterError, "Expected a string or an array of strings"
|
685
|
+
end
|
686
|
+
rescue ZbxAPI_GeneralError => e
|
687
|
+
if e.message["code"]==-32602
|
688
|
+
return 0
|
689
|
+
else
|
690
|
+
raise e
|
691
|
+
end
|
692
|
+
end
|
693
|
+
end
|
694
|
+
end
|
695
|
+
|
696
|
+
#******************************************************************************
|
697
|
+
|
698
|
+
#
|
699
|
+
# Class ZbxAPI_Application
|
700
|
+
#
|
701
|
+
# Class encapsulating application functions
|
702
|
+
#
|
703
|
+
# API Function Status
|
704
|
+
# get Not implemented
|
705
|
+
# getById Implemented
|
706
|
+
# getId Not implemented
|
707
|
+
# create Not implemented
|
708
|
+
# update Not implemented
|
709
|
+
# delete Not implemented
|
710
|
+
#
|
711
|
+
#******************************************************************************
|
712
|
+
|
713
|
+
|
714
|
+
class ZbxAPI_Application < ZbxAPI_Sub
|
715
|
+
def get(options={})
|
716
|
+
debug(8, "Application.get Start")
|
717
|
+
checkauth
|
718
|
+
checkversion(1,1)
|
719
|
+
|
720
|
+
obj=do_request(json_obj('application.get',options))
|
721
|
+
return obj['result']
|
722
|
+
end
|
723
|
+
|
724
|
+
def create(options={})
|
725
|
+
debug(8, "Application.create Start")
|
726
|
+
checkauth
|
727
|
+
checkversion(1,1)
|
728
|
+
|
729
|
+
obj=do_request(json_obj('application.create',options))
|
730
|
+
return obj['result']
|
731
|
+
end
|
732
|
+
|
733
|
+
# Alias function for code written against 1.0 API
|
734
|
+
def add(options={})
|
735
|
+
puts "WARNING API Function Application.add is deprecated and will be removed in the future without further warning"
|
736
|
+
create(options)
|
737
|
+
end
|
738
|
+
|
739
|
+
def getid(options={})
|
740
|
+
debug(8, "Application.getid Start")
|
741
|
+
checkauth
|
742
|
+
checkversion(1,1)
|
743
|
+
|
744
|
+
begin
|
745
|
+
obj=do_request(json_obj('application.getid',options))
|
746
|
+
rescue ZbxAPI_GeneralError => e
|
747
|
+
if e.message["code"]==-32400
|
748
|
+
return 0
|
749
|
+
else
|
750
|
+
raise e
|
751
|
+
end
|
752
|
+
end
|
753
|
+
return obj['result']
|
754
|
+
end
|
755
|
+
end
|
756
|
+
|
757
|
+
#******************************************************************************
|
758
|
+
#
|
759
|
+
# Class ZbxAPI_Trigger
|
760
|
+
#
|
761
|
+
# Class encapsulating trigger functions
|
762
|
+
#
|
763
|
+
# get Implemented
|
764
|
+
# getById Not implemented
|
765
|
+
# getId Not implemented
|
766
|
+
# create Implemented
|
767
|
+
# update Not implemented
|
768
|
+
# delete Not implemented
|
769
|
+
# addDependency Not implemented
|
770
|
+
#
|
771
|
+
#******************************************************************************
|
772
|
+
|
773
|
+
|
774
|
+
class ZbxAPI_Trigger < ZbxAPI_Sub
|
775
|
+
def get(options={})
|
776
|
+
debug(8, "Trigger.get Start")
|
777
|
+
checkauth
|
778
|
+
checkversion(1,1)
|
779
|
+
|
780
|
+
obj=do_request(json_obj('trigger.get',options))
|
781
|
+
return obj['result']
|
782
|
+
end
|
783
|
+
|
784
|
+
# Function name changed to reflect 1.1 API changes
|
785
|
+
def create(options={})
|
786
|
+
debug(8, "Trigger.create Start")
|
787
|
+
checkauth
|
788
|
+
checkversion(1,1)
|
789
|
+
|
790
|
+
obj=do_request(json_obj('trigger.create',options))
|
791
|
+
return obj['result']
|
792
|
+
end
|
793
|
+
|
794
|
+
# Alias function for code written against 1.0 api
|
795
|
+
def add(options={})
|
796
|
+
puts "WARNING API Function Trigger.add is deprecated and will be removed in the future without further warning"
|
797
|
+
create(options)
|
798
|
+
end
|
799
|
+
end
|
800
|
+
|
801
|
+
#******************************************************************************
|
802
|
+
#
|
803
|
+
# Class ZbxAPI_Sysmap
|
804
|
+
#
|
805
|
+
# Class encapsulating sysmap functions
|
806
|
+
#
|
807
|
+
# get Not implemented
|
808
|
+
# cr eate Basic implementation
|
809
|
+
#
|
810
|
+
#******************************************************************************
|
811
|
+
|
812
|
+
class ZbxAPI_Sysmap < ZbxAPI_Sub
|
813
|
+
def create(options={})
|
814
|
+
debug(8, "Sysmap.create Start")
|
815
|
+
checkauth
|
816
|
+
checkversion(1,1)
|
817
|
+
|
818
|
+
obj=do_request(json_obj('map.create',options))
|
819
|
+
return obj['result']
|
820
|
+
end
|
821
|
+
|
822
|
+
# Alias function for code written against 1.0 API
|
823
|
+
def add(options={})
|
824
|
+
puts "WARNING API Function Sysmap.add is deprecated and will be removed in the future without further warning"
|
825
|
+
create(options)
|
826
|
+
end
|
827
|
+
|
828
|
+
def addelement(options={})
|
829
|
+
debug(8, "Sysmap.addelement Start")
|
830
|
+
checkauth
|
831
|
+
checkversion(1,1)
|
832
|
+
|
833
|
+
obj=do_request(json_obj('map.addelement',options))
|
834
|
+
return obj['result']
|
835
|
+
end
|
836
|
+
|
837
|
+
def addlink(options={})
|
838
|
+
debug(8, "Sysmap.addlink Start")
|
839
|
+
checkauth
|
840
|
+
checkversion(1,1)
|
841
|
+
|
842
|
+
obj=do_request(json_obj('map.addlink',options))
|
843
|
+
return obj['result']
|
844
|
+
end
|
845
|
+
|
846
|
+
def getseid(options={})
|
847
|
+
debug(8, "Sysmap.getseid Start")
|
848
|
+
checkauth
|
849
|
+
checkversion(1,1)
|
850
|
+
|
851
|
+
obj=do_request(json_obj('map.getseid',options))
|
852
|
+
return obj['result']
|
853
|
+
end
|
854
|
+
|
855
|
+
def addlinktrigger(options={})
|
856
|
+
debug(8, "Sysmap.addlinktrigger Start")
|
857
|
+
checkauth
|
858
|
+
checkversion(1,1)
|
859
|
+
|
860
|
+
obj=do_request(json_obj('map.addlinktrigger',options))
|
861
|
+
return obj['result']
|
862
|
+
end
|
863
|
+
end
|
864
|
+
|
865
|
+
#******************************************************************************
|
866
|
+
#
|
867
|
+
# Class ZbxAPI_History
|
868
|
+
#
|
869
|
+
# Class encapsulating history functions
|
870
|
+
#
|
871
|
+
# get
|
872
|
+
#
|
873
|
+
#******************************************************************************
|
874
|
+
|
875
|
+
class ZbxAPI_History
|
876
|
+
|
877
|
+
def initialize(server)
|
878
|
+
@server=server
|
879
|
+
end
|
880
|
+
|
881
|
+
#Get the history for an item.
|
882
|
+
# itemids is a required option
|
883
|
+
# example: get({"itemids"=>12345})
|
884
|
+
def get(options)
|
885
|
+
@server.checkauth
|
886
|
+
@server.checkversion(1,3)
|
887
|
+
|
888
|
+
raise ZbxAPI_ParameterError, "Missing 'itemid'", "History.get" if options["itemids"].nil?
|
889
|
+
|
890
|
+
p obj=@server.raw_api("history.get",options)
|
891
|
+
return obj['result']
|
892
|
+
end
|
893
|
+
|
894
|
+
end
|
895
|
+
|
896
|
+
|
897
|
+
#******************************************************************************
|
898
|
+
|
899
|
+
|
900
|
+
if __FILE__ == $0
|
901
|
+
|
902
|
+
puts "Performing login"
|
903
|
+
zbx_api = ZabbixAPI.new('http://localhost')
|
904
|
+
zbx_api.login('apitest','test')
|
905
|
+
|
906
|
+
puts
|
907
|
+
puts "Getting user groups"
|
908
|
+
p zbx_api.usergroup.get
|
909
|
+
|
910
|
+
puts
|
911
|
+
puts "testing user.get"
|
912
|
+
zbx_api.debug_level=8
|
913
|
+
p zbx_api.user.get()
|
914
|
+
p zbx_api.user.get({"extendoutput"=>true})
|
915
|
+
zbx_api.debug_level=0
|
916
|
+
|
917
|
+
|
918
|
+
puts
|
919
|
+
puts "Getting by username, admin, number should be seen"
|
920
|
+
p zbx_api.user.getid('admin')
|
921
|
+
puts "Trying a bogus username"
|
922
|
+
p zbx_api.user.getid('bogus')
|
923
|
+
|
924
|
+
puts
|
925
|
+
puts "adding the user 'test' to Zabbix"
|
926
|
+
uid= zbx_api.user.create(
|
927
|
+
[{ "name"=>"test",
|
928
|
+
"alias"=>"testapiuser",
|
929
|
+
"password"=>"test",
|
930
|
+
"url"=>"",
|
931
|
+
"autologin"=>0,
|
932
|
+
"autologout"=>900,
|
933
|
+
"theme"=>"default.css",
|
934
|
+
"refresh"=>60,
|
935
|
+
"rows_per_page"=>50,
|
936
|
+
"lang"=>"en_GB",
|
937
|
+
"type"=>3}])
|
938
|
+
p uid
|
939
|
+
puts "Deleting userid #{uid.keys[0]}"
|
940
|
+
p zbx_api.user.delete(uid.values[0])
|
941
|
+
|
942
|
+
puts
|
943
|
+
puts "getting items"
|
944
|
+
p zbx_api.item.get
|
945
|
+
|
946
|
+
puts
|
947
|
+
puts "getting items by host"
|
948
|
+
puts "host: #{hosts.values[0]}"
|
949
|
+
p items=zbx_api.item.get({'hostids'=>hosts.values[0]})
|
950
|
+
|
951
|
+
end
|