zabcon 0.0.6 → 0.0.327

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,76 +18,22 @@
18
18
 
19
19
  ##########################################
20
20
  # Subversion information
21
- # $Id: argument_processor.rb 281 2011-04-06 18:10:16Z nelsonab $
22
- # $Revision: 281 $
21
+ # $Id: argument_processor.rb 325 2011-09-26 08:57:00Z nelsonab $
22
+ # $Revision: 325 $
23
23
  ##########################################
24
24
 
25
- require 'libs/zdebug'
26
- require 'libs/exceptions'
25
+ require 'zbxapi/zdebug'
26
+ require 'zbxapi/exceptions'
27
27
  require 'libs/zabcon_exceptions'
28
28
  require 'libs/zabcon_globals'
29
+ require 'libs/command_tree'
29
30
 
30
- # ArgumentProcessor This class contains the functions for processing the arguments passed to each command
31
- # The functions will return two hashes.
32
- # :api_params - parameters to pass to the API call
33
- # :show_params - parameters to pass to the print routines
34
- # The args parameter is a hash of the
35
-
36
- # All functions for argument processing are in alphabetical order except for default functions which are placed first
37
-
38
- class ArgumentProcessor
39
-
40
- include ZDebug
41
-
42
- attr_reader :help, :default, :default_get
43
-
44
- def initialize
45
- @help=self.method(:help_processor)
46
- @default=self.method(:default_processor)
47
- @default_get=self.method(:default_get_processor)
48
- end
49
31
 
50
32
 
51
- def strip_comments(str)
52
- str.lstrip!
53
- str.chomp!
54
-
55
- tmp=""
56
- escaped=false
57
- quoted=false
58
- endquote=''
59
-
60
- str.each_char{|char|
61
- if quoted
62
- if char==endquote
63
- quoted=false
64
- tmp+=char
65
- else
66
- tmp+=char
67
- end
68
- elsif escaped
69
- escaped=false
70
- tmp+=char
71
- elsif char=='\\'
72
- escaped=true
73
- tmp+=char
74
- elsif char=='"' or char=="'"
75
- quoted=true
76
- case char
77
- when "'" # single quote
78
- endquote="'"
79
- when '"' # double quote
80
- endquote='"'
81
- end
82
- tmp+=char
83
- elsif char=='#'
84
- break
85
- else
86
- tmp+=char
87
- end
88
- }
33
+ # All functions for argument processing are in alphabetical order except for default functions which are placed first
34
+ module ArgumentProcessor
89
35
 
90
- tmp.chomp
36
+ class ParseError < Exception
91
37
  end
92
38
 
93
39
  # converts item to the appropriate data type if need be
@@ -101,7 +47,7 @@ class ArgumentProcessor
101
47
  return text
102
48
  elsif item =~ /^\[(.*?)\]$/
103
49
  array_s=Regexp.last_match(1)
104
- array=safe_split(array_s,',')
50
+ array=array_s.split2(:split_char=>',')
105
51
  results=array.collect do |i|
106
52
  i.lstrip!
107
53
  i.rstrip!
@@ -112,7 +58,7 @@ class ArgumentProcessor
112
58
  return true if item.downcase=="true"
113
59
  return false
114
60
  else
115
- array=safe_split(item,',')
61
+ array=item.split2(:split_char=>',')
116
62
  if !array.nil? && array.length<=1
117
63
  return item
118
64
  else
@@ -121,70 +67,6 @@ class ArgumentProcessor
121
67
  end
122
68
  end
123
69
 
124
- #splits a line at boundaries defined by boundary.
125
- def safe_split(line,boundary=nil)
126
- debug(9,line,"line")
127
- debug(9,boundary,"boundary")
128
-
129
- return line if line.class!=String
130
-
131
- items=[]
132
- item=""
133
- quoted=false
134
- qchar=''
135
- splitchar= boundary.nil? ? /\s/ : /#{boundary}/
136
- escaped=false #used for when the escape character "\" is found
137
- line.each_char do |char| # split up incoming line and account for item="stuff n stuff"
138
- # p char
139
- add_char=true # are we going to add this character?
140
-
141
- if char=="\\" and !escaped
142
- escaped=true # We've found an escape character which means add the next char'
143
- next
144
- end
145
-
146
- # puts "char->#{char}, quoted->#{quoted}, qchar->#{qchar}, item->#{item}"
147
- if (char !~ splitchar) && (!escaped || !quoted) # add the space if we're in a quoted string and not escaped
148
- if !quoted # This block will group text found inside "" or []
149
- if char=='"' # is the character a quote?
150
- # puts "quote found"
151
- qchar='"' # set our end quote character
152
- quoted=true # set our mode to be quoted
153
- # add_char=false # do not add this character
154
- # elsif char=='('
155
- # qchar=')'
156
- # quoted=true
157
- elsif char=='[' # is the character a open bracket?
158
- qchar=']' # set our end quote character
159
- quoted=true # enable quoted mode
160
- end
161
- else #quoted == false
162
- if char==qchar # we have found our quote boundary
163
- # puts "found quote"
164
- quoted=false
165
- # add_char=false if char=='"' # do not add the character if it is a quote character
166
- end
167
- end # end !quoted block
168
-
169
- item<<char if add_char
170
- # p item
171
-
172
- elsif escaped || quoted # add the character since we're escaped'
173
- item<<char
174
- else # we have found our split boundary, add the item
175
- items<<item if item.length>0
176
- item=""
177
- end # end (char!~splitchar or quoted) && !escaped
178
-
179
- escaped=false # when we set escape to true we use next to skip the rest of the block
180
- end
181
- items<<item if item.length>0 # be sure not to forget the last element from the block
182
-
183
- raise ParseError.new("Closing #{qchar} not found!") if quoted
184
-
185
- items
186
- end
187
-
188
70
  # Params to hash breaks up an incoming line into individual elements.
189
71
  # It's kinda messy, and could probably one day use some cleaning up
190
72
  # The basic concept is it will return a hash based on what it finds.
@@ -195,16 +77,13 @@ class ArgumentProcessor
195
77
  #
196
78
  # TODO this could use some cleanup.
197
79
  def params_to_hash(line)
198
- debug(6,line,"line")
199
- params=safe_split(line)
200
- debug(6,params,"After safe_split")
80
+ params=line.split2
201
81
 
202
82
  retval = {}
203
83
  params.each do |item|
204
- debug(9,item,"parsing")
205
- item.lstrip!
206
- item.chomp!
84
+ item.strip!
207
85
  if item =~ /^(.*?)=(.*?)$/ then
86
+ Regexp.last_match
208
87
  lside=Regexp.last_match(1)
209
88
  rside=convert_or_parse(Regexp.last_match(2))
210
89
  if rside.class==Array #check to see if we have hashes inside the array
@@ -233,15 +112,32 @@ class ArgumentProcessor
233
112
  retval.merge!(lside=>rside)
234
113
  else
235
114
  if item =~ /^"(.*?)"$/
236
- item=Regexp.last_match(1)
115
+ item=Regexp.last_match(1)
237
116
  end
238
- retval.merge!(item=>true)
117
+ retval.merge!(item=>true)
239
118
  end
240
- debug(9,retval,"parsed")
241
119
  end
242
120
  retval
243
121
  end
244
122
 
123
+ def params_to_hash2(line)
124
+ params=line.split2
125
+ p line
126
+
127
+
128
+ params.map { |item|
129
+ item.strip!
130
+ if item =~ /^(.*?)=(.*)$/ then
131
+ Regexp.last_match
132
+ lside=Regexp.last_match(1)
133
+ rside=Regexp.last_match(2)
134
+ {lside=>params_to_hash2(rside)}
135
+ else
136
+ {item=>true}
137
+ end
138
+ }
139
+ end
140
+
245
141
  #substitute_vars
246
142
  #This function will substitute the variable tokens in the string args for the values in the global object
247
143
  #GlobalVars
@@ -289,40 +185,13 @@ class ArgumentProcessor
289
185
  args.gsub(/\\\$/, '$')
290
186
  end
291
187
 
292
- debug(2,args,"Pre substitution")
188
+ debug(2,:msg=>"Pre substitution",:var=>args)
293
189
  args=unescape(substitute(split(args)).join)
294
- debug(2,args,"Post substitution")
190
+ debug(2,:var=>args,:msg=>"Post substitution")
295
191
 
296
192
  return args
297
193
  end
298
194
 
299
- def call_help(help_func)
300
- help_func.call if !help_func.nil?
301
- end
302
-
303
- # The help processor just passes the args back in the api_params key
304
- # Note, this is the only processor which does not process for variables.
305
- def help_processor(help_func,valid_args,args,user_vars,*options)
306
- args=substitute_vars(args)
307
- {:api_params=>args, :show_params=>{}}
308
- end
309
-
310
- alias raw_processor help_processor
311
-
312
- # The default helper process the "show=*" argument
313
- # If there is a show argument "extendoutput" is sent to the API and the show argument is passed to the print routines
314
- def default_helper(args)
315
- debug(7,args,"default helper")
316
- api_params = args
317
- show_params = {}
318
- if args.class!=Array && !args["show"].nil?
319
- show_params={:show=>args["show"]}
320
- api_params.delete("show")
321
- api_params.merge({"extendoutput"=>true})
322
- end
323
-
324
- {:api_params=>api_params, :show_params=>show_params}
325
- end
326
195
 
327
196
  # This is the default Parameter processor. This is passed to the Command Tree object when it is instantiated
328
197
  # The default processor also checks the incoming parameters against a list of valid arguments, and merges
@@ -331,423 +200,417 @@ class ArgumentProcessor
331
200
  # If :use_array_processor is passed as an option the array processor will be used
332
201
  # In :num_args is passed with a value, and error will be returned if more than that many args are passed
333
202
 
334
- def default_processor(help_func,valid_args,args,user_vars,*options)
335
- debug(5,args,"default_processor args")
336
- debug(5,options,"default_processor options")
337
-
338
- #if the intersection of options and [:array] is not empty then we will return an array
339
- return_array = !(options[0] & [:use_array_processor]).empty?
340
- check_not_empty = !(options[0] & [:not_empty]).empty?
341
-
342
- num_args=options[0].map { |i|
343
- i[:num_args] if i.class==Hash
344
- }.compact[0] # will be nil if :num_args not found
203
+ def default_processor(args,valid_args,*flags)
204
+ args=args.strip #remove preceding and trailing whitespace
205
+ flags=flags[0]
345
206
 
346
- if check_not_empty
347
- raise ParameterError.new("No arguments",:retry=>true, :help_func=>help_func) if args==""
207
+ if flags[:not_empty]
208
+ raise ParameterError.new("No arguments",:retry=>true) if args.empty?
348
209
  end
349
210
 
350
- if return_array
351
- args=safe_split(args)
211
+ if flags[:array_params]
212
+ args=args.split2
213
+ elsif flags[:string_params]
214
+ args=args
352
215
  else
353
- args=substitute_vars(args)
354
216
  args=params_to_hash(args)
355
- if !(invalid=check_parameters(args, valid_args)).empty?
356
- msg="Invalid parameters:\n"
357
- msg+=invalid.join(", ")
358
- raise ParameterError_Invalid.new(msg,:retry=>true, :help_func=>help_func)
359
- end
360
-
361
- valid_user_vars = {}
362
-
363
- if !valid_args.nil?
364
- valid_args.each {|item|
365
- valid_user_vars[item]=user_vars[item] if !user_vars[item].nil?
366
- }
367
- end
368
- args = valid_user_vars.merge(args)
369
- end
370
-
371
- if !num_args.nil?
372
- eval_exp="#{args.length}#{num_args}"
373
- raise ParameterError.new("Too many arguments (#{args.length})",:retry=>true, :help_func=>help_func) if !eval(eval_exp)
374
- end
375
217
 
376
- default_helper(args)
377
- end
378
218
 
379
- # This processor does not do anything fancy. All items passed in via args are passed back in api_params
380
- def simple_processor(help_func,valid_args,args,user_vars,*options)
381
- debug(7,args,"default argument processor")
382
-
383
- args=substitute_vars(args)
384
- args=params_to_hash(args)
385
-
386
- {:api_params=>args, :show_params=>{}}
387
- end
388
-
389
- # This is the default processor for get commands. It adds "limit" and "extendoutput" as needed
390
- def default_get_processor(help_func, valid_args, args, user_vars, *options)
391
- debug(7,args,"default get helper")
392
-
393
- # let the default processor set things up
394
- retval=default_processor(help_func,valid_args,args,user_vars,options)
395
-
396
- if retval[:api_params]["limit"].nil?
397
- retval[:api_params]["limit"]=100
398
- end
399
- if retval[:api_params]["show"].nil?
400
- retval[:api_params]["extendoutput"]=true
401
- end
402
-
403
- retval
404
- end
405
-
406
- #Helper function to ensure the proper hash is returned
407
- def return_helper(parameters,show_parameters=nil)
408
- {:api_params=>parameters, :show_params=>show_parameters}
409
- end
219
+ # if !(invalid=check_parameters(args, valid_args)).empty?
220
+ # msg="Invalid parameters:\n"
221
+ # msg+=invalid.join(", ")
222
+ # raise ParameterError_Invalid.new(msg,:retry=>true, :help_func=>help_func)
223
+ # end
224
+ # args=substitute_vars(args)
410
225
 
411
- # Helper function the check for required parameters.
412
- # Parameters is the hash of parameters from the user
413
- # required_parameters is an array of parameters which are required
414
- # returns an array of missing required items
415
- # if the returned array is empty all required items found
416
- def check_required(parameters,required_parameters)
417
- r_params=required_parameters.clone # Arrays are pass by reference
418
- parameters.keys.each{|key| r_params.delete(key)}
419
- r_params
420
- end
421
226
 
422
- # Helper function to check the validity of the parameters from the user
423
- # Parameters is the hash of parameters from the user
424
- # valid_parameters is an array of parameters which are valid
425
- # returns an array of invalid parameters
426
- # if the returned array is empty all parameters are valid
427
- def check_parameters(parameters,valid_parameters)
428
- if !valid_parameters.nil?
429
- keys=parameters.keys
430
- valid_parameters.each {|key| keys.delete(key)}
431
- return keys
432
- else
433
- return []
434
- end
435
- end
436
-
437
- # hash_processor is a helper function which takes the incoming arguments
438
- # and chunks them into a hash of pairs
439
- # example:
440
- # input: one two three four
441
- # result: "one"=>"two", "three"=>"four"
442
- # Exception will be raised when error found
443
- # processor does not do variable substitution
444
- # TODO: Consider removing function as it appears not be used
445
- def hash_processor(help_func,valid_args,args,user_vars,*options)
446
- debug(6,args,"Args")
447
- debug(6,options,"Options")
448
- items=safe_split(args)
449
- if items.count % 2 == 0
450
- rethash={}
451
- while items.count!=0
452
- rethash[items[0]]=items[1]
453
- items.delete_at(0)
454
- items.delete_at(0) #make sure we delete the first two items
455
- end
456
- return_helper(rethash)
457
- else
458
- msg="Invalid input\n"
459
- msg+="Odd number of arguments found"
460
- raise ParameterError.new(msg,:retry=>true)
227
+ # valid_user_vars = {}
228
+ #
229
+ # if !valid_args.nil?
230
+ # valid_args.each {|item|
231
+ # valid_user_vars[item]=user_vars[item] if !user_vars[item].nil?
232
+ # }
233
+ # end
234
+ # args = valid_user_vars.merge(args)
461
235
  end
462
- end
463
-
464
- ##############################################################################################
465
- # End of default and helper functions
466
- ##############################################################################################
467
236
 
468
- def add_user(help_func,valid_args,args, user_vars, *options)
469
- debug(4,args,"args")
470
-
471
- if args.empty?
472
- call_help(help_func)
473
- raise ParameterError.new("No arguments",:retry=>true, :help_func=>help_func)
474
- end
237
+ # if !num_args.nil?
238
+ # eval_exp="#{args.length}#{num_args}"
239
+ # raise ParameterError.new("Too many arguments (#{args.length})",:retry=>true, :help_func=>help_func) if !eval(eval_exp)
240
+ # end
475
241
 
476
- valid_parameters=['name', 'surname', 'alias', 'passwd', 'url', 'autologin',
477
- 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type']
478
- default_processor(help_func,valid_parameters,args,user_vars,options)
242
+ Command::Arguments.new(args, flags)
479
243
  end
480
244
 
481
- def add_host(help_func,valid_args,args,user_vars,*options)
482
- debug(4,args,"args")
483
- debug(4,options,"options")
484
-
485
- if args.empty?
486
- call_help(help_func)
487
- return nil
488
- end
489
-
490
- #TODO, add the ability for both groups and groupids
491
-
492
- valid_parameters=['host', 'groups', 'port', 'status', 'useip', 'dns', 'ip',
493
- 'proxy_hostid', 'useipmi', 'ipmi_ip', 'ipmi_port', 'ipmi_authtype',
494
- 'ipmi_privilege', 'ipmi_username', 'ipmi_password', 'templates']
495
-
496
- parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
497
-
498
- required_parameters=[ 'host', 'groups' ]
499
-
500
- # p required_parameters
501
- # p parameters
502
-
503
- # if !parameters["dns"].nil? and !required_parameters.find("ip")
504
- # required_parameters.delete("ip")
505
- # elsif !parameters["ip"].nil? and !required_parameters["dns"]
506
- # required_parameters.delete("dns")
245
+ # # This processor does not do anything fancy. All items passed in via args are passed back in api_params
246
+ # def simple_processor(help_func,valid_args,args,user_vars,*options)
247
+ #
248
+ # args=substitute_vars(args)
249
+ # args=params_to_hash(args)
250
+ #
251
+ # {:api_params=>args, :show_params=>{}}
252
+ # end
253
+ #
254
+ # # This is the default processor for get commands. It adds "limit" and "extendoutput" as needed
255
+ # def default_get_processor(help_func, valid_args, args, user_vars, *options)
256
+ #
257
+ # # let the default processor set things up
258
+ # retval=default_processor(help_func,valid_args,args,user_vars,options)
259
+ #
260
+ # if retval[:api_params]["limit"].nil?
261
+ # retval[:api_params]["limit"]=100
507
262
  # end
508
-
509
- if !(missing=check_required(parameters,required_parameters)).empty?
510
- # if !required_parameters["ip"].nil? and !required_parameters["dns"].nil?
511
- # puts "Missing parameter dns and/or ip"
512
- # required_parameters["ip"].delete
513
- # required_parameters["dns"].delete
263
+ # if retval[:api_params]["show"].nil?
264
+ # retval[:api_params]["extendoutput"]=true
265
+ # end
266
+ #
267
+ # retval
268
+ # end
269
+ #
270
+ # #Helper function to ensure the proper hash is returned
271
+ # def return_helper(parameters,show_parameters=nil)
272
+ # {:api_params=>parameters, :show_params=>show_parameters}
273
+ # end
274
+ #
275
+ # # Helper function the check for required parameters.
276
+ # # Parameters is the hash of parameters from the user
277
+ # # required_parameters is an array of parameters which are required
278
+ # # returns an array of missing required items
279
+ # # if the returned array is empty all required items found
280
+ # def check_required(parameters,required_parameters)
281
+ # r_params=required_parameters.clone # Arrays are pass by reference
282
+ # parameters.keys.each{|key| r_params.delete(key)}
283
+ # r_params
284
+ # end
285
+ #
286
+ # # Helper function to check the validity of the parameters from the user
287
+ # # Parameters is the hash of parameters from the user
288
+ # # valid_parameters is an array of parameters which are valid
289
+ # # returns an array of invalid parameters
290
+ # # if the returned array is empty all parameters are valid
291
+ # def check_parameters(parameters,valid_parameters)
292
+ # if !valid_parameters.nil?
293
+ # keys=parameters.keys
294
+ # valid_parameters.each {|key| keys.delete(key)}
295
+ # return keys
296
+ # else
297
+ # return []
298
+ # end
299
+ # end
300
+ #
301
+ # # hash_processor is a helper function which takes the incoming arguments
302
+ # # and chunks them into a hash of pairs
303
+ # # example:
304
+ # # input: one two three four
305
+ # # result: "one"=>"two", "three"=>"four"
306
+ # # Exception will be raised when error found
307
+ # # processor does not do variable substitution
308
+ # # TODO: Consider removing function as it appears not be used
309
+ # def hash_processor(help_func,valid_args,args,user_vars,*options)
310
+ # debug(6,options,"Options")
311
+ # items=args.split2
312
+ # if items.count % 2 == 0
313
+ # rethash={}
314
+ # while items.count!=0
315
+ # rethash[items[0]]=items[1]
316
+ # items.delete_at(0)
317
+ # items.delete_at(0) #make sure we delete the first two items
514
318
  # end
515
- msg = "Required parameters missing\n"
516
- msg += missing.join(", ")
517
-
518
- raise ParameterError_Missing.new(msg,:retry=>true, :help_func=>help_func)
519
- end
520
-
521
- groups=convert_or_parse(parameters['groupids'])
522
- if groups.class==Fixnum
523
- parameters['groups']=[{"groupid"=>groups}]
524
- end
525
-
526
- return_helper(parameters)
527
- end
528
-
529
- def add_item_active(help_func,parameters,*options)
530
- valid_parameters = ['hostid','description','key','delta','history','multiplier','value_type', 'data_type',
531
- 'units','delay','trends','status','valuemapid','applications']
532
- required_parameters = ['hostid','description','key']
533
- end
534
-
535
- def add_item(help_func,valid_args,args,user_vars,*options)
536
- debug(4,args,"args")
537
- debug(4,options,"options")
538
- debug(4,user_vars,"User Variables")
539
-
540
- if args.empty?
541
- call_help(help_func)
542
- return nil
543
- end
544
-
545
- # Item types
546
- # 0 Zabbix agent - Passive
547
- # 1 SNMPv1 agent - SNMP
548
- # 2 Zabbix trapper - Trapper
549
- # 3 Simple check - Simple
550
- # 4 SNMPv2 agent - SNMP2
551
- # 5 Zabbix internal - Internal
552
- # 6 SNMPv3 agent - SNMP3
553
- # 7 Zabbix agent (active) - Active
554
- # 8 Zabbix aggregate - Aggregate
555
- # 10 External check - External
556
- # 11 Database monitor - Database
557
- # 12 IPMI agent - IPMI
558
- # 13 SSH agent - SSH
559
- # 14 TELNET agent - Telnet
560
- # 15 Calculated - Calculated
561
-
562
- #value types
563
- # 0 Numeric (float)
564
- # 1 Character
565
- # 2 Log
566
- # 3 Numeric (unsigned)
567
- # 4 Text
568
-
569
- # Data Types
570
- # 0 Decimal
571
- # 1 Octal
572
- # 2 Hexadecimal
573
-
574
- # Status Types
575
- # 0 Active
576
- # 1 Disabled
577
- # 2 Not Supported
578
-
579
- # Delta Types
580
- # 0 As is
581
- # 1 Delta (Speed per second)
582
- # 2 Delta (simple change)
583
-
584
-
585
- valid_parameters= ['hostid', 'snmpv3_securitylevel','snmp_community', 'publickey', 'delta', 'history', 'key_',
586
- 'key', 'snmp_oid', 'delay_flex', 'multiplier', 'delay', 'mtime', 'username', 'authtype',
587
- 'data_type', 'ipmi_sensor','snmpv3_authpassphrase', 'prevorgvalue', 'units', 'trends',
588
- 'snmp_port', 'formula', 'type', 'params', 'logtimefmt', 'snmpv3_securityname',
589
- 'trapper_hosts', 'description', 'password', 'snmpv3_privpassphrase',
590
- 'status', 'privatekey', 'valuemapid', 'templateid', 'value_type', 'groups']
591
-
592
- parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
593
-
594
- # valid_user_vars = {}
595
- #
596
- # valid_parameters.each {|item|
597
- # valid_user_vars[item]=user_vars[item] if !user_vars[item].nil?
598
- # }
599
- # p parameters
600
- # p valid_user_vars
601
- # parameters = valid_user_vars.merge(parameters)
602
- # p parameters
603
-
604
- required_parameters=[ 'type' ]
605
-
606
- if parameters["type"].nil?
607
- puts "Missing required parameter 'type'"
608
- return nil
609
- end
610
-
611
- if !(invalid=check_parameters(parameters,valid_parameters)).empty?
612
- puts "Invalid items"
613
- puts invalid.join(", ")
614
- return nil
615
- end
616
-
617
- case parameters["type"].downcase
618
- when "passive"
619
- parameters["type"]=0
620
- required_parameters = ['hostid','description','key']
621
- when "active"
622
- parameters["type"]=7
623
- required_parameters = ['hostid','description','key']
624
- when "trapper"
625
- parameters["type"]=2
626
- required_parameters = ['hostid','description','key']
627
- end
628
-
629
- if !(missing=check_required(parameters,required_parameters)).empty?
630
- puts "Required parameters missing"
631
-
632
- puts missing.join(", ")
633
-
634
- return nil
635
- end
636
-
637
- # perform some translations
638
-
639
- parameters["key_"]=parameters["key"]
640
- parameters.delete("key")
641
-
642
- return_helper(parameters)
643
- end
644
-
645
-
646
- def delete_host(help_func,valid_args,args,user_vars,*options)
647
- debug(6,args,"args")
648
-
649
- args=default_processor(help_func,valid_args,args,user_vars,options)[:api_params]
650
-
651
- if args["id"].nil?
652
- puts "Missing parameter \"id\""
653
- call_help(help_func)
654
- return nil
655
- end
656
-
657
- return_helper(args["id"])
658
- end
659
-
660
- def delete_user(help_func,valid_args,args,user_vars,*options)
661
- debug(6,args,"args")
662
- if (args.split(" ").length>1) or (args.length==0)
663
- raise ParameterError("Incorrect number of parameters",:retry=>true, :help_func=>help_func)
664
- end
665
-
666
- args=default_processor(help_func,valid_args,args,user_vars)[:api_params]
667
-
668
- if !args["id"].nil?
669
- return return_helper(args) if args["id"].class==Fixnum
670
- puts "\"id\" must be a number"
671
- call_help(help_func)
672
- return nil
673
- end
674
-
675
- puts "Invalid arguments"
676
- call_help(help_func)
677
- return nil
678
-
679
- end
680
-
681
- #TODO: Document why this function does not use the default processor
682
- def get_group_id(help_func,valid_args,args,user_vars,*options)
683
- debug(4,valid_args,"valid_args")
684
- debug(4,args,"args")
685
-
686
- args=substitute_vars(args)
687
- args=params_to_hash(args)
688
-
689
- {:api_params=>args.keys, :show_params=>nil}
690
- end
691
-
692
- def get_user(help_func,valid_args,args,user_vars,*options)
693
- debug(4,valid_args,"valid_args")
694
- debug(4,args, "args")
695
-
696
- retval=default_get_processor(help_func,valid_args,args,user_vars)
697
- error=false
698
- msg=''
699
-
700
- if !retval[:show_params][:show].nil?
701
- show_options=retval[:show_params][:show]
702
- if !show_options.include?("all")
703
- valid_show_options=['name','attempt_clock','theme','autologout','autologin','url','rows_per_page','attempt_ip',
704
- 'refresh','attempt_failed','type','userid','lang','alias','surname','passwd']
705
-
706
- invalid_show_options=show_options-valid_show_options
707
-
708
- if invalid_show_options.length!=0
709
- error=true
710
- msg = "Invalid show options: #{invalid_show_options}"
711
- end
712
- elsif show_options.length!=1
713
- error=true
714
- msg = "Show header option \"all\" cannot be included with other headers"
715
- end
716
- end
717
- # raise ParameterError(msg,help_func) if error
718
-
719
- return retval
720
- end
721
-
722
- #TODO: Use helper functions to make login more robust
723
- def login(help_func,valid_args,args,user_vars,*options)
724
- debug(4,args, "args")
725
- args=args.split
726
- if args.length!=3
727
- call_help(help_func)
728
- return nil
729
- end
730
- params={}
731
- params[:server]=args[0]
732
- params[:username]=args[1]
733
- params[:password]=args[2]
734
- return {:api_params=>params}
735
- end
736
-
737
- def raw_api(help_func,valid_args,args,user_vars,*options)
738
- debug(7,args,"raw_api argument processor")
739
-
740
- args=substitute_vars(args)
741
-
742
- items=safe_split(args)
743
- method=items[0]
744
- items.delete_at(0)
745
- args=items.join(" ")
746
- args=params_to_hash(args)
747
- args=nil if args=={}
748
-
749
- {:api_params=>{:method=>method, :params=>args}, :show_params=>{}}
750
- end
319
+ # return_helper(rethash)
320
+ # else
321
+ # msg="Invalid input\n"
322
+ # msg+="Odd number of arguments found"
323
+ # raise ParameterError.new(msg,:retry=>true)
324
+ # end
325
+ # end
326
+ #
327
+ # ##############################################################################################
328
+ # # End of default and helper functions
329
+ # ##############################################################################################
330
+ #
331
+ # def add_user(help_func,valid_args,args, user_vars, *options)
332
+ # debug(4,args,"args")
333
+ #
334
+ # if args.empty?
335
+ # call_help(help_func)
336
+ # raise ParameterError.new("No arguments",:retry=>true, :help_func=>help_func)
337
+ # end
338
+ #
339
+ # valid_parameters=['name', 'surname', 'alias', 'passwd', 'url', 'autologin',
340
+ # 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type']
341
+ # default_processor(help_func,valid_parameters,args,user_vars,options)
342
+ # end
343
+ #
344
+ # def add_host(help_func,valid_args,args,user_vars,*options)
345
+ # debug(4,args,"args")
346
+ # debug(4,options,"options")
347
+ #
348
+ # if args.empty?
349
+ # call_help(help_func)
350
+ # return nil
351
+ # end
352
+ #
353
+ # #TODO, add the ability for both groups and groupids
354
+ #
355
+ # valid_parameters=['host', 'groups', 'port', 'status', 'useip', 'dns', 'ip',
356
+ # 'proxy_hostid', 'useipmi', 'ipmi_ip', 'ipmi_port', 'ipmi_authtype',
357
+ # 'ipmi_privilege', 'ipmi_username', 'ipmi_password', 'templates']
358
+ #
359
+ # parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
360
+ #
361
+ # required_parameters=[ 'host', 'groups' ]
362
+ #
363
+ ## p required_parameters
364
+ ## p parameters
365
+ #
366
+ ## if !parameters["dns"].nil? and !required_parameters.find("ip")
367
+ ## required_parameters.delete("ip")
368
+ ## elsif !parameters["ip"].nil? and !required_parameters["dns"]
369
+ ## required_parameters.delete("dns")
370
+ ## end
371
+ #
372
+ # if !(missing=check_required(parameters,required_parameters)).empty?
373
+ ## if !required_parameters["ip"].nil? and !required_parameters["dns"].nil?
374
+ ## puts "Missing parameter dns and/or ip"
375
+ ## required_parameters["ip"].delete
376
+ ## required_parameters["dns"].delete
377
+ ## end
378
+ # msg = "Required parameters missing\n"
379
+ # msg += missing.join(", ")
380
+ #
381
+ # raise ParameterError_Missing.new(msg,:retry=>true, :help_func=>help_func)
382
+ # end
383
+ #
384
+ # groups=convert_or_parse(parameters['groupids'])
385
+ # if groups.class==Fixnum
386
+ # parameters['groups']=[{"groupid"=>groups}]
387
+ # end
388
+ #
389
+ # return_helper(parameters)
390
+ # end
391
+ #
392
+ # def add_item_active(help_func,parameters,*options)
393
+ # valid_parameters = ['hostid','description','key','delta','history','multiplier','value_type', 'data_type',
394
+ # 'units','delay','trends','status','valuemapid','applications']
395
+ # required_parameters = ['hostid','description','key']
396
+ # end
397
+ #
398
+ # def add_item(help_func,valid_args,args,user_vars,*options)
399
+ # debug(4,args,"args")
400
+ # debug(4,options,"options")
401
+ # debug(4,user_vars,"User Variables")
402
+ #
403
+ # if args.empty?
404
+ # call_help(help_func)
405
+ # return nil
406
+ # end
407
+ #
408
+ # # Item types
409
+ # # 0 Zabbix agent - Passive
410
+ # # 1 SNMPv1 agent - SNMP
411
+ # # 2 Zabbix trapper - Trapper
412
+ # # 3 Simple check - Simple
413
+ # # 4 SNMPv2 agent - SNMP2
414
+ # # 5 Zabbix internal - Internal
415
+ # # 6 SNMPv3 agent - SNMP3
416
+ # # 7 Zabbix agent (active) - Active
417
+ # # 8 Zabbix aggregate - Aggregate
418
+ # # 10 External check - External
419
+ # # 11 Database monitor - Database
420
+ # # 12 IPMI agent - IPMI
421
+ # # 13 SSH agent - SSH
422
+ # # 14 TELNET agent - Telnet
423
+ # # 15 Calculated - Calculated
424
+ #
425
+ # #value types
426
+ # # 0 Numeric (float)
427
+ # # 1 Character
428
+ # # 2 Log
429
+ # # 3 Numeric (unsigned)
430
+ # # 4 Text
431
+ #
432
+ # # Data Types
433
+ # # 0 Decimal
434
+ # # 1 Octal
435
+ # # 2 Hexadecimal
436
+ #
437
+ # # Status Types
438
+ # # 0 Active
439
+ # # 1 Disabled
440
+ # # 2 Not Supported
441
+ #
442
+ # # Delta Types
443
+ # # 0 As is
444
+ # # 1 Delta (Speed per second)
445
+ # # 2 Delta (simple change)
446
+ #
447
+ #
448
+ # valid_parameters= ['hostid', 'snmpv3_securitylevel','snmp_community', 'publickey', 'delta', 'history', 'key_',
449
+ # 'key', 'snmp_oid', 'delay_flex', 'multiplier', 'delay', 'mtime', 'username', 'authtype',
450
+ # 'data_type', 'ipmi_sensor','snmpv3_authpassphrase', 'prevorgvalue', 'units', 'trends',
451
+ # 'snmp_port', 'formula', 'type', 'params', 'logtimefmt', 'snmpv3_securityname',
452
+ # 'trapper_hosts', 'description', 'password', 'snmpv3_privpassphrase',
453
+ # 'status', 'privatekey', 'valuemapid', 'templateid', 'value_type', 'groups']
454
+ #
455
+ # parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
456
+ #
457
+ ## valid_user_vars = {}
458
+ ##
459
+ ## valid_parameters.each {|item|
460
+ ## valid_user_vars[item]=user_vars[item] if !user_vars[item].nil?
461
+ ## }
462
+ ## p parameters
463
+ ## p valid_user_vars
464
+ ## parameters = valid_user_vars.merge(parameters)
465
+ ## p parameters
466
+ #
467
+ # required_parameters=[ 'type' ]
468
+ #
469
+ # if parameters["type"].nil?
470
+ # puts "Missing required parameter 'type'"
471
+ # return nil
472
+ # end
473
+ #
474
+ # if !(invalid=check_parameters(parameters,valid_parameters)).empty?
475
+ # puts "Invalid items"
476
+ # puts invalid.join(", ")
477
+ # return nil
478
+ # end
479
+ #
480
+ # case parameters["type"].downcase
481
+ # when "passive"
482
+ # parameters["type"]=0
483
+ # required_parameters = ['hostid','description','key']
484
+ # when "active"
485
+ # parameters["type"]=7
486
+ # required_parameters = ['hostid','description','key']
487
+ # when "trapper"
488
+ # parameters["type"]=2
489
+ # required_parameters = ['hostid','description','key']
490
+ # end
491
+ #
492
+ # if !(missing=check_required(parameters,required_parameters)).empty?
493
+ # puts "Required parameters missing"
494
+ #
495
+ # puts missing.join(", ")
496
+ #
497
+ # return nil
498
+ # end
499
+ #
500
+ # # perform some translations
501
+ #
502
+ # parameters["key_"]=parameters["key"]
503
+ # parameters.delete("key")
504
+ #
505
+ # return_helper(parameters)
506
+ # end
507
+ #
508
+ #
509
+ # def delete_host(help_func,valid_args,args,user_vars,*options)
510
+ # debug(6,args,"args")
511
+ #
512
+ # args=default_processor(help_func,valid_args,args,user_vars,options)[:api_params]
513
+ #
514
+ # if args["id"].nil?
515
+ # puts "Missing parameter \"id\""
516
+ # call_help(help_func)
517
+ # return nil
518
+ # end
519
+ #
520
+ # return_helper(args["id"])
521
+ # end
522
+ #
523
+ # def delete_user(help_func,valid_args,args,user_vars,*options)
524
+ # debug(6,args,"args")
525
+ # if (args.split(" ").length>1) or (args.length==0)
526
+ # raise ParameterError("Incorrect number of parameters",:retry=>true, :help_func=>help_func)
527
+ # end
528
+ #
529
+ # args=default_processor(help_func,valid_args,args,user_vars)[:api_params]
530
+ #
531
+ # if !args["id"].nil?
532
+ # return return_helper(args) if args["id"].class==Fixnum
533
+ # puts "\"id\" must be a number"
534
+ # call_help(help_func)
535
+ # return nil
536
+ # end
537
+ #
538
+ # puts "Invalid arguments"
539
+ # call_help(help_func)
540
+ # return nil
541
+ #
542
+ # end
543
+ #
544
+ # #TODO: Document why this function does not use the default processor
545
+ # def get_group_id(help_func,valid_args,args,user_vars,*options)
546
+ # debug(4,valid_args,"valid_args")
547
+ # debug(4,args,"args")
548
+ #
549
+ # args=substitute_vars(args)
550
+ # args=params_to_hash(args)
551
+ #
552
+ # {:api_params=>args.keys, :show_params=>nil}
553
+ # end
554
+ #
555
+ # def get_user(help_func,valid_args,args,user_vars,*options)
556
+ # debug(4,valid_args,"valid_args")
557
+ # debug(4,args, "args")
558
+ #
559
+ # retval=default_get_processor(help_func,valid_args,args,user_vars)
560
+ # error=false
561
+ # msg=''
562
+ #
563
+ # if !retval[:show_params][:show].nil?
564
+ # show_options=retval[:show_params][:show]
565
+ # if !show_options.include?("all")
566
+ # valid_show_options=['name','attempt_clock','theme','autologout','autologin','url','rows_per_page','attempt_ip',
567
+ # 'refresh','attempt_failed','type','userid','lang','alias','surname','passwd']
568
+ #
569
+ # invalid_show_options=show_options-valid_show_options
570
+ #
571
+ # if invalid_show_options.length!=0
572
+ # error=true
573
+ # msg = "Invalid show options: #{invalid_show_options}"
574
+ # end
575
+ # elsif show_options.length!=1
576
+ # error=true
577
+ # msg = "Show header option \"all\" cannot be included with other headers"
578
+ # end
579
+ # end
580
+ ## raise ParameterError(msg,help_func) if error
581
+ #
582
+ # return retval
583
+ # end
584
+ #
585
+ # #TODO: Use helper functions to make login more robust
586
+ # def login(help_func,valid_args,args,user_vars,*options)
587
+ # debug(4,args, "args")
588
+ # args=args.split
589
+ # if args.length!=3
590
+ # call_help(help_func)
591
+ # return nil
592
+ # end
593
+ # params={}
594
+ # params[:server]=args[0]
595
+ # params[:username]=args[1]
596
+ # params[:password]=args[2]
597
+ # return {:api_params=>params}
598
+ # end
599
+ #
600
+ # def raw_api(help_func,valid_args,args,user_vars,*options)
601
+ # debug(7,args,"raw_api argument processor")
602
+ #
603
+ # args=substitute_vars(args)
604
+ #
605
+ # items=args.split2
606
+ # method=items[0]
607
+ # items.delete_at(0)
608
+ # args=items.join(" ")
609
+ # args=params_to_hash(args)
610
+ # args=nil if args=={}
611
+ #
612
+ # {:api_params=>{:method=>method, :params=>args}, :show_params=>{}}
613
+ # end
751
614
 
752
615
  end
753
616