Added mcollective 2.3.1 package
[packages/trusty/mcollective.git] / plugins / mcollective / application / facts.rb
1 class MCollective::Application::Facts<MCollective::Application
2   description "Reports on usage for a specific fact"
3
4   def post_option_parser(configuration)
5     configuration[:fact] = ARGV.shift if ARGV.size > 0
6   end
7
8   def validate_configuration(configuration)
9     raise "Please specify a fact to report for" unless configuration.include?(:fact)
10   end
11
12   def show_single_fact_report(fact, facts, verbose=false)
13     puts("Report for fact: #{fact}\n\n")
14
15     facts.keys.sort.each do |k|
16       printf("        %-40sfound %d times\n", k, facts[k].size)
17
18       if verbose
19         puts
20
21         facts[k].sort.each do |f|
22           puts("            #{f}")
23         end
24
25         puts
26       end
27     end
28   end
29
30   def main
31     rpcutil = rpcclient("rpcutil")
32     rpcutil.progress = false
33
34     facts = {}
35
36     rpcutil.get_fact(:fact => configuration[:fact]) do |resp|
37       begin
38         value = resp[:body][:data][:value]
39         if value
40           facts.include?(value) ? facts[value] << resp[:senderid] : facts[value] = [ resp[:senderid] ]
41         end
42       rescue Exception => e
43         STDERR.puts "Could not parse facts for #{resp[:senderid]}: #{e.class}: #{e}"
44       end
45     end
46
47     show_single_fact_report(configuration[:fact], facts, options[:verbose])
48
49     printrpcstats
50
51     halt rpcutil.stats
52   end
53 end