zabcon 0.0.327 → 0.0.332

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 325 2011-09-26 08:57:00Z nelsonab $
22
- # $Revision: 325 $
21
+ # $Id: argument_processor.rb 330 2011-09-27 06:16:40Z nelsonab $
22
+ # $Revision: 330 $
23
23
  ##########################################
24
24
 
25
25
  require 'zbxapi/zdebug'
@@ -197,12 +197,15 @@ module ArgumentProcessor
197
197
  # The default processor also checks the incoming parameters against a list of valid arguments, and merges
198
198
  # the user variables with the inbound arguments with the inbound arguments taking precedence, raises an
199
199
  # exception if there is an error
200
+ # arg_info should be a hash containing two keys, :required_args, :valid_args
200
201
  # If :use_array_processor is passed as an option the array processor will be used
201
202
  # In :num_args is passed with a value, and error will be returned if more than that many args are passed
202
203
 
203
- def default_processor(args,valid_args,*flags)
204
+ def default_processor(args,arg_info,flags={})
204
205
  args=args.strip #remove preceding and trailing whitespace
205
- flags=flags[0]
206
+ #valid_args=arg_info[:valid_args]
207
+ #required_args=arg_info[:required_args]
208
+ invalid_args=[]
206
209
 
207
210
  if flags[:not_empty]
208
211
  raise ParameterError.new("No arguments",:retry=>true) if args.empty?
@@ -215,30 +218,36 @@ module ArgumentProcessor
215
218
  else
216
219
  args=params_to_hash(args)
217
220
 
221
+ if !arg_info[:valid_args].empty?
222
+ args_keys=args.keys
218
223
 
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)
224
+ invalid_args=args_keys-arg_info[:valid_args] if arg_info[:valid_args]
225
+ raise ParameterError.new("Invalid parameters: "+invalid_args.join(", "),
226
+ :retry=>true) if !invalid_args.empty?
225
227
 
228
+ required_args=arg_info[:required_args].reject{|i| i.class==Array }
229
+ required_or_args=arg_info[:required_args].reject{|i| i.class!=Array }
226
230
 
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)
235
- end
231
+ missing_args=[]
232
+ missing_args=required_args-args_keys
236
233
 
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
234
+ required_or_args.delete_if do |i|
235
+ count=i.length
236
+ missing_args<<i if (i-args_keys).count==count
237
+ end
241
238
 
239
+ if !missing_args.empty?
240
+ msg=missing_args.map do |i|
241
+ if i.class==Array
242
+ "(#{i.join(" | ")})"
243
+ else
244
+ i
245
+ end
246
+ end.join(", ")
247
+ raise ParameterError.new("Missing required arguments: #{msg}",:retry=>true)
248
+ end
249
+ end
250
+ end
242
251
  Command::Arguments.new(args, flags)
243
252
  end
244
253
 
data/libs/command_tree.rb CHANGED
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: command_tree.rb 325 2011-09-26 08:57:00Z nelsonab $
23
- # $Revision: 325 $
22
+ # $Id: command_tree.rb 330 2011-09-27 06:16:40Z nelsonab $
23
+ # $Revision: 330 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -71,9 +71,10 @@ class ZabconExecuteCommand < ZabconExecuteBase
71
71
  @command_obj=cmd_obj.command_obj
72
72
  begin
73
73
  arg_result=@command_obj.call_arg_processor(cmd_obj.parameters)
74
- rescue ParameterError => e
75
- e.help_func=cmd_obj.command_obj.help_method
76
- raise e
74
+ #TODO Fix showing help messages
75
+ #rescue ParameterError => e
76
+ # e.help_func=cmd_obj.command_obj.help_method
77
+ # raise e
77
78
  end
78
79
  @cmd_params=arg_result.cmd_params
79
80
  @show_params=arg_result.show_params
@@ -250,6 +251,9 @@ class Command
250
251
  end
251
252
  end
252
253
 
254
+ class NonFatalError < Exception
255
+ end
256
+
253
257
  class LoginRequired < Exception
254
258
  end
255
259
 
@@ -266,7 +270,7 @@ class Command
266
270
  raise "Path must be an array" if path.class!=Array
267
271
  @path=path
268
272
  @cmd_method=nil
269
- @valid_args=[]
273
+ @valid_args=@required_args=[]
270
274
  @aliases=[]
271
275
  @flags={}
272
276
  @result_type=nil
@@ -311,9 +315,20 @@ class Command
311
315
  new_alias
312
316
  end
313
317
 
318
+ #Sets up a list of required arguments
319
+ #args is an array of items. If there are multiple options
320
+ #which are optional but one of which is required, it shall
321
+ #be passed as a sub-array
322
+ #example, host, useip are required, but only one of dns and ip is required
323
+ #[host,useip,[dns,ip]]
324
+ def required_args(*args)
325
+ @required_args=args
326
+ @valid_args=@valid_args|args.flatten
327
+ end
328
+
314
329
  #accepts an array of valid arguments
315
- def set_valid_args(args)
316
- @valid_args=args
330
+ def set_valid_args(*args)
331
+ @valid_args=@valid_args|args
317
332
  end
318
333
 
319
334
  def default_show(cols)
@@ -359,7 +374,7 @@ class Command
359
374
  end
360
375
 
361
376
  @arguments=Arguments.new("",@flags)
362
- result=@argument_processor.call(parameters,@valid_args,@flags)
377
+ result=@argument_processor.call(parameters,{:valid_args=>@valid_args,:required_args=>@required_args},@flags)
363
378
  return result if result.is_a?(Arguments)
364
379
  if !result.is_a?(String) && !result.is_a?(Hash) && !result.is_a?(Array)
365
380
  raise ("Arugment processor for \"#{command_name}\" returned invalid parameters: class: #{result.class}, #{result}")
data/libs/lexer.rb CHANGED
@@ -19,8 +19,8 @@
19
19
  #--
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: lexer.rb 325 2011-09-26 08:57:00Z nelsonab $
23
- # $Revision: 325 $
22
+ # $Id: lexer.rb 330 2011-09-27 06:16:40Z nelsonab $
23
+ # $Revision: 330 $
24
24
  ##########################################
25
25
  #++
26
26
 
@@ -34,7 +34,7 @@ require "zbxapi/zdebug"
34
34
  #This is a wrapper class for creating a generalized lexer.
35
35
  class Lexr
36
36
 
37
- class NoLexerError < RuntimeError
37
+ class NoLexerError < ZError
38
38
  end
39
39
 
40
40
  def self.setup(&block)
@@ -51,11 +51,37 @@ class Lexr
51
51
  end
52
52
 
53
53
  def parse
54
- retval=[]
54
+ tokens=[]
55
55
  until self.end?
56
- retval << self.next
56
+ tokens << self.next
57
57
  end
58
- retval
58
+
59
+ join_escape(tokens)
60
+ end
61
+
62
+ def join_escape(tokens)
63
+ tmp=[]
64
+ escapes=0
65
+ tokens=tokens.map do |i|
66
+ if i.kind==:escape
67
+ escapes+=1
68
+ tmp<<i.value
69
+ nil
70
+ elsif escapes>0 && i.kind!=:end
71
+ escapes=0
72
+ tmp<<i.value
73
+ token=Token.new(tmp.join.to_s,:escape)
74
+ tmp=[]
75
+ token
76
+ else
77
+ i
78
+ end
79
+ end.compact
80
+ if escapes>0
81
+ tokens[-1]=Token.new(tmp.join.to_s,:escape)
82
+ tokens<<Lexr::Token.end
83
+ end
84
+ tokens
59
85
  end
60
86
 
61
87
  def next
@@ -278,6 +304,7 @@ ExpressionLexer = Lexr.setup {
278
304
  matches /[-+]?\d+/ => :number, :convert_with => lambda { |v| Integer(v) }
279
305
  matches "=" => :equals
280
306
  matches "\"" => :umatched_quote, :raises=> "Unmatched quote"
307
+ matches /#.*$/ => :comment
281
308
  default /[^\s^\\^"^\(^\)^\{^\}^\[^\]^,^=]+/ => :word
282
309
  }
283
310
 
@@ -332,18 +359,34 @@ class Tokenizer < Array
332
359
  end
333
360
  end
334
361
 
335
- def initialize(str)
362
+ class EscapeEnd < InvalidCharacter
363
+ def initialize(message=nil, params={})
364
+ super(message,params)
365
+ @message=message || "Cannot escape the end of a string"
366
+ end
367
+ end
368
+
369
+ attr_accessor :parsed
370
+ attr :items
371
+
372
+ #Takes a string str and and hash arguments and creates a Lexical token reference of str
373
+ #It will also parse the lexical tokens into an array of items.
374
+ #:keep_escape determines weather or not to keep all escape backslashes, default false
375
+ def initialize(str,args={})
336
376
  super()
337
377
  debug(8,:msg=>"Initial String",:var=>str.inspect)
338
378
  replace(str.lexer_parse(ExpressionLexer))
339
379
  debug(8,:msg=>"Tokens",:var=>self)
340
380
  debug(8,:msg=>"Tokens(Length)",:var=>length)
341
381
  @available_tokens=ExpressionLexer.available_tokens
382
+ @parsed=parse(args)
383
+ @items=@parsed.clone
342
384
  end
343
385
 
344
386
  def parse(args={})
345
387
  pos=args[:pos] || 0
346
- pos,tmp=unravel(pos)
388
+ args.delete(:pos)
389
+ pos,tmp=unravel(pos,args)
347
390
  if tmp.length==1 && tmp[0].class==Array
348
391
  tmp[0]
349
392
  else
@@ -367,6 +410,7 @@ class Tokenizer < Array
367
410
  return :whitespace if of_type?(pos,:whitespace)
368
411
  return :comma if of_type?(pos,:comma)
369
412
  return :escape if of_type?(pos,:escape)
413
+ return :comment if of_type?(pos,:comment)
370
414
  return :paren if of_type?(pos,:paren)
371
415
  return :close if close?(pos)
372
416
  return :hash if hash?(pos)
@@ -586,59 +630,55 @@ class Tokenizer < Array
586
630
  end
587
631
 
588
632
  def get_escape(pos,args={})
589
- keep_initial=args[:keep_initial] || false
633
+ keep_initial=args[:keep_escape] || false
634
+ debug(8,:msg=>"(#{self[pos].value.inspect}).length => #{self[pos].value.length}")
635
+ invalid_character(pos, :error=>EscapeEnd) if self[pos].value.length==1 && end?(pos+1)
590
636
 
591
- invalid_character(pos,:msg=>"Escape characters cannot be last") if end?(pos+1)
592
- pos+=1 if !keep_initial #gobble the first escape char
593
- retval=[]
594
- while !end?(pos) && self[pos].kind==:escape
595
- retval<<self[pos].value
596
- pos+=1
597
- end
598
- invalid_character "Unexpected End of String during escape" if end?(pos)
599
- retval<<self[pos].value
600
- pos+=1
601
- return pos,retval.flatten.join
637
+ return pos+1,self[pos].value if keep_initial
638
+ return pos+1,self[pos].value[1..self[pos].value.length]
602
639
  end
603
640
 
604
641
  class Status
605
- attr_accessor :close, :nothing_seen, :delim
606
- attr_accessor :have_delim, :have_item, :item_seen, :delim_seen
607
642
 
608
643
  def initialize(pos,tokenizer,args={})
609
- super()
644
+ @named_vars=[:close, :nothing_seen, :delim, :have_delim, :have_item,
645
+ :item_seen, :delim_seen]
646
+
610
647
  @tokenizer=tokenizer
611
- @start_pos=pos
612
- @close=args[:close] || nil
613
- @item_seen=args[:preload].nil?
614
648
 
615
- @have_item = @have_delim = @delim_seen = false
649
+ @stat_hash={}
650
+ @named_vars.each {|i| @stat_hash[i]=nil} #preload for debugging
651
+ self.have_item = self.have_delim = self.delim_seen = false
652
+ @stat_hash.merge!(args) #args overwrites @stat_hash where keys are equal
653
+
654
+ self.item_seen=@stat_hash[:preload].nil?
616
655
 
617
656
  #If we expect to find a closing element, the delimiter will be a comma
618
657
  #Otherwise we'll discover it later
619
- if args[:delim].nil?
620
- @delim=close.nil? ? nil : :comma
621
- else
622
- @delim=args[:delim]
623
- end
658
+ self.delim=:comma if self.delim.nil? && !self.close.nil?
624
659
 
625
- #self[:have_item]=false
626
- #self[:have_delim]=false
627
- #self[:nothing_seen]=true
628
- @stat_hash={}
629
- @stat_hash[:skip_until_close]=args[:skip_until_close]==true || false
660
+ self.skip_until_close=self.skip_until_close==true || false #enforce boolean result
630
661
  end
631
662
 
632
- def inspect
633
- str="#<#{self.class}:0x#{self.__id__.to_s(16)} "
634
- arr=[]
663
+ def args
664
+ hash={}
635
665
  vars=instance_variables
636
666
  vars.delete("@tokenizer")
637
667
  vars.delete("@stat_hash")
638
- vars.each{|i| arr<<"#{i}=#{instance_variable_get(i).inspect}" }
639
- @stat_hash.each_pair{ |k,v| arr<<"@#{k.to_s}=#{v.inspect}" }
640
- str+=arr.join(", ")+">"
641
- str
668
+ vars.delete("@named_vars")
669
+ vars.each do |i|
670
+ hash.merge!(i.split("@")[-1].to_sym=>instance_variable_get(i))
671
+ end
672
+ hash.merge!(@stat_hash)
673
+ end
674
+
675
+ def merge(other_hash)
676
+ raise ZError.new("Hash Value required",:retry=>false) if other_hash.class!=Hash
677
+ args.merge(other_hash)
678
+ end
679
+
680
+ def inspect
681
+ "#<#{self.class}:0x#{self.__id__.to_s(16)} " + args.inspect + ">"
642
682
  end
643
683
 
644
684
  def []=(key,value)
@@ -650,48 +690,51 @@ class Tokenizer < Array
650
690
  end
651
691
 
652
692
  def method_missing(sym,*args)
653
- have_key=@stat_hash.has_key?(sym)
654
- if have_key && args.empty?
655
- return @stat_hash[sym]
656
- elsif have_key && !args.empty?
657
- str=sym.to_s
658
- if str[str.length-1..str.length]=="="
659
- str.chop!
660
- @stat_hash[str.intern]=args
661
- return args
662
- end
693
+ key=sym.to_s.split("=")[0].intern
694
+ have_equal=(key.to_s!=sym.to_s)
695
+ val=@stat_hash[key]
696
+ return val if val && !have_equal && args.empty?
697
+
698
+ if have_equal
699
+ @stat_hash[key]=*args
700
+ return *args
663
701
  end
664
702
 
665
- super(sym,args)
703
+ #just to be sure let's kick this to the super class, otherwise return nil
704
+ begin
705
+ return super(sym,args)
706
+ rescue NoMethodError
707
+ return nil
708
+ end
666
709
  end
667
710
 
668
711
  def item(pos)
669
712
  #set the delimiter to whitespace if we've never seen a delimiter but have an item
670
- if @delim.nil? && @have_item && !@delim_seen
671
- @delim=:whitespace
672
- @delim_seen=true
713
+ if self.delim.nil? && self.have_item && !self.delim_seen
714
+ self.delim=:whitespace
715
+ self.delim_seen=true
673
716
  end
674
717
 
675
718
  @tokenizer.invalid_character(pos, :error=>DelimiterExpected) if
676
- @have_item && @delim!=:whitespace && !@tokenizer.of_type?(pos,[:open,:close])
677
- @item_seen=true
678
- @have_item=true
679
- @have_delim=false
719
+ self.have_item && self.delim!=:whitespace && !@tokenizer.of_type?(pos,[:open,:close])
720
+ self.item_seen=true
721
+ self.have_item=true
722
+ self.have_delim=false
680
723
  end
681
724
 
682
725
  def delimiter(pos)
683
726
  if @tokenizer.of_type?(pos,:comma)
684
- @tokenizer.invalid_character(pos,:error=>WhitespaceExpected) if @delim==:whitespace
685
- @tokenizer.invalid_character(pos,:error=>ItemExpected) if @delim==:comma and @have_delim
727
+ @tokenizer.invalid_character(pos,:error=>WhitespaceExpected) if self.delim==:whitespace
728
+ @tokenizer.invalid_character(pos,:error=>ItemExpected) if self.delim==:comma and self.have_delim
686
729
  elsif @tokenizer.of_type?(pos,:whitespace)
687
- @delim=:whitespace if @delim.nil? && @seen_item
730
+ self.delim=:whitespace if self.delim.nil? && self.seen_item
688
731
  else
689
732
  @tokenizer.invalid_character(pos)
690
733
  end
691
734
 
692
- @delim_seen=true
693
- @have_item=false
694
- @have_delim=true
735
+ self.delim_seen=true
736
+ self.have_item=false
737
+ self.have_delim=true
695
738
  end
696
739
 
697
740
  end
@@ -725,13 +768,6 @@ class Tokenizer < Array
725
768
 
726
769
  invalid_character(pos,:error=>UnexpectedClose) if close?(pos) && status.close.nil?
727
770
 
728
- if of_type?(pos,:escape)
729
- debug(8,:msg=>"escape",:var=>[pos,self[pos]])
730
- pos,result=get_escape(pos)
731
- retval<<result
732
- next
733
- end
734
-
735
771
  if status.skip_until_close
736
772
  debug(8,:msg=>"skip_until_close",:var=>[pos,self[pos]])
737
773
  retval<<self[pos].value
@@ -741,9 +777,13 @@ class Tokenizer < Array
741
777
  end
742
778
 
743
779
  case what_is?(pos)
780
+ when :comment
781
+ return pos,retval if !status.keep_comment
782
+ retval<<self[pos].value
783
+ return pos,retval
744
784
  when :escape
745
785
  status.item(pos)
746
- pos,result=get_escape(pos)
786
+ pos,result=get_escape(pos,status.args)
747
787
  retval<<result
748
788
  when :paren
749
789
  status.item(pos)
data/libs/printer.rb CHANGED
@@ -19,8 +19,8 @@
19
19
 
20
20
  ##########################################
21
21
  # Subversion information
22
- # $Id: printer.rb 325 2011-09-26 08:57:00Z nelsonab $
23
- # $Revision: 325 $
22
+ # $Id: printer.rb 332 2011-09-30 07:24:38Z nelsonab $
23
+ # $Revision: 332 $
24
24
  ##########################################
25
25
 
26
26
  require 'zbxapi/zdebug'
@@ -285,7 +285,7 @@ class OutputPrinter
285
285
  header=["groupid","name"]
286
286
  when :hostgroupid
287
287
  header=["name", "groupid", "internal"]
288
- when :raw
288
+ when :raw_api
289
289
  header=results[0].keys
290
290
  when nil
291
291
  header=results[0].keys
@@ -334,7 +334,7 @@ class OutputPrinter
334
334
 
335
335
  def print_hash(dataset,cols)
336
336
  puts "Hash object printing not implemented, here is the raw result"
337
- p dataset.result
337
+ p dataset.data
338
338
  end
339
339
 
340
340
 
data/libs/revision.rb CHANGED
@@ -1 +1 @@
1
- REVISION=327
1
+ REVISION=332
@@ -17,8 +17,8 @@
17
17
 
18
18
  ##########################################
19
19
  # Subversion information
20
- # $Id: zabcon_commands.rb 325 2011-09-26 08:57:00Z nelsonab $
21
- # $Revision: 325 $
20
+ # $Id: zabcon_commands.rb 332 2011-09-30 07:24:38Z nelsonab $
21
+ # $Revision: 332 $
22
22
  ##########################################
23
23
 
24
24
  require "zbxapi/zdebug"
@@ -234,17 +234,19 @@ ZabconCommand.add_command "raw api" do
234
234
  set_method do |params|
235
235
  api_func=params[:method]
236
236
  params=params[:params]
237
-
238
237
  server.connection.raw_api(api_func, params)
239
238
  end
240
239
 
241
- arg_processor do |params,valid_args,flags|
240
+ arg_processor do |params,args,flags|
241
+ params=Tokenizer.new(params).parse
242
242
  parameter_error "Command \"raw api\" requires parameters" if params.empty?
243
- params=params.split2
244
243
  api_func=params[0]
245
244
  params.delete_at(0)
246
- retval= params_to_hash(params.join(" "))
247
- {:method=>api_func, :params=>retval}
245
+ params2={}
246
+ params.each do |i|
247
+ params2.merge!(i)
248
+ end
249
+ {:method=>api_func, :params=>params2}
248
250
  end
249
251
  set_flag :login_required
250
252
  set_flag :print_output
@@ -276,17 +278,6 @@ ZabconCommand.add_command "raw json" do
276
278
  result_type :raw_api
277
279
  end
278
280
 
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
281
  ###############################################################################
291
282
  #Application Application#
292
283
  ###############################################################################
@@ -349,6 +340,28 @@ ZabconCommand.add_command "get host" do
349
340
  result_type :host
350
341
  end
351
342
 
343
+ ZabconCommand.add_command "update host" do
344
+ set_method do |params|
345
+ raise Command::NonFatalError.new("Update host requires a parameter to update.") if params.size<2
346
+
347
+ #TODO Add host.update to the zbxapi library
348
+ server.connection.raw_api("host.update",params)
349
+ end
350
+
351
+ set_valid_args 'hostid', 'proxy_hostid', 'host', 'dns', 'useip', 'ip',
352
+ 'port', 'status', 'useipmi', 'ipmi_port', 'ipmi_authtype',
353
+ 'ipmi_privilege', 'ipmi_username', 'ipmi_password',
354
+ 'ipmi_ip'
355
+ required_args "hostid"
356
+
357
+ # default_show ["itemid", "key_", "description"]
358
+ set_flag :login_required
359
+ set_flag :print_output
360
+ set_help_tag :update_user
361
+ result_type :host
362
+ end
363
+
364
+
352
365
  ###############################################################################
353
366
  #Host Group Host Group#
354
367
  ###############################################################################
@@ -403,9 +416,9 @@ ZabconCommand.add_command "get item" do
403
416
  set_method do |params|
404
417
  server.connection.item.get(params)
405
418
  end
406
- set_valid_args ['itemids','hostids','groupids', 'triggerids','applicationids',
419
+ set_valid_args 'itemids','hostids','groupids', 'triggerids','applicationids',
407
420
  'status','templated_items','editable','count','pattern','limit',
408
- 'order', 'show']
421
+ 'order', 'show'
409
422
  default_show ["itemid", "key_", "description"]
410
423
  set_flag :login_required
411
424
  set_flag :print_output
@@ -514,9 +527,9 @@ ZabconCommand.add_command "update user" do
514
527
  @connection.user.update([parameters])
515
528
  end
516
529
  end
517
- set_valid_args ['userid','name', 'surname', 'alias', 'passwd', 'url',
530
+ set_valid_args 'userid','name', 'surname', 'alias', 'passwd', 'url',
518
531
  'autologin', 'autologout', 'lang', 'theme', 'refresh',
519
- 'rows_per_page', 'type',]
532
+ 'rows_per_page', 'type'
520
533
  default_show ["itemid", "key_", "description"]
521
534
  set_flag :login_required
522
535
  set_flag :print_output
@@ -544,12 +557,25 @@ end
544
557
  ZabconCommand.add_command "clone host" do
545
558
  set_method do |params|
546
559
  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
-
560
+ target=server.connection.host.get({"output"=>"extend", "filter"=>{"host"=>[params["host"]]}})
561
+ if !target.empty?
562
+ raise Command::NonFatalError.new("host #{params["host"]} already exists with hostid #{target[0]["hostid"]}")
563
+ end
564
+ source_info=server.connection.host.get({"output"=>"extend", "selectParentTemplates"=>"refer", "filter"=>{"host"=>[source]}})
565
+ source_hostid=source_info[0]["hostid"]
566
+ templates=source_info[0]["parentTemplates"].map{|i| i["hostid"] }
567
+ groups=server.connection.hostgroup.get(
568
+ {"hostids"=>[source_hostid],
569
+ "output"=>"extend"}).map do |i| {"groupid"=>i["groupid"]}
570
+ end
571
+ result=server.connection.host.create({"host"=>params["host"],
572
+ "ip"=>params["ip"],"useip"=>params["useip"],"groups"=>groups,
573
+ "port"=>params["port"],
574
+ "templates"=>templates.map {|i| {"templateid"=>i}} })
575
+ result
551
576
  end
552
577
 
578
+ required_args "host", ["ip","dns"], "useip", "port", "source"
553
579
  set_flag :login_required
554
580
  set_flag :print_output
555
581
  set_help_tag :clone_host
data/libs/zabcon_core.rb CHANGED
@@ -20,8 +20,8 @@
20
20
 
21
21
  ##########################################
22
22
  # Subversion information
23
- # $Id: zabcon_core.rb 325 2011-09-26 08:57:00Z nelsonab $
24
- # $Revision: 325 $
23
+ # $Id: zabcon_core.rb 330 2011-09-27 06:16:40Z nelsonab $
24
+ # $Revision: 330 $
25
25
  ##########################################
26
26
 
27
27
  require 'libs/utility_items'
@@ -187,6 +187,9 @@ class ZabconCore
187
187
  rescue CommandList::InvalidCommand => e
188
188
  puts e.message
189
189
  retry
190
+ rescue Command::NonFatalError => e
191
+ puts e.message
192
+ retry
190
193
  rescue Command::ParameterError => e
191
194
  puts e.message
192
195
  retry
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabcon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.327
4
+ hash: 647
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 332
10
+ version: 0.0.332
5
11
  platform: ruby
6
12
  authors:
7
13
  - A. Nelson
@@ -9,39 +15,53 @@ autorequire:
9
15
  bindir: .
10
16
  cert_chain: []
11
17
 
12
- date: 2011-09-26 00:00:00 -04:00
18
+ date: 2011-09-30 00:00:00 -04:00
13
19
  default_executable: zabcon
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: zbxapi
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 659
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 324
23
34
  version: 0.1.324
24
- version:
35
+ type: :runtime
36
+ version_requirements: *id001
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: parseconfig
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
30
42
  requirements:
31
43
  - - ">="
32
44
  - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
33
48
  version: "0"
34
- version:
49
+ type: :runtime
50
+ version_requirements: *id002
35
51
  - !ruby/object:Gem::Dependency
36
52
  name: highline
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
40
56
  requirements:
41
57
  - - ">="
42
58
  - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
43
62
  version: "0"
44
- version:
63
+ type: :runtime
64
+ version_requirements: *id003
45
65
  description: Zabcon is a command line interface for Zabbix written in Ruby
46
66
  email: nelsonab@red-tux.net
47
67
  executables:
@@ -68,6 +88,7 @@ files:
68
88
  - libs/zabcon_globals.rb
69
89
  - libs/zabbix_server.rb
70
90
  - libs/utility_items.rb
91
+ - ./zabcon.rb
71
92
  has_rdoc: true
72
93
  homepage: http://trac.red-tux.net/
73
94
  licenses:
@@ -78,21 +99,29 @@ rdoc_options: []
78
99
  require_paths:
79
100
  - .
80
101
  required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
81
103
  requirements:
82
104
  - - ">="
83
105
  - !ruby/object:Gem::Version
106
+ hash: 59
107
+ segments:
108
+ - 1
109
+ - 8
110
+ - 6
84
111
  version: 1.8.6
85
- version:
86
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
87
114
  requirements:
88
115
  - - ">="
89
116
  - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
90
120
  version: "0"
91
- version:
92
121
  requirements:
93
122
  - Requires zbxapi, parseconfig and highline
94
123
  rubyforge_project: zabcon
95
- rubygems_version: 1.3.5
124
+ rubygems_version: 1.3.7
96
125
  signing_key:
97
126
  specification_version: 3
98
127
  summary: Zabcon command line interface for Zabbix