From 44ff9ca0dc38ccbe789a090aafd8d5e863ab9e6e Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 8 Oct 2015 15:00:56 +0200 Subject: [PATCH] #3818 Optimize configuration generation --- .../configGenerate/generateFiles.php | 4 +- .../configGenerate/xml/generateFiles.php | 356 +++++++----------- .../configGenerate/xml/moveFiles.php | 44 ++- 3 files changed, 159 insertions(+), 245 deletions(-) diff --git a/www/include/configuration/configGenerate/generateFiles.php b/www/include/configuration/configGenerate/generateFiles.php index 63d76047dc5..6270bd881ee 100644 --- a/www/include/configuration/configGenerate/generateFiles.php +++ b/www/include/configuration/configGenerate/generateFiles.php @@ -59,7 +59,7 @@ switch ($o) { default : - require_once($path."formGenerateFiles.php"); + require_once($path . "formGenerateFiles.php"); break; } -?> \ No newline at end of file +?> diff --git a/www/include/configuration/configGenerate/xml/generateFiles.php b/www/include/configuration/configGenerate/xml/generateFiles.php index 68fd8a43cef..792a959613e 100644 --- a/www/include/configuration/configGenerate/xml/generateFiles.php +++ b/www/include/configuration/configGenerate/xml/generateFiles.php @@ -43,145 +43,25 @@ exit; } -function printDebug($xml, $tabs) -{ - global $pearDB, $ret, $nagiosCFGPath, $oreon; - - $DBRESULT_Servers = $pearDB->query("SELECT `nagios_bin` FROM `nagios_server` WHERE `localhost` = '1' ORDER BY ns_activate DESC LIMIT 1"); - $nagios_bin = $DBRESULT_Servers->fetchRow(); - $DBRESULT_Servers->free(); - $msg_debug = array(); - $tab_server = array(); - foreach ($tabs as $tab) { - if (isset($ret["host"]) && ($ret["host"] == 0 || $ret["host"] == $tab['id'])) { - $tab_server[$tab["id"]] = array("id" => $tab["id"], "name" => $tab["name"], "localhost" => $tab["localhost"]); - } - } - foreach ($tab_server as $host) { - $stdout = shell_exec($nagios_bin["nagios_bin"] . " -v ".$nagiosCFGPath.$host["id"]."/nagiosCFG.DEBUG 2>&1"); - $stdout = htmlspecialchars($stdout, ENT_QUOTES, "UTF-8"); - $msg_debug[$host['id']] = str_replace ("\n", "
", $stdout); - $msg_debug[$host['id']] = str_replace ("Warning:", "Warning", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("warning: ", "Warning ", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("Error:", "Error", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("error:", "Error", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("reading", "Reading", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("running", "Running", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("Total Warnings: 0", "Total Warnings: 0", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("Total Errors: 0", "Total Errors: 0", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = str_replace ("
License:", " - License:", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = preg_replace('/\[[0-9]+?\] /', '', $msg_debug[$host['id']]); - - $lines = preg_split("/\/", $msg_debug[$host['id']]); - $msg_debug[$host['id']] = ""; - $i = 0; - foreach ($lines as $line) { - if (strncmp($line, "Processing object config file", strlen("Processing object config file")) - && strncmp($line, "Website: http://www.nagios.org", strlen("Website: http://www.nagios.org"))) - $msg_debug[$host['id']] .= $line . "
"; - $i++; - } - - } - - $xml->startElement("debug"); - $str = ""; - $returnCode = 0; - foreach ($msg_debug as $pollerId => $message) { - $show = "none"; - $toggler = ""; - $pollerNameColor = "green"; - if (preg_match_all("/Total (Errors|Warnings)\:[ ]+([0-9]+)/", $message, $globalMatches, PREG_SET_ORDER)) { - foreach ($globalMatches as $matches) { - if ($matches[2] != "0") { - $show = "block"; - $toggler = ""; - if ($matches[1] == "Errors") { - $pollerNameColor = "red"; - $returnCode = 1; - } elseif($matches[1] == "Warnings") { - $pollerNameColor = "orange"; - } - } - } - } else { - $show = "block"; - $pollerNameColor = "red"; - $toggler = ""; - $returnCode = 1; - } - $str .= ""; - $str .= $toggler . " "; - $str .= "".$tab_server[$pollerId]['name'] . "
"; - $str .= "
".htmlentities($message) . "

"; - } - $xml->text($str); - $xml->endElement(); - return $returnCode; -} - /** * List of error from php */ global $generatePhpErrors; $generatePhpErrors = array(); -/** - * The error handler for get error from PHP - * - * @see set_error_handler - */ -function log_error($errno, $errstr, $errfile, $errline) -{ - global $generatePhpErrors; - if (!(error_reporting() & $errno)) { - return; - } - - switch ($errno) { - case E_ERROR: - case E_USER_ERROR: - case E_CORE_ERROR: - $generatePhpErrors[] = array('error', $errstr); - break; - case E_WARNING: - case E_USER_WARNING: - case E_CORE_WARNING: - $generatePhpErrors[] = array('warning', $errstr); - break; - } - return true; -} - $path = $centreon_path . "www/include/configuration/configGenerate/"; -require_once $path."DB-Func.php"; $nagiosCFGPath = $centreon_path . "filesGeneration/nagiosCFG/"; $centreonBrokerPath = $centreon_path . "filesGeneration/broker/"; $DebugPath = "filesGeneration/nagiosCFG/"; -$poller = $_POST['poller']; -$comment = ($_POST['comment'] == "true") ? 1 : 0; -$debug = ($_POST['debug'] == "true") ? 1 : 0; -$generate = ($_POST['generate'] == "true") ? 1 : 0; - -$ret = array(); -$ret['host'] = $poller; -$ret['comment'] = $comment; -$ret['debug'] = $debug; - chdir($centreon_path . "www"); require_once $centreon_path . "www/include/configuration/configGenerate/DB-Func.php"; -require_once $centreon_path . "www/class/centreonDB.class.php"; -require_once $centreon_path . "www/class/centreonSession.class.php"; -require_once $centreon_path . "www/class/centreonUtils.class.php"; +require_once $centreon_path . 'www/class/config-generate/generate.class.php'; require_once $centreon_path . "www/class/centreon.class.php"; -require_once $centreon_path . "www/class/centreonXML.class.php"; -require_once $centreon_path . "www/class/centreonConfigCentreonBroker.php"; -require_once $centreon_path . "www/class/centreonUser.class.php"; -require_once $centreon_path . "www/class/centreonACL.class.php"; -require_once $centreon_path . "www/class/centreonLDAP.class.php"; require_once $centreon_path . "www/class/centreonContactgroup.class.php"; -require_once $centreon_path . 'www/include/configuration/configGenerate/genCentreonBrokerCorrelation.php'; +require_once $centreon_path . "www/class/centreonACL.class.php"; +require_once $centreon_path . "www/class/centreonDB.class.php"; +require_once $centreon_path . "www/class/centreonXML.class.php"; session_start(); if ($_POST['sid'] != session_id()) { @@ -192,6 +72,18 @@ function log_error($errno, $errstr, $errfile, $errline) $xml = new CentreonXML(); $pearDB = new CentreonDB(); +$config_generate = new Generate(); + +$poller = $_POST['poller']; +$comment = ($_POST['comment'] == "true") ? 1 : 0; +$debug = ($_POST['debug'] == "true") ? 1 : 0; +$generate = ($_POST['generate'] == "true") ? 1 : 0; + +$ret = array(); +$ret['host'] = $poller; +$ret['comment'] = $comment; +$ret['debug'] = $debug; + /* Set new error handler */ set_error_handler('log_error'); @@ -200,111 +92,27 @@ function log_error($errno, $errstr, $errfile, $errline) $xml->startElement("response"); try { - /* - * Check dependancies - */ - $gbArr = manageDependencies(); - - /** - * Declare Poller ID - */ - global $pollerID; - - /* - * Request id and host type. - */ + $tabs = array(); if ($generate) { $pearDBO = new CentreonDB('centstorage'); $tabs = $oreon->user->access->getPollerAclConf(array('fields' => array('id', 'name', 'localhost', 'monitoring_engine'), 'order' => array('name'), 'keys' => array('id'), 'conditions' => array('ns_activate' => '1'))); - $cgObj = new CentreonContactgroup($pearDB); - $cgObj->syncWithLdap(); - foreach ($tabs as $tab){ - if (isset($poller) && ($tab['id'] == $poller || $poller == 0)) { - $pollerID = $tab['id']; - unset($DBRESULT2); - require $path."genCGICFG.php"; - require $path."genNagiosCFG.php"; - require $path."genNdomod.php"; - require $path."genNdo2db.php"; - require $path."genCentreonBroker.php"; - require $path."genNagiosCFG-DEBUG.php"; - require $path."genResourceCFG.php"; - require $path."genTimeperiods.php"; - require $path."genCommands.php"; - require $path."genConnectors.php"; - require $path."genContacts.php"; - require $path."genContactTemplates.php"; - require $path."genContactGroups.php"; - require $path."genHostTemplates.php"; - require $path."genHosts.php"; - require $path."genHostGroups.php"; - require $path."genServiceTemplates.php"; - require $path."genServices.php"; - require $path."genServiceGroups.php"; - require $path."genEscalations.php"; - require $path."genDependencies.php"; - require $path."centreon_pm.php"; - - if ($tab['localhost']) { - $flag_localhost = $tab['localhost']; - - /* - * Meta Services Generation - */ - if ($files = glob($path . "metaService/*.php")) { - foreach ($files as $filename) { - include $filename; - } - } - } + } - /* - * Module Generation - */ - foreach ($centreon->modules as $key => $value) { - $addModule = true; - if (function_exists('zend_loader_enabled') && (zend_loader_file_encoded() == true)) { - $module_license_validity = zend_loader_install_license ($centreon_path . "www/modules/".$key."license/merethis_lic.zl", true); - if ($module_license_validity == false) - $addModule = false; - } + # Sync contactgroups to ldap + $cgObj = new CentreonContactgroup($pearDB); + $cgObj->syncWithLdap(); - if ($addModule) { - if ($value["gen"] && $files = glob($centreon_path . "www/modules/".$key."/generate_files/*.php")) { - foreach ($files as $filename) - include $filename; - } - } - } - unset($generatedHG); - unset($generatedSG); - unset($generatedS); - } - } - require_once $path."genIndexData.php"; - - /* - * Generate correlation file - */ - $brokerObj = new CentreonConfigCentreonBroker($pearDB); - $correlationPath = $brokerObj->getCorrelationFile(); - $localId = getLocalhostId(); - if (false !== $correlationPath && false !== $localId) { - $tmpFilename = $centreonBrokerPath . '/' . $localId . '/'; - $query = "SELECT id FROM nagios_server"; - $res = $pearDB->query($query); - $pollers = array(); - while ($row = $res->fetchRow()) { - generateCentreonBrokerCorrelation($brokerObj, $tmpFilename, $row['id'], $pearDB); - $pollers[] = $row['id']; - } - generateCentreonBrokerCorrelationMain($tmpFilename, $correlationPath, $pollers); - } + # Generate configuration + if ($poller == '0') { + $config_generate->configPollers(); + } else { + $config_generate->configPollerFromId($poller); } + # Debug configuration $statusMsg = $okMsg; $statusCode = 0; if ($debug) { @@ -313,13 +121,15 @@ function log_error($errno, $errstr, $errfile, $errline) if ($statusCode == 1) { $statusMsg = $nokMsg; } + $xml->writeElement("status", $statusMsg); $xml->writeElement("statuscode", $statusCode); -} catch (Exception $e) { +} catch (Exception $e) { $xml->writeElement("status", $nokMsg); $xml->writeElement("statuscode", 1); $xml->writeElement("error", $e->getMessage()); } + /* Restore default error handler */ restore_error_handler(); @@ -342,3 +152,109 @@ function log_error($errno, $errstr, $errfile, $errline) header('Cache-Control: no-cache, must-revalidate'); $xml->output(); + + +/** + * The error handler for get error from PHP + * + * @see set_error_handler + */ +function log_error($errno, $errstr, $errfile, $errline) +{ + global $generatePhpErrors; + if (!(error_reporting() & $errno)) { + return; + } + + switch ($errno) { + case E_ERROR: + case E_USER_ERROR: + case E_CORE_ERROR: + $generatePhpErrors[] = array('error', $errstr); + break; + case E_WARNING: + case E_USER_WARNING: + case E_CORE_WARNING: + $generatePhpErrors[] = array('warning', $errstr); + break; + } + return true; +} + +function printDebug($xml, $tabs) +{ + global $pearDB, $ret, $oreon, $nagiosCFGPath; + + $DBRESULT_Servers = $pearDB->query("SELECT `nagios_bin` FROM `nagios_server` WHERE `localhost` = '1' ORDER BY ns_activate DESC LIMIT 1"); + $nagios_bin = $DBRESULT_Servers->fetchRow(); + $DBRESULT_Servers->free(); + $msg_debug = array(); + + $tab_server = array(); + foreach ($tabs as $tab) { + if (isset($ret["host"]) && ($ret["host"] == 0 || $ret["host"] == $tab['id'])) { + $tab_server[$tab["id"]] = array("id" => $tab["id"], "name" => $tab["name"], "localhost" => $tab["localhost"]); + } + } + + foreach ($tab_server as $host) { + $stdout = shell_exec($nagios_bin["nagios_bin"] . " -v " . $nagiosCFGPath . $host["id"]."/nagiosCFG.DEBUG 2>&1"); + $stdout = htmlspecialchars($stdout, ENT_QUOTES, "UTF-8"); + $msg_debug[$host['id']] = str_replace ("\n", "
", $stdout); + $msg_debug[$host['id']] = str_replace ("Warning:", "Warning", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("warning: ", "Warning ", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("Error:", "Error", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("error:", "Error", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("reading", "Reading", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("running", "Running", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("Total Warnings: 0", "Total Warnings: 0", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("Total Errors: 0", "Total Errors: 0", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = str_replace ("
License:", " - License:", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = preg_replace('/\[[0-9]+?\] /', '', $msg_debug[$host['id']]); + + $lines = preg_split("/\/", $msg_debug[$host['id']]); + $msg_debug[$host['id']] = ""; + $i = 0; + foreach ($lines as $line) { + if (strncmp($line, "Processing object config file", strlen("Processing object config file")) + && strncmp($line, "Website: http://www.nagios.org", strlen("Website: http://www.nagios.org"))) + $msg_debug[$host['id']] .= $line . "
"; + $i++; + } + } + + $xml->startElement("debug"); + $str = ""; + $returnCode = 0; + foreach ($msg_debug as $pollerId => $message) { + $show = "none"; + $toggler = ""; + $pollerNameColor = "green"; + if (preg_match_all("/Total (Errors|Warnings)\:[ ]+([0-9]+)/", $message, $globalMatches, PREG_SET_ORDER)) { + foreach ($globalMatches as $matches) { + if ($matches[2] != "0") { + $show = "block"; + $toggler = ""; + if ($matches[1] == "Errors") { + $pollerNameColor = "red"; + $returnCode = 1; + } elseif($matches[1] == "Warnings") { + $pollerNameColor = "orange"; + } + } + } + } else { + $show = "block"; + $pollerNameColor = "red"; + $toggler = ""; + $returnCode = 1; + } + $str .= ""; + $str .= $toggler . " "; + $str .= "".$tab_server[$pollerId]['name'] . "
"; + $str .= "
".htmlentities($message) . "

"; + } + $xml->text($str); + $xml->endElement(); + return $returnCode; +} diff --git a/www/include/configuration/configGenerate/xml/moveFiles.php b/www/include/configuration/configGenerate/xml/moveFiles.php index 8bc976c59ec..461d708af52 100644 --- a/www/include/configuration/configGenerate/xml/moveFiles.php +++ b/www/include/configuration/configGenerate/xml/moveFiles.php @@ -106,7 +106,7 @@ function log_error($errno, $errstr, $errfile, $errline) /* Set new error handler */ set_error_handler('log_error'); - $centcore_pipe = "@CENTREON_VARLIB@/centcore.cmd"; + $centcore_pipe = "/var/lib/centreon/centcore.cmd"; if ($centcore_pipe == "/centcore.cmd") { $centcore_pipe = "/var/lib/centreon/centcore.cmd"; } @@ -129,33 +129,18 @@ function log_error($errno, $errstr, $errfile, $errline) } } - /* - * Copy correlation file - */ - $brokerObj = new CentreonConfigCentreonBroker($pearDB); - $correlationPath = $brokerObj->getCorrelationFile(); - $localId = getLocalhostId(); - if (false !== $correlationPath && false !== $localId) { - $tmpFilename = $centreonBrokerPath . '/' . $localId . '/correlation_*.xml'; - /* Purge file */ - $listRemovesFiles = glob(dirname($correlationPath) . '/correlation_*.xml'); - foreach ($listRemovesFiles as $file) { - @unlink($file); - } - /* Copy file */ - $listFiles = glob($tmpFilename); - $listFiles[] = $correlationPath; - foreach ($listFiles as $file) { - @copy($file, dirname($correlationPath)); - } - } - - $tab_server = array(); $tabs = $oreon->user->access->getPollerAclConf(array('fields' => array('name', 'id', 'localhost'), 'order' => array('name'), 'conditions' => array('ns_activate' => '1'), 'keys' => array('id'))); + + + # Get correlation infos + $brokerObj = new CentreonConfigCentreonBroker($pearDB); + $correlationPath = $brokerObj->getCorrelationFile(); + $localId = getLocalhostId(); + foreach ($tabs as $tab) { if (isset($ret["host"]) && ($ret["host"] == 0 || $ret["host"] == $tab['id'])) { $tab_server[$tab["id"]] = array("id" => $tab["id"], "name" => $tab["name"], "localhost" => $tab["localhost"]); @@ -163,6 +148,19 @@ function log_error($errno, $errstr, $errfile, $errline) } foreach ($tab_server as $host) { + # Manage correlation files + if (false !== $correlationPath && false !== $localId) { + $tmpFilename = $centreonBrokerPath . '/' . $host['id'] . '/correlation_' . $host['id'] . '.xml'; + $filenameToGenerate = dirname($correlationPath) . '/correlation_' . $host['id'] . '.xml'; + # Purge file + if (file_exists($filenameToGenerate)) { + @unlink($filenameToGenerate); + } + # Copy file + if (file_exists($tmpFilename)) { + @copy($tmpFilename, $filenameToGenerate); + } + } if (isset($poller) && ($poller == 0 || $poller == $host['id'])) { if (isset($host['localhost']) && $host['localhost'] == 1) { /*