2 # The main runner for the daemon, supports running in the foreground
3 # and the background, keeps detailed stats and provides hooks to access
8 def initialize(configfile)
9 @config = Config.instance
10 @config.loadconfig(configfile) unless @config.configured
11 @config.mode = :server
13 @stats = PluginManager["global_stats"]
15 @security = PluginManager["security_plugin"]
16 @security.initiated_by = :node
18 @connection = PluginManager["connector_plugin"]
24 Signal.trap("USR1") do
25 log_code(:PLMC2, "Reloading all agents after receiving USR1 signal", :info)
29 Signal.trap("USR2") do
30 log_code(:PLMC3, "Cycling logging level due to USR2 signal", :info)
35 Util.setup_windows_sleeper
39 # Starts the main loop, before calling this you should initialize the MCollective::Config singleton.
41 Data.load_data_sources
43 Util.subscribe(Util.make_subscriptions("mcollective", :broadcast))
44 Util.subscribe(Util.make_subscriptions("mcollective", :directed)) if @config.direct_addressing
46 # Start the registration plugin if interval isn't 0
48 PluginManager["registration_plugin"].run(@connection) unless @config.registerinterval == 0
50 logexception(:PLMC4, "Failed to start registration plugin: %{error}", :error, e)
57 unless request.agent == "mcollective"
60 log_code(:PLMC5, "Received a control message, possibly via 'mco controller' but this has been deprecated", :error)
62 rescue SignalException => e
63 logexception(:PLMC7, "Exiting after signal: %{error}", :warn, e)
64 @connection.disconnect
67 rescue MsgTTLExpired => e
68 logexception(:PLMC9, "Expired Message: %{error}", :warn, e)
70 rescue NotTargettedAtUs => e
71 log_code(:PLMC6, "Message does not pass filters, ignoring", :debug)
74 logexception(:PLMC10, "Failed to handle message: %{error}", :warn, e, true)
80 # Deals with messages directed to agents
82 log_code(:PLMC8, "Handling message for agent '%{agent}' on collective '%{collective} with requestid '%{requestid}'", :debug, :agent => request.agent, :collective => request.collective, :requestid => request.requestid)
84 @agents.dispatch(request, @connection) do |reply_message|
85 reply(reply_message, request) if reply_message
89 # Deals with messages sent to our control topic
90 def controlmsg(request)
91 Log.debug("Handling message for mcollectived controller")
94 case request.payload[:body]
96 reply(@stats.to_hash, request)
98 when /^reload_agent (.+)$/
99 reply("reloaded #{$1} agent", request) if @agents.loadagent($1)
101 when /^reload_agents$/
103 reply("reloaded all agents", request) if @agents.loadagents
106 Log.error("Received an unknown message to the controller")
109 rescue Exception => e
110 Log.error("Failed to handle control message: #{e}")
114 # Receive a message from the connection handler
116 request = @connection.receive
117 request.type = :request
127 # Sends a reply to a specific target topic
128 def reply(msg, request)
129 msg = Message.new(msg, nil, :request => request)