zabcon 0.0.6 → 0.0.327

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,558 @@
1
+ #License:: GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
2
+ #Copyright:: Copyright (C) 2009,2010 Andrew Nelson nelsonab(at)red-tux(dot)net
3
+ #
4
+ #This program is free software; you can redistribute it and/or
5
+ #modify it under the terms of the GNU General Public License
6
+ #as published by the Free Software Foundation; either version 2
7
+ #of the License, or (at your option) any later version.
8
+ #
9
+ #This program is distributed in the hope that it will be useful,
10
+ #but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ #GNU General Public License for more details.
13
+ #
14
+ #You should have received a copy of the GNU General Public License
15
+ #along with this program; if not, write to the Free Software
16
+ #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
+
18
+ ##########################################
19
+ # Subversion information
20
+ # $Id: zabcon_commands.rb 325 2011-09-26 08:57:00Z nelsonab $
21
+ # $Revision: 325 $
22
+ ##########################################
23
+
24
+ require "zbxapi/zdebug"
25
+
26
+
27
+ ZabconCommand.add_command "exit" do
28
+ set_method do
29
+ puts "Exiting"
30
+ throw :exit
31
+ end
32
+ set_help_tag :exit
33
+ add_alias "quit"
34
+ end
35
+
36
+ ZabconCommand.add_command "help" do
37
+ set_method do |params|
38
+ if params.empty?
39
+ puts CommandHelp.get(:help)
40
+ else
41
+ cmd=CommandList.instance.get(params)
42
+ tag=!cmd.nil? ? cmd.help_tag : :_unknown
43
+ if tag==:_unknown or tag==:_none
44
+ puts "Unable to find help for: \"#{params.join(" ")}\""
45
+ else
46
+ puts CommandHelp.get(tag)
47
+ end
48
+ end
49
+ end
50
+ set_flag :array_params
51
+ set_help_tag :help
52
+ end
53
+
54
+ ZabconCommand.add_command "help commands" do
55
+ set_method do
56
+ CommandList.instance.get_command_list.each {|item|
57
+ puts item
58
+ }
59
+ end
60
+ set_help_tag :help_commands
61
+ end
62
+
63
+ ZabconCommand.add_command "login" do
64
+ set_method do |params|
65
+ # login server username password
66
+ server.server_url=params[0]
67
+ server.username=params[1]
68
+ server.password=params[2]
69
+ server.login
70
+ end
71
+ set_help_tag :help
72
+ set_flag :array_params
73
+ end
74
+
75
+ ZabconCommand.add_command "logout" do
76
+ set_method do
77
+ server.logout
78
+ end
79
+ set_help_tag :logout
80
+ end
81
+
82
+ ZabconCommand.add_command "info" do
83
+ set_method do
84
+ puts "Current settings"
85
+ puts "Server"
86
+ if !server.connected?
87
+ puts "Not connected"
88
+ else
89
+ puts " Server Name: %s" % server.server_url
90
+ puts " Username: %-15s Password: %-12s" % [server.username, Array.new(server.password.length,'*')]
91
+ end
92
+ puts "Display"
93
+ puts " Current screen length #{env["sheight"]}"
94
+ puts "Other"
95
+ puts " Debug level %d" % env["debug"]
96
+ end
97
+ set_help_tag :info
98
+ end
99
+
100
+ ZabconCommand.add_command "set env" do
101
+ set_method do |params|
102
+
103
+ params.each { |key,val|
104
+ env[key]=val
105
+ puts "#{key} : #{val.inspect}"
106
+ }
107
+ end
108
+ set_help_tag :set_env
109
+ end
110
+
111
+ ZabconCommand.add_command "load config" do
112
+ set_method do |params|
113
+ env.load_config(params)
114
+ end
115
+ set_help_tag :load_config
116
+ end
117
+
118
+ ZabconCommand.add_command "set debug" do
119
+ set_method do |params|
120
+ env["debug"]=params[0]
121
+ end
122
+ depreciated "set env debug=N"
123
+ set_flag :array_params
124
+ set_help_tag :set_debug
125
+ end
126
+
127
+ ZabconCommand.add_command "set lines" do
128
+ set_method do |params|
129
+ env["lines"]=params[0]
130
+ end
131
+ depreciated "set env lines=N"
132
+ set_flag :array_params
133
+ set_help_tag :set_lines
134
+ end
135
+
136
+ ZabconCommand.add_command "set pause" do
137
+ set_method do |params|
138
+ if params.nil? then
139
+ puts "set pause requires either Off or On"
140
+ return
141
+ end
142
+
143
+ if params.keys[0].upcase=="OFF"
144
+ env["lines"]=env["lines"].abs*(-1)
145
+ elsif params.keys[0].upcase=="ON"
146
+ env["lines"]=env["lines"].abs
147
+ else
148
+ puts "set pause requires either Off or On"
149
+ end
150
+ env["lines"]=24 if env["lines"]==0
151
+ end
152
+ set_flag :array_params
153
+ set_help_tag :set_pause
154
+ end
155
+
156
+ ZabconCommand.add_command "show var" do
157
+ set_method do |params|
158
+ if params.empty?
159
+ if GlobalVars.instance.empty?
160
+ puts "No variables defined"
161
+ else
162
+ GlobalVars.instance.each { |key,val|
163
+ puts "#{key} : #{val.inspect}"
164
+ }
165
+ end
166
+ else
167
+ params.each { |item|
168
+ if GlobalVars.instance[item].nil?
169
+ puts "#{item} *** Not Defined ***"
170
+ else
171
+ puts "#{item} : #{GlobalVars.instance[item].inspect}"
172
+ end
173
+ }
174
+ end
175
+ end
176
+ set_help_tag :show_var
177
+ set_flag :array_params
178
+ end
179
+
180
+ ZabconCommand.add_command "show env" do
181
+ set_method do |params|
182
+ if params.empty?
183
+ if env.empty?
184
+ puts "No variables defined"
185
+ else
186
+ env.each { |key,val|
187
+ puts "#{key} : #{val.inspect}"
188
+ }
189
+ end
190
+ else
191
+ params.each { |item|
192
+ if env[item].nil?
193
+ puts "#{item} *** Not Defined ***"
194
+ else
195
+ puts "#{item} : #{env[item].inspect}"
196
+ end
197
+ }
198
+ end
199
+ end
200
+ set_help_tag :show_env
201
+ set_flag :array_params
202
+ end
203
+
204
+ ZabconCommand.add_command "set var" do
205
+ set_method do |params|
206
+ params.each { |key,val|
207
+ GlobalVars.instance[key]=val
208
+ puts "#{key} : #{val.inspect}"
209
+ }
210
+ end
211
+ set_help_tag :set_var
212
+ end
213
+
214
+ ZabconCommand.add_command "unset var" do
215
+ set_method do |params|
216
+ if params.empty?
217
+ puts "No variables given to unset"
218
+ else
219
+ params.each { |item|
220
+ if GlobalVars.instance[item].nil?
221
+ puts "#{item} *** Not Defined ***"
222
+ else
223
+ GlobalVars.instance.delete(item)
224
+ puts "#{item} Deleted"
225
+ end
226
+ }
227
+ end
228
+ end
229
+ set_flag :array_params
230
+ set_help_tag :unset_var
231
+ end
232
+
233
+ ZabconCommand.add_command "raw api" do
234
+ set_method do |params|
235
+ api_func=params[:method]
236
+ params=params[:params]
237
+
238
+ server.connection.raw_api(api_func, params)
239
+ end
240
+
241
+ arg_processor do |params,valid_args,flags|
242
+ parameter_error "Command \"raw api\" requires parameters" if params.empty?
243
+ params=params.split2
244
+ api_func=params[0]
245
+ params.delete_at(0)
246
+ retval= params_to_hash(params.join(" "))
247
+ {:method=>api_func, :params=>retval}
248
+ end
249
+ set_flag :login_required
250
+ set_flag :print_output
251
+ set_help_tag :raw_api
252
+ result_type :raw_api
253
+ end
254
+
255
+ ZabconCommand.add_command "raw json" do
256
+ set_method do |params|
257
+ begin
258
+ result=server.connection.do_request(params)
259
+ return result["result"]
260
+ rescue ZbxAPI_GeneralError => e
261
+ puts "An error was received from the Zabbix server"
262
+ if e.message.class==Hash
263
+ puts "Error code: #{e.message["code"]}"
264
+ puts "Error message: #{e.message["message"]}"
265
+ puts "Error data: #{e.message["data"]}"
266
+ end
267
+ puts "Original text:"
268
+ puts parameters
269
+ puts
270
+ return nil
271
+ end
272
+ end
273
+ set_flag :login_required
274
+ set_flag :print_output
275
+ set_help_tag :raw_api
276
+ result_type :raw_api
277
+ end
278
+
279
+ ZabconCommand.add_command "test" do
280
+ set_method do |params|
281
+ test="one=[one two three four=4] two three four=five=six=seven"
282
+ p test
283
+ p params_to_hash2(test)
284
+ end
285
+ # set_flag :print_output
286
+ set_help_tag :print
287
+ result_type :none
288
+ end
289
+
290
+ ###############################################################################
291
+ #Application Application#
292
+ ###############################################################################
293
+
294
+ ZabconCommand.add_command "add app" do
295
+ set_method do |params|
296
+ server.connection.application.create(params)
297
+ end
298
+ set_flag :login_required
299
+ set_flag :print_output
300
+ set_help_tag :add_app
301
+ end
302
+
303
+
304
+ ZabconCommand.add_command "get app" do
305
+ set_method do |params|
306
+ server.connection.application.get(params)
307
+ end
308
+ set_flag :login_required
309
+ set_flag :print_output
310
+ set_help_tag :get_app
311
+ end
312
+
313
+ ###############################################################################
314
+ #Host Host#
315
+ ###############################################################################
316
+
317
+ ZabconCommand.add_command "add host" do
318
+ set_method do |params|
319
+ result=server.connection.host.create(params)
320
+ set_result_message "The following host was created: #{result['hostids']}"
321
+ result
322
+ end
323
+ set_flag :login_required
324
+ set_flag :print_output
325
+ set_help_tag :add_host
326
+ result_type :host
327
+ end
328
+
329
+ ZabconCommand.add_command "delete host" do
330
+ set_method do |params|
331
+ result=server.connection.host.delete(params)
332
+ set_result_message "The following host was deleted: #{result['hostids']}"
333
+ result
334
+ end
335
+ set_flag :login_required
336
+ set_flag :print_output
337
+ set_help_tag :delete_host
338
+ result_type :host
339
+ end
340
+
341
+ ZabconCommand.add_command "get host" do
342
+ set_method do |params|
343
+ server.connection.host.get(params)
344
+ end
345
+ default_show ["hostid", "host", "dns", "ip"]
346
+ set_flag :login_required
347
+ set_flag :print_output
348
+ set_help_tag :get_host
349
+ result_type :host
350
+ end
351
+
352
+ ###############################################################################
353
+ #Host Group Host Group#
354
+ ###############################################################################
355
+
356
+ ZabconCommand.add_command "add host group" do
357
+ set_method do |params|
358
+ server.connection.hostgroup.create(params)
359
+ end
360
+ set_flag :login_required
361
+ set_flag :print_output
362
+ set_help_tag :add_host_group
363
+ result_type :host
364
+ end
365
+
366
+ ZabconCommand.add_command "get host group" do
367
+ set_method do |params|
368
+ server.connection.hostgroup.get(params)
369
+ end
370
+ default_show ["groupid", "name"]
371
+ set_flag :print_output
372
+ set_help_tag :get_host_group
373
+ result_type :host
374
+ end
375
+
376
+ ###############################################################################
377
+ #Item Item#
378
+ ###############################################################################
379
+
380
+
381
+ ZabconCommand.add_command "add item" do
382
+ set_method do |params|
383
+ server.connection.item.create(params)
384
+ end
385
+ set_flag :login_required
386
+ set_flag :print_output
387
+ set_help_tag :add_item
388
+ result_type :item
389
+ end
390
+
391
+ ZabconCommand.add_command "delete item" do
392
+ set_method do |params|
393
+ server.connection.item.delete(params)
394
+ end
395
+ set_flag :login_required
396
+ set_flag :print_output
397
+ set_help_tag :delete_item
398
+ result_type :item
399
+ end
400
+
401
+
402
+ ZabconCommand.add_command "get item" do
403
+ set_method do |params|
404
+ server.connection.item.get(params)
405
+ end
406
+ set_valid_args ['itemids','hostids','groupids', 'triggerids','applicationids',
407
+ 'status','templated_items','editable','count','pattern','limit',
408
+ 'order', 'show']
409
+ default_show ["itemid", "key_", "description"]
410
+ set_flag :login_required
411
+ set_flag :print_output
412
+ set_help_tag :get_item
413
+ result_type :item
414
+ end
415
+
416
+ ###############################################################################
417
+ #Trigger Trigger#
418
+ ###############################################################################
419
+
420
+ #TODO Improve parameter checking for add trigger
421
+ # addtrigger( { trigger1, trigger2, triggern } )
422
+ # Only expression and description are mandatory.
423
+ # { { expression, description, type, priority, status, comments, url }, { ...} }
424
+ ZabconCommand.add_command "add trigger" do
425
+ set_method do |params|
426
+ server.connection.trigger.create(params)
427
+ end
428
+ default_show ["triggerid","description", "status"]
429
+ set_flag :login_required
430
+ set_flag :print_output
431
+ set_help_tag :add_trigger
432
+ result_type :trigger
433
+ end
434
+
435
+ ZabconCommand.add_command "get trigger" do
436
+ set_method do |params|
437
+ server.connection.trigger.get(params)
438
+ end
439
+ default_show ["triggerid","description", "status"]
440
+ set_flag :login_required
441
+ set_flag :print_output
442
+ set_help_tag :get_trigger
443
+ result_type :trigger
444
+ end
445
+
446
+ ###############################################################################
447
+ #User User#
448
+ ###############################################################################
449
+
450
+ ZabconCommand.add_command "add user" do
451
+ set_method do |params|
452
+ uid=server.connection.user.create(params)
453
+ puts "Created userid: #{uid["userids"]}"
454
+ end
455
+ set_flag :login_required
456
+ set_help_tag :add_user
457
+ result_type :user
458
+ end
459
+
460
+ ZabconCommand.add_command "delete user" do
461
+ set_method do |params|
462
+ id=0
463
+ if !params["name"].nil?
464
+ users=server.connection.user.get({"pattern"=>params["name"], "extendoutput"=>true})
465
+ users.each { |user| id=user["userid"] if user["alias"]==parameter }
466
+ else
467
+ id=params["id"]
468
+ end
469
+ result=@connection.user.delete(id)
470
+
471
+ if !result.empty?
472
+ puts "Deleted user id #{result["userids"]}"
473
+ else
474
+ puts "Error deleting #{params.to_a[0][1]}"
475
+ end
476
+ end
477
+ set_flag :login_required
478
+ set_help_tag :delete_user
479
+ result_type :user
480
+ end
481
+
482
+ ZabconCommand.add_command "get user" do
483
+ set_method do |params|
484
+ server.connection.user.get(params)
485
+ end
486
+ default_show ["userid","name","surname","alias"]
487
+ set_flag :login_required
488
+ set_flag :print_output
489
+ set_help_tag :get_user
490
+ result_type :user
491
+ end
492
+
493
+ #TODO Test this command
494
+ ZabconCommand.add_command "update user" do
495
+ set_method do |params|
496
+ if parameters.nil? or parameters["userid"].nil? then
497
+ puts "Edit User requires arguments, valid fields are:"
498
+ puts "name, surname, alias, passwd, url, autologin, autologout, lang, theme, refresh"
499
+ puts "rows_per_page, type"
500
+ puts "userid is a required field"
501
+ puts "example: edit user userid=<id> name=someone alias=username passwd=pass autologout=0"
502
+ return nil
503
+ else
504
+ p_keys = parameters.keys
505
+
506
+ valid_parameters.each {|key| p_keys.delete(key)}
507
+ if !p_keys.empty? then
508
+ puts "Invalid items"
509
+ p p_keys
510
+ return false
511
+ elsif parameters["userid"].nil?
512
+ puts "Missing required userid statement."
513
+ end
514
+ @connection.user.update([parameters])
515
+ end
516
+ end
517
+ set_valid_args ['userid','name', 'surname', 'alias', 'passwd', 'url',
518
+ 'autologin', 'autologout', 'lang', 'theme', 'refresh',
519
+ 'rows_per_page', 'type',]
520
+ default_show ["itemid", "key_", "description"]
521
+ set_flag :login_required
522
+ set_flag :print_output
523
+ set_help_tag :update_user
524
+ result_type :user
525
+ end
526
+
527
+ ###############################################################################
528
+ #Special Special#
529
+ ###############################################################################
530
+
531
+
532
+ #ZabconCommand.add_command "add host" do
533
+ # set_method do |params|
534
+ # result=server.connection.host.create(params)
535
+ # set_result_message "The following host was created: #{result['hostids']}"
536
+ # result
537
+ # end
538
+ # set_flag :login_required
539
+ # set_flag :print_output
540
+ # set_help_tag :add_host
541
+ # result_type :host
542
+ #end
543
+
544
+ ZabconCommand.add_command "clone host" do
545
+ set_method do |params|
546
+ source=params["source"]
547
+ p source_info=server.connection.host.get({"output"=>"extend", "selectParentTemplates"=>"refer", "filter"=>{"host"=>[source]}})
548
+ p source_hostid=source_info[0]["hostid"]
549
+ p source_templates=source_info[0]["parentTemplates"].map{|i| i["hostid"]}
550
+
551
+ end
552
+
553
+ set_flag :login_required
554
+ set_flag :print_output
555
+ set_help_tag :clone_host
556
+ result_type :host
557
+ end
558
+