ca285dd9c00c7e39db0c6f5434db3264a80f082f
[packages/precise/mcollective.git] / bin / mc-call-agent
1 #!/usr/bin/env ruby
2
3 require 'mcollective'
4 require 'pp'
5
6 oparser = MCollective::Optionparser.new({:verbose => true}, "filter")
7
8 options = oparser.parse{|parser, options|
9   parser.define_head "Call an agent parsing an argument to it"
10   parser.banner = "Usage: mc-call-agent [options] --agent agent --argument arg"
11
12   parser.on('-a', '--agent AGENT', 'Agent to call') do |v|
13     options[:agent] = v
14   end
15
16   parser.on('--arg', '--argument ARGUMENT', 'Argument to pass to agent') do |v|
17     options[:argument] = v
18   end
19 }
20
21 if options[:agent] == nil || options[:argument] == nil
22   puts("Please use either --agent or --argument")
23   exit 1
24 end
25
26 begin
27   options[:filter]["agent"] << options[:agent]
28
29   client = MCollective::Client.new(options[:config])
30   client.options = options
31
32   c = 0
33
34   stats = client.discovered_req(options[:argument], options[:agent]) do |resp|
35     next if resp == nil
36
37     c += 1
38
39     if options[:verbose]
40       puts("#{resp[:senderid]}>")
41       pp resp[:body]
42     else
43       puts if c % 4 == 1
44       printf("%-30s", resp[:senderid])
45     end
46   end
47
48   client.disconnect
49 rescue Exception => e
50   STDERR.puts "Could not call remote agent: #{e}"
51   exit 1
52 end
53
54 client.display_stats(stats)