From cf961d81d6a5d773bde8284be6ef1d442bc0f548 Mon Sep 17 00:00:00 2001 From: Marcello Coutinho Date: Tue, 7 May 2013 12:47:30 -0300 Subject: check_mk_agent for nagios - first package files --- config/checkmk-agent/checkmk.inc | 302 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 302 insertions(+) create mode 100644 config/checkmk-agent/checkmk.inc (limited to 'config/checkmk-agent/checkmk.inc') diff --git a/config/checkmk-agent/checkmk.inc b/config/checkmk-agent/checkmk.inc new file mode 100644 index 00000000..056a39eb --- /dev/null +++ b/config/checkmk-agent/checkmk.inc @@ -0,0 +1,302 @@ + 0) + mwexec("/etc/rc.d/inetd restart"); + else + mwexec("/etc/rc.d/inetd stop"); + + //Write config if any file from filesystem was loaded + if ($update_conf > 0) + write_config(); + + // mount filesystem readonly + conf_mount_ro(); + + checkmk_sync_on_changes(); +} + +function checkmk_validate_input($post, &$input_errors) { + foreach ($post as $key => $value) { + if (empty($value)) + continue; + if (substr($key, 0, 3) == "port" && !preg_match("/^\d+$/", $value)) + $input_errors[] = "{$value} is no a valid port number"; + if (substr($key, 0, 11) == "description" && !preg_match("@^[a-zA-Z0-9 _/.-]+$@", $value)) + $input_errors[] = "Do not use special characters on description"; + if (substr($key, 0, 8) == "fullfile" && !preg_match("@^[a-zA-Z0-9_/.-]+$@", $value)) + $input_errors[] = "Do not use special characters on filename"; + + } +} +############################################## +/* Uses XMLRPC to synchronize the changes to a remote node */ +function checkmk_sync_on_changes() { + global $config, $g; + if (is_array($config['installedpackages']['checkmksync']['config'])){ + $checkmk_sync=$config['installedpackages']['checkmksync']['config'][0]; + $synconchanges = $checkmk_sync['synconchanges']; + $synctimeout = $checkmk_sync['synctimeout']; + switch ($synconchanges){ + case "manual": + if (is_array($checkmk_sync[row])){ + $rs=$checkmksync[row]; + } + else{ + log_error("[Check_mk-agent] xmlrpc sync is enabled but there is no hosts to push on squid config."); + return; + } + break; + case "auto": + if (is_array($config['installedpackages']['carpsettings']) && is_array($config['installedpackages']['carpsettings']['config'])){ + $system_carp=$config['installedpackages']['carpsettings']['config'][0]; + $rs[0]['ipaddress']=$system_carp['synchronizetoip']; + $rs[0]['username']=$system_carp['username']; + $rs[0]['password']=$system_carp['password']; + } + else{ + log_error("[Check_mk-agent] xmlrpc sync is enabled but there is no system backup hosts to push squid config."); + return; + } + break; + default: + return; + break; + } + if (is_array($rs)){ + log_error("[Check_mk-agent] xmlrpc sync is starting."); + foreach($rs as $sh){ + $sync_to_ip = $sh['ipaddress']; + $password = $sh['password']; + if($sh['username']) + $username = $sh['username']; + else + $username = 'admin'; + if($password && $sync_to_ip) + checkmk_do_xmlrpc_sync($sync_to_ip, $username, $password,$synctimeout); + } + log_error("[Check_mk-agent] xmlrpc sync is ending."); + } + } +} +############################################## +/* Do the actual XMLRPC sync */ +function checkmk_do_xmlrpc_sync($sync_to_ip, $username, $password,$synctimeout) { + global $config, $g; + + if(!$username) + return; + + if(!$password) + return; + + if(!$sync_to_ip) + return; + + $xmlrpc_sync_neighbor = $sync_to_ip; + if($config['system']['webgui']['protocol'] != "") { + $synchronizetoip = $config['system']['webgui']['protocol']; + $synchronizetoip .= "://"; + } + $port = $config['system']['webgui']['port']; + /* if port is empty lets rely on the protocol selection */ + if($port == "") { + if($config['system']['webgui']['protocol'] == "http") + $port = "80"; + else + $port = "443"; + } + $synchronizetoip .= $sync_to_ip; + + /* xml will hold the sections to sync */ + $xml = array(); + $xml['checkmk'] = $config['installedpackages']['checkmk']; + + /* assemble xmlrpc payload */ + $params = array( + XML_RPC_encode($password), + XML_RPC_encode($xml) + ); + + /* set a few variables needed for sync code borrowed from filter.inc */ + $url = $synchronizetoip; + log_error("[Check_mk-agent] Beginning checkmk XMLRPC sync to {$url}:{$port}."); + $method = 'pfsense.merge_installedpackages_section_xmlrpc'; + $msg = new XML_RPC_Message($method, $params); + $cli = new XML_RPC_Client('/xmlrpc.php', $url, $port); + $cli->setCredentials($username, $password); + if($g['debug']) + $cli->setDebug(1); + /* send our XMLRPC message and timeout after 250 seconds */ + $resp = $cli->send($msg, $synctimeout); + if(!$resp) { + $error = "[Check_mk-agent] A communications error occurred while attempting checkmk XMLRPC sync with {$url}:{$port}."; + log_error($error); + file_notice("sync_settings", $error, "checkmk Settings Sync", ""); + } elseif($resp->faultCode()) { + $cli->setDebug(1); + $resp = $cli->send($msg, $synctimeout); + $error = "[Check_mk-agent] An error code was received while attempting checkmk XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString(); + log_error($error); + file_notice("sync_settings", $error, "checkmk Settings Sync", ""); + } else { + log_error("[Check_mk-agent] XMLRPC sync successfully completed with {$url}:{$port}."); + } + + /* tell checkmk to reload our settings on the destionation sync host. */ + $method = 'pfsense.exec_php'; + $execcmd = "require_once('/usr/local/pkg/checkmk.inc');\n"; + $execcmd .= "sync_package_checkmk();"; + /* assemble xmlrpc payload */ + $params = array( + XML_RPC_encode($password), + XML_RPC_encode($execcmd) + ); + + log_error("[Check_mk-agent] XMLRPC reload data {$url}:{$port}."); + $msg = new XML_RPC_Message($method, $params); + $cli = new XML_RPC_Client('/xmlrpc.php', $url, $port); + $cli->setCredentials($username, $password); + $resp = $cli->send($msg, $synctimeout); + if(!$resp) { + $error = "[Check_mk-agent] A communications error occurred while attempting checkmk XMLRPC sync with {$url}:{$port} (pfsense.exec_php)."; + log_error($error); + file_notice("sync_settings", $error, "checkmk Settings Sync", ""); + } elseif($resp->faultCode()) { + $cli->setDebug(1); + $resp = $cli->send($msg, $synctimeout); + $error = "[Check_mk-agent] An error code was received while attempting checkmk XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString(); + log_error($error); + file_notice("sync_settings", $error, "checkmk Settings Sync", ""); + } else { + log_error("[Check_mk-agent] XMLRPC reload data success with {$url}:{$port} (pfsense.exec_php)."); + } + +} + +?> -- cgit v1.2.3