function generateServicesCfg($file=0, $tag='') { global $DB; PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "Starting generateServicesCfg services ($tag) ...\n" ); $pMonitoringCommand = new PluginMonitoringCommand(); $pmEventhandler = new PluginMonitoringEventhandler(); $pMonitoringCheck = new PluginMonitoringCheck(); $pmComponent = new PluginMonitoringComponent(); $pmEntity = new PluginMonitoringEntity(); $pmContact_Item = new PluginMonitoringContact_Item(); $networkPort = new NetworkPort(); $pmService = new PluginMonitoringService(); $pmComponentscatalog = new PluginMonitoringComponentscatalog(); $pmHostconfig = new PluginMonitoringHostconfig(); $calendar = new Calendar(); $user = new User(); $profile_User = new Profile_User(); $a_services = array(); $i=0; // TODO: only contacts in allowed entities ... // Prepare individual contacts $a_contacts_entities = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog' AND `users_id`>0"); foreach ($a_list_contact as $data) { $contactentities = getSonsOf('glpi_entities', $data['entities_id']); if (isset($a_contacts_entities[$data['items_id']][$data['users_id']])) { $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$data['users_id']]); } $a_contacts_entities[$data['items_id']][$data['users_id']] = $contactentities; } // Prepare groups contacts $group = new Group(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog' AND `groups_id`>0"); foreach ($a_list_contact as $data) { $group->getFromDB($data['groups_id']); if ($group->fields['is_recursive'] == 1) { $contactentities = getSonsOf('glpi_entities', $group->fields['entities_id']); } else { $contactentities = array($group->fields['entities_id'] => $group->fields['entities_id']); } $queryg = "SELECT * FROM `glpi_groups_users` WHERE `groups_id`='".$data['groups_id']."'"; $resultg = $DB->query($queryg); while ($datag=$DB->fetch_array($resultg)) { if (isset($a_contacts_entities[$data['items_id']][$datag['users_id']])) { $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$datag['users_id']]); } $a_contacts_entities[$data['items_id']][$datag['users_id']] = $contactentities; } } $a_entities_allowed = $pmEntity->getEntitiesByTag($tag); // Toolbox::logInFile("pm-shinken", " Allowed entities:\n"); $a_entities_list = array(); foreach ($a_entities_allowed as $entity) { $a_entities_list = getSonsOf("glpi_entities", $entity); } $where = ''; if (! isset($a_entities_allowed['-1'])) { $where = getEntitiesRestrictRequest("WHERE", "glpi_plugin_monitoring_services", '', $a_entities_list); } // -------------------------------------------------- // "Normal" services .... $query = "SELECT * FROM `glpi_plugin_monitoring_services` $where"; PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "Services: $query\n" ); $result = $DB->query($query); while ($data=$DB->fetch_array($result)) { // Toolbox::logInFile("pm-shinken", " - fetch service ".$data['id']."\n"); // if (isset($a_entities_allowed['-1']) // OR isset($a_entities_allowed[$item->fields['entities_id']])) { $notadd = 0; $notadddescription = ''; $a_component = current($pmComponent->find("`id`='".$data['plugin_monitoring_components_id']."'", "", 1)); if (empty($a_component)) { continue; } $a_hostname = array(); $a_hostname_type = array(); $a_hostname_id = array(); $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts` WHERE `id` = '".$data['plugin_monitoring_componentscatalogs_hosts_id']."' LIMIT 1"; $resulth = $DB->query($queryh); $hostname = ''; $plugin_monitoring_componentscatalogs_id = 0; while ($datah=$DB->fetch_array($resulth)) { $itemtype = $datah['itemtype']; $item = new $itemtype(); if ($item->getFromDB($datah['items_id'])) { // if (isset($a_entities_allowed['-1']) // OR isset($a_entities_allowed[$item->fields['entities_id']])) { // Fix if hostname is not defined ... if (! empty($item->fields['name'])) { $a_hostname[] = preg_replace("/[^A-Za-z0-9\-_]/","",$item->fields['name']); $a_hostname_type[] = $datah['itemtype']; $a_hostname_id[] = $datah['items_id']; $hostname = $item->fields['name']; $plugin_monitoring_componentscatalogs_id = $datah['plugin_monitoring_componentscalalog_id']; } // } } } if (count($a_hostname) > 0) { if (isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) { $a_services[$i]['use'] = $_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']]; } $a_services[$i]['host_name'] = implode(",", array_unique($a_hostname)); $a_services[$i]['_HOSTITEMSID'] = implode(",", array_unique($a_hostname_id)); $a_services[$i]['_HOSTITEMTYPE'] = implode(",", array_unique($a_hostname_type)); // Define display_name / service_description $a_services[$i]['service_description'] = (! empty($a_component['description'])) ? $a_component['description'] : preg_replace("/[^A-Za-z0-9\-_]/","",$a_component['name']); // In case have multiple networkt port, may have description different, else be dropped by shinken if ($data['networkports_id'] > 0) { $networkPort->getFromDB($data['networkports_id']); $a_services[$i]['service_description'] .= '-'.preg_replace("/[^A-Za-z0-9\-_]/", "", $networkPort->fields['name']); } $a_services[$i]['display_name'] = $a_component['name']; // $a_services[$i]['_ENTITIESID'] = $item->fields['entities_id']; // $a_services[$i]['_ITEMSID'] = $data['id']; // $a_services[$i]['_ITEMTYPE'] = 'Service'; PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', " - add service ".$a_services[$i]['service_description']." on ".$a_services[$i]['host_name']."\n" ); if (isset(self::$shinkenParameters['glpi']['entityId'])) { $a_services[$i][self::$shinkenParameters['glpi']['entityId']] = $item->fields['entities_id']; } if (isset(self::$shinkenParameters['glpi']['itemType'])) { $a_services[$i][self::$shinkenParameters['glpi']['itemType']] = 'Service'; } if (isset(self::$shinkenParameters['glpi']['itemId'])) { $a_services[$i][self::$shinkenParameters['glpi']['itemId']] = $data['id']; } // Manage freshness if ($a_component['freshness_count'] == 0) { $a_services[$i]['check_freshness'] = '0'; $a_services[$i]['freshness_threshold'] = '3600'; } else { $multiple = 1; if ($a_component['freshness_type'] == 'seconds') { $multiple = 1; } else if ($a_component['freshness_type'] == 'minutes') { $multiple = 60; } else if ($a_component['freshness_type'] == 'hours') { $multiple = 3600; } else if ($a_component['freshness_type'] == 'days') { $multiple = 86400; } $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = (string)($a_component['freshness_count'] * $multiple); } $pMonitoringCommand->getFromDB($a_component['plugin_monitoring_commands_id']); // Manage arguments $array = array(); preg_match_all("/\\$(ARG\d+)\\$/", $pMonitoringCommand->fields['command_line'], $array); sort($array[0]); $a_arguments = importArrayFromDB($a_component['arguments']); $a_argumentscustom = importArrayFromDB($data['arguments']); foreach ($a_argumentscustom as $key=>$value) { $a_arguments[$key] = $value; } foreach ($a_arguments as $key=>$value) { $a_arguments[$key] = str_replace('!', '\!', html_entity_decode($value)); } $args = ''; foreach ($array[0] as $arg) { if ($arg != '$PLUGINSDIR$' AND $arg != '$NAGIOSPLUGINSDIR$' AND $arg != '$HOSTADDRESS$' AND $arg != '$MYSQLUSER$' AND $arg != '$MYSQLPASSWORD$') { $arg = str_replace('$', '', $arg); if (!isset($a_arguments[$arg])) { $args .= '!'; } else { if (strstr($a_arguments[$arg], "[[HOSTNAME]]")) { $a_arguments[$arg] = str_replace("[[HOSTNAME]]", $hostname, $a_arguments[$arg]); } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTDESCR]]")){ if (class_exists("PluginFusioninventoryNetworkPort")) { $pfNetworkPort = new PluginFusioninventoryNetworkPort(); $pfNetworkPort->loadNetworkport($data['networkports_id']); $descr = $pfNetworkPort->getValue("ifdescr"); $a_arguments[$arg] = str_replace("[[NETWORKPORTDESCR]]", $descr, $a_arguments[$arg]); } } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNUM]]")){ $networkPort = new NetworkPort(); $networkPort->getFromDB($data['networkports_id']); $logicalnum = $pfNetworkPort->fields['logical_number']; $a_arguments[$arg] = str_replace("[[NETWORKPORTNUM]]", $logicalnum, $a_arguments[$arg]); } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNAME]]")){ if (isset($data['networkports_id']) && $data['networkports_id'] > 0) { $networkPort = new NetworkPort(); $networkPort->getFromDB($data['networkports_id']); $portname = $pfNetworkPort->fields['name']; $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $portname, $a_arguments[$arg]); } else if ($a_services[$i]['_HOSTITEMTYPE'] == 'Computer') { // Get networkportname of networkcard defined $pmHostaddress = new PluginMonitoringHostaddress(); $a_hostaddresses = $pmHostaddress->find("`itemtype`='Computer'" . " AND `items_id`='".$a_services[$i]['_HOSTITEMSID']."'", '', 1); if (count($a_hostaddresses) == 1) { $a_hostaddress = current($a_hostaddresses); if ($a_hostaddress['networkports_id'] > 0) { $networkPort = new NetworkPort(); $networkPort->getFromDB($a_hostaddress['networkports_id']); $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $networkPort->fields['name'], $a_arguments[$arg]); } } } } else if (strstr($a_arguments[$arg], "[")) { $a_arguments[$arg] = PluginMonitoringService::convertArgument($data['id'], $a_arguments[$arg]); } if ($a_arguments == '') { $notadd = 1; if ($notadddescription != '') { $notadddescription .= ", "; } $notadddescription .= "Argument ".$a_arguments[$arg]." do not have value"; } $args .= '!'.$a_arguments[$arg]; if ($a_arguments[$arg] == '' AND $a_component['alias_command'] != '') { $args .= $a_component['alias_command']; } } } } // End manage arguments if ($a_component['remotesystem'] == 'nrpe') { if ($a_component['alias_command'] != '') { $alias_command = $a_component['alias_command']; if (strstr($alias_command, '[[IP]]')) { $split = explode('-', current($a_hostname)); $ip = PluginMonitoringHostaddress::getIp($split[1], $split[0], ''); $alias_command = str_replace("[[IP]]", $ip, $alias_command); } $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . "check_nrpe!".$alias_command; } else { $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . "check_nrpe!".$pMonitoringCommand->fields['command_name']; } } else { $a_services[$i]['check_command'] = PluginMonitoringCommand::$command_prefix . $pMonitoringCommand->fields['command_name'].$args; } // * Manage event handler if ($a_component['plugin_monitoring_eventhandlers_id'] > 0) { if ($pmEventhandler->getFromDB($a_component['plugin_monitoring_eventhandlers_id'])) { $a_services[$i]['event_handler'] = $pmEventhandler->fields['command_name']; } } if (! empty(self::$shinkenParameters['shinken']['services']['process_perf_data'])) $a_services[$i]['process_perf_data'] = self::$shinkenParameters['shinken']['services']['process_perf_data']; if (! empty(self::$shinkenParameters['shinken']['services']['notes'])) $a_services[$i]['notes'] = self::$shinkenParameters['shinken']['services']['notes']; if (! empty(self::$shinkenParameters['shinken']['services']['notes_url'])) $a_services[$i]['notes_url'] = self::$shinkenParameters['shinken']['services']['notes_url']; if (! empty(self::$shinkenParameters['shinken']['services']['action_url'])) $a_services[$i]['action_url'] = self::$shinkenParameters['shinken']['services']['action_url']; if (! empty(self::$shinkenParameters['shinken']['services']['icon_image'])) $a_services[$i]['icon_image'] = self::$shinkenParameters['shinken']['services']['icon_image']; if (! empty(self::$shinkenParameters['shinken']['services']['icon_image_alt'])) $a_services[$i]['icon_image_alt'] = self::$shinkenParameters['shinken']['services']['icon_image_alt']; // * Contacts $a_contacts = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog' AND `items_id`='".$plugin_monitoring_componentscatalogs_id."'"); foreach ($a_list_contact as $data_contact) { if ($data_contact['users_id'] > 0) { if (isset($a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) { if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) { $user->getFromDB($data_contact['users_id']); $a_contacts[] = $user->fields['name']; } } } else if ($data_contact['groups_id'] > 0) { $queryg = "SELECT * FROM `glpi_groups_users` WHERE `groups_id`='".$data_contact['groups_id']."'"; $resultg = $DB->query($queryg); while ($datag=$DB->fetch_array($resultg)) { if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$datag['users_id']])) { $user->getFromDB($datag['users_id']); $a_contacts[] = $user->fields['name']; } } } } $a_contacts_unique = array_unique($a_contacts); $a_services[$i]['contacts'] = implode(',', $a_contacts_unique); // ** If shinken not use templates or template not defined : if (!isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) { $pMonitoringCheck->getFromDB($a_component['plugin_monitoring_checks_id']); $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval']; $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval']; $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts']; if ($calendar->getFromDB($a_component['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } $a_services[$i]['notification_interval'] = '30'; $a_services[$i]['notification_period'] = "24x7"; $a_services[$i]['notification_options'] = 'w,u,c,r,f,s'; $a_services[$i]['process_perf_data'] = '1'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['passive_checks_enabled'] = '1'; $a_services[$i]['parallelize_check'] = '1'; $a_services[$i]['obsess_over_service'] = '1'; $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = '3600'; $a_services[$i]['notifications_enabled'] = '1'; if (isset($a_services[$i]['event_handler'])) { $a_services[$i]['event_handler_enabled'] = '1'; } else { $a_services[$i]['event_handler_enabled'] = '0'; // $a_services[$i]['event_handler_enabled'] = ''; } $a_services[$i]['flap_detection_enabled'] = '1'; $a_services[$i]['failure_prediction_enabled'] = '1'; $a_services[$i]['retain_status_information'] = '1'; $a_services[$i]['retain_nonstatus_information'] = '1'; $a_services[$i]['is_volatile'] = '0'; // $a_services[$i]['_httpstink'] = 'NO'; } else { // Notification options $a_services[$i]['notification_interval'] = '30'; $pmComponentscatalog->getFromDB($plugin_monitoring_componentscatalogs_id); if ($pmComponentscatalog->fields['notification_interval'] != '30') { $a_services[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval']; } $a_services[$i]['notification_period'] = '24x7'; $a_services[$i]['check_period'] = '24x7'; if ($calendar->getFromDB($a_component['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } } // WebUI user interface ... if (isset(self::$shinkenParameters['webui']['serviceIcons']['name'])) { $a_services[$i][self::$shinkenParameters['webui']['serviceIcons']['name']] = self::$shinkenParameters['webui']['serviceIcons']['value']; } if ($notadd == '1') { unset($a_services[$i]); $input = array(); $input['id'] = $data['id']; $input['event'] = $notadddescription; $input['state'] = "CRITICAL"; $input['state_type'] = "HARD"; $pmService->update($input); } else { $i++; } } // } } PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "End generateServicesCfg services\n" ); PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "Starting generateServicesCfg business rules ...\n" ); // -------------------------------------------------- // Business rules services ... $pmService = new PluginMonitoringService(); $pmServicescatalog = new PluginMonitoringServicescatalog(); $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmBusinessrule = new PluginMonitoringBusinessrule(); $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host(); $pmBusinessrule_component = new PluginMonitoringBusinessrule_component(); // Prepare individual contacts $a_contacts_entities = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog' AND `users_id`>0"); foreach ($a_list_contact as $data) { $contactentities = getSonsOf('glpi_entities', $data['entities_id']); if (isset($a_contacts_entities[$data['items_id']][$data['users_id']])) { $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$data['users_id']]); } $a_contacts_entities[$data['items_id']][$data['users_id']] = $contactentities; } // Prepare groups contacts $group = new Group(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog' AND `groups_id`>0"); foreach ($a_list_contact as $data) { $group->getFromDB($data['groups_id']); if ($group->fields['is_recursive'] == 1) { $contactentities = getSonsOf('glpi_entities', $group->fields['entities_id']); } else { $contactentities = array($group->fields['entities_id'] => $group->fields['entities_id']); } $queryg = "SELECT * FROM `glpi_groups_users` WHERE `groups_id`='".$data['groups_id']."'"; $resultg = $DB->query($queryg); while ($datag=$DB->fetch_array($resultg)) { if (isset($a_contacts_entities[$data['items_id']][$datag['users_id']])) { $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$datag['users_id']]); } $a_contacts_entities[$data['items_id']][$datag['users_id']] = $contactentities; } } // Services catalogs $a_listBA = $pmServicescatalog->find("`is_generic`='0'"); foreach ($a_listBA as $dataBA) { if (isset($a_entities_allowed['-1']) OR isset($a_entities_allowed[$dataBA['entities_id']])) { $a_grouplist = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$dataBA['id']."'"); $a_group = array(); foreach ($a_grouplist as $gdata) { $pmBusinessrule_component->replayDynamicServices($gdata['id']); $a_listBR = $pmBusinessrule->find( "`plugin_monitoring_businessrulegroups_id`='".$gdata['id']."'"); foreach ($a_listBR as $dataBR) { if ($pmService->getFromDB($dataBR['plugin_monitoring_services_id'])) { if ($pmService->getHostName() != '') { $hostname = preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName()); if ($gdata['operator'] == 'and' OR $gdata['operator'] == 'or' OR strstr($gdata['operator'], ' of:')) { $operator = '|'; if ($gdata['operator'] == 'and') { $operator = '&'; } if (!isset($a_group[$gdata['id']])) { $a_group[$gdata['id']] = ''; if (strstr($gdata['operator'], ' of:')) { $a_group[$gdata['id']] = $gdata['operator']; } $a_group[$gdata['id']] .= $hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true))); } else { $a_group[$gdata['id']] .= $operator.$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true))); } } else { $a_group[$gdata['id']] = $gdata['operator']." ".$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$item->getName()); } } } PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', " - SC group : ".$a_group[$gdata['id']]."\n" ); } } if (count($a_group) > 0) { $pMonitoringCheck->getFromDB($dataBA['plugin_monitoring_checks_id']); $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval']; $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval']; $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts']; if ($calendar->getFromDB($dataBA['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } $a_services[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['bp_host']; $a_services[$i]['business_impact'] = $dataBA['business_priority']; $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9\-_]/","",$dataBA['name']); $a_services[$i]['_ENTITIESID'] = $dataBA['id']; $a_services[$i]['_ITEMSID'] = $dataBA['id']; $a_services[$i]['_ITEMTYPE'] = 'ServiceCatalog'; $command = "bp_rule!"; foreach ($a_group as $key=>$value) { if (!strstr($value, "&") AND !strstr($value, "|")) { $a_group[$key] = trim($value); } else { $a_group[$key] = "(".trim($value).")"; } } $a_services[$i]['check_command'] = $command.implode("&", $a_group); if ($dataBA['notification_interval'] != '30') { $a_services[$i]['notification_interval'] = $dataBA['notification_interval']; } else { $a_services[$i]['notification_interval'] = '30'; } $a_services[$i]['notification_period'] = "24x7"; $a_services[$i]['notification_options'] = 'w,u,c,r,f,s'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['process_perf_data'] = '1'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['passive_checks_enabled'] = '1'; $a_services[$i]['parallelize_check'] = '1'; $a_services[$i]['obsess_over_service'] = '1'; $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = '3600'; $a_services[$i]['notifications_enabled'] = '1'; $a_services[$i]['event_handler_enabled'] = '0'; //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE'; $a_services[$i]['flap_detection_enabled'] = '1'; $a_services[$i]['failure_prediction_enabled'] = '1'; $a_services[$i]['retain_status_information'] = '1'; $a_services[$i]['retain_nonstatus_information'] = '1'; $a_services[$i]['is_volatile'] = '0'; // $a_services[$i]['_httpstink'] = 'NO'; // * Contacts $a_contacts = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog' AND `items_id`='".$dataBA['id']."'"); foreach ($a_list_contact as $data_contact) { if ($data_contact['users_id'] > 0) { if (isset($a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) { if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) { $user->getFromDB($data_contact['users_id']); $a_contacts[] = $user->fields['name']; } } } else if ($data_contact['groups_id'] > 0) { $queryg = "SELECT * FROM `glpi_groups_users` WHERE `groups_id`='".$data_contact['groups_id']."'"; $resultg = $DB->query($queryg); while ($datag=$DB->fetch_array($resultg)) { if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$datag['users_id']])) { $user->getFromDB($datag['users_id']); $a_contacts[] = $user->fields['name']; } } } } $a_contacts_unique = array_unique($a_contacts); $a_services[$i]['contacts'] = implode(',', $a_contacts_unique); $i++; } } } PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "End generateServicesCfg business rules\n" ); PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "Starting generateServicesCfg business rules templates ...\n" ); // Services catalogs templates // TODO : correctly test and improve it ! $a_listBA = $pmServicescatalog->find("`is_generic`='1'"); foreach ($a_listBA as $dataBA) { PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', " - SC : ".$dataBA['id']."\n" ); if (isset($a_entities_allowed['-1']) OR isset($a_entities_allowed[$dataBA['entities_id']])) { $pmServicescatalog->getFromDB($dataBA['id']); $a_entitiesServices = $pmServicescatalog->getGenericServicesEntities(); foreach ($a_entitiesServices as $idEntity=>$a_entityServices) { // New entity ... so new business rule ! PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', " - SC templated services for an entity : ".$idEntity."\n" ); $pmDerivatedSC = new PluginMonitoringServicescatalog(); $a_derivatedSC = $pmDerivatedSC->find("`entities_id`='$idEntity' AND `name` LIKE '".$dataBA['name']."%'"); foreach ($a_derivatedSC as $a_derivated) { PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', " - a_derivated : ".$a_derivated['name']."\n" ); $a_derivatedSC = $a_derivated; } $a_group = array(); foreach ($a_entityServices as $services) { if ($pmService->getFromDB($services['serviceId'])) { // Toolbox::logInFile("pm-shinken", " - SC templated service entity : ".$services['entityId'].", service : ".$pmService->getName(true)." on ".$pmService->getHostName()."\n"); if ($pmService->getHostName() != '') { $hostname = preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName()); $serviceFakeId = $services['entityId']; $pmBusinessrulegroup->getFromDB($services['BRgroupId']); $BRoperator = $pmBusinessrulegroup->getField('operator'); if ($BRoperator == 'and' OR $BRoperator == 'or' OR strstr($BRoperator, ' of:')) { $operator = '|'; if ($BRoperator == 'and') { $operator = '&'; } if (!isset($a_group[$serviceFakeId])) { $a_group[$serviceFakeId] = ''; if (strstr($BRoperator, ' of:')) { $a_group[$serviceFakeId] = $BRoperator; } $a_group[$serviceFakeId] .= $hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true))); } else { $a_group[$serviceFakeId] .= $operator.$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getName(array('shinken'=>true))); } } else { $a_group[$serviceFakeId] = $BRoperator." ".$hostname.",".preg_replace("/[^A-Za-z0-9\-_]/","",$pmService->getHostName()); } // Toolbox::logInFile("pm-shinken", " - SCT group : ".$a_group[$serviceFakeId]."\n"); } } } if (count($a_group) > 0) { $pMonitoringCheck->getFromDB($a_derivatedSC['plugin_monitoring_checks_id']); $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval']; $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval']; $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts']; if ($calendar->getFromDB($a_derivatedSC['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } $a_services[$i]['host_name'] = preg_replace("/[^A-Za-z0-9\-_]/","",$a_derivatedSC['name']); $a_services[$i]['host_name'] = self::$shinkenParameters['shinken']['fake_hosts']['name_prefix'] . self::$shinkenParameters['shinken']['fake_hosts']['bp_host']; $a_services[$i]['business_impact'] = $a_derivatedSC['business_priority']; $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9\-_]/","",$a_derivatedSC['name']); $a_services[$i]['_ENTITIESID'] = $a_derivatedSC['entities_id']; $a_services[$i]['_ITEMSID'] = $a_derivatedSC['id']; $a_services[$i]['_ITEMTYPE'] = 'ServiceCatalog'; $command = "bp_rule!"; foreach ($a_group as $key=>$value) { if (!strstr($value, "&") AND !strstr($value, "|")) { $a_group[$key] = trim($value); } else { $a_group[$key] = "(".trim($value).")"; } } $a_services[$i]['check_command'] = $command.implode("&", $a_group); if ($a_derivatedSC['notification_interval'] != '30') { $a_services[$i]['notification_interval'] = $a_derivatedSC['notification_interval']; } else { $a_services[$i]['notification_interval'] = '30'; } $a_services[$i]['notification_period'] = "24x7"; $a_services[$i]['notification_options'] = 'w,u,c,r,f,s'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['process_perf_data'] = '1'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['passive_checks_enabled'] = '1'; $a_services[$i]['parallelize_check'] = '1'; $a_services[$i]['obsess_over_service'] = '1'; $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = '3600'; $a_services[$i]['notifications_enabled'] = '1'; $a_services[$i]['event_handler_enabled'] = '0'; //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE'; $a_services[$i]['flap_detection_enabled'] = '1'; $a_services[$i]['failure_prediction_enabled'] = '1'; $a_services[$i]['retain_status_information'] = '1'; $a_services[$i]['retain_nonstatus_information'] = '1'; $a_services[$i]['is_volatile'] = '0'; // $a_services[$i]['_httpstink'] = 'NO'; // * Contacts $a_contacts = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringServicescatalog' AND `items_id`='".$dataBA['id']."'"); foreach ($a_list_contact as $data_contact) { if ($data_contact['users_id'] > 0) { if (isset($a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) { if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$data_contact['users_id']])) { $user->getFromDB($data_contact['users_id']); $a_contacts[] = $user->fields['name']; } } } else if ($data_contact['groups_id'] > 0) { $queryg = "SELECT * FROM `glpi_groups_users` WHERE `groups_id`='".$data_contact['groups_id']."'"; $resultg = $DB->query($queryg); while ($datag=$DB->fetch_array($resultg)) { if (in_array($data['entities_id'], $a_contacts_entities[$dataBA['id']][$datag['users_id']])) { $user->getFromDB($datag['users_id']); $a_contacts[] = $user->fields['name']; } } } } $a_contacts_unique = array_unique($a_contacts); $a_services[$i]['contacts'] = implode(',', $a_contacts_unique); $i++; } } } } PluginMonitoringToolbox::logIfExtradebug( 'pm-shinken', "End generateServicesCfg business rules templates\n" ); if ($file == "1") { $config = "# Generated by plugin monitoring for GLPI\n# on ".date("Y-m-d H:i:s")."\n\n"; foreach ($a_services as $data) { $config .= $this->writeFile("service", $data); } return array('services.cfg', $config); } else { return $a_services; } }
function showWidget($id) { global $LANG, $DB, $CFG_GLPI; $pMonitoringBusinessrule = new PluginMonitoringBusinessrule(); $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pMonitoringService = new PluginMonitoringService(); $this->getFromDB($id); $data = $this->fields; $input = ''; $input .= '<table class="tab_cadre_fixe" style="width:200px;height:200px">'; $input .= '<tr class="tab_bg_1">'; $input .= '<th colspan="2" style="font-size:20px;" height="50">'; $input .= $data['name']; $input .= '</th>'; $input .= '</tr>'; $input .= '<tr class="tab_bg_1">'; $input .= '<td>'; $input .= $LANG['state'][0] . " :"; $input .= '</td>'; $input .= '<td width="40">'; switch ($data['state']) { case 'UP': case 'OK': $input .= '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/pics/box_green_40.png"/>'; break; case 'DOWN': case 'UNREACHABLE': case 'CRITICAL': case 'DOWNTIME': $input .= '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/pics/box_red_40.png"/>'; break; case 'WARNING': case 'UNKNOWN': case 'RECOVERY': case 'FLAPPING': case '': $input .= '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/pics/box_orange_40.png"/>'; break; } $input .= '</td>'; $input .= '</tr>'; $input .= '<tr class="tab_bg_1">'; $input .= '<td>'; $input .= $LANG['plugin_monitoring']['servicescatalog'][1] . " :"; $input .= '</td>'; $input .= '<td width="40" align="center">'; $a_group = $pMonitoringBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $data['id'] . "'"); $a_gstate = array(); foreach ($a_group as $gdata) { $a_brules = $pMonitoringBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $gdata['id'] . "'"); $state = array(); $state['OK'] = 0; $state['WARNING'] = 0; $state['CRITICAL'] = 0; foreach ($a_brules as $brulesdata) { if ($pMonitoringService->getFromDB($brulesdata['plugin_monitoring_services_id'])) { switch ($pMonitoringService->fields['state']) { case 'UP': case 'OK': $state['OK']++; break; case 'DOWN': case 'UNREACHABLE': case 'CRITICAL': case 'DOWNTIME': $state['CRITICAL']++; break; case 'WARNING': case 'UNKNOWN': case 'RECOVERY': case 'FLAPPING': $state['WARNING']++; break; } } } if ($state['CRITICAL'] >= 1) { $a_gstate[$gdata['id']] = "CRITICAL"; } else { if ($state['WARNING'] >= 1) { $a_gstate[$gdata['id']] = "WARNING"; } else { $a_gstate[$gdata['id']] = "OK"; } } } $state = array(); $state['OK'] = 0; $state['WARNING'] = 0; $state['CRITICAL'] = 0; foreach ($a_gstate as $value) { $state[$value]++; } $color = 'green'; if ($state['CRITICAL'] > 0) { $color = 'red'; } else { if ($state['WARNING'] > 0) { $color = 'orange'; } } $input .= '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/pics/box_' . $color . '_32.png" />'; $input .= '</td>'; $input .= '</tr>'; $input .= '<tr class="tab_bg_1">'; $input .= '<td colspan="2" align="center">'; $input .= '<a href="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/front/servicescatalog.form.php?id=' . $data['id'] . '&detail=1">Détail</a>'; $input .= '</td>'; $input .= '</tr>'; $input .= '</table>'; return $input; }
function showForm($servicescatalogs_id, $options = array()) { global $DB, $LANG; $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); // // Add group $pMonitoringBusinessrulegroup->showForm(0, $servicescatalogs_id); // Display each group $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringBusinessrulegroup") . "`\n WHERE `plugin_monitoring_servicescatalogs_id`='" . $servicescatalogs_id . "'\n ORDER BY `name`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { $pMonitoringBusinessrulegroup->showForm($data['id'], $servicescatalogs_id); // // echo "<table class='tab_cadre' width='600'>"; // echo "<tr class='tab_bg_1'>"; // echo "<th>".$LANG['plugin_monitoring']['service'][2]." :</th>"; // echo "</tr>"; // echo "<tr class='tab_bg_1'>"; // echo "<td>"; // echo "<form name='form' method='post' action='".$CFG_GLPI['root_doc']."/plugins/monitoring/front/businessrule.form.php'>"; // echo "<input type='hidden' name='plugin_monitoring_businessrulegroups_id' value='".$data['id']."' />"; // self::dropdownService(0, array('name' => 'type')); // echo "<input type='submit' name='add' value=\"".$LANG['buttons'][8]."\" class='submit'>"; // echo "</form>"; // echo "</td>"; // echo "</tr>"; // echo "</table>"; // // echo "<table class='tab_cadre' width='600'>"; // echo "<tr class='tab_bg_1'>"; // echo "<th></th>"; // echo "<th>".$LANG['plugin_monitoring']['service'][0]."</th>"; // echo "</tr>"; // $a_services = $this->find("`plugin_monitoring_businessrulegroups_id`='".$data['id']."'"); // foreach ($a_services as $gdata) { // echo "<tr class='tab_bg_1'>"; // echo "<td>"; // echo "<input type='checkbox'/>"; // echo "</td>"; // echo "<td>"; // $pMonitoringService = new PluginMonitoringService(); // $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host(); // $pMonitoringService->getFromDB($gdata["plugin_monitoring_services_id"]); // $pmComponentscatalog_Host->getFromDB($pMonitoringService->fields['plugin_monitoring_componentscatalogs_hosts_id']); // echo $pMonitoringService->getLink(1); // echo " ".$LANG['networking'][25]." "; // $itemtype2 = $pmComponentscatalog_Host->fields['itemtype']; // $item2 = new $itemtype2(); // $item2->getFromDB($pmComponentscatalog_Host->fields['items_id']); // echo $item2->getLink(1); // echo "</td>"; // echo "</tr>"; // } // echo "</table>"; // echo "<br/>"; } return; echo "<table class='tab_cadre_fixe'>"; echo "<tr class='tab_bg_1'>"; echo "<th>"; echo "</th>"; echo "<th>"; echo $LANG['plugin_monitoring']['businessrule'][11]; echo "</th>"; echo "<th>"; echo $LANG['rulesengine'][9]; echo "</th>"; echo "<th>"; echo $LANG['plugin_monitoring']['service'][20]; echo "</th>"; echo "</tr>"; $query = "SELECT * FROM `" . getTableForItemType($this->getType()) . "`\n WHERE `plugin_monitoring_servicescatalogs_id`='" . $servicescatalogs_id . "'\n ORDER BY `group`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { echo "<tr class='tab_bg_1'>"; echo "<td>"; echo "<input type='checkbox'/>"; echo "</td>"; echo "<td>"; echo $data['group']; echo "</td>"; echo "<td>"; echo "</td>"; echo "<td>"; echo "</td>"; echo "</tr>"; } echo "</table>"; }
function updateGenericServicesCatalogs($action = 'update') { global $DB; $entity = new Entity(); $pmServicescatalog = new PluginMonitoringServicescatalog(); $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component(); $existingSCs = array(); // Find existing instances of generic services catalog ... $a_SCs = $this->find("`name` LIKE '" . $this->getName() . "%'"); foreach ($a_SCs as $a_SC) { // Toolbox::logInFile("pm", "SC : ".$a_SC['id'].", name : ".$a_SC['name'].", generic : ".$a_SC['is_generic']."\n"); if ($a_SC['name'] == $this->getField('name')) { continue; } $existingSCs[$a_SC['name']] = $a_SC; } if ($action == 'delete') { foreach ($existingSCs as $name => $a_SC) { $pmServicescatalog->getFromDB($a_SC['id']); $pmServicescatalog->delete($pmServicescatalog->fields); // Toolbox::logInFile("pm", "Deleted : ".$a_SC['name']."\n"); $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component(); $pmBusinessrule = new PluginMonitoringBusinessrule(); // Get business rules groups ... $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $a_SC['id'] . "'"); // Delete business groups components foreach ($a_BRgroups as $a_BRgroup) { // Toolbox::logInFile("pm", "a_BRgroup : ".$a_BRgroup['id']."\n"); $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'"); foreach ($a_brcomponents as $a_brcomponent) { // Toolbox::logInFile("pm", "a_brcomponent : ".$a_brcomponent['id']."\n"); $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']); $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields); } } // Delete business groups rules foreach ($a_BRgroups as $a_BRgroup) { $a_brs = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'"); foreach ($a_brs as $a_br) { // Toolbox::logInFile("pm", "a_br : ".$a_br['id']."\n"); $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']); $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields); } } } return; } else { foreach ($existingSCs as $name => $a_SC) { $pmServicescatalog->getFromDB($a_SC['id']); $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component(); $pmBusinessrule = new PluginMonitoringBusinessrule(); // Get business rules groups ... $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $a_SC['id'] . "'"); // Delete business groups components foreach ($a_BRgroups as $a_BRgroup) { // Toolbox::logInFile("pm", "a_BRgroup : ".$a_BRgroup['id']."\n"); $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'"); foreach ($a_brcomponents as $a_brcomponent) { // Toolbox::logInFile("pm", "a_brcomponent : ".$a_brcomponent['id']."\n"); $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']); $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields); } } // Delete business groups rules foreach ($a_BRgroups as $a_BRgroup) { $a_brs = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'"); foreach ($a_brs as $a_br) { // Toolbox::logInFile("pm", "a_br : ".$a_br['id']."\n"); $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']); $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields); } } } } // Find entities concerned ... $a_entitiesServices = $this->getGenericServicesEntities(); foreach ($a_entitiesServices as $idEntity => $a_entityServices) { // New entity ... so it must exist a derivated SC ! $entity->getFromDB($idEntity); // Toolbox::logInFile("pm", "Found entity : ".$idEntity." / ".$entity->getName()."\n"); $scName = $this->getName() . " - " . $entity->getName(); if (isset($existingSCs[$scName])) { // Update SC $pmServicescatalog->getFromDB($existingSCs[$scName]['id']); $pmServicescatalog->fields = $this->fields; unset($pmServicescatalog->fields['id']); $pmServicescatalog->fields['id'] = $existingSCs[$scName]['id']; $pmServicescatalog->fields['entities_id'] = $idEntity; $pmServicescatalog->fields['is_generic'] = 0; $pmServicescatalog->fields['name'] = $DB->escape($scName); $pmServicescatalog->update($pmServicescatalog->fields); /* // Finish updating if needed ... $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$this->fields['id']."'"); foreach ($a_BRgroups as $a_BRgroup) { $pmBusinessrulegroup = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$a_SC['id']."' AND `name`='".$a_BRgroup['name']."'"); $pmBusinessrulegroup->fields = $a_BRgroup->fields; unset($pmBusinessrulegroup->fields['id']); $pmBusinessrulegroup->fields['plugin_monitoring_servicescatalogs_id'] = $pmServicescatalog->fields['id']; $pmBusinessrulegroup->update($pmBusinessrulegroup->fields); } */ // Toolbox::logInFile("pm", "Updated : ".$scName."\n"); } else { // Add SC $pmServicescatalog = new PluginMonitoringServicescatalog(); $pmServicescatalog->getEmpty(); $pmServicescatalog->fields = $this->fields; unset($pmServicescatalog->fields['id']); $pmServicescatalog->fields['entities_id'] = $idEntity; $pmServicescatalog->fields['is_recursive'] = 0; $pmServicescatalog->fields['is_generic'] = 0; $pmServicescatalog->fields['name'] = $DB->escape($scName); $pmServicescatalog->fields['plugin_monitoring_servicescatalogs_id'] = $this->fields['id']; $pmServicescatalog->add($pmServicescatalog->fields); /* // Finish updating if needed ... $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$this->fields['id']."'"); foreach ($a_BRgroups as $a_BRgroup) { $ref = new PluginMonitoringBusinessrulegroup(); $ref->getFromDB($a_BRgroup['id']); $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmBusinessrulegroup->getEmpty(); $pmBusinessrulegroup->fields = $ref->fields; unset($pmBusinessrulegroup->fields['id']); $pmBusinessrulegroup->fields['plugin_monitoring_servicescatalogs_id'] = $pmServicescatalog->fields['id']; $pmBusinessrulegroup->add($pmBusinessrulegroup->fields); $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='".$a_BRgroup['id']."'"); foreach ($a_brcomponents as $a_brcomponent) { $ref = new PluginMonitoringBusinessrule_component(); $ref->getFromDB($a_brcomponent['id']); $pmBusinessrule_component = new PluginMonitoringBusinessrule_component(); $pmBusinessrule_component->getEmpty(); $pmBusinessrule_component->fields = $ref->fields; unset($pmBusinessrule_component->fields['id']); $pmBusinessrule_component->fields['plugin_monitoring_businessrulegroups_id'] = $pmBusinessrulegroup->fields['id']; $pmBusinessrule_component->add($pmBusinessrule_component->fields); } } */ // Toolbox::logInFile("pm", "Added : ".$scName."\n"); } } }
@author David Durieux @co-author @comment @copyright Copyright (c) 2011-2012 Plugin Monitoring for GLPI team @license AGPL License 3.0 or (at your option) any later version http://www.gnu.org/licenses/agpl-3.0-standalone.html @link https://forge.indepnet.net/projects/monitoring/ @since 2011 ------------------------------------------------------------------------ */ define('GLPI_ROOT', '../../..'); include GLPI_ROOT . "/inc/includes.php"; PluginMonitoringProfile::checkRight("servicescatalog", "w"); commonHeader($LANG['plugin_monitoring']['title'][0], $_SERVER["PHP_SELF"], "plugins", "monitoring", "businessrules"); $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); foreach ($_POST as $key => $value) { if (strstr($key, 'deletebusinessrules-')) { $split = explode("-", $key); $pmBusinessrule = new PluginMonitoringBusinessrule(); $pmBusinessrule->delete(array('id' => $split[1])); glpi_header($_SERVER['HTTP_REFERER']); } } if (isset($_POST['update'])) { $pMonitoringBusinessrulegroup->update($_POST); } else { if (isset($_POST['add']) and isset($_POST['id'])) { if (isset($_POST['plugin_monitoring_services_id'])) { $pmBusinessrule = new PluginMonitoringBusinessrule(); $pmBusinessrule->add(array('plugin_monitoring_businessrulegroups_id' => $_POST['plugin_monitoring_businessrulegroups_id'], 'plugin_monitoring_services_id' => $_POST['plugin_monitoring_services_id']));
function replayDynamicServices($plugin_monitoring_businessrulegroups_id) { global $DB; if ($plugin_monitoring_businessrulegroups_id == 0) { return; } // Get entity and if recursif $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmServicescatalog = new PluginMonitoringServicescatalog(); $pmBusinessrulegroup->getFromDB($plugin_monitoring_businessrulegroups_id); $pmServicescatalog->getFromDB($pmBusinessrulegroup->fields['plugin_monitoring_servicescatalogs_id']); if ($pmServicescatalog->fields['is_recursive']) { $a_sons = getSonsOf("glpi_entities", $pmServicescatalog->fields['entities_id']); $restrict_entities = "AND ( `glpi_plugin_monitoring_services`.`entities_id` IN ('" . implode("','", $a_sons) . "') )"; } else { $restrict_entities = "AND ( `glpi_plugin_monitoring_services`.`entities_id` = '" . $pmServicescatalog->fields['entities_id'] . "' )"; } $a_brcomponents = $this->find("`plugin_monitoring_businessrulegroups_id`='" . $plugin_monitoring_businessrulegroups_id . "'"); $a_services = array(); foreach ($a_brcomponents as $a_brcomponent) { $pmComponentscatalog_Component = new PluginMonitoringComponentscatalog_Component(); $pmComponentscatalog_Component->getFromDB($a_brcomponent['plugin_monitoring_componentscatalogs_components_id']); // Get all services of component of component catalog $query = "SELECT `glpi_plugin_monitoring_services`.`id` FROM `glpi_plugin_monitoring_services`" . " LEFT JOIN `glpi_plugin_monitoring_componentscatalogs_hosts`" . " ON plugin_monitoring_componentscatalogs_hosts_id=" . " `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`" . " WHERE `glpi_plugin_monitoring_services`.`plugin_monitoring_components_id`" . " = '" . $pmComponentscatalog_Component->fields['plugin_monitoring_components_id'] . "' " . $restrict_entities; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { $a_services[$data['id']] = $data['id']; } } // get static services of the group (so not add dynamic if yet in static) $pmBusinessrule_component = new PluginMonitoringBusinessrule_component(); $pmBusinessrule = new PluginMonitoringBusinessrule(); $a_static = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $plugin_monitoring_businessrulegroups_id . "'" . " AND `is_dynamic`=0"); foreach ($a_static as $data) { if (isset($a_services[$data['plugin_monitoring_services_id']])) { unset($a_services[$data['plugin_monitoring_services_id']]); // Update generic status $pmBusinessrule->getFromDB($data['id']); $input = array('id' => $data['id'], 'is_generic' => $pmServicescatalog->fields['is_generic']); $pmBusinessrule->update($input); } } // update services + is_dynamic=1 $query = "SELECT * FROM `glpi_plugin_monitoring_businessrules`" . " WHERE `plugin_monitoring_businessrulegroups_id`='" . $plugin_monitoring_businessrulegroups_id . "'" . " AND `is_dynamic`=1"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { // Update if yet in DB if (isset($a_services[$data['plugin_monitoring_services_id']])) { unset($a_services[$data['plugin_monitoring_services_id']]); // Update generic status $pmBusinessrule->getFromDB($data['id']); $input = array('id' => $data['id'], 'is_generic' => $pmServicescatalog->fields['is_generic']); $pmBusinessrule->update($input); } else { // delete if not exist $pmBusinessrule->delete($data); } } // Add new foreach ($a_services as $services_id) { $input = array('plugin_monitoring_businessrulegroups_id' => $plugin_monitoring_businessrulegroups_id, 'plugin_monitoring_services_id' => $services_id, 'is_dynamic' => '1', 'is_generic' => $pmServicescatalog->fields['is_generic']); $pmBusinessrule->add($input); } }
function generateServicesCfg($file = 0, $tag = '') { global $DB; $pMonitoringCommand = new PluginMonitoringCommand(); $pMonitoringCheck = new PluginMonitoringCheck(); $pmComponent = new PluginMonitoringComponent(); $pmEntity = new PluginMonitoringEntity(); $pmContact_Item = new PluginMonitoringContact_Item(); $pmService = new PluginMonitoringService(); $pmComponentscatalog = new PluginMonitoringComponentscatalog(); $calendar = new Calendar(); $user = new User(); $pmLog = new PluginMonitoringLog(); $profile_User = new Profile_User(); if (isset($_SERVER['HTTP_USER_AGENT']) and strstr($_SERVER['HTTP_USER_AGENT'], 'xmlrpclib.py')) { if (!isset($_SESSION['glpi_currenttime'])) { $_SESSION['glpi_currenttime'] = date("Y-m-d H:i:s"); } $input = array(); $input['user_name'] = "Shinken"; $input['action'] = "restart"; $input['date_mod'] = date("Y-m-d H:i:s"); $pmLog->add($input); } $hostnamebp = ''; $a_services = array(); $i = 0; // * Prepare contacts $a_contacts_entities = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n AND `users_id`>0"); foreach ($a_list_contact as $data) { $contactentities = getSonsOf('glpi_entities', $data['entities_id']); if (isset($a_contacts_entities[$data['items_id']][$data['users_id']])) { $contactentities = array_merge($contactentities, $a_contacts_entities[$data['items_id']][$data['users_id']]); } $a_contacts_entities[$data['items_id']][$data['users_id']] = $contactentities; } $a_entities_allowed = $pmEntity->getEntitiesByTag($tag); $query = "SELECT * FROM `glpi_plugin_monitoring_services`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { $notadd = 0; $notadddescription = ''; $a_component = current($pmComponent->find("`id`='" . $data['plugin_monitoring_components_id'] . "'", "", 1)); $a_hostname = array(); $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts` \n WHERE `id` = '" . $data['plugin_monitoring_componentscatalogs_hosts_id'] . "'\n LIMIT 1"; $resulth = $DB->query($queryh); $hostname = ''; $plugin_monitoring_componentscatalogs_id = 0; while ($datah = $DB->fetch_array($resulth)) { $itemtype = $datah['itemtype']; $item = new $itemtype(); if ($item->getFromDB($datah['items_id'])) { if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$item->fields['entities_id']])) { $a_hostname[] = $itemtype . "-" . $datah['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $item->fields['name']); $hostname = $item->fields['name']; $plugin_monitoring_componentscatalogs_id = $datah['plugin_monitoring_componentscalalog_id']; } } } if (count($a_hostname) > 0) { if (isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) { $a_services[$i]['use'] = $_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']]; } $a_services[$i]['host_name'] = implode(",", array_unique($a_hostname)); $hostnamebp = $a_services[$i]['host_name']; // For business rules $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9]/", "", $a_component['name']) . "-" . $data['id']; $pMonitoringCommand->getFromDB($a_component['plugin_monitoring_commands_id']); // Manage arguments $array = array(); preg_match_all("/\\\$(ARG\\d+)\\\$/", $pMonitoringCommand->fields['command_line'], $array); sort($array[0]); $a_arguments = importArrayFromDB($a_component['arguments']); $a_argumentscustom = importArrayFromDB($data['arguments']); foreach ($a_argumentscustom as $key => $value) { $a_arguments[$key] = $value; } $args = ''; foreach ($array[0] as $arg) { if ($arg != '$PLUGINSDIR$' and $arg != '$HOSTADDRESS$' and $arg != '$MYSQLUSER$' and $arg != '$MYSQLPASSWORD$') { $arg = str_replace('$', '', $arg); if (!isset($a_arguments[$arg])) { $args .= '!'; } else { if (strstr($a_arguments[$arg], "[[HOSTNAME]]")) { $a_arguments[$arg] = str_replace("[[HOSTNAME]]", $hostname, $a_arguments[$arg]); } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTDESCR]]")) { if (class_exists("PluginFusinvsnmpNetworkPort")) { $pfNetworkPort = new PluginFusinvsnmpNetworkPort(); $pfNetworkPort->loadNetworkport($data['networkports_id']); $descr = ''; $descr = $pfNetworkPort->getValue("ifdescr"); $a_arguments[$arg] = str_replace("[[NETWORKPORTDESCR]]", $descr, $a_arguments[$arg]); } } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNUM]]")) { $networkPort = new NetworkPort(); $networkPort->getFromDB($data['networkports_id']); $logicalnum = $pfNetworkPort->fields['logical_number']; $a_arguments[$arg] = str_replace("[[NETWORKPORTNUM]]", $logicalnum, $a_arguments[$arg]); } elseif (strstr($a_arguments[$arg], "[[NETWORKPORTNAME]]")) { $networkPort = new NetworkPort(); $networkPort->getFromDB($data['networkports_id']); $portname = $pfNetworkPort->fields['name']; $a_arguments[$arg] = str_replace("[[NETWORKPORTNAME]]", $portname, $a_arguments[$arg]); } else { if (strstr($a_arguments[$arg], "[")) { $a_arguments[$arg] = PluginMonitoringService::convertArgument($data['id'], $a_arguments[$arg]); } } if ($a_arguments == '') { $notadd = 1; if ($notadddescription != '') { $notadddescription .= ", "; } $notadddescription .= "Argument " . $a_arguments[$arg] . " Not have value"; } $args .= '!' . $a_arguments[$arg]; if ($a_arguments[$arg] == '' and $a_component['alias_command'] != '') { $args .= $a_component['alias_command']; } } } } // End manage arguments if ($a_component['remotesystem'] == 'nrpe') { if ($a_component['alias_command'] != '') { $a_services[$i]['check_command'] = "check_nrpe!" . $a_component['alias_command']; } else { $a_services[$i]['check_command'] = "check_nrpe!" . $pMonitoringCommand->fields['command_name']; } } else { $a_services[$i]['check_command'] = $pMonitoringCommand->fields['command_name'] . $args; } // * Contacts $a_contacts = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n AND `items_id`='" . $plugin_monitoring_componentscatalogs_id . "'"); foreach ($a_list_contact as $data_contact) { if (isset($a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) { if (in_array($data['entities_id'], $a_contacts_entities[$plugin_monitoring_componentscatalogs_id][$data_contact['users_id']])) { // $pmContact->getFromDB($data_contact['plugin_monitoring_contacts_id']); $user->getFromDB($data_contact['users_id']); $a_contacts[] = $user->fields['name']; } } } $a_contacts_unique = array_unique($a_contacts); $a_services[$i]['contacts'] = implode(',', $a_contacts_unique); // ** If shinken not use templates or template not defined : if (!isset($_SESSION['plugin_monitoring']['servicetemplates'][$a_component['id']])) { $pMonitoringCheck->getFromDB($a_component['plugin_monitoring_checks_id']); $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval']; $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval']; $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts']; if ($calendar->getFromDB($a_component['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } $a_services[$i]['notification_interval'] = '30'; if ($calendar->getFromDB($a_component['calendars_id'])) { $a_services[$i]['notification_period'] = $calendar->fields['name']; } else { $a_services[$i]['notification_period'] = "24x7"; } $a_services[$i]['notification_options'] = 'w,c,r'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['process_perf_data'] = '1'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['passive_checks_enabled'] = '1'; $a_services[$i]['parallelize_check'] = '1'; $a_services[$i]['obsess_over_service'] = '1'; $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = '1'; $a_services[$i]['notifications_enabled'] = '1'; $a_services[$i]['event_handler_enabled'] = '0'; //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE'; $a_services[$i]['flap_detection_enabled'] = '1'; $a_services[$i]['failure_prediction_enabled'] = '1'; $a_services[$i]['retain_status_information'] = '1'; $a_services[$i]['retain_nonstatus_information'] = '1'; $a_services[$i]['is_volatile'] = '0'; $a_services[$i]['_httpstink'] = 'NO'; } $pmComponentscatalog->getFromDB($plugin_monitoring_componentscatalogs_id); if ($pmComponentscatalog->fields['notification_interval'] != '30') { $a_services[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval']; } if ($notadd == '1') { unset($a_services[$i]); $input = array(); $input['id'] = $data['id']; $input['event'] = $notadddescription; $input['state'] = "CRITICAL"; $input['state_type'] = "HARD"; $pmService->update($input); } else { $i++; } } } // // Business rules.... $pmService = new PluginMonitoringService(); $pmServicescatalog = new PluginMonitoringServicescatalog(); $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); $pmBusinessrule = new PluginMonitoringBusinessrule(); $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host(); $a_listBA = $pmServicescatalog->find(); foreach ($a_listBA as $dataBA) { if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$dataBA['entities_id']])) { $a_grouplist = $pMonitoringBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $dataBA['id'] . "'"); $a_group = array(); foreach ($a_grouplist as $gdata) { $a_listBR = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $gdata['id'] . "'"); foreach ($a_listBR as $dataBR) { if ($pmService->getFromDB($dataBR['plugin_monitoring_services_id'])) { $pmComponentscatalog_Host->getFromDB($pmService->fields['plugin_monitoring_componentscatalogs_hosts_id']); $itemtype = $pmComponentscatalog_Host->fields['itemtype']; $item = new $itemtype(); if ($item->getFromDB($pmComponentscatalog_Host->fields['items_id'])) { $hostname = $itemtype . "-" . $pmComponentscatalog_Host->fields['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $item->fields['name']); if ($gdata['operator'] == 'and' or $gdata['operator'] == 'or' or strstr($gdata['operator'], ' of:')) { $operator = '|'; if ($gdata['operator'] == 'and') { $operator = '&'; } if (!isset($a_group[$gdata['id']])) { $a_group[$gdata['id']] = ''; if (strstr($gdata['operator'], ' of:')) { $a_group[$gdata['id']] = $gdata['operator']; } $a_group[$gdata['id']] .= $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $pmService->fields['name']) . "-" . $pmService->fields['id']; } else { $a_group[$gdata['id']] .= $operator . $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $pmService->fields['name']) . "-" . $pmService->fields['id']; } } else { $a_group[$gdata['id']] = $gdata['operator'] . " " . $hostname . "," . preg_replace("/[^A-Za-z0-9]/", "", $item->getName()) . "-" . $item->fields['id']; } } } } } if (count($a_group) > 0) { $pMonitoringCheck->getFromDB($dataBA['plugin_monitoring_checks_id']); $a_services[$i]['check_interval'] = $pMonitoringCheck->fields['check_interval']; $a_services[$i]['retry_interval'] = $pMonitoringCheck->fields['retry_interval']; $a_services[$i]['max_check_attempts'] = $pMonitoringCheck->fields['max_check_attempts']; if ($calendar->getFromDB($dataBA['calendars_id'])) { $a_services[$i]['check_period'] = $calendar->fields['name']; } $a_services[$i]['host_name'] = $hostnamebp; $a_services[$i]['service_description'] = preg_replace("/[^A-Za-z0-9]/", "", $dataBA['name']) . "-" . $dataBA['id'] . "-businessrules"; $command = "bp_rule!"; foreach ($a_group as $key => $value) { if (!strstr($value, "&") and !strstr($value, "|")) { $a_group[$key] = trim($value); } else { $a_group[$key] = "(" . trim($value) . ")"; } } $a_services[$i]['check_command'] = $command . implode("&", $a_group); $a_services[$i]['notification_interval'] = '30'; if ($calendar->getFromDB($dataBA['calendars_id'])) { $a_services[$i]['notification_period'] = $calendar->fields['name']; } else { $a_services[$i]['notification_period'] = "24x7"; } $a_services[$i]['notification_options'] = 'w,c,r'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['process_perf_data'] = '1'; $a_services[$i]['active_checks_enabled'] = '1'; $a_services[$i]['passive_checks_enabled'] = '1'; $a_services[$i]['parallelize_check'] = '1'; $a_services[$i]['obsess_over_service'] = '1'; $a_services[$i]['check_freshness'] = '1'; $a_services[$i]['freshness_threshold'] = '1'; $a_services[$i]['notifications_enabled'] = '1'; $a_services[$i]['event_handler_enabled'] = '0'; //$a_services[$i]['event_handler'] = 'super_event_kill_everyone!DIE'; $a_services[$i]['flap_detection_enabled'] = '1'; $a_services[$i]['failure_prediction_enabled'] = '1'; $a_services[$i]['retain_status_information'] = '1'; $a_services[$i]['retain_nonstatus_information'] = '1'; $a_services[$i]['is_volatile'] = '0'; $a_services[$i]['_httpstink'] = 'NO'; $a_services[$i]['contacts'] = ''; $i++; } } } if ($file == "1") { $config = "# Generated by plugin monitoring for GLPI\n# on " . date("Y-m-d H:i:s") . "\n\n"; foreach ($a_services as $data) { $config .= $this->constructFile("service", $data); } return array('services.cfg', $config); } else { return $a_services; } }
function showForm($servicescatalogs_id, $options = array()) { global $DB; $pMonitoringBusinessrulegroup = new PluginMonitoringBusinessrulegroup(); // // Add group $pMonitoringBusinessrulegroup->showForm(0, $servicescatalogs_id); // Display each group $query = "SELECT * FROM `" . getTableForItemType("PluginMonitoringBusinessrulegroup") . "`\n WHERE `plugin_monitoring_servicescatalogs_id`='" . $servicescatalogs_id . "'\n ORDER BY `name`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { $pMonitoringBusinessrulegroup->showForm($data['id'], $servicescatalogs_id); } return; echo "<table class='tab_cadre_fixe'>"; echo "<tr class='tab_bg_1'>"; echo "<th>"; echo "</th>"; echo "<th>"; echo __('Group', 'monitoring'); echo "</th>"; echo "<th>"; echo __('Logical operator'); echo "</th>"; echo "<th>"; echo __('Resource', 'monitoring'); echo "</th>"; echo "</tr>"; $query = "SELECT * FROM `" . getTableForItemType($this->getType()) . "`\n WHERE `plugin_monitoring_servicescatalogs_id`='" . $servicescatalogs_id . "'\n ORDER BY `group`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { echo "<tr class='tab_bg_1'>"; echo "<td>"; echo "<input type='checkbox'/>"; echo "</td>"; echo "<td>"; echo $data['group']; echo "</td>"; echo "<td>"; echo "</td>"; echo "<td>"; echo "</td>"; echo "</tr>"; } echo "</table>"; }