/** * Display list of hosts */ function showHostsBoard($params, $width = '', $limit = '') { global $DB, $CFG_GLPI; if (!isset($_SESSION['plugin_monitoring_reduced_interface'])) { $_SESSION['plugin_monitoring_reduced_interface'] = false; } $col_to_display = array(0, 10, 1, 2, 3, 6, 7, 8, 9, 11); $data = Search::prepareDatasForSearch('PluginMonitoringHost', $params, $col_to_display); $data['tocompute'] = $data['toview']; Search::constructSQL($data); //echo "<pre>"; print_r($data['sql']['search']); Search::constructDatas($data); // if (! isset($_GET['order'])) { // $_GET['order'] = "ASC"; // } // if (! isset($_GET['sort'])) { // $_GET['sort'] = ""; // } // // $order = "ASC"; // if (isset($_GET['order'])) { // $order = $_GET['order']; // } // $where = ''; // if (isset($_GET['field'])) { // foreach ($_GET['field'] as $key=>$value) { // $wheretmp = ''; // if (isset($_GET['link'][$key])) { // $wheretmp.= " ".$_GET['link'][$key]." "; // } // $wheretmp .= Search::addWhere( // "", // 0, // "PluginMonitoringHost", // $_GET['field'][$key], // $_GET['searchtype'][$key], // $_GET['contains'][$key]); // if (!strstr($wheretmp, "``.``")) { // if ($where != '' // AND !isset($_GET['link'][$key])) { // $where .= " AND "; // } // $where .= $wheretmp; // } // } // } // if ($where != '') { // $where = "(".$where; // $where .= ") AND "; // } // $where .= " CONCAT_WS('', `glpi_computers`.`entities_id`, `glpi_printers`.`entities_id`, `glpi_networkequipments`.`entities_id`) IN (".$_SESSION['glpiactiveentities_string'].")"; // // if ($where != '') { // $where = " WHERE ".$where; // $where = str_replace("`".getTableForItemType("PluginMonitoringDisplay")."`.", // "", $where); // // } // // $leftjoin = " // LEFT JOIN `glpi_computers` // ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_computers`.`id` // AND `glpi_plugin_monitoring_hosts`.`itemtype`='Computer' // LEFT JOIN `glpi_printers` // ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_printers`.`id` // AND `glpi_plugin_monitoring_hosts`.`itemtype`='Printer' // LEFT JOIN `glpi_networkequipments` // ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_networkequipments`.`id` // AND `glpi_plugin_monitoring_hosts`.`itemtype`='NetworkEquipment' // LEFT JOIN `glpi_entities` // ON CONCAT_WS('', `glpi_computers`.`entities_id`, `glpi_printers`.`entities_id`, `glpi_networkequipments`.`entities_id`) = `glpi_entities`.`id` // // "; // // // * ORDER // $ORDERQUERY = "ORDER BY entity_name ASC, host_name ASC"; // $toview = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // $toviewComplete = array( // 'ITEM_0' => 'entity_name', // 'ITEM_1' => 'host_name', // 'ITEM_2' => 'host_state', // 'ITEM_3' => 'service_state', // 'ITEM_4' => 'last_check', // 'ITEM_5' => 'event', // 'ITEM_6' => 'perf_data', // 'ITEM_7' => 'is_acknowledged' // ); // foreach ($toview as $key => $val) { // if ($_GET['sort']==$val) { // $ORDERQUERY = Search::addOrderBy("PluginMonitoringHost", $_GET['sort'], // $_GET['order'], $key); // foreach ($toviewComplete as $keyi=>$vali) { // $ORDERQUERY= str_replace($keyi, $vali, $ORDERQUERY); // } // } // } // //// `glpi_computers`.* // // $query = "SELECT // `glpi_entities`.`name` AS entity_name, // CONCAT_WS('', `glpi_computers`.`id`, `glpi_printers`.`id`, `glpi_networkequipments`.`id`) AS idComputer, // CONCAT_WS('', `glpi_computers`.`name`, `glpi_printers`.`name`, `glpi_networkequipments`.`name`) AS host_name, // `glpi_plugin_monitoring_hosts`.*, // `glpi_plugin_monitoring_hosts`.`state` AS host_state, // `glpi_plugin_monitoring_hosts`.`is_acknowledged` AS host_acknowledged // FROM `glpi_plugin_monitoring_hosts` // ".$leftjoin." // ".$where." // ".$ORDERQUERY; // // Toolbox::logInFile("pm", "Query hosts - $query\n"); // // $result = $DB->query($query); // // if (! isset($_GET["start"])) { // $_GET["start"]=0; // } // $start=$_GET['start']; // if (! isset($_GET["order"])) { // $_GET["order"]="ASC"; // } $rand = mt_rand(); if (!isset($data['data']) || !isset($data['data']['totalcount'])) { return false; } // Contruct Pager parameters $globallinkto = Toolbox::append_params(array('criteria' => Toolbox::stripslashes_deep($data['search']['criteria']), 'metacriteria' => Toolbox::stripslashes_deep($data['search']['metacriteria'])), '&'); $parameters = "sort=" . $data['search']['sort'] . "&order=" . $data['search']['order'] . '&' . $globallinkto; if (isset($_GET['_in_modal'])) { $parameters .= "&_in_modal=1"; } // If the begin of the view is before the number of items if ($data['data']['count'] > 0) { // Display pager only for HTML if ($data['display_type'] == Search::HTML_OUTPUT) { $search_config_top = ""; $search_config_bottom = ""; Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, $data['itemtype'], 0, $search_config_top); } // Define begin and end var for loop // Search case $begin_display = $data['data']['begin']; $end_display = $data['data']['end']; } echo '<div id="custom_date" style="display:none"></div>'; echo '<div id="custom_time" style="display:none"></div>'; if ($width == '') { echo "<table class='tab_cadrehov' style='width:100%;'>"; } else { echo "<table class='tab_cadrehov' style='width:" . $width . "px;'>"; } $num = 0; if (Session::haveRight("plugin_monitoring_hostcommand", CREATE)) { // Host test command ... $pmCommand = new PluginMonitoringCommand(); $a_commands = array(); $a_list = $pmCommand->find("command_name LIKE 'host_action'"); foreach ($a_list as $dt) { $host_command_name = $dt['name']; $host_command_command = $dt['command_line']; } } echo "<tr class='tab_bg_1'>"; $this->showHeaderItem(__('Entity'), 0, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); $this->showHeaderItem(__('Type'), 0, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); $this->showHeaderItem(__('Host', 'monitoring'), 1, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); $this->showHeaderItem(__('Host state'), 2, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); if (isset($host_command_name)) { echo '<th>' . __('Host action', 'monitoring') . '</th>'; } echo '<th>' . __('Host resources state', 'monitoring') . '</th>'; echo '<th>' . __('IP address', 'monitoring') . '</th>'; $this->showHeaderItem(__('Last check', 'monitoring'), 4, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); $this->showHeaderItem(__('Result details', 'monitoring'), 5, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); $this->showHeaderItem(__('Performance data', 'monitoring'), 6, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); if (Session::haveRight("plugin_monitoring_acknowledge", READ) || Session::haveRight("plugin_monitoring_downtime", READ)) { $this->showHeaderItem(__('Maintenance', 'monitoring'), 7, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost'); } echo "</tr>"; foreach ($data['data']['rows'] as $row) { // Reduced array or not ? if ($_SESSION['plugin_monitoring_reduced_interface'] && $row[3]['displayname'] == 'UP') { continue; } if (isset($host_command_name)) { $row['host_command_name'] = $host_command_name; $row['host_command_command'] = $host_command_command; } // Get all host services except if state is ok or is already acknowledged ... $a_ret = PluginMonitoringHost::getServicesState($row['id'], "`glpi_plugin_monitoring_services`.`state` != 'OK'\n AND `glpi_plugin_monitoring_services`.`is_acknowledged` = '0'"); $row['host_services_state'] = $a_ret[0]; $row['host_services_state_list'] = $a_ret[1]; // Get host first IP address $row['ip'] = __('Unknown IP address', 'monitoring'); $ip = PluginMonitoringHostaddress::getIp($row[9]['displayname'], $row[1]['displayname'], ''); if ($ip != '') { $row['ip'] = $ip; } echo "<tr class='tab_bg_3'>"; $this->displayHostLine($row); echo "</tr>"; } echo "</table>"; echo "<br/>"; Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, '', 0, $search_config_bottom); }
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; } }
static function getIp($items_id, $itemtype, $hostname) { global $DB; $networkPort = new NetworkPort(); $networkName = new NetworkName(); $iPAddress = new IPAddress(); $pmHostaddress = new PluginMonitoringHostaddress(); $ip = ''; $networkports_id = 0; $query = "SELECT * FROM `" . $pmHostaddress->getTable() . "`\n WHERE `items_id`='" . $items_id . "'\n AND `itemtype`='" . $itemtype . "'\n LIMIT 1"; $result = $DB->query($query); if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); if ($data['ipaddresses_id'] > 0) { $iPAddress->getFromDB($data['ipaddresses_id']); return $iPAddress->fields['name']; } else { $networkports_id = $data['networkports_id']; } } if ($ip == '') { if ($networkports_id > 0) { $a_listnetwork = $networkPort->find("`id`='" . $networkports_id . "'"); } else { $a_listnetwork = $networkPort->find("`itemtype`='" . $itemtype . "'\n AND `items_id`='" . $items_id . "'", "`id`"); } foreach ($a_listnetwork as $networkports_id => $datanetwork) { $a_networknames_find = current($networkName->find("`items_id`='" . $networkports_id . "'\n AND `itemtype`='NetworkPort'", "", 1)); if (isset($a_networknames_find['id'])) { $networknames_id = $a_networknames_find['id']; $a_ips_fromDB = $iPAddress->find("`itemtype`='NetworkName'\n AND `items_id`='" . $networknames_id . "'"); foreach ($a_ips_fromDB as $data) { if ($data['name'] != '' && $data['name'] != '127.0.0.1' && $data['name'] != '::1') { return $data['name']; } } } } } return $hostname; }
function manageServices($itemtype, $items_id) { if ($itemtype == 'Computer') { $pmHostaddress = new PluginMonitoringHostaddress(); $item = new $itemtype(); if ($item->can($items_id, UPDATE)) { $pmHostaddress->showForm($items_id, $itemtype); } } $pmServices = new PluginMonitoringService(); $pmServices->listByHost($itemtype, $items_id); }
@package Plugin Monitoring for GLPI @author David Durieux @co-author @comment @copyright Copyright (c) 2011-2014 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 ------------------------------------------------------------------------ */ include "../../../inc/includes.php"; Session::checkCentralAccess(); Html::header(__('Monitoring', 'monitoring'), $_SERVER["PHP_SELF"], "plugins", "monitoring", "hostaddress"); $pmHostaddress = new PluginMonitoringHostaddress(); if (isset($_POST["add"])) { $pmHostaddress->add($_POST); Html::back(); } else { if (isset($_POST["update"])) { $pmHostaddress->update($_POST); Html::back(); } else { if (isset($_POST["delete"])) { $pmHostaddress->delete($_POST); Html::back(); } } } Html::footer();
static function getIp($items_id, $itemtype, $hostname) { global $DB; $networkPort = new NetworkPort(); $pmHostaddress = new PluginMonitoringHostaddress(); $ip = $hostname; if ($itemtype == 'NetworkEquipment') { $class = new $itemtype(); $class->getFromDB($items_id); if ($class->fields['ip'] != '') { $ip = $class->fields['ip']; } } else { $query = "SELECT * FROM `" . $pmHostaddress->getTable() . "`\n WHERE `items_id`='" . $items_id . "'\n AND `itemtype`='" . $itemtype . "'\n LIMIT 1"; $result = $DB->query($query); if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $pmHostaddress->getFromDB($data['id']); $networkPort->getFromDB($pmHostaddress->fields['networkports_id']); $ip = $networkPort->fields['ip']; } else { $a_listnetwork = $networkPort->find("`itemtype`='" . $itemtype . "'\n AND `items_id`='" . $items_id . "'", "`id`"); foreach ($a_listnetwork as $datanetwork) { if ($datanetwork['ip'] != '' and $datanetwork['ip'] != '127.0.0.1' and $ip != '') { $ip = $datanetwork['ip']; break; } } } } return $ip; }
You should have received a copy of the GNU Affero General Public License along with Monitoring. If not, see <http://www.gnu.org/licenses/>. ------------------------------------------------------------------------ @package Plugin Monitoring for GLPI @author David Durieux @co-author @comment @copyright Copyright (c) 2011-2013 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 2013 ------------------------------------------------------------------------ */ if (strpos($_SERVER['PHP_SELF'], "dropdownipaddress.php")) { include "../../../inc/includes.php"; header("Content-Type: text/html; charset=UTF-8"); Html::header_nocache(); } Session::checkCentralAccess(); if (!isset($_POST['ipaddresses_id'])) { $_POST['ipaddresses_id'] = 0; } if (!isset($_POST['networkports_id'])) { $_POST['networkports_id'] = 0; } PluginMonitoringHostaddress::dropdownIP($_POST['ipaddresses_id'], $_POST['networkports_id']);
function generateHostsCfg($file = 0, $tag = '') { global $DB; $pmCommand = new PluginMonitoringCommand(); $pmCheck = new PluginMonitoringCheck(); $pmComponent = new PluginMonitoringComponent(); $pmEntity = new PluginMonitoringEntity(); $pmHostconfig = new PluginMonitoringHostconfig(); $pmHost = new PluginMonitoringHost(); $calendar = new Calendar(); $pmRealm = new PluginMonitoringRealm(); $networkEquipment = new NetworkEquipment(); $pmContact_Item = new PluginMonitoringContact_Item(); $profile_User = new Profile_User(); $user = new User(); $a_hosts = array(); $i = 0; $a_parents_found = array(); $a_hosts_found = array(); $a_entities_allowed = $pmEntity->getEntitiesByTag($tag); // * 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; } $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n GROUP BY `itemtype`, `items_id`"; $result = $DB->query($query); while ($data = $DB->fetch_array($result)) { $classname = $data['itemtype']; $class = new $classname(); if ($class->getFromDB($data['items_id'])) { if (isset($a_entities_allowed['-1']) or isset($a_entities_allowed[$class->fields['entities_id']])) { $a_hosts[$i]['host_name'] = $classname . "-" . $data['items_id'] . "-" . preg_replace("/[^A-Za-z0-9]/", "", $class->fields['name']); $a_hosts_found[$a_hosts[$i]['host_name']] = 1; $a_hosts[$i]['alias'] = preg_replace("/[^A-Za-z0-9]/", "", $class->fields['name']) . " / " . $classname . "-" . $data['items_id']; if (isset($class->fields['networkequipmenttypes_id'])) { if ($class->fields['networkequipmenttypes_id'] > 0) { $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_networkequipmenttypes", $class->fields['networkequipmenttypes_id']) . ")"; } } else { if (isset($class->fields['computertypes_id'])) { if ($class->fields['computertypes_id'] > 0) { $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_computertypes", $class->fields['computertypes_id']) . ")"; } } else { if (isset($class->fields['printertypes_id'])) { if ($class->fields['printertypes_id'] > 0) { $a_hosts[$i]['alias'] .= " (" . Dropdown::getDropdownName("glpi_printertypes", $class->fields['printertypes_id']) . ")"; } } } } $ip = PluginMonitoringHostaddress::getIp($data['items_id'], $data['itemtype'], $class->fields['name']); $a_hosts[$i]['address'] = $ip; // Manage dependencies $parent = ''; if ($data['itemtype'] != 'NetworkEquipment') { $networkPort = new NetworkPort(); $a_networkports = $networkPort->find("`itemtype`='" . $data['itemtype'] . "'\n AND `items_id`='" . $data['items_id'] . "'"); foreach ($a_networkports as $data_n) { $networkports_id = $networkPort->getContact($data_n['id']); if ($networkports_id) { $networkPort->getFromDB($networkports_id); if ($networkPort->fields['itemtype'] == 'NetworkEquipment') { $networkEquipment->getFromDB($networkPort->fields['items_id']); $parent = 'NetworkEquipment-' . $networkPort->fields['items_id'] . '-' . preg_replace("/[^A-Za-z0-9]/", "", $networkEquipment->fields['name']); $a_parents_found[$parent] = 1; $pmHost->updateDependencies($classname, $data['items_id'], 'NetworkEquipment-' . $networkPort->fields['items_id']); } } } } $a_hosts[$i]['parents'] = $parent; $a_fields = array(); $pmCommand->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_commands_id', $class->fields['entities_id'], $classname, $class->getID())); $a_component = current($pmComponent->find("`plugin_monitoring_commands_id`='" . $pmCommand->fields['id'] . "'", "", 1)); $a_fields = $a_component; $a_hosts[$i]['check_command'] = $pmCommand->fields['command_name']; $pmCheck->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_checks_id', $class->fields['entities_id'], $classname, $class->getID())); $a_hosts[$i]['check_interval'] = $pmCheck->fields['check_interval']; $a_hosts[$i]['retry_interval'] = $pmCheck->fields['retry_interval']; $a_hosts[$i]['max_check_attempts'] = $pmCheck->fields['max_check_attempts']; if ($calendar->getFromDB($pmHostconfig->getValueAncestor('calendars_id', $class->fields['entities_id'], $classname, $class->getID()))) { $a_hosts[$i]['check_period'] = $calendar->fields['name']; } else { $a_hosts[$i]['check_period'] = "24x7"; } $pmRealm->getFromDB($pmHostconfig->getValueAncestor('plugin_monitoring_realms_id', $class->fields['entities_id'], $classname, $class->getID())); $a_hosts[$i]['realm'] = $pmRealm->fields['name']; $a_hosts[$i]['process_perf_data'] = '1'; $a_hosts[$i]['notification_interval'] = '30'; // For contact check if a service with this component $a_hosts[$i]['contacts'] = ''; $querycont = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n LEFT JOIN `glpi_plugin_monitoring_services`\n ON `plugin_monitoring_componentscatalogs_hosts_id`\n = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n WHERE `plugin_monitoring_components_id`='" . $a_component['id'] . "'\n AND `items_id`='" . $data['items_id'] . "'\n AND `itemtype`='" . $data['itemtype'] . "'\n LIMIT 1"; $resultcont = $DB->query($querycont); if ($DB->numrows($resultcont) != 0) { $a_componentscatalogs_hosts = $DB->fetch_assoc($resultcont); // Notification interval $pmComponentscatalog = new PluginMonitoringComponentscatalog(); $pmComponentscatalog->getFromDB($a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']); $a_hosts[$i]['notification_interval'] = $pmComponentscatalog->fields['notification_interval']; $a_contacts = array(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n AND `items_id`='" . $a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id'] . "'"); foreach ($a_list_contact as $data_contact) { if (isset($a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) { if (in_array($class->fields['entities_id'], $a_contacts_entities[$a_componentscatalogs_hosts['plugin_monitoring_componentscalalog_id']][$data_contact['users_id']])) { $user->getFromDB($data_contact['users_id']); $a_contacts[] = $user->fields['name']; } } } if (count($a_contacts) > 0) { $a_contacts_unique = array_unique($a_contacts); $a_hosts[$i]['contacts'] = implode(',', $a_contacts_unique); } } if ($calendar->getFromDB($a_fields['calendars_id'])) { $a_hosts[$i]['notification_period'] = $calendar->fields['name']; } else { $a_hosts[$i]['notification_period'] = "24x7"; } $a_hosts[$i]['notification_options'] = 'd,u,r'; $i++; } } } // Check if parents all exist in hosts config foreach ($a_parents_found as $host => $num) { if (!isset($a_hosts_found[$host])) { // Delete parents not added in hosts config foreach ($a_hosts as $id => $data) { if ($data['parents'] == $host) { $a_hosts[$id]['parents'] = ''; } } } } if ($file == "1") { $config = "# Generated by plugin monitoring for GLPI\n# on " . date("Y-m-d H:i:s") . "\n\n"; foreach ($a_hosts as $data) { $config .= $this->constructFile("host", $data); } return array('hosts.cfg', $config); } else { return $a_hosts; } }
function generateServicesCfg($file = 0, $tag = '') { global $DB; $pMonitoringCommand = new PluginMonitoringCommand(); $pmEventhandler = new PluginMonitoringEventhandler(); $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; } // Groups $group = new Group(); $a_list_contact = $pmContact_Item->find("`itemtype`='PluginMonitoringComponentscatalog'\n 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`\n 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); $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("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]]")) { $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'] != '') { $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'] = "check_nrpe!" . $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; } // * 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']; } } // * 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 ($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`\n 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'; 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'; 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'; } $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; } }