Example #1
0
 /**
  * @param CentreonEngine\Events\GetMacroHost $event
  */
 public static function execute(MacroHostEvent $event)
 {
     /* Macros for Host */
     $hosts = array_keys(HostRepository::getHostsByPollerId($event->getPollerId()));
     foreach ($hosts as $hostId) {
         $arrCustomMacro = CustomMacroRepository::loadHostCustomMacro($hostId);
         foreach ($arrCustomMacro as $customMacro) {
             $event->setMacro($hostId, $customMacro['macro_name'], $customMacro['macro_value']);
         }
     }
 }
 /**
  * Update a host
  *
  *
  * @method post
  * @route /hosttemplate/update
  */
 public function updateAction()
 {
     $givenParameters = $this->getParams('post');
     $macroList = array();
     $aTagList = array();
     $aTags = array();
     $aTagsTemplate = array();
     $aTagsInTpl = array();
     $aTagsIdTpl = array();
     if (isset($givenParameters['macro_name']) && isset($givenParameters['macro_value'])) {
         $macroName = $givenParameters['macro_name'];
         $macroValue = $givenParameters['macro_value'];
         $macroHidden = $givenParameters['macro_hidden'];
         foreach ($macroName as $key => $name) {
             if (!empty($name)) {
                 if (isset($macroHidden[$key])) {
                     $isPassword = '******';
                 } else {
                     $isPassword = '******';
                 }
                 $macroList[$name] = array('value' => $macroValue[$key], 'ispassword' => $isPassword);
             }
         }
     }
     if ($givenParameters['host_create_services_from_template']) {
         \CentreonConfiguration\Models\Host::deployServices($givenParameters['object_id']);
     }
     if (count($macroList) > 0) {
         try {
             CustomMacroRepository::saveHostCustomMacro(self::$objectName, $givenParameters['object_id'], $macroList);
         } catch (\Exception $ex) {
             $errorMessage = $ex->getMessage();
             $this->router->response()->json(array('success' => false, 'error' => $errorMessage));
         }
     }
     //Delete tags directilly rattached to the object
     TagsRepository::deleteTagsForResource(self::$objectName, $givenParameters['object_id'], 0);
     //Insert tags affected to the HOST
     if (isset($givenParameters['host_tags'])) {
         $aTagList = explode(",", $givenParameters['host_tags']);
         foreach ($aTagList as $var) {
             $var = trim($var);
             if (!empty($var)) {
                 array_push($aTags, $var);
             }
         }
         if (count($aTags) > 0) {
             TagsRepository::saveTagsForResource(self::$objectName, $givenParameters['object_id'], $aTags, '', false, 1);
         }
     }
     parent::updateAction();
 }
Example #3
0
 /**
  * Add a service
  *
  * @method post
  * @route /service/add
  */
 public function createAction()
 {
     $macroList = array();
     $aTagList = array();
     $aTags = array();
     $givenParameters = $this->getParams('post');
     if (isset($givenParameters['macro_name']) && isset($givenParameters['macro_value'])) {
         $macroName = $givenParameters['macro_name'];
         $macroValue = $givenParameters['macro_value'];
         $macroHidden = $givenParameters['macro_hidden'];
         foreach ($macroName as $key => $name) {
             if (!empty($name)) {
                 if (isset($macroHidden[$key])) {
                     $isPassword = '******';
                 } else {
                     $isPassword = '******';
                 }
                 $macroList[$name] = array('value' => $macroValue[$key], 'ispassword' => $isPassword);
             }
         }
     }
     $id = parent::createAction(false);
     if (count($macroList) > 0) {
         try {
             CustomMacroRepository::saveServiceCustomMacro(self::$objectName, $id, $macroList);
         } catch (\Exception $ex) {
             $errorMessage = $ex->getMessage();
             $this->router->response()->json(array('success' => false, 'error' => $errorMessage));
         }
     }
     if (isset($givenParameters['service_tags'])) {
         $aTagList = explode(",", $givenParameters['service_tags']);
         foreach ($aTagList as $var) {
             $var = trim($var);
             if (!empty($var)) {
                 array_push($aTags, $var);
             }
         }
         if (count($aTags) > 0) {
             TagsRepository::saveTagsForResource(self::$objectName, $id, $aTags, '', false, 1);
         }
     }
     $this->router->response()->json(array('success' => true));
 }
 /**
  * 
  * @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);
 }
Example #5
0
 /**
  * 
  * @param string $object
  * @param string $macro
  */
 public function removeMacroAction($object, $macro)
 {
     //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);
     }
     switch ($this->objectName) {
         case 'hosttemplate':
         case 'host':
             CustomMacroRepository::setAttributesMap(self::$attributesMapHost);
             CustomMacroRepository::transco($macro);
             CustomMacroRepository::deleteHostCustomMacro($objectId, $macro);
             break;
         case 'servicetemplate':
         case 'service':
             CustomMacroRepository::setAttributesMap(self::$attributesMapService);
             CustomMacroRepository::transco($macro);
             CustomMacroRepository::deleteServiceCustomMacro($objectId, $macro);
             break;
         default:
             break;
     }
     InputOutput::display("The macro '" . $macro . "' has been successfully removed from the object", true, 'green');
     /* } catch (\Exception $ex) {
            InputOutput::display($ex->getMessage(), true, 'red');
        }*/
 }
 /**
  * 
  * @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;
 }
 /**
  * 
  * @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);
 }