zabcon 0.0.1

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.
@@ -0,0 +1,45 @@
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: zabcon_exceptions.rb 258 2010-12-28 22:49:21Z nelsonab $
22
+ # $Revision: 258 $
23
+ ##########################################
24
+
25
+ #------------------------------------------------------------------------------
26
+ #
27
+ # Class ZbxAP_ParameterError
28
+ #
29
+ # Exception class for parameter errors for Argument Processor calls.
30
+ #
31
+ #------------------------------------------------------------------------------
32
+
33
+ require 'libs/zdebug'
34
+ require 'libs/exceptions'
35
+
36
+ #---------------------------------------------------------
37
+ #These general exceptions are only used internally and are
38
+ #not meant to be used by the end consumer of this program
39
+ #---------------------------------------------------------
40
+
41
+ class Redirect < Exception
42
+ end
43
+
44
+ class ZabconError < ZError
45
+ end
@@ -0,0 +1,210 @@
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: zabcon_exceptions.rb 181 2010-04-08 03:33:18Z nelsonab $
22
+ # $Revision: 181 $
23
+ ##########################################
24
+
25
+ #------------------------------------------------------------------------------
26
+ #
27
+ # Class ZbxAP_ParameterError
28
+ #
29
+ # Exception class for parameter errors for Argument Processor calls.
30
+ #
31
+ #------------------------------------------------------------------------------
32
+
33
+ require 'libs/zdebug'
34
+
35
+ require 'singleton'
36
+ require 'parseconfig'
37
+
38
+ # This class is for storing global variables. This is accomplished by inheriting
39
+ # the singleton class. To use a global variable it must be registered and then
40
+ # if some part of the program needs to be notified of a change a notifier can
41
+ # be registered for that variable.
42
+ class GlobalsBase
43
+ include Singleton
44
+ include ZDebug
45
+
46
+ def initialize
47
+ @hash={}
48
+ @callbacks={}
49
+ end
50
+
51
+ # overload the assignment operator
52
+ def []=(key,val)
53
+ debug(9,[key,val],"Entering []= (key,val)",nil,true)
54
+ if val.nil?
55
+ delete(key) if !@hash[key].nil?
56
+ else
57
+ if !@hash[key].nil? and (@hash[key].class==Fixnum or @hash[key].class==Bignum)
58
+ @hash[key]=Integer(val)
59
+ else
60
+ @hash[key]=val
61
+ end
62
+
63
+ if !@callbacks[key].nil?
64
+ @callbacks[key].each {|proc|
65
+ proc.call(@hash[key]) #use the value stored in the hash should there have been a conversion
66
+ }
67
+ end
68
+ end
69
+ end
70
+
71
+ # overload the array operator
72
+ def [](key)
73
+ debug(9,key,"Entering [] (key)",nil,true)
74
+ if @hash[key].nil?
75
+ return nil
76
+ else
77
+ return @hash[key]
78
+ end
79
+ end
80
+
81
+ def delete(key)
82
+ @hash.delete(key)
83
+ end
84
+
85
+ def empty?
86
+ return @hash.empty?
87
+ end
88
+
89
+ def each
90
+ @hash.each {|k,v| yield k,v }
91
+ end
92
+
93
+ # Register a function to be called when the value of key changes.
94
+ def register_notifier(key,proc)
95
+ debug(9,[key,proc],"Entering register_notifier (key,proc)")
96
+ if @callbacks[key].nil?
97
+ @callbacks[key]=[proc]
98
+ else
99
+ @callbacks[key]<<proc
100
+ end
101
+ end
102
+ end
103
+
104
+ class GlobalVars < GlobalsBase
105
+
106
+ def initialize
107
+ super()
108
+ end
109
+
110
+ end
111
+
112
+ class EnvVars < GlobalsBase
113
+
114
+ def initialize
115
+ super()
116
+ end
117
+
118
+ #overrides is a hash of options which will override what is found in the config file.
119
+ #useful for command line options.
120
+ #if there is a hash called "config_file" this will override the default config file.
121
+ def load_config(overrides={})
122
+ begin
123
+ config_file = overrides["config_file"].nil? ? self["config_file"] : overrides["config_file"]
124
+ config = overrides["load_config"]==false ? # nil != false
125
+ {} : ParseConfig.new(config_file).params
126
+ # If we are not loading the config use an empty hash
127
+ rescue Errno::EACCES
128
+ if !(config_file=="zabcon.conf" and !File::exists?(config_file))
129
+ puts "Unable to access configuration file: #{config_file}"
130
+ end
131
+ config={}
132
+ end
133
+
134
+ config.merge!(overrides) # merge the two option sets together but give precedence
135
+ # to command line options
136
+
137
+ config.each_pair { |k,v|
138
+ self[k]=v
139
+ }
140
+
141
+
142
+ # debug(6,params)
143
+ #
144
+ # #Setup a local OpenStruct to copy potentially passed in OpenStruct
145
+ # #rather than query every time weather or not params is an OpenStruct
146
+ # localoptions=OpenStruct.new
147
+ #
148
+ # env=EnvVars.instance # Instantiate the global EnvVars
149
+ #
150
+ # if params.nil? or params.empty? # nil or empty, use conffile
151
+ # fname=@conffile
152
+ # elsif params.class==OpenStruct # use OpenStruct value or conffile
153
+ # if params.configfile.nil?
154
+ # fname=@conffile
155
+ # else
156
+ # fname=params.configfile
157
+ # localoptions=params # Since we have an OpenStruct passed in let's setup
158
+ # # our local OpenStruct variable for use later
159
+ # end
160
+ # elsif params.class==Hash # use Hash[:filename] or raise an exception
161
+ # if params[:filename].nil?
162
+ # raise ZabconError.new("Expected a hash with the key 'filename'")
163
+ # else
164
+ # fname=params[:filename]
165
+ # end
166
+ # else # If we get here something went wrong.
167
+ # raise ZabconError.new("OH NO!!! Received something unexpected in do_load_config. Try again with debug level 6.")
168
+ # end
169
+ #
170
+ # begin
171
+ # config=ParseConfig.new(fname).params
172
+ # debug(1,config)
173
+ #
174
+ # if !config["debug"].nil?
175
+ # # If the command line option debug was not passed in use the config file
176
+ # env["debug"]=config["debug"].to_i if localoptions.debug.nil?
177
+ # end
178
+ #
179
+ # if !config["server"].nil? and !config["username"].nil? and !config["password"].nil? then
180
+ # do_login({:server=>config["server"], :username=>config["username"],:password=>config["password"]})
181
+ # else
182
+ # puts "Missing one of the following, server, username or password or bad syntax"
183
+ # end
184
+ #
185
+ # if !config["lines"].nil?
186
+ # env["sheight"]=config["lines"].to_i
187
+ # end
188
+ #
189
+ # if !config["language"].nil?
190
+ # env["language"]=config["language"]
191
+ # end
192
+ #
193
+ # rescue Errno::EACCES
194
+ # puts "Unable to open file #{fname}"
195
+ # rescue ZbxAPI_GeneralError => e
196
+ # puts "An error was received from the Zabbix server"
197
+ # if e.message.class==Hash
198
+ # puts "Error code: #{e.message["code"]}"
199
+ # puts "Error message: #{e.message["message"]}"
200
+ # puts "Error data: #{e.message["data"]}"
201
+ # retry
202
+ # else
203
+ # puts "Error: #{e.message}"
204
+ # e.attempt_retry
205
+ # end
206
+ # end
207
+ end
208
+
209
+
210
+ end
@@ -0,0 +1,310 @@
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 258 2010-12-28 22:49:21Z nelsonab $
22
+ # $Revision: 258 $
23
+ ##########################################
24
+
25
+ require 'zbxapi'
26
+ require 'libs/zdebug'
27
+ require 'libs/zabcon_globals'
28
+
29
+ class ZbxCliServer
30
+
31
+ include ZDebug
32
+
33
+ attr_reader :server_url, :user, :password
34
+
35
+ def initialize(server,user,password,debuglevel=0)
36
+ @server_url=server
37
+ @user=user
38
+ @password=password
39
+ @debuglevel=debuglevel
40
+ # *Note* Do not rescue errors here, rescue in function that calls this block
41
+ @server=ZabbixAPI.new(@server_url,@debuglevel)
42
+ @server.login(@user, @password)
43
+ GlobalVars.instance["auth"]=@server.auth
44
+ end
45
+
46
+ def debuglevel=(level)
47
+ @server.debug_level=level
48
+ end
49
+
50
+ def login?
51
+ !@server.nil?
52
+ end
53
+
54
+ def version
55
+ @server.API_version
56
+ end
57
+
58
+ def reconnect
59
+ @server.login(@user,@password)
60
+ end
61
+
62
+ def getuser(parameters)
63
+ debug(6,parameters)
64
+
65
+ result=@server.user.get(parameters)
66
+ {:class=>:user, :result=>result}
67
+ end
68
+
69
+ def gethost(parameters)
70
+ debug(6,parameters)
71
+
72
+ result=@server.host.get(parameters)
73
+ {:class=>:host, :result=>result}
74
+ end
75
+
76
+ def addhost(parameters)
77
+ debug(6,parameters)
78
+ result=@server.host.create(parameters)
79
+ {:class=>:host, :message=>"The following host was created: #{result['hostids']}", :result=>result}
80
+ end
81
+
82
+ def deletehost(parameters)
83
+ debug(6,parameters)
84
+ result=@server.host.delete(parameters)
85
+ {:class=>:host, :message=>"The following host(s) was/were deleted: #{result['hostids']}", :result=>result}
86
+ end
87
+
88
+ def getitem(parameters)
89
+ debug(6,parameters)
90
+
91
+ result=@server.item.get(parameters)
92
+ {:class=>:item, :result=>result}
93
+ end
94
+
95
+ def additem(parameters)
96
+ debug(6,parameters)
97
+ {:class=>:item, :result=>@server.item.create(parameters)}
98
+ end
99
+
100
+ def deleteitem(parameters)
101
+ debug(6,parameters)
102
+ {:class=>:item, :result=>@server.item.delete(parameters)}
103
+ end
104
+
105
+ def adduser(parameters)
106
+ debug(6,parameters)
107
+ begin
108
+ uid=@server.user.create(parameters)
109
+ puts "Created userid: #{uid["userids"]}"
110
+ rescue ZbxAPI_ParameterError => e
111
+ puts "Add user failed, error: #{e.message}"
112
+ end
113
+ end
114
+
115
+ def deleteuser(parameter)
116
+ debug(6,parameter)
117
+ id=0 #id to delete
118
+ # if parameters.nil? then
119
+ # puts "User id required"
120
+ # return
121
+ # end
122
+
123
+ if !parameter["name"].nil?
124
+ users=@server.user.get({"pattern"=>parameter["name"], "extendoutput"=>true})
125
+ users.each { |user| id=user["userid"] if user["alias"]==parameter }
126
+ else
127
+ id=parameter["id"]
128
+ end
129
+ result=@server.user.delete(id)
130
+ if !result.empty?
131
+ puts "Deleted user id #{result["userids"]}"
132
+ else
133
+ puts "Error deleting #{parameter.to_a[0][1]}"
134
+ end
135
+ end
136
+
137
+ def updateuser(parameters)
138
+ debug(6,parameters)
139
+ valid_parameters=['userid','name', 'surname', 'alias', 'passwd', 'url', 'autologin',
140
+ 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type',]
141
+ if parameters.nil? or parameters["userid"].nil? then
142
+ puts "Edit User requires arguments, valid fields are:"
143
+ puts "name, surname, alias, passwd, url, autologin, autologout, lang, theme, refresh"
144
+ puts "rows_per_page, type"
145
+ puts "userid is a required field"
146
+ puts "example: edit user userid=<id> name=someone alias=username passwd=pass autologout=0"
147
+ return false
148
+ else
149
+ p_keys = parameters.keys
150
+
151
+ valid_parameters.each {|key| p_keys.delete(key)}
152
+ if !p_keys.empty? then
153
+ puts "Invalid items"
154
+ p p_keys
155
+ return false
156
+ elsif parameters["userid"].nil?
157
+ puts "Missing required userid statement."
158
+ end
159
+ p @server.user.update([parameters]) #TODO: remove print statement or comment if needed
160
+ end
161
+ end
162
+
163
+ def addusermedia(parameters)
164
+ debug(6,parameters)
165
+ valid_parameters=["userid", "mediatypeid", "sendto", "severity", "active", "period"]
166
+
167
+ if parameters.nil? then
168
+ puts "add usermedia requires arguments, valid fields are:"
169
+ puts "userid, mediatypeid, sendto, severity, active, period"
170
+ puts "example: add usermedia userid=<id> mediatypeid=1 sendto=myemail@address.com severity=63 active=1 period=\"\""
171
+ else
172
+
173
+ p_keys = parameters.keys
174
+
175
+ valid_parameters.each {|key| p_keys.delete(key)}
176
+ if !p_keys.empty? then
177
+ puts "Invalid items"
178
+ p p_keys
179
+ return false
180
+ elsif parameters["userid"].nil?
181
+ puts "Missing required userid statement."
182
+ end
183
+ begin
184
+ @server.user.addmedia(parameters)
185
+ rescue ZbxAPI_ParameterError => e
186
+ puts e.message
187
+ end
188
+ end
189
+
190
+ end
191
+
192
+ def addhostgroup(parameters)
193
+ debug(6,parameters)
194
+ result = @server.hostgroup.create(parameters)
195
+ {:class=>:hostgroup, :result=>result}
196
+ end
197
+
198
+ def gethostgroup(parameters)
199
+ debug(6,parameters)
200
+
201
+ result=@server.hostgroup.get(parameters)
202
+ {:class=>:hostgroup, :result=>result}
203
+ end
204
+
205
+ def gethostgroupid(parameters)
206
+ debug(6,parameters)
207
+ result = @server.hostgroup.getObjects(parameters)
208
+ {:class=>:hostgroupid, :result=>result}
209
+ end
210
+
211
+ def getapp(parameters)
212
+ debug(6,parameters)
213
+
214
+ result=@server.application.get(parameters)
215
+ {:class=>:application, :result=>result}
216
+ end
217
+
218
+ def addapp(parameters)
219
+ debug(6,parameters)
220
+ result=@server.application.create(parameters)
221
+ {:class=>:application, :result=>result}
222
+ end
223
+
224
+ def getappid(parameters)
225
+ debug(6,parameters)
226
+ result=@server.application.getid(parameters)
227
+ {:class=>:application, :result=>result}
228
+ end
229
+
230
+ def gettrigger(parameters)
231
+ debug(6,parameters)
232
+ result=@server.trigger.get(parameters)
233
+ {:class=>:trigger, :result=>result}
234
+ end
235
+
236
+ # addtrigger( { trigger1, trigger2, triggern } )
237
+ # Only expression and description are mandatory.
238
+ # { { expression, description, type, priority, status, comments, url }, { ...} }
239
+ def addtrigger(parameters)
240
+ debug(6,parameters)
241
+ result=@server.trigger.create(parameters)
242
+ {:class=>:trigger, :result=>result}
243
+ end
244
+
245
+ def addlink(parameters)
246
+ debug(6,parameters)
247
+ result=@server.sysmap.addlink(parameters)
248
+ {:class=>:map, :result=>result}
249
+ end
250
+
251
+ def addsysmap(parameters)
252
+ debug(6,parameters)
253
+ result=@server.sysmap.create(parameters)
254
+ {:class=>:map, :result=>result}
255
+ end
256
+
257
+ def addelementtosysmap(parameters)
258
+ debug(6,parameters)
259
+ result=@server.sysmap.addelement(parameters)
260
+ {:class=>:map, :result=>result}
261
+ end
262
+
263
+ def getseid(parameters)
264
+ debug(6,parameters)
265
+ result=@server.sysmap.getseid(parameters)
266
+ {:class=>:map, :result=>result}
267
+ end
268
+
269
+ def addlinktrigger(parameters)
270
+ debug(6,parameters)
271
+ result=@server.sysmap.addlinktrigger(parameters)
272
+ {:class=>:map, :result=>result}
273
+ end
274
+
275
+ def raw_api(parameters)
276
+ debug(6,parameters)
277
+ result=@server.raw_api(parameters[:method],parameters[:params])
278
+ {:class=>:raw, :result=>result}
279
+ end
280
+
281
+ def raw_json(parameters)
282
+ debug(6,parameters)
283
+ begin
284
+ result=@server.do_request(parameters)
285
+ {:class=>:raw, :result=>result["result"]}
286
+ rescue ZbxAPI_GeneralError => e
287
+ puts "An error was received from the Zabbix server"
288
+ if e.message.class==Hash
289
+ puts "Error code: #{e.message["code"]}"
290
+ puts "Error message: #{e.message["message"]}"
291
+ puts "Error data: #{e.message["data"]}"
292
+ end
293
+ puts "Original text:"
294
+ puts parameters
295
+ puts
296
+ return {:class=>:raw, :result=>nil}
297
+ end
298
+ end
299
+
300
+ end
301
+
302
+ ##############################################
303
+ # Unit test
304
+ ##############################################
305
+
306
+ if __FILE__ == $0
307
+ zbxcliserver = ZbxCliServer.new("http://localhost/","apitest","test") #Change as appropriate for platform
308
+
309
+ p zbxcliserver.getuser(nil)
310
+ end