106 m_machine_state(http_state_retriving_comand_line),
122 std::string hello_str =
"# munin node at ";
123 hello_str += m_host_name +
"\n";
124 send_hook(hello_str);
146 const char* pbuff = (
const char*)ptr;
147 std::string recvd_buff(pbuff, cb);
148 LOG_PRINT(
"munin_recv: \n" << recvd_buff, LOG_LEVEL_3);
150 m_cache += recvd_buff;
152 bool stop_handling =
false;
153 while(!stop_handling)
155 switch(m_machine_state)
157 case http_state_retriving_comand_line:
160 std::string::size_type fpos = m_cache.find(
'\n');
161 if(std::string::npos != fpos )
163 bool res = handle_command(m_cache);
166 m_cache.erase(0, fpos+1);
169 stop_handling =
true;
172 case http_state_error:
173 stop_handling =
true;
176 LOG_ERROR(
"Error in munin state machine! Unknown state=" << m_machine_state);
177 stop_handling =
true;
178 m_machine_state = http_state_error;
192 char hostname[64] = {0};
193 int res = gethostname(hostname, 64);
195 m_host_name = hostname;
198 bool handle_command(
const std::string& command)
201 STATIC_REGEXP_EXPR_1(rexp_match_command_line,
"^((list)|(nodes)|(config)|(fetch)|(version)|(quit))(\\s+(\\S+))?", boost::regex::icase | boost::regex::normal);
203 size_t match_len = 0;
204 boost::smatch result;
205 if(boost::regex_search(command, result, rexp_match_command_line, boost::match_default) && result[0].matched)
207 if(result[2].matched)
209 return handle_list_command();
210 }
else if(result[3].matched)
212 return handle_nodes_command();
213 }
else if(result[4].matched)
215 if(result[9].matched)
216 return handle_config_command(result[9]);
219 send_hook(
"Unknown service\n");
221 }
else if(result[5].matched)
223 if(result[9].matched)
224 return handle_fetch_command(result[9]);
227 send_hook(
"Unknown service\n");
229 }
else if(result[6].matched)
231 return handle_version_command();
232 }
else if(result[7].matched)
234 return handle_quit_command();
237 return send_hook(
"Unknown command. Try list, nodes, config, fetch, version or quit\n");
240 return send_hook(
"Unknown command. Try list, nodes, config, fetch, version or quit\n");;
243 bool handle_list_command()
245 std::string buff_to_send;
246 for(std::list<munin_service>::const_iterator it = m_config.m_services.begin(); it!=m_config.m_services.end();it++)
248 buff_to_send += it->m_service_name +
" ";
251 return send_hook(buff_to_send);
253 bool handle_nodes_command()
256 send_hook(m_host_name +
"\n.\n");
259 bool handle_config_command(
const std::string& service_name)
261 munin_service* psrv = get_service_by_name(service_name);
263 return send_hook(std::string() +
"Unknown service\n");
266 return send_hook(psrv->m_service_config_string +
".\n");
269 bool handle_fetch_command(
const std::string& service_name)
271 munin_service* psrv = get_service_by_name(service_name);
273 return send_hook(std::string() +
"Unknown service\n");
276 psrv->m_pdata_provider->update_service_data(psrv, buff);
279 return send_hook(buff);
281 bool handle_version_command()
283 return send_hook(
"Munin node component by Andrey Sabelnikov\n");
285 bool handle_quit_command()
290 bool send_hook(
const std::string& buff)
292 LOG_PRINT(
"munin_send: \n" << buff, LOG_LEVEL_3);
301 munin_service* get_service_by_name(
const std::string& srv_name)
303 std::list<munin_service>::iterator it = m_config.m_services.begin();
304 for(; it!=m_config.m_services.end(); it++)
305 if(it->m_service_name == srv_name)
308 if(it==m_config.m_services.end())
315 http_state_retriving_comand_line,
321 machine_state m_machine_state;
323 std::string m_host_name;