zabcon 0.0.6 → 0.0.327

Sign up to get free protection for your applications and to get access to all the features.
data/libs/zbxcliserver.rb DELETED
@@ -1,325 +0,0 @@
1
- #GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
2
- #Zabbix CLI Tool and associated files
3
- #Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
4
- #
5
- #This program is free software; you can redistribute it and/or
6
- #modify it under the terms of the GNU General Public License
7
- #as published by the Free Software Foundation; either version 2
8
- #of the License, or (at your option) any later version.
9
- #
10
- #This program 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
13
- #GNU General Public License for more details.
14
- #
15
- #You should have received a copy of the GNU General Public License
16
- #along with this program; if not, write to the Free Software
17
- #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
-
19
- ##########################################
20
- # Subversion information
21
- # $Id: zbxcliserver.rb 281 2011-04-06 18:10:16Z nelsonab $
22
- # $Revision: 281 $
23
- ##########################################
24
-
25
- require 'zbxapi'
26
- require 'libs/zdebug'
27
- require 'libs/zabcon_globals'
28
-
29
- class ZabbixServer < ZabbixAPI
30
- alias zbxapi_initialize initialize
31
- alias zbxapi_do_request do_request
32
-
33
- def initialize(url,debug_level=0)
34
- @env = EnvVars.instance
35
- zbxapi_initialize(url,debug_level)
36
- end
37
-
38
- #truncate_length is set to the symbol :not_used as do_request is passed a different variable
39
- def do_request(json_obj,truncate_length=:not_used)
40
- zbxapi_do_request(json_obj,@env["truncate_length"])
41
- end
42
- end
43
-
44
- class ZbxCliServer
45
-
46
- include ZDebug
47
-
48
- attr_reader :server_url, :user, :password
49
-
50
- def initialize(server,user,password,debuglevel=0)
51
- @server_url=server
52
- @user=user
53
- @password=password
54
- @debuglevel=debuglevel
55
- # *Note* Do not rescue errors here, rescue in function that calls this block
56
- @server=ZabbixServer.new(@server_url,@debuglevel)
57
- @server.login(@user, @password)
58
- GlobalVars.instance["auth"]=@server.auth
59
- end
60
-
61
- def debuglevel=(level)
62
- @server.debug_level=level
63
- end
64
-
65
- def login?
66
- !@server.nil?
67
- end
68
-
69
- def version
70
- @server.API_version
71
- end
72
-
73
- def reconnect
74
- @server.login(@user,@password)
75
- end
76
-
77
- def getuser(parameters)
78
- debug(6,parameters)
79
-
80
- result=@server.user.get(parameters)
81
- {:class=>:user, :result=>result}
82
- end
83
-
84
- def gethost(parameters)
85
- debug(6,parameters)
86
-
87
- result=@server.host.get(parameters)
88
- {:class=>:host, :result=>result}
89
- end
90
-
91
- def addhost(parameters)
92
- debug(6,parameters)
93
- result=@server.host.create(parameters)
94
- {:class=>:host, :message=>"The following host was created: #{result['hostids']}", :result=>result}
95
- end
96
-
97
- def deletehost(parameters)
98
- debug(6,parameters)
99
- result=@server.host.delete(parameters)
100
- {:class=>:host, :message=>"The following host(s) was/were deleted: #{result['hostids']}", :result=>result}
101
- end
102
-
103
- def getitem(parameters)
104
- debug(6,parameters)
105
-
106
- result=@server.item.get(parameters)
107
- {:class=>:item, :result=>result}
108
- end
109
-
110
- def additem(parameters)
111
- debug(6,parameters)
112
- {:class=>:item, :result=>@server.item.create(parameters)}
113
- end
114
-
115
- def deleteitem(parameters)
116
- debug(6,parameters)
117
- {:class=>:item, :result=>@server.item.delete(parameters)}
118
- end
119
-
120
- def adduser(parameters)
121
- debug(6,parameters)
122
- begin
123
- uid=@server.user.create(parameters)
124
- puts "Created userid: #{uid["userids"]}"
125
- rescue ZbxAPI_ParameterError => e
126
- puts "Add user failed, error: #{e.message}"
127
- end
128
- end
129
-
130
- def deleteuser(parameter)
131
- debug(6,parameter)
132
- id=0 #id to delete
133
- # if parameters.nil? then
134
- # puts "User id required"
135
- # return
136
- # end
137
-
138
- if !parameter["name"].nil?
139
- users=@server.user.get({"pattern"=>parameter["name"], "extendoutput"=>true})
140
- users.each { |user| id=user["userid"] if user["alias"]==parameter }
141
- else
142
- id=parameter["id"]
143
- end
144
- result=@server.user.delete(id)
145
- if !result.empty?
146
- puts "Deleted user id #{result["userids"]}"
147
- else
148
- puts "Error deleting #{parameter.to_a[0][1]}"
149
- end
150
- end
151
-
152
- def updateuser(parameters)
153
- debug(6,parameters)
154
- valid_parameters=['userid','name', 'surname', 'alias', 'passwd', 'url', 'autologin',
155
- 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type',]
156
- if parameters.nil? or parameters["userid"].nil? then
157
- puts "Edit User requires arguments, valid fields are:"
158
- puts "name, surname, alias, passwd, url, autologin, autologout, lang, theme, refresh"
159
- puts "rows_per_page, type"
160
- puts "userid is a required field"
161
- puts "example: edit user userid=<id> name=someone alias=username passwd=pass autologout=0"
162
- return false
163
- else
164
- p_keys = parameters.keys
165
-
166
- valid_parameters.each {|key| p_keys.delete(key)}
167
- if !p_keys.empty? then
168
- puts "Invalid items"
169
- p p_keys
170
- return false
171
- elsif parameters["userid"].nil?
172
- puts "Missing required userid statement."
173
- end
174
- p @server.user.update([parameters]) #TODO: remove print statement or comment if needed
175
- end
176
- end
177
-
178
- def addusermedia(parameters)
179
- debug(6,parameters)
180
- valid_parameters=["userid", "mediatypeid", "sendto", "severity", "active", "period"]
181
-
182
- if parameters.nil? then
183
- puts "add usermedia requires arguments, valid fields are:"
184
- puts "userid, mediatypeid, sendto, severity, active, period"
185
- puts "example: add usermedia userid=<id> mediatypeid=1 sendto=myemail@address.com severity=63 active=1 period=\"\""
186
- else
187
-
188
- p_keys = parameters.keys
189
-
190
- valid_parameters.each {|key| p_keys.delete(key)}
191
- if !p_keys.empty? then
192
- puts "Invalid items"
193
- p p_keys
194
- return false
195
- elsif parameters["userid"].nil?
196
- puts "Missing required userid statement."
197
- end
198
- begin
199
- @server.user.addmedia(parameters)
200
- rescue ZbxAPI_ParameterError => e
201
- puts e.message
202
- end
203
- end
204
-
205
- end
206
-
207
- def addhostgroup(parameters)
208
- debug(6,parameters)
209
- result = @server.hostgroup.create(parameters)
210
- {:class=>:hostgroup, :result=>result}
211
- end
212
-
213
- def gethostgroup(parameters)
214
- debug(6,parameters)
215
-
216
- result=@server.hostgroup.get(parameters)
217
- {:class=>:hostgroup, :result=>result}
218
- end
219
-
220
- def gethostgroupid(parameters)
221
- debug(6,parameters)
222
- result = @server.hostgroup.getObjects(parameters)
223
- {:class=>:hostgroupid, :result=>result}
224
- end
225
-
226
- def getapp(parameters)
227
- debug(6,parameters)
228
-
229
- result=@server.application.get(parameters)
230
- {:class=>:application, :result=>result}
231
- end
232
-
233
- def addapp(parameters)
234
- debug(6,parameters)
235
- result=@server.application.create(parameters)
236
- {:class=>:application, :result=>result}
237
- end
238
-
239
- def getappid(parameters)
240
- debug(6,parameters)
241
- result=@server.application.getid(parameters)
242
- {:class=>:application, :result=>result}
243
- end
244
-
245
- def gettrigger(parameters)
246
- debug(6,parameters)
247
- result=@server.trigger.get(parameters)
248
- {:class=>:trigger, :result=>result}
249
- end
250
-
251
- # addtrigger( { trigger1, trigger2, triggern } )
252
- # Only expression and description are mandatory.
253
- # { { expression, description, type, priority, status, comments, url }, { ...} }
254
- def addtrigger(parameters)
255
- debug(6,parameters)
256
- result=@server.trigger.create(parameters)
257
- {:class=>:trigger, :result=>result}
258
- end
259
-
260
- def addlink(parameters)
261
- debug(6,parameters)
262
- result=@server.sysmap.addlink(parameters)
263
- {:class=>:map, :result=>result}
264
- end
265
-
266
- def addsysmap(parameters)
267
- debug(6,parameters)
268
- result=@server.sysmap.create(parameters)
269
- {:class=>:map, :result=>result}
270
- end
271
-
272
- def addelementtosysmap(parameters)
273
- debug(6,parameters)
274
- result=@server.sysmap.addelement(parameters)
275
- {:class=>:map, :result=>result}
276
- end
277
-
278
- def getseid(parameters)
279
- debug(6,parameters)
280
- result=@server.sysmap.getseid(parameters)
281
- {:class=>:map, :result=>result}
282
- end
283
-
284
- def addlinktrigger(parameters)
285
- debug(6,parameters)
286
- result=@server.sysmap.addlinktrigger(parameters)
287
- {:class=>:map, :result=>result}
288
- end
289
-
290
- def raw_api(parameters)
291
- debug(6,parameters)
292
- result=@server.raw_api(parameters[:method],parameters[:params])
293
- {:class=>:raw, :result=>result}
294
- end
295
-
296
- def raw_json(parameters)
297
- debug(6,parameters)
298
- begin
299
- result=@server.do_request(parameters)
300
- {:class=>:raw, :result=>result["result"]}
301
- rescue ZbxAPI_GeneralError => e
302
- puts "An error was received from the Zabbix server"
303
- if e.message.class==Hash
304
- puts "Error code: #{e.message["code"]}"
305
- puts "Error message: #{e.message["message"]}"
306
- puts "Error data: #{e.message["data"]}"
307
- end
308
- puts "Original text:"
309
- puts parameters
310
- puts
311
- return {:class=>:raw, :result=>nil}
312
- end
313
- end
314
-
315
- end
316
-
317
- ##############################################
318
- # Unit test
319
- ##############################################
320
-
321
- if __FILE__ == $0
322
- zbxcliserver = ZbxCliServer.new("http://localhost/","apitest","test") #Change as appropriate for platform
323
-
324
- p zbxcliserver.getuser(nil)
325
- end