zabcon 0.0.370 → 0.0.392

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,8 +18,8 @@
18
18
 
19
19
  ##########################################
20
20
  # Subversion information
21
- # $Id: argument_processor.rb 345 2011-11-23 04:28:53Z nelsonab $
22
- # $Revision: 345 $
21
+ # $Id: argument_processor.rb 384 2012-04-23 04:50:17Z nelsonab $
22
+ # $Revision: 384 $
23
23
  ##########################################
24
24
 
25
25
  require 'zbxapi/zdebug'
@@ -252,376 +252,6 @@ module ArgumentProcessor
252
252
  Command::Arguments.new(args, flags)
253
253
  end
254
254
 
255
- # # This processor does not do anything fancy. All items passed in via args are passed back in api_params
256
- # def simple_processor(help_func,valid_args,args,user_vars,*options)
257
- #
258
- # args=substitute_vars(args)
259
- # args=params_to_hash(args)
260
- #
261
- # {:api_params=>args, :show_params=>{}}
262
- # end
263
- #
264
- # # This is the default processor for get commands. It adds "limit" and "extendoutput" as needed
265
- # def default_get_processor(help_func, valid_args, args, user_vars, *options)
266
- #
267
- # # let the default processor set things up
268
- # retval=default_processor(help_func,valid_args,args,user_vars,options)
269
- #
270
- # if retval[:api_params]["limit"].nil?
271
- # retval[:api_params]["limit"]=100
272
- # end
273
- # if retval[:api_params]["show"].nil?
274
- # retval[:api_params]["extendoutput"]=true
275
- # end
276
- #
277
- # retval
278
- # end
279
- #
280
- # #Helper function to ensure the proper hash is returned
281
- # def return_helper(parameters,show_parameters=nil)
282
- # {:api_params=>parameters, :show_params=>show_parameters}
283
- # end
284
- #
285
- # # Helper function the check for required parameters.
286
- # # Parameters is the hash of parameters from the user
287
- # # required_parameters is an array of parameters which are required
288
- # # returns an array of missing required items
289
- # # if the returned array is empty all required items found
290
- # def check_required(parameters,required_parameters)
291
- # r_params=required_parameters.clone # Arrays are pass by reference
292
- # parameters.keys.each{|key| r_params.delete(key)}
293
- # r_params
294
- # end
295
- #
296
- # # Helper function to check the validity of the parameters from the user
297
- # # Parameters is the hash of parameters from the user
298
- # # valid_parameters is an array of parameters which are valid
299
- # # returns an array of invalid parameters
300
- # # if the returned array is empty all parameters are valid
301
- # def check_parameters(parameters,valid_parameters)
302
- # if !valid_parameters.nil?
303
- # keys=parameters.keys
304
- # valid_parameters.each {|key| keys.delete(key)}
305
- # return keys
306
- # else
307
- # return []
308
- # end
309
- # end
310
- #
311
- # # hash_processor is a helper function which takes the incoming arguments
312
- # # and chunks them into a hash of pairs
313
- # # example:
314
- # # input: one two three four
315
- # # result: "one"=>"two", "three"=>"four"
316
- # # Exception will be raised when error found
317
- # # processor does not do variable substitution
318
- # # TODO: Consider removing function as it appears not be used
319
- # def hash_processor(help_func,valid_args,args,user_vars,*options)
320
- # debug(6,options,"Options")
321
- # items=args.split2
322
- # if items.count % 2 == 0
323
- # rethash={}
324
- # while items.count!=0
325
- # rethash[items[0]]=items[1]
326
- # items.delete_at(0)
327
- # items.delete_at(0) #make sure we delete the first two items
328
- # end
329
- # return_helper(rethash)
330
- # else
331
- # msg="Invalid input\n"
332
- # msg+="Odd number of arguments found"
333
- # raise ParameterError.new(msg,:retry=>true)
334
- # end
335
- # end
336
- #
337
- # ##############################################################################################
338
- # # End of default and helper functions
339
- # ##############################################################################################
340
- #
341
- # def add_user(help_func,valid_args,args, user_vars, *options)
342
- # debug(4,args,"args")
343
- #
344
- # if args.empty?
345
- # call_help(help_func)
346
- # raise ParameterError.new("No arguments",:retry=>true, :help_func=>help_func)
347
- # end
348
- #
349
- # valid_parameters=['name', 'surname', 'alias', 'passwd', 'url', 'autologin',
350
- # 'autologout', 'lang', 'theme', 'refresh', 'rows_per_page', 'type']
351
- # default_processor(help_func,valid_parameters,args,user_vars,options)
352
- # end
353
- #
354
- # def add_host(help_func,valid_args,args,user_vars,*options)
355
- # debug(4,args,"args")
356
- # debug(4,options,"options")
357
- #
358
- # if args.empty?
359
- # call_help(help_func)
360
- # return nil
361
- # end
362
- #
363
- # #TODO, add the ability for both groups and groupids
364
- #
365
- # valid_parameters=['host', 'groups', 'port', 'status', 'useip', 'dns', 'ip',
366
- # 'proxy_hostid', 'useipmi', 'ipmi_ip', 'ipmi_port', 'ipmi_authtype',
367
- # 'ipmi_privilege', 'ipmi_username', 'ipmi_password', 'templates']
368
- #
369
- # parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
370
- #
371
- # required_parameters=[ 'host', 'groups' ]
372
- #
373
- ## p required_parameters
374
- ## p parameters
375
- #
376
- ## if !parameters["dns"].nil? and !required_parameters.find("ip")
377
- ## required_parameters.delete("ip")
378
- ## elsif !parameters["ip"].nil? and !required_parameters["dns"]
379
- ## required_parameters.delete("dns")
380
- ## end
381
- #
382
- # if !(missing=check_required(parameters,required_parameters)).empty?
383
- ## if !required_parameters["ip"].nil? and !required_parameters["dns"].nil?
384
- ## puts "Missing parameter dns and/or ip"
385
- ## required_parameters["ip"].delete
386
- ## required_parameters["dns"].delete
387
- ## end
388
- # msg = "Required parameters missing\n"
389
- # msg += missing.join(", ")
390
- #
391
- # raise ParameterError_Missing.new(msg,:retry=>true, :help_func=>help_func)
392
- # end
393
- #
394
- # groups=convert_or_parse(parameters['groupids'])
395
- # if groups.class==Fixnum
396
- # parameters['groups']=[{"groupid"=>groups}]
397
- # end
398
- #
399
- # return_helper(parameters)
400
- # end
401
- #
402
- # def add_item_active(help_func,parameters,*options)
403
- # valid_parameters = ['hostid','description','key','delta','history','multiplier','value_type', 'data_type',
404
- # 'units','delay','trends','status','valuemapid','applications']
405
- # required_parameters = ['hostid','description','key']
406
- # end
407
- #
408
- # def add_item(help_func,valid_args,args,user_vars,*options)
409
- # debug(4,args,"args")
410
- # debug(4,options,"options")
411
- # debug(4,user_vars,"User Variables")
412
- #
413
- # if args.empty?
414
- # call_help(help_func)
415
- # return nil
416
- # end
417
- #
418
- # # Item types
419
- # # 0 Zabbix agent - Passive
420
- # # 1 SNMPv1 agent - SNMP
421
- # # 2 Zabbix trapper - Trapper
422
- # # 3 Simple check - Simple
423
- # # 4 SNMPv2 agent - SNMP2
424
- # # 5 Zabbix internal - Internal
425
- # # 6 SNMPv3 agent - SNMP3
426
- # # 7 Zabbix agent (active) - Active
427
- # # 8 Zabbix aggregate - Aggregate
428
- # # 10 External check - External
429
- # # 11 Database monitor - Database
430
- # # 12 IPMI agent - IPMI
431
- # # 13 SSH agent - SSH
432
- # # 14 TELNET agent - Telnet
433
- # # 15 Calculated - Calculated
434
- #
435
- # #value types
436
- # # 0 Numeric (float)
437
- # # 1 Character
438
- # # 2 Log
439
- # # 3 Numeric (unsigned)
440
- # # 4 Text
441
- #
442
- # # Data Types
443
- # # 0 Decimal
444
- # # 1 Octal
445
- # # 2 Hexadecimal
446
- #
447
- # # Status Types
448
- # # 0 Active
449
- # # 1 Disabled
450
- # # 2 Not Supported
451
- #
452
- # # Delta Types
453
- # # 0 As is
454
- # # 1 Delta (Speed per second)
455
- # # 2 Delta (simple change)
456
- #
457
- #
458
- # valid_parameters= ['hostid', 'snmpv3_securitylevel','snmp_community', 'publickey', 'delta', 'history', 'key_',
459
- # 'key', 'snmp_oid', 'delay_flex', 'multiplier', 'delay', 'mtime', 'username', 'authtype',
460
- # 'data_type', 'ipmi_sensor','snmpv3_authpassphrase', 'prevorgvalue', 'units', 'trends',
461
- # 'snmp_port', 'formula', 'type', 'params', 'logtimefmt', 'snmpv3_securityname',
462
- # 'trapper_hosts', 'description', 'password', 'snmpv3_privpassphrase',
463
- # 'status', 'privatekey', 'valuemapid', 'templateid', 'value_type', 'groups']
464
- #
465
- # parameters=default_processor(help_func,valid_parameters,args,user_vars,options)[:api_params]
466
- #
467
- ## valid_user_vars = {}
468
- ##
469
- ## valid_parameters.each {|item|
470
- ## valid_user_vars[item]=user_vars[item] if !user_vars[item].nil?
471
- ## }
472
- ## p parameters
473
- ## p valid_user_vars
474
- ## parameters = valid_user_vars.merge(parameters)
475
- ## p parameters
476
- #
477
- # required_parameters=[ 'type' ]
478
- #
479
- # if parameters["type"].nil?
480
- # puts "Missing required parameter 'type'"
481
- # return nil
482
- # end
483
- #
484
- # if !(invalid=check_parameters(parameters,valid_parameters)).empty?
485
- # puts "Invalid items"
486
- # puts invalid.join(", ")
487
- # return nil
488
- # end
489
- #
490
- # case parameters["type"].downcase
491
- # when "passive"
492
- # parameters["type"]=0
493
- # required_parameters = ['hostid','description','key']
494
- # when "active"
495
- # parameters["type"]=7
496
- # required_parameters = ['hostid','description','key']
497
- # when "trapper"
498
- # parameters["type"]=2
499
- # required_parameters = ['hostid','description','key']
500
- # end
501
- #
502
- # if !(missing=check_required(parameters,required_parameters)).empty?
503
- # puts "Required parameters missing"
504
- #
505
- # puts missing.join(", ")
506
- #
507
- # return nil
508
- # end
509
- #
510
- # # perform some translations
511
- #
512
- # parameters["key_"]=parameters["key"]
513
- # parameters.delete("key")
514
- #
515
- # return_helper(parameters)
516
- # end
517
- #
518
- #
519
- # def delete_host(help_func,valid_args,args,user_vars,*options)
520
- # debug(6,args,"args")
521
- #
522
- # args=default_processor(help_func,valid_args,args,user_vars,options)[:api_params]
523
- #
524
- # if args["id"].nil?
525
- # puts "Missing parameter \"id\""
526
- # call_help(help_func)
527
- # return nil
528
- # end
529
- #
530
- # return_helper(args["id"])
531
- # end
532
- #
533
- # def delete_user(help_func,valid_args,args,user_vars,*options)
534
- # debug(6,args,"args")
535
- # if (args.split(" ").length>1) or (args.length==0)
536
- # raise ParameterError("Incorrect number of parameters",:retry=>true, :help_func=>help_func)
537
- # end
538
- #
539
- # args=default_processor(help_func,valid_args,args,user_vars)[:api_params]
540
- #
541
- # if !args["id"].nil?
542
- # return return_helper(args) if args["id"].class==Fixnum
543
- # puts "\"id\" must be a number"
544
- # call_help(help_func)
545
- # return nil
546
- # end
547
- #
548
- # puts "Invalid arguments"
549
- # call_help(help_func)
550
- # return nil
551
- #
552
- # end
553
- #
554
- # #TODO: Document why this function does not use the default processor
555
- # def get_group_id(help_func,valid_args,args,user_vars,*options)
556
- # debug(4,valid_args,"valid_args")
557
- # debug(4,args,"args")
558
- #
559
- # args=substitute_vars(args)
560
- # args=params_to_hash(args)
561
- #
562
- # {:api_params=>args.keys, :show_params=>nil}
563
- # end
564
- #
565
- # def get_user(help_func,valid_args,args,user_vars,*options)
566
- # debug(4,valid_args,"valid_args")
567
- # debug(4,args, "args")
568
- #
569
- # retval=default_get_processor(help_func,valid_args,args,user_vars)
570
- # error=false
571
- # msg=''
572
- #
573
- # if !retval[:show_params][:show].nil?
574
- # show_options=retval[:show_params][:show]
575
- # if !show_options.include?("all")
576
- # valid_show_options=['name','attempt_clock','theme','autologout','autologin','url','rows_per_page','attempt_ip',
577
- # 'refresh','attempt_failed','type','userid','lang','alias','surname','passwd']
578
- #
579
- # invalid_show_options=show_options-valid_show_options
580
- #
581
- # if invalid_show_options.length!=0
582
- # error=true
583
- # msg = "Invalid show options: #{invalid_show_options}"
584
- # end
585
- # elsif show_options.length!=1
586
- # error=true
587
- # msg = "Show header option \"all\" cannot be included with other headers"
588
- # end
589
- # end
590
- ## raise ParameterError(msg,help_func) if error
591
- #
592
- # return retval
593
- # end
594
- #
595
- # #TODO: Use helper functions to make login more robust
596
- # def login(help_func,valid_args,args,user_vars,*options)
597
- # debug(4,args, "args")
598
- # args=args.split
599
- # if args.length!=3
600
- # call_help(help_func)
601
- # return nil
602
- # end
603
- # params={}
604
- # params[:server]=args[0]
605
- # params[:username]=args[1]
606
- # params[:password]=args[2]
607
- # return {:api_params=>params}
608
- # end
609
- #
610
- # def raw_api(help_func,valid_args,args,user_vars,*options)
611
- # debug(7,args,"raw_api argument processor")
612
- #
613
- # args=substitute_vars(args)
614
- #
615
- # items=args.split2
616
- # method=items[0]
617
- # items.delete_at(0)
618
- # args=items.join(" ")
619
- # args=params_to_hash(args)
620
- # args=nil if args=={}
621
- #
622
- # {:api_params=>{:method=>method, :params=>args}, :show_params=>{}}
623
- # end
624
-
625
255
  end
626
256
 
627
257
 
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: command_tree.rb 365 2011-12-22 04:26:54Z nelsonab $
23
- # $Revision: 365 $
22
+ # $Id: command_tree.rb 392 2012-05-17 22:24:15Z nelsonab $
23
+ # $Revision: 392 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -131,12 +131,12 @@ class ZabconExecuteContainer
131
131
  end
132
132
 
133
133
  cmd_str=tokens.map{|i|
134
- if i.kind==:variable
135
- name=/^\$(.*)/.match(i.value)[1]
136
- GlobalVars.instance[name] || env[name]
137
- else
134
+ # if i.kind==:variable
135
+ # name=/^\$(.*)/.match(i.value)[1]
136
+ # GlobalVars.instance[name] || env[name]
137
+ # else
138
138
  i.value
139
- end
139
+ # end
140
140
  }.join
141
141
 
142
142
  debug(5,:msg=>"Command String",:var=>cmd_str)
@@ -215,7 +215,8 @@ class Command
215
215
  show=args["show"] || flags[:default_cols]
216
216
  @show_params={:show=>show}
217
217
  @cmd_params.delete("show") if args["show"]
218
- @cmd_params.merge!({"extendoutput"=>true})
218
+ #@cmd_params.merge!({"extendoutput"=>true})
219
+ @cmd_params.merge!({"output"=>"extend"})
219
220
  end
220
221
  end
221
222
  end
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: lexer.rb 361 2011-12-19 00:21:52Z richlv $
23
- # $Revision: 361 $
22
+ # $Id: lexer.rb 378 2012-04-21 03:06:30Z nelsonab $
23
+ # $Revision: 378 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -140,26 +140,61 @@ class Lexr
140
140
  @text[@position..-1]
141
141
  end
142
142
 
143
- class Token
144
- attr_reader :value, :kind, :opts
143
+ #class Token
144
+ #Token dynamically generates sub classes when Token.new is called
145
+ #The sub classes are descendant from Lexr::Token::Sub
146
+ class Token
147
+ class Sub
148
+ attr_reader :value
145
149
 
146
- def initialize(value, kind = nil, opts={})
147
- # @opts, @value, @kind = value, kind, opts
148
- @value, @kind, @opts = value, kind, opts
149
- end
150
+ def initialize(value)
151
+ @value = value
152
+ end
150
153
 
151
- def self.method_missing(sym, *args)
152
- self.new(args.first, sym)
153
- end
154
+ def to_s
155
+ "#{kind}(#{value})"
156
+ end
154
157
 
155
- def to_s
156
- "#{kind}(#{value})"
157
- end
158
+ def ==(other)
159
+ self.class == other.class && @value == other.value
160
+ end
158
161
 
159
- def ==(other)
160
- @kind == other.kind && @value == other.value
161
- end
162
- end
162
+ def is_a?(obj)
163
+ if obj.class==Symbol
164
+ obj==self.class.to_s.split("::")[-1].downcase.intern
165
+ else
166
+ super(obj)
167
+ end
168
+ end
169
+
170
+ def kind
171
+ self.class.to_s.split("::")[-1].downcase.intern
172
+ end
173
+ end
174
+
175
+ class Variable < Sub
176
+ def set_value(val)
177
+ @value=val
178
+ end
179
+ end
180
+
181
+ def self.new(value,kind=nil)
182
+ obj_name=kind.to_s.capitalize
183
+ begin
184
+ # self::Sub.new(value,kind,opts)
185
+ obj=self.const_get(obj_name)
186
+ obj.new(value)
187
+ rescue NameError
188
+ self.const_set(obj_name,Class.new(Token::Sub))
189
+ retry
190
+ end
191
+ end
192
+
193
+ def self.method_missing(sym, *args)
194
+ self.new(args.first, sym)
195
+ end
196
+
197
+ end
163
198
 
164
199
  class Rule
165
200
  attr_reader :pattern, :symbol, :raises
@@ -450,6 +485,7 @@ class BasicExpressionTokenizer < Tokenizer
450
485
 
451
486
  end
452
487
 
488
+ #TODO Clean up code now that Lexr::Token is a dynamic class generator
453
489
  class ExpressionTokenizer < Tokenizer
454
490
 
455
491
  class UnexpectedClose < InvalidCharacter
@@ -1015,7 +1051,7 @@ class ExpressionTokenizerHash < ExpressionTokenizer
1015
1051
  elsif item.is_a?(Numeric) || item.is_a?(String)
1016
1052
  val={item.to_s=>@default_val}
1017
1053
  else
1018
- raise InvaliItem.new("Invalid token for hash key",:invalid_item=>item.to_s)
1054
+ raise InvalidItem.new("Invalid token for hash key",:invalid_item=>item.to_s)
1019
1055
  end
1020
1056
  ret_hash.merge!(val)
1021
1057
  end
@@ -1047,16 +1083,86 @@ class SimpleTokenizerString < SimpleTokenizer
1047
1083
  end
1048
1084
 
1049
1085
 
1086
+ def walk(tokens,pos,skip=[:whitespace])
1087
+ pos+=1 while (pos<tokens.length &&
1088
+ !(skip & [tokens[pos].kind]).empty? &&
1089
+ tokens[pos].kind!=:end)
1090
+ pos
1091
+ end
1050
1092
 
1051
- #p test_str="\"test\"=test1,2.0,3, 4 \"quote test\" value = { a = { b = [ c = { d = [1,a,g=f,3,4] }, e=5,6,7,8] } }"
1052
- #p test_str="value = { a = { b = [ c = { d = [1,a,g=f,3,4] }, e=5,6,7,8] } }"
1053
- #p test_str="test=4, 5, 6, 7 {a={b=4,c=5}} test2=[1,2,[3,[4]],5] value=9, 9"
1054
- #p test_str="a=[1,2] b={g=2} c 1,two,[1.1,1.2,1.3,[A]],three,4 d e[1,2] "
1055
- #p test_str="word1 word2,word3 , d , e,"
1056
- #p test_str=" test a=1, bla {b={c=2}}"
1057
- #p test_str="a=b \\(a=c\\)"
1058
- #p test_str="\\)"
1093
+ def group(tokens)
1094
+ retval,=group2(tokens,[])
1095
+ retval
1096
+ end
1059
1097
 
1060
- #p tokens=ExpressionTokenizer.new(test_str)
1061
- #p result=tokens.parse
1098
+ def group2(tokens,stack)
1099
+ raise "Empty tokens" if tokens.empty?
1100
+
1101
+ retval=[]
1102
+ need_comma=stack.length>0
1103
+
1104
+ pos=-1
1105
+ while ((pos+=1)<tokens.length)
1106
+ case tokens[pos].kind
1107
+ when stack[-1]
1108
+ return retval,pos+1
1109
+ when :end
1110
+ return retval,pos+1
1111
+ when :word, :number, :variable, :escape
1112
+ lpos= stack[-1]==:whitespace ? pos+1 : lpos=walk(tokens,pos+1)
1113
+ if tokens[lpos].kind==:equals
1114
+ new_stack=stack.empty? ? [:whitespace] : stack+[stack[-1]]
1115
+ lside=tokens[pos]
1116
+ rside, lpos=group2(tokens[lpos+1..-1], new_stack)
1117
+ pos+=lpos
1118
+ retval<<{lside => rside}
1119
+ elsif tokens[lpos].kind==:comma && !need_comma
1120
+ new_stack=stack.empty? ? [:whitespace] : stack+[stack[-1]]
1121
+ ret, pos=group2(tokens[pos..-1], new_stack)
1122
+ retval<<ret
1123
+ else
1124
+ retval<<tokens[pos]
1125
+ end
1126
+ when :l_square
1127
+ rside, lpos=group2(tokens[pos+1..-1],stack+[:r_square])
1128
+ pos+=lpos
1129
+ retval<<rside
1130
+ when :comma
1131
+ raise "Unexpected comma" if !need_comma
1132
+ when :whitespace
1133
+ #do nothing
1134
+ else
1135
+ puts "pos: #{pos}"
1136
+ puts "tokens[pos]: #{tokens[pos].inspect}"
1137
+ puts "stack: #{stack.inspect}"
1138
+ exit
1139
+ end
1140
+ end
1141
+
1142
+ return retval,pos
1143
+ end
1144
+
1145
+
1146
+ if ($0==__FILE__)
1147
+
1148
+ include ZDebug
1149
+ set_debug_level(1)
1150
+ require "pp"
1151
+ #p test_str="\"test\"=test1,2.0,3, 4 \"quote test\" value = { a = { b = [ c = { d = [1,a,g=f,3,4] }, e=5,6,7,8] } }"
1152
+ #p test_str="value = { a = { b = [ c = { d = [1,a,g=f,3,4] }, e=5,6,7,8] } }"
1153
+ #p test_str="test=4, 5, 6, 7 {a={b=4,c=5}} test2=[1,2,[3,[4]],5] value=9, 9"
1154
+ p test_str="a=[1,2] b={g=2} c 1,two,[1.1,1.2,1.3,[A]],three,4 d e[1,2] $var"
1155
+ #p test_str="word1 word2,word3 , d , e,"
1156
+ #p test_str=" test a=1, bla {b={c=2}}"
1157
+ #p test_str="a=b \\(a=c\\)"
1158
+ #p test_str="\\)"
1159
+ #p test_str="a=b=[c, d] a [1,2] $var=5"
1160
+
1161
+ pp tokens=ExpressionTokenizer.new(test_str)
1162
+ puts "----"
1163
+
1164
+ pp result=tokens.parse
1165
+ #puts "----"
1166
+ #pp group(tokens)
1167
+ end
1062
1168