/** * @param CentreonEngine\Events\GetMacroService $event */ public static function execute(MacroServiceEvent $event) { /* Macros domain for service*/ $services = array_keys(ServiceRepository::getServicesByPollerId($event->getPollerId())); foreach ($services as $serviceId) { $arr = ServiceRepository::getDomain($serviceId); foreach ($arr as $domainName) { $event->setMacro($serviceId, self::MACRO_DOMAIN, $domainName); } } /* Macros domain for service template */ $servicesTmpl = ServicetemplateRepository::getList('service_id'); foreach ($servicesTmpl as $serviceTmpl) { $arr = ServicetemplateRepository::getDomain($serviceTmpl['service_id']); foreach ($arr as $domainName) { $event->setMacro($serviceTmpl['service_id'], self::MACRO_DOMAIN, $domainName); } } }
/** * Get list of services for a specific host * * @method get * @route /host/[i:id]/service */ public function hostForServiceAction() { $requestParam = $this->getParams('named'); $relationClass = static::$relationMap['host_services']; $services = $relationClass::getMergedParameters(array(), array('service_id', 'service_description'), -1, 0, null, "ASC", array('host_id' => $requestParam['id']), "AND"); $finalServices = array(); foreach ($services as $service) { $finalService = ServicetemplateRepository::getInheritanceValues($service['service_id'], true); $finalService['service_id'] = $service['service_id']; $finalService['service_description'] = $service['service_description']; $finalService = ServiceRepository::formatDataForSlider($finalService); $finalServices[] = $finalService; } $this->router->response()->json(array('service' => $finalServices, 'success' => true)); }
/** * Get the list of metric name by a service template id * * @param int $tmplId The service template id * @return array */ public static function getMetricsNameByServiceTemplate($tmplId) { $services = ServicetemplateRepository::getServices($tmplId); if (count($services) == 0) { return array(); } /* Check if all data are integer */ array_map(function ($value) { if (false === is_numeric($value)) { throw new \Exception('The value is not numeric'); } }, $services); $dbconn = \Centreon\Internal\Di::getDefault()->get('db_centreon'); $query = "SELECT m.metric_name\n FROM rt_metrics m, rt_index_data i\n WHERE m.index_id = i.index_id\n AND i.service_id IN (" . join(', ', $services) . ")"; $stmt = $dbconn->query($query); $metrics = array(); while ($row = $stmt->fetch()) { $metrics[] = $row['metric_name']; } return array_unique($metrics); }
/** * * @param array $resultSet */ protected function formatDatas(&$resultSet) { $router = Di::getDefault()->get('router'); foreach ($resultSet as &$myServiceSet) { $myServiceSet['service_description'] = '<span class="icoListing">' . ServiceRepository::getIconImage($myServiceSet['service_id']) . '</span>' . $myServiceSet['service_description']; // Set Tpl Chain $tplStr = null; if (isset($myServiceSet["service_template_model_stm_id"])) { $tplArr = ServicetemplateRepository::getMyServiceTemplateModels($myServiceSet["service_template_model_stm_id"]); $tplRoute = str_replace("//", "/", Di::getDefault()->get('router')->getPathFor('/centreon-configuration/servicetemplate/[i:id]', array('id' => $tplArr['id']))); if (isset($tplArr['description'])) { $tplStr .= '<span><a href="' . $tplRoute . '">' . $tplArr['description'] . '</a></span>'; } } $sideMenuCustom = new SlideMenu($myServiceSet['service_id']); $events = Di::getDefault()->get('events'); $events->emit('centreon-configuration.slide.menu.service.template', array($sideMenuCustom)); //$myHostSet['DT_RowData']['right_side_details'] = $router->getPathFor('/centreon-configuration/host/snapshot/').$myHostSet['host_id']; $myServiceSet['DT_RowData']['right_side_menu_list'] = $sideMenuCustom->getMenu(); $myServiceSet['DT_RowData']['right_side_default_menu'] = $sideMenuCustom->getDefaultMenu(); $myServiceSet['service_template_model_stm_id'] = $tplStr; /* Display human readable the check/retry interval */ $myServiceSet['service_normal_check_interval'] = HumanReadable::convert($myServiceSet['service_normal_check_interval'], 's', $units, null, true); $myServiceSet['service_retry_check_interval'] = HumanReadable::convert($myServiceSet['service_retry_check_interval'], 's', $units, null, true); /* Get personal tags */ $myServiceSet['tagname'] = ''; $aTagUsed = array(); $aTags = TagsRepository::getList('service', $myServiceSet['service_id'], 0, 0); foreach ($aTags as $oTags) { if (!in_array($oTags['id'], $aTagUsed)) { $aTagUsed[] = $oTags['id']; $myServiceSet['tagname'] .= TagsRepository::getTag('service', $myServiceSet['service_id'], $oTags['id'], $oTags['text'], $oTags['user_id'], 1); } } $myServiceSet['tagname'] .= TagsRepository::getAddTag('service', $myServiceSet['service_id']); } }
/** * * @param array $filesList * @param int $poller_id * @param string $path * @param string $filename * @param CentreonEngine\Events\GetMacroService $serviceMacroEvent */ public static function generate(&$filesList, $poller_id, $path, $filename, $serviceMacroEvent) { $di = Di::getDefault(); /* Get Database Connexion */ $dbconn = $di->get('db_centreon'); /* Field to not display */ $disableField = static::getTripleChoice(); $field = "service_id, service_description, service_alias, service_template_model_stm_id, " . "command_command_id_arg, command_command_id AS check_command, timeperiod_tp_id AS check_period, " . "command_command_id_arg2, command_command_id2 AS event_handler, service_is_volatile, " . "service_max_check_attempts, service_normal_check_interval, service_retry_check_interval, " . "service_active_checks_enabled, service_passive_checks_enabled, initial_state, " . "service_obsess_over_service, service_check_freshness, " . "service_freshness_threshold, service_event_handler_enabled, service_low_flap_threshold, " . "service_high_flap_threshold, service_flap_detection_enabled, service_check_timeout "; /* Init Content Array */ $content = array(); /* Get information into the database. */ $query = "SELECT {$field} " . "FROM cfg_services " . "WHERE service_activate = '1' " . "AND service_register = '0' " . "ORDER BY service_description"; $stmt = $dbconn->prepare($query); $stmt->execute(); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $tmp = array("type" => "service"); $tmpData = array(); $args = ""; foreach ($row as $key => $value) { if ($key == "service_id") { $service_id = $row["service_id"]; } elseif (isset($disableField[$key]) && $value != 2 && $value != "") { $key = str_replace("service_", "", $key); $tmpData[$key] = $value; } elseif (!isset($disableField[$key]) && $value != "") { $writeParam = 1; $key = str_replace("service_", "", $key); if ($key == 'description') { $key = "name"; } else { if ($key == 'alias') { $key = "service_description"; } else { if ($key == 'normal_check_interval') { $key = "check_interval"; } else { if ($key == 'retry_check_interval') { $key = "retry_interval"; } else { if ($key == 'command_command_id_arg' || $key == 'command_command_id_arg2') { $args = $value; $writeParam = 0; } else { if ($key == 'check_command' || $key == 'event_handler') { $value = CommandConfigurationRepository::getCommandName($value) . html_entity_decode($args); $args = ""; } else { if ($key == 'check_period') { $value = TimePeriodConfigurationRepository::getPeriodName($value); } else { if ($key == "template_model_stm_id") { $key = "use"; $value = ServicetemplateConfigurationRepository::getTemplateName($value); } } } } } } } } if ($writeParam == 1) { $tmpData[$key] = $value; } } } /* Generate macro */ $macros = CustomMacroRepository::loadServiceCustomMacro($service_id); if (is_array($macros) && count($macros)) { foreach ($macros as $macro) { if (preg_match('/^\\$_SERVICE(.+)\\$$/', $macro['macro_name'], $m)) { $name = "_{$m[1]}"; $tmpData[$name] = $macro['macro_value']; } } } /* Macros that can be generated from other modules */ $extraMacros = $serviceMacroEvent->getMacro($service_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, $user = "******"); unset($content); }
/** * * @param int $host_id * @param CentreonEngine\Events\GetMacroService $serviceMacroEvent * @return int */ public static function generate($host_id, $serviceMacroEvent) { $di = Di::getDefault(); /* Get Database Connexion */ $dbconn = $di->get('db_centreon'); /* Field to not display */ $disableField = static::getTripleChoice(); $field = "host_id, h.host_name, service_id, " . "service_description, service_alias, service_template_model_stm_id, command_command_id_arg, " . "s.command_command_id AS check_command, s.timeperiod_tp_id AS check_period, " . "s.command_command_id_arg2, s.command_command_id2 AS event_handler, " . "service_is_volatile, service_max_check_attempts, service_normal_check_interval, " . "service_retry_check_interval, service_active_checks_enabled, " . "s.initial_state, service_obsess_over_service, service_check_freshness, " . "service_freshness_threshold, service_event_handler_enabled, service_low_flap_threshold, " . "service_high_flap_threshold, service_flap_detection_enabled, service_check_timeout "; /* Init Content Array */ $content = array(); /* Get information into the database. */ $query = "SELECT {$field} " . "FROM cfg_hosts h, cfg_services s, cfg_hosts_services_relations r " . "WHERE h.host_id = {$host_id} " . "AND h.host_id = r.host_host_id " . "AND s.service_id = r.service_service_id " . "AND service_activate = '1' " . "AND (service_register = '1' " . "OR service_register = '2') " . "ORDER BY host_name, service_description"; $stmt = $dbconn->prepare($query); $stmt->execute(); $serviceList = $stmt->fetchAll(\PDO::FETCH_ASSOC); $event = $di->get('events'); $addServiceEvent = new AddServiceEvent($host_id, $serviceList); $event->emit('centreon-engine.add.service', array($addServiceEvent)); $serviceList = $addServiceEvent->getServiceList(); foreach ($serviceList as $service) { $tmp = array("type" => "service"); $tmpData = array(); $args = ""; foreach ($service as $key => $value) { if ($key == "service_id" || $key == "host_id") { $host_id = $service["host_id"]; $service_id = $service["service_id"]; /* Add service_id macro for broker - This is mandatory*/ $tmpData["_SERVICE_ID"] = $service_id; } elseif (isset($disableField[$key]) && $value != 2 && $value != "") { $key = str_replace("service_", "", $key); $tmpData[$key] = $value; } elseif (!isset($disableField[$key]) && $value != "") { if ($key != 'service_description') { $key = str_replace("service_", "", $key); } if ($key == 'normal_check_interval') { $key = "check_interval"; } else { if ($key == 'retry_check_interval') { $key = "retry_interval"; } else { if ($key == 'command_command_id_arg' || $key == 'command_command_id_arg2') { $args = $value; $writeParam = 0; } else { if ($key == 'check_command' || $key == 'event_handler') { if (is_numeric($value)) { $value = CommandConfigurationRepository::getCommandName($value) . html_entity_decode($args); } $args = ""; } else { if ($key == 'check_period') { $value = TimePeriodConfigurationRepository::getPeriodName($value); } else { if ($key == "template_model_stm_id") { $key = "use"; $value = ServicetemplateConfigurationRepository::getTemplateName($value); } } } } } } $tmpData[$key] = $value; } } /* Generate macro */ $macros = CustomMacroRepository::loadServiceCustomMacro($service_id); if (is_array($macros) && count($macros)) { foreach ($macros as $macro) { if (preg_match('/^\\$_SERVICE(.+)\\$$/', $macro['macro_name'], $m)) { $name = "_{$m[1]}"; $tmpData[$name] = $macro['macro_value']; } } } /* Macros that can be generated from other modules */ $extraMacros = $serviceMacroEvent->getMacro($service_id); foreach ($extraMacros as $macroName => $macroValue) { $macroName = "_{$macroName}"; $tmpData[$macroName] = $macroValue; } $tmpData["register"] = 1; $tmp["content"] = $tmpData; $content[] = $tmp; } return $content; }
/** * 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)); } }