zabcon 0.0.370 → 0.0.392

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- REVISION=370
1
+ REVISION=392
@@ -17,8 +17,8 @@
17
17
 
18
18
  ##########################################
19
19
  # Subversion information
20
- # $Id: utility_items.rb 361 2011-12-19 00:21:52Z richlv $
21
- # $Revision: 361 $
20
+ # $Id: utility_items.rb 379 2012-04-21 03:43:19Z nelsonab $
21
+ # $Revision: 379 $
22
22
  ##########################################
23
23
 
24
24
  require "zbxapi/zdebug"
@@ -27,8 +27,27 @@ Object.class_eval do
27
27
  def env
28
28
  EnvVars.instance
29
29
  end
30
+
31
+ def vars
32
+ GlobalVars.instance
33
+ end
30
34
  end
31
35
 
36
+ Hash.class_eval do
37
+ def select_keys(keys)
38
+ result={}
39
+ keys.each {|k|
40
+ result.merge!({k=>self[k]})
41
+ }
42
+ result
43
+ end
44
+
45
+ def delete_keys(keys)
46
+ keys.each {|k|
47
+ self.delete(k)
48
+ }
49
+ end
50
+ end
32
51
 
33
52
  String.class_eval do
34
53
  # String::split2(*options)
@@ -18,8 +18,8 @@
18
18
 
19
19
  ##########################################
20
20
  # Subversion information
21
- # $Id: zabbix_server.rb 370 2011-12-22 05:51:45Z nelsonab $
22
- # $Revision: 370 $
21
+ # $Id: zabbix_server.rb 384 2012-04-23 04:50:17Z nelsonab $
22
+ # $Revision: 384 $
23
23
  ##########################################
24
24
 
25
25
  require 'yaml'
@@ -60,57 +60,72 @@ class ZabbixServer
60
60
  end
61
61
  end
62
62
 
63
- attr_accessor :server_url, :username, :password
63
+ # attr_accessor :server_url, :username, :password
64
64
  attr_reader :version, :connected, :connection
65
65
 
66
66
  def initialize
67
- @server_url=nil
68
- @username=nil
69
- @password=nil
67
+ @credentials={}
68
+ #@server_url=nil
69
+ #@username=nil
70
+ #@password=nil
70
71
  @connected=false
71
72
  @version=nil
72
73
  @connection=nil
73
74
  end
74
75
 
76
+ def server_url
77
+ @credentials["server"]
78
+ end
79
+
80
+ def server_url=(server)
81
+ @credentials["server"]=server
82
+ end
83
+
84
+ def username
85
+ @credentials["username"]
86
+ end
87
+
88
+ def username=(username)
89
+ @credentials["username"]=username
90
+ end
91
+
92
+ def password
93
+ @credentials["password"]
94
+ end
95
+
96
+ def password=(password)
97
+ @credentials["password"]=password
98
+ end
99
+
75
100
  #login
76
101
  # Perform the actual login to the Zabbix server
77
102
  # If the object variables url, username, and password have not been
78
103
  # set previously, an attempt will be made to use the global environment
79
104
  # variables. If that does not work an exception will be raised.
80
- def login
81
- @server_url = @server_url.nil? ? env["server"] : @server_url
82
- @username = @username.nil? ? env["username"] : @username
83
- @password = @password.nil? ? env["password"] : @password
105
+ def login(server={})
106
+ @credentials.merge!(server)
84
107
 
85
108
  error_msg=[]
86
- error_msg<<"Url not set" if @server_url.nil?
87
- error_msg<<"Username not set" if @username.nil?
88
- error_msg<<"Password not set" if @password.nil?
109
+ error_msg<<"Url not set" if !@credentials["server"]
110
+ error_msg<<"Username not set" if !@credentials["username"]
111
+ error_msg<<"Password not set" if !@credentials["password"]
89
112
 
90
113
  raise ConnectionProblem.new(error_msg.join(", ")) if !error_msg.empty?
91
114
 
92
- @connection = ZabbixServer_overload.new(@server_url,env["debug"])
93
- if env["proxy_server"]
94
- puts "proxy server"
95
- p env["proxy_server"],env["proxy_port"],env["proxy_user"],env["proxy_password"]
96
- @connection.set_proxy(env["proxy_server"],env["proxy_port"],
97
- env["proxy_user"],env["proxy_password"])
115
+ @connection = ZabbixServer_overload.new(@credentials["server"],env["debug"])
116
+ if @credentials["proxy_server"]
117
+ @connection.set_proxy(@credentials["proxy_server"],@credentials["proxy_port"],
118
+ @credentials["proxy_user"],@credentials["proxy_password"])
98
119
  end
99
- @connection.login(@username,@password)
120
+ @connection.login(@credentials["username"],@credentials["password"])
100
121
  @connected=true
101
- GlobalVars.instance["auth"]=@connection.auth
122
+ ServerCredentials.instance[@credentials["name"]]["auth"]=
123
+ @connection.auth
102
124
  @version=@connection.API_version
103
125
  puts "#{@server_url} connected" if env["echo"]
104
126
  puts "API Version: #{@version}" if env["echo"]
105
127
 
106
- if env["session_file"] && !env["session_file"].empty?
107
- path=File.expand_path(env["session_file"])
108
- File.open(path,"w") do |f|
109
- f.write({"auth"=>@connection.auth}.to_yaml)
110
- end
111
- #Enforce that the auth cache file isn't world readable
112
- File.chmod(0600,path)
113
- end
128
+ save_auth
114
129
 
115
130
  end
116
131
 
@@ -124,16 +139,46 @@ class ZabbixServer
124
139
  @connection=nil
125
140
  @connected=false
126
141
  @version=nil
142
+
143
+ if @credentials["name"]
144
+ ServerCredentials.instance[@credentials["name"]].delete("auth")
145
+ end
127
146
  GlobalVars.instance.delete("auth")
128
- puts "Logout complete from #{@server_url}" if env["echo"]
147
+ save_auth
148
+ puts "Logout complete from #{server_url}" if env["echo"]
149
+ end
150
+ end
151
+
152
+ def save_auth
153
+ if env["session_file"] && !env["session_file"].empty?
154
+ path=File.expand_path(env["session_file"])
155
+ creds={}
156
+ ServerCredentials.instance.each {|name,values|
157
+ creds[name]=values["auth"] if values["auth"]
158
+ }
159
+ File.open(path,"w") do |f|
160
+ f.write({"auth"=>creds}.to_yaml)
161
+ end
162
+ #Enforce that the auth cache file isn't world readable
163
+ File.chmod(0600,path)
129
164
  end
165
+
130
166
  end
131
167
 
132
- def use_auth(auth)
133
- @server_url = @server_url.nil? ? env["server"] : @server_url
168
+ def use_auth(server)
169
+ debug(6,:msg=>"Server",:var=>server)
170
+ @credentials.merge!(server)
171
+ debug(6,:msg=>"credentials",:var=>@credentials)
172
+
173
+ # @server_url = server["server"] || @server_url
174
+
175
+ @connection = ZabbixServer_overload.new(server_url,env["debug"])
176
+ @connection.auth=@credentials["auth"]
177
+ if @credentials["proxy_server"]
178
+ @connection.set_proxy(@credentials["proxy_server"],@credentials["proxy_port"],
179
+ @credentials["proxy_user"],@credentials["proxy_password"])
180
+ end
134
181
 
135
- @connection = ZabbixServer_overload.new(@server_url,env["debug"])
136
- @connection.auth=auth
137
182
  major,minor=@connection.do_request(@connection.json_obj('APIInfo.version',{}))['result'].split('.')
138
183
  @connection.major=major.to_i
139
184
  @connection.minor=minor.to_i
@@ -163,107 +208,6 @@ class ZabbixServer
163
208
  @connection.login(@user,@password)
164
209
  end
165
210
 
166
- # def getuser(parameters)
167
- # debug(6,parameters)
168
- #
169
- # result=@connection.user.get(parameters)
170
- # {:class=>:user, :result=>result}
171
- # end
172
- #
173
- # def gethost(parameters)
174
- # debug(6,parameters)
175
- #
176
- # result=@connection.host.get(parameters)
177
- # {:class=>:host, :result=>result}
178
- # end
179
-
180
- # def addhost(parameters)
181
- # debug(6,parameters)
182
- # result=@connection.host.create(parameters)
183
- # {:class=>:host, :message=>"The following host was created: #{result['hostids']}", :result=>result}
184
- # end
185
-
186
- # def deletehost(parameters)
187
- # debug(6,parameters)
188
- # result=@connection.host.delete(parameters)
189
- # {:class=>:host, :message=>"The following host(s) was/were deleted: #{result['hostids']}", :result=>result}
190
- # end
191
-
192
- # def getitem(parameters)
193
- # debug(6,parameters)
194
- #
195
- # result=@connection.item.get(parameters)
196
- # {:class=>:item, :result=>result}
197
- # end
198
-
199
- # def additem(parameters)
200
- # debug(6,parameters)
201
- # {:class=>:item, :result=>@connection.item.create(parameters)}
202
- # end
203
-
204
- # def deleteitem(parameters)
205
- # debug(6,parameters)
206
- # {:class=>:item, :result=>@connection.item.delete(parameters)}
207
- # end
208
-
209
- # def adduser(parameters)
210
- # debug(6,parameters)
211
- # begin
212
- # uid=@connection.user.create(parameters)
213
- # puts "Created userid: #{uid["userids"]}"
214
- # rescue ZbxAPI_ParameterError => e
215
- # puts "Add user failed, error: #{e.message}"
216
- # end
217
- # end
218
-
219
- # def deleteuser(parameter)
220
- # debug(6,parameter)
221
- # id=0 #id to delete
222
- ## if parameters.nil? then
223
- ## puts "User id required"
224
- ## return
225
- ## end
226
- #
227
- # if !parameter["name"].nil?
228
- # users=@connection.user.get({"pattern"=>parameter["name"], "extendoutput"=>true})
229
- # users.each { |user| id=user["userid"] if user["alias"]==parameter }
230
- # else
231
- # id=parameter["id"]
232
- # end
233
- # result=@connection.user.delete(id)
234
- # if !result.empty?
235
- # puts "Deleted user id #{result["userids"]}"
236
- # else
237
- # puts "Error deleting #{parameter.to_a[0][1]}"
238
- # end
239
- # end
240
-
241
- # def updateuser(parameters)
242
- # debug(6,parameters)
243
- # valid_parameters=['userid','name', 'surname', 'alias', 'passwd', 'url', 'autologin',
244
- # 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type',]
245
- # if parameters.nil? or parameters["userid"].nil? then
246
- # puts "Edit User requires arguments, valid fields are:"
247
- # puts "name, surname, alias, passwd, url, autologin, autologout, lang, theme, refresh"
248
- # puts "rows_per_page, type"
249
- # puts "userid is a required field"
250
- # puts "example: edit user userid=<id> name=someone alias=username passwd=pass autologout=0"
251
- # return false
252
- # else
253
- # p_keys = parameters.keys
254
- #
255
- # valid_parameters.each {|key| p_keys.delete(key)}
256
- # if !p_keys.empty? then
257
- # puts "Invalid items"
258
- # p p_keys
259
- # return false
260
- # elsif parameters["userid"].nil?
261
- # puts "Missing required userid statement."
262
- # end
263
- # p @connection.user.update([parameters]) #TODO: remove print statement or comment if needed
264
- # end
265
- # end
266
-
267
211
  def addusermedia(parameters)
268
212
  debug(6,:var=>parameters)
269
213
  valid_parameters=["userid", "mediatypeid", "sendto", "severity", "active", "period"]
@@ -17,8 +17,8 @@
17
17
 
18
18
  ##########################################
19
19
  # Subversion information
20
- # $Id: zabcon_commands.rb 370 2011-12-22 05:51:45Z nelsonab $
21
- # $Revision: 370 $
20
+ # $Id: zabcon_commands.rb 392 2012-05-17 22:24:15Z nelsonab $
21
+ # $Revision: 392 $
22
22
  ##########################################
23
23
 
24
24
  require "zbxapi/zdebug"
@@ -64,10 +64,18 @@ end
64
64
  ZabconCommand.add_command "login" do
65
65
  set_method do |params|
66
66
  # login server username password
67
- server.server_url=params[0]
68
- server.username=params[1]
69
- server.password=params[2]
70
- server.login
67
+ servers=ServerCredentials.instance
68
+ if servers[params[0]]
69
+ creds=servers[params[0]]
70
+ elsif params.length==0
71
+ creds=servers["global"]
72
+ else
73
+ creds=servers["global"].select_keys(["proxy_server",
74
+ "proxy_port","proxy_user","proxy_password"])
75
+ creds.merge!({"server"=>params[0],"username"=>params[1],
76
+ "password"=>params[2]})
77
+ end
78
+ server.login(creds)
71
79
  end
72
80
  set_help_tag :help
73
81
  # set_flag :array_params
@@ -93,11 +101,11 @@ end
93
101
  ZabconCommand.add_command "logout" do
94
102
  set_method do
95
103
  server.logout
96
- path=File.expand_path(env["session_file"])
97
- begin
98
- File.delete(path)
99
- rescue Errno::ENOENT
100
- end
104
+ #path=File.expand_path(env["session_file"])
105
+ #begin
106
+ # File.delete(path)
107
+ #rescue Errno::ENOENT
108
+ #end
101
109
  end
102
110
  set_help_tag :logout
103
111
  end
@@ -229,6 +237,30 @@ ZabconCommand.add_command "show env" do
229
237
  tokenizer SimpleTokenizer
230
238
  end
231
239
 
240
+ ZabconCommand.add_command "show credentials" do
241
+ set_method do |params|
242
+ if params.empty?
243
+ if ServerCredentials.instance.empty?
244
+ puts "No Server Credentials Defined"
245
+ else
246
+ ServerCredentials.instance.each{|k,v|
247
+ puts "#{k} : #{v.inspect}"
248
+ }
249
+ end
250
+ else
251
+ params.each {|item|
252
+ if ServerCredentials.instance[item].nil?
253
+ puts "#{item} is not a valid server name"
254
+ else
255
+ puts "#{item} : #{ServerCredentials.instance[item].inspect}"
256
+ end
257
+ }
258
+ end
259
+ end
260
+ set_help_tag :show_credentials
261
+ tokenizer(SimpleTokenizer.options(:remove_whitespace))
262
+ end
263
+
232
264
  ZabconCommand.add_command "set var" do
233
265
  set_method do |params|
234
266
  params.each { |key,val|
@@ -305,8 +337,7 @@ ZabconCommand.add_command "raw api" do
305
337
  set_flag :print_output
306
338
  set_help_tag :raw_api
307
339
  result_type :raw_api
308
- tokenizer CommandTokenizer
309
- # tokenizer SimpleTokenizer
340
+ tokenizer ExpressionTokenizer
310
341
  end
311
342
 
312
343
  ZabconCommand.add_command "raw json" do
@@ -331,7 +362,7 @@ ZabconCommand.add_command "raw json" do
331
362
  #arg_processor do |params,args,flags|
332
363
  # params
333
364
  #end
334
- set_flag :login_required
365
+ # set_flag :login_required
335
366
  set_flag :print_output
336
367
  set_help_tag :raw_api
337
368
  result_type :raw_api
@@ -451,6 +482,45 @@ end
451
482
  #Item Item#
452
483
  ###############################################################################
453
484
 
485
+ # Item types
486
+ # 0 Zabbix agent - Passive
487
+ # 1 SNMPv1 agent - SNMP
488
+ # 2 Zabbix trapper - Trapper
489
+ # 3 Simple check - Simple
490
+ # 4 SNMPv2 agent - SNMP2
491
+ # 5 Zabbix internal - Internal
492
+ # 6 SNMPv3 agent - SNMP3
493
+ # 7 Zabbix agent (active) - Active
494
+ # 8 Zabbix aggregate - Aggregate
495
+ # 10 External check - External
496
+ # 11 Database monitor - Database
497
+ # 12 IPMI agent - IPMI
498
+ # 13 SSH agent - SSH
499
+ # 14 TELNET agent - Telnet
500
+ # 15 Calculated - Calculated
501
+
502
+ #value types
503
+ # 0 Numeric (float)
504
+ # 1 Character
505
+ # 2 Log
506
+ # 3 Numeric (unsigned)
507
+ # 4 Text
508
+
509
+ # Data Types
510
+ # 0 Decimal
511
+ # 1 Octal
512
+ # 2 Hexadecimal
513
+
514
+ # Status Types
515
+ # 0 Active
516
+ # 1 Disabled
517
+ # 2 Not Supported
518
+
519
+ # Delta Types
520
+ # 0 As is
521
+ # 1 Delta (Speed per second)
522
+ # 2 Delta (simple change)
523
+
454
524
 
455
525
  ZabconCommand.add_command "add item" do
456
526
  set_method do |params|
@@ -478,8 +548,14 @@ ZabconCommand.add_command "get item" do
478
548
  server.connection.item.get(params)
479
549
  end
480
550
  set_valid_args 'itemids','hostids','groupids', 'triggerids','applicationids',
481
- 'status','templated_items','editable','count','pattern','limit',
482
- 'order', 'show'
551
+ 'editable','pattern','limit','search','nodeids','templateids',
552
+ 'proxyids','graphids','webitems','inherited','templated','host',
553
+ 'monitored','filter','group','application','belongs',
554
+ 'with_triggers','startSearch','excludeSearch','searchWildcardsEnabled',
555
+ 'output','select_hosts','select_triggers','select_graphs',
556
+ 'select_applications','countOutput','groupOutput','preservekeys',
557
+ 'sortfield','sortorder','show'
558
+
483
559
  default_show ["itemid", "key_", "description"]
484
560
  set_flag :login_required
485
561
  set_flag :print_output