/**
  * 
  * @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);
 }
Exemple #2
0
 /**
  * 
  * @param string $object
  */
 public function listMacroAction($object = null)
 {
     //try {
     $repository = $this->repository;
     //$objectId = $repository::getIdFromUnicity($this->parseObjectParams($object));
     $sName = $this->objectName;
     $repository::transco($object);
     $aId = $repository::getListBySlugName($object[$sName]);
     if (count($aId) > 0) {
         $objectId = $aId[0]['id'];
     } else {
         throw new \Exception(static::OBJ_NOT_EXIST, 1);
     }
     $macros = array();
     switch ($this->objectName) {
         case 'hosttemplate':
         case 'host':
             $macros = CustomMacroRepository::loadHostCustomMacro($objectId);
             break;
         case 'servicetemplate':
         case 'service':
             $macros = CustomMacroRepository::loadServiceCustomMacro($objectId);
             break;
         default:
             break;
     }
     //$this->tableDisplay(array('macro_name'=>'name','macro_value'=>'value','macro_hidden'=>'hidden'),$macros);
     if (!empty($macros)) {
         echo "macro_name;macro_value;macro_hidden";
     } else {
         //InputOutput::display('No results', true, 'red');
         throw new \Exception('No results', 1);
     }
     foreach ($macros as $macro) {
         echo "\n" . $macro['macro_name'] . ";" . $macro['macro_value'] . ";" . $macro['macro_hidden'];
     }
     /*} catch (\Exception $ex) {
           InputOutput::display($ex->getMessage(), true, 'red');
       }*/
 }
 /**
  * 
  * @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;
 }