/** * Display the configuration snapshot of a host * with template inheritance * * @method get * @route /hosttemplate/snapshotslide/[i:id] */ public function snapshotslideAction() { $params = $this->getParams(); $data = HostTemplateRepository::getInheritanceValues($params['id'], true); $host = HostTemplate::get($params['id'], 'host_name'); $data['host_name'] = $host['host_name']; $data['icon'] = HostRepository::getIconImage($data['host_name']); $hostConfiguration = HostRepository::formatDataForSlider($data); /* $aDataRealTime = HostRealtime::get($params['id']); $hostReal = HostRepository::formatDataForSlider($aDataRealTime); $servicesStatus = ServiceRealTimeRepository::countAllStatusForHost($params['id']); */ $edit_url = $this->router->getPathFor("/centreon-configuration/hosttemplate/" . $params['id']); $this->router->response()->json(array('hostConfig' => $hostConfiguration, 'edit_url' => $edit_url, 'success' => true)); }
/** * Get inheritance value * * @method get * @route /host/[i:id]/inheritance */ public function getInheritanceAction() { $router = Di::getDefault()->get('router'); $requestParam = $this->getParams('named'); $inheritanceValues = HostRepository::getInheritanceValues($requestParam['id']); array_walk($inheritanceValues, function (&$item, $key) { if (false === is_null($item)) { $item = HostTemplateRepository::getTextValue($key, $item); } }); $router->response()->json(array('success' => true, 'values' => $inheritanceValues)); }
/** * * @param array $filesList * @param int $poller_id * @param string $path * @param string $filename * @param CentreonEngine\Events\GetMacroHost $hostMacroEvent */ public static function generate(&$filesList, $poller_id, $path, $filename, $hostMacroEvent) { $di = Di::getDefault(); /* Get Database Connexion */ $dbconn = $di->get('db_centreon'); /* Get disfield */ $disableFields = static::getTripleChoice(); /* Init Content Array */ $content = array(); /* Get information into the database. */ $fields = static::getFields(); $query = "SELECT {$fields}\n FROM cfg_hosts \n WHERE host_activate = '1' \n AND host_register = ? \n ORDER BY host_name"; $stmt = $dbconn->prepare($query); $stmt->execute(array(static::$register)); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $tmp = array("type" => "host"); $tmpData = array(); $args = ""; $host_id = null; foreach ($row as $key => $value) { if ($key == "host_id") { $host_id = $row["host_id"]; } elseif ($key == "host_snmp_community" && $value != "") { $tmpData["_SNMPCOMMUNITY"] = $value; } elseif ($key == "host_snmp_version" && $value != "") { $tmpData["_SNMPVERSION"] = $value; } elseif (isset($disableField[$key]) && $value != 2 && $value != "") { $key = str_replace("host_", "", $key); $tmpData[$key] = $value; } elseif (!isset($disableField[$key]) && $value != "") { $key = str_replace("host_", "", $key); if ($key == 'command_command_id_arg1' || $key == 'command_command_id_arg2') { $args = $value; } else { if ($key == 'check_command' || $key == 'event_handler') { $value = CommandConfigurationRepository::getCommandName($value) . $args; $args = ""; } else { if ($key == 'check_period') { $value = TimePeriodConfigurationRepository::getPeriodName($value); } } } $tmpData[$key] = $value; } } if (!is_null($host_id)) { $templates = HostTemplateConfigurationRepository::getTemplates($host_id); if ($templates != "") { $tmpData['use'] = $templates; } } /* Generate macro */ $macros = CustomMacroRepository::loadHostCustomMacro($host_id); if (is_array($macros) && count($macros)) { foreach ($macros as $macro) { if (preg_match('/^\\$_HOST(.+)\\$$/', $macro['macro_name'], $m)) { $name = "_{$m[1]}"; $tmpData[$name] = $macro['macro_value']; } } } /* Macros that can be generated from other modules */ $extraMacros = $hostMacroEvent->getMacro($host_id); foreach ($extraMacros as $macroName => $macroValue) { $macroName = "_{$macroName}"; $tmpData[$macroName] = $macroValue; } $tmpData['register'] = 0; $tmp["content"] = $tmpData; $content[] = $tmp; } /* Write Check-Command configuration file */ WriteConfigFile::writeObjectFile($content, $path . $poller_id . "/objects.d/" . $filename, $filesList, "API"); unset($content); }
/** * Get template chain ordinated in tree (id, text) * * @param int $hostId The host or host template Id * @param array $alreadyProcessed The host templates already processed * @param int $depth The depth to search * @return array */ public static function getTemplateChainTree($hostId, $depth = -1) { $templates = array(); $db = Di::getDefault()->get('db_centreon'); $router = Di::getDefault()->get('router'); $sql = "SELECT h.* " . " FROM cfg_hosts h, cfg_hosts_templates_relations htr" . " WHERE h.host_id=htr.host_tpl_id" . " AND htr.host_host_id=:host_id" . " AND host_activate = '1'" . " AND host_register = '0'" . " ORDER BY `order` ASC"; $stmt = $db->prepare($sql); $stmt->bindParam(':host_id', $hostId, \PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchAll(); foreach ($row as $template) { $templatesTmp = self::formatDataForSlider($template); $templatesTmp['url_edit'] = $router->getPathFor('/centreon-configuration/hosttemplate/' . $template['host_id']); $templatesTmp['icon'] = self::getIconImagePath($template['host_id']); $templatesTmp['servicesTemplate'] = HostTemplateRepository::getRelationsCustom("\\CentreonConfiguration\\Models\\Relation\\Hosttemplate\\Servicetemplate", $template['host_id']); $templatesTmp['templates'] = self::getTemplateChainTree($template['host_id'], $depth); $templates[] = $templatesTmp; } return $templates; }
/** * Get inheritance value from a list of template * * @method post * @route /servicetemplate/inheritance */ public function getInheritanceTmplAction() { $router = Di::getDefault()->get('router'); $params = $this->getParams('post'); $tmpl = $params['tmpl']; if ($tmpl == "") { $router->response()->json(array('success' => true, 'values' => array())); } else { $values = ServicetemplateRepository::getInheritanceValues($tmpl, true); array_walk($values, function (&$item, $key) { if (false === is_null($item)) { $item = HostTemplateRepository::getTextValue($key, $item); } }); $router->response()->json(array('success' => true, 'values' => $values)); } }
/** * * @param array $filesList * @param int $poller_id * @param string $path * @param string $filename * @param CentreonEngine\Events\GetMacroHost $hostMacroEvent * @param CentreonEngine\Events\GetMacroService $serviceMacroEvent */ public static function generate(&$filesList, $poller_id, $path, $filename, $hostMacroEvent, $serviceMacroEvent) { $di = Di::getDefault(); /* Get Database Connexion */ $dbconn = $di->get('db_centreon'); /* Get disabled fields */ $disableField = static::getTripleChoice(); /* Init Content Array */ $content = array(); /* Get information into the database. */ $fields = static::getFields(); $query = "SELECT {$fields} \n FROM cfg_hosts \n WHERE host_activate = '1' \n AND (host_register = '1' \n OR host_register = '2') \n AND poller_id = ?\n ORDER BY host_name"; $stmt = $dbconn->prepare($query); $stmt->execute(array($poller_id)); $hostList = $stmt->fetchAll(\PDO::FETCH_ASSOC); $event = $di->get('events'); $addHostEvent = new AddHostEvent($poller_id, $hostList); $event->emit('centreon-engine.add.host', array($addHostEvent)); $hostList = $addHostEvent->getHostList(); foreach ($hostList as $host) { $content = array(); $tmp = array("type" => "host"); $tmpData = array(); $args = ""; $host_id = null; /* Write Host Properties */ foreach ($host as $key => $value) { if ($key == "host_id") { $host_id = $host["host_id"]; /* Add host_id macro for broker - This is mandatory*/ $tmpData["_HOST_ID"] = $host_id; $host_name = ""; } elseif ($key == "host_snmp_community" && $value != "") { $tmpData["_SNMPCOMMUNITY"] = $value; } elseif ($key == "host_snmp_version" && $value != "") { $tmpData["_SNMPVERSION"] = $value; } elseif (isset($disableField[$key]) && $value != 2 && $value != "") { $key = str_replace("host_", "", $key); $tmpData[$key] = $value; } elseif (!isset($disableField[$key]) && $value != "") { if ($key != 'host_name') { $key = str_replace("host_", "", $key); } else { $host_name = $value; } if ($key == 'command_command_id_arg1' || $key == 'command_command_id_arg2') { $args = $value; } else { if ($key == 'check_command' || $key == 'event_handler') { $value = CommandConfigurationRepository::getCommandName($value) . $args; $args = ""; } else { if ($key == 'check_period') { $value = TimeperiodConfigurationRepository::getPeriodName($value); } else { if ($key == 'timezone_id') { $key = 'timezone'; if ($value != 'NULL') { $tName = \CentreonAdministration\Models\Timezone::getParameters($value, array('name')); $value = ':' . $tName['name']; } } } } } $tmpData[$key] = $value; } } if (!is_null($host_id)) { $templates = HostTemplateConfigurationRepository::getTemplates($host_id); if ($templates != "") { $tmpData['use'] = $templates; } } /* Generate macro */ $macros = CustomMacroRepository::loadHostCustomMacro($host_id); if (is_array($macros) && count($macros)) { foreach ($macros as $macro) { if (preg_match('/^\\$_HOST(.+)\\$$/', $macro['macro_name'], $m)) { $name = "_{$m[1]}"; $tmpData[$name] = $macro['macro_value']; } } } /* Macros that can be generated from other modules */ $extraMacros = $hostMacroEvent->getMacro($host_id); foreach ($extraMacros as $macroName => $macroValue) { $macroName = "_{$macroName}"; $tmpData[$macroName] = $macroValue; } $tmpData['register'] = 1; $tmp["content"] = $tmpData; $content[] = $tmp; /* Write Service Properties */ $services = ServiceRepository::generate($host_id, $serviceMacroEvent); foreach ($services as $contentService) { $content[] = $contentService; } WriteConfigFile::writeObjectFile($content, $path . $poller_id . "/objects.d/" . $filename . $host_name . "-" . $host_id . ".cfg", $filesList, "API"); } unset($content); }