1c8c71e36d4caa5351c237196cba1ec0ecc1385e
[packages/precise/mcollective.git] / plugins / mcollective / agent / rpcutil.rb
1 module MCollective
2   module Agent
3     class Rpcutil<RPC::Agent
4       # Basic system inventory, same as the basic discovery agent
5       action "inventory" do
6         reply[:agents] = Agents.agentlist
7         reply[:facts] = PluginManager["facts_plugin"].get_facts
8         reply[:version] = MCollective.version
9         reply[:classes] = []
10         reply[:main_collective] = config.main_collective
11         reply[:collectives] = config.collectives
12         reply[:data_plugins] = PluginManager.grep(/_data$/)
13
14         cfile = Config.instance.classesfile
15         if File.exist?(cfile)
16           reply[:classes] = File.readlines(cfile).map {|i| i.chomp}
17         end
18       end
19
20       # Retrieve a single fact from the node
21       action "get_fact" do
22         reply[:fact] = request[:fact]
23         reply[:value] = Facts[request[:fact]]
24       end
25
26       # Get the global stats for this mcollectied
27       action "daemon_stats" do
28         stats = PluginManager["global_stats"].to_hash
29
30         reply[:threads] = stats[:threads]
31         reply[:agents] = stats[:agents]
32         reply[:pid] = stats[:pid]
33         reply[:times] = stats[:times]
34         reply[:configfile] = Config.instance.configfile
35         reply[:version] = MCollective.version
36
37         reply.data.merge!(stats[:stats])
38       end
39
40       # Builds an inventory of all agents on teh machine
41       # including license, version and timeout information
42       action "agent_inventory" do
43         reply[:agents] = []
44
45         Agents.agentlist.sort.each do |target_agent|
46           agent = PluginManager["#{target_agent}_agent"]
47           actions = agent.methods.grep(/_agent/)
48
49           agent_data = {:agent => target_agent,
50                         :license => "unknown",
51                         :timeout => agent.timeout,
52                         :description => "unknown",
53                         :name => target_agent,
54                         :url => "unknown",
55                         :version => "unknown",
56                         :author => "unknown"}
57
58           agent_data.merge!(agent.meta)
59
60           reply[:agents] << agent_data
61         end
62       end
63
64       # Retrieves a single config property that is in effect
65       action "get_config_item" do
66         reply.fail! "Unknown config property #{request[:item]}" unless config.respond_to?(request[:item])
67
68         reply[:item] = request[:item]
69         reply[:value] = config.send(request[:item])
70       end
71
72       # Responds to PING requests with the local timestamp
73       action "ping" do
74         reply[:pong] = Time.now.to_i
75       end
76
77       # Returns all configured collectives
78       action "collective_info" do
79         config = Config.instance
80         reply[:main_collective] = config.main_collective
81         reply[:collectives] = config.collectives
82       end
83
84       action "get_data" do
85         if request[:query]
86           query = Data.ddl_transform_input(Data.ddl(request[:source]), request[:query].to_s)
87         else
88           query = nil
89         end
90
91         data = Data[ request[:source] ].lookup(query)
92
93         data.keys.each do |key|
94           reply[key] = data[key]
95         end
96       end
97     end
98   end
99 end