function sendDowntime($host_id = -1, $service_id = -1, $author = '', $comment = '', $flexible = '0', $start_time = '0', $end_time = '0', $duration = '3600', $operation = '') { global $DB; if ($host_id == -1 && $service_id == -1) { return false; } $pmTag = new PluginMonitoringTag(); $pmService = new PluginMonitoringService(); $pmService->getFromDB($service_id); $service_description = $pmService->getName(array('shinken' => '1')); $pmHost = new PluginMonitoringHost(); $pmHost->getFromDB($host_id == -1 ? $pmService->getHostID() : $host_id); $hostname = $pmHost->getName(true); // Downtime an host ... $acknowledgeServiceOnly = true; $a_fields = array(); if ($host_id == -1) { $tag = PluginMonitoringEntity::getTagByEntities($pmService->getEntityID()); } else { // ... one service of the host. $tag = PluginMonitoringEntity::getTagByEntities($pmHost->getEntityID()); } $ip = $pmTag->getIP($tag); $auth = $pmTag->getAuth($tag); $port = $pmTag->getPort($tag); $url = 'http://' . $ip . ':' . $port . '/'; $action = 'downtime'; $a_fields = array('action' => empty($operation) ? 'add' : $operation, 'host_name' => $hostname, 'service_description' => $service_description, 'author' => $author, 'comment' => mb_convert_encoding($comment, "iso-8859-1"), 'flexible' => $flexible, 'start_time' => PluginMonitoringServiceevent::convert_datetime_timestamp($start_time), 'end_time' => PluginMonitoringServiceevent::convert_datetime_timestamp($end_time), 'trigger_id' => '0', 'duration' => $duration); // Send downtime command ... return $this->sendCommand($url, $action, $a_fields, '', $auth); }
function parseToDB($plugin_monitoring_services_id) { global $DB; $pmService = new PluginMonitoringService(); $pmComponent = new PluginMonitoringComponent(); if ($pmService->getFromDB($plugin_monitoring_services_id)) { $pmComponent->getFromDB($pmService->fields['plugin_monitoring_components_id']); if (!isset($pmComponent->fields['plugin_monitoring_commands_id'])) { return; } if (is_null($pmComponent->fields['graph_template'])) { return; } $pmUnavaibility = new PluginMonitoringUnavaibility(); $pmUnavaibility->runUnavaibility($plugin_monitoring_services_id); $a_dateold = array(); // *** 1 day (x 30 min) $dateold = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m'), date('d') - 1, date('Y'))); $a_dateold['30m'] = $dateold; // Gest last value, and see if we must calculate new values $query = "SELECT * FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='30m'\n ORDER BY `date` DESC\n LIMIT 1"; $result = $DB->query($query); $lastdate = $dateold; $new_date = ''; if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $new_date = PluginMonitoringServiceevent::convert_datetime_timestamp($data['date']); $new_date += 30 * 60; } else { $split = explode(" ", $lastdate); $a_time = explode(":", $split[1]); $a_date = explode("-", $split[0]); $new_date = date('U', mktime($a_time[0], '0', '0', $a_date[1], $a_date[2], $a_date[0])); } //get data in serviceevents for each 30 minutes from this date to now while ($new_date + 15 * 60 < date('U')) { $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `date` >= '" . date('Y-m-d H:i:m', $new_date - 15 * 60) . "'\n AND`date` <= '" . date('Y-m-d H:i:m', $new_date + 15 * 60) . "'\n ORDER BY `date`"; $result = $DB->query($query); // get data $mydatat = array(); $pmServiceevent = new PluginMonitoringServiceevent(); $pmService = new PluginMonitoringService(); $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template']); if (is_array($ret)) { $mydatat = $ret[0]; } $array_data = array(); foreach ($mydatat as $name => $a_values) { $valfloat = array_sum($a_values) / count($a_values); if ($valfloat > 2) { $array_data[$name] = round($valfloat); } else { $array_data[$name] = round($valfloat, 2); } } $input = array(); $input['plugin_monitoring_services_id'] = $plugin_monitoring_services_id; $input['date'] = date('Y-m-d H:i:s', $new_date); $input['data'] = exportArrayToDB($array_data); $input['type'] = '30m'; $this->add($input); $new_date = $new_date + 30 * 60; } // *** 1w (x 6 hours) $dateold = date('Y-m-d H:i:s', date('U') - 7 * 24 * 3600); $a_dateold['6h'] = $dateold; // Gest last value, and see if we must calculate new values $query = "SELECT * FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='6h'\n ORDER BY `date` DESC\n LIMIT 1"; $result = $DB->query($query); $lastdate = $dateold; $new_date = ''; if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $new_date = PluginMonitoringServiceevent::convert_datetime_timestamp($data['date']); $new_date += 6 * 3600; } else { $split = explode(" ", $lastdate); $a_time = explode(":", $split[1]); $a_date = explode("-", $split[0]); $hour = '0'; $addtime = 0; if ($a_time[0] > 18) { $hour = 0; $addtime = 24 * 3600; } else { if ($a_time[0] > 12) { $hour = 18; } else { if ($a_time[0] > 6) { $hour = 12; } else { $hour = 6; } } } $new_date = date('U', mktime($hour, '0', '0', $a_date[1], $a_date[2], $a_date[0])) + $addtime; } //get data in serviceevents for each 6 hours from this date to now while ($new_date + 3 * 3600 < date('U')) { $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `date` >= '" . date('Y-m-d H:i:m', $new_date - 3 * 3600) . "'\n AND`date` <= '" . date('Y-m-d H:i:m', $new_date + 3 * 3600) . "'\n ORDER BY `date`"; $result = $DB->query($query); // get data $mydatat = array(); $pmServiceevent = new PluginMonitoringServiceevent(); $pmService = new PluginMonitoringService(); $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template']); if (is_array($ret)) { $mydatat = $ret[0]; } $array_data = array(); foreach ($mydatat as $name => $a_values) { $valfloat = array_sum($a_values) / count($a_values); if ($valfloat > 2) { $array_data[$name] = round($valfloat); } else { $array_data[$name] = round($valfloat, 2); } } $input = array(); $input['plugin_monitoring_services_id'] = $plugin_monitoring_services_id; $input['date'] = date('Y-m-d H:i:s', $new_date); $input['data'] = exportArrayToDB($array_data); $input['type'] = '6h'; $this->add($input); $new_date = $new_date + 6 * 3600; } // *** 1m (x 1 day) $dateold = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m') - 1, date('d'), date('Y'))); // $a_dateold['1d'] = $dateold; //NOTE to keep data for each day (if we want use it in future) // Gest last value, and see if we must calculate new values $query = "SELECT * FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='1d'\n ORDER BY `date` DESC\n LIMIT 1"; $result = $DB->query($query); $lastdate = $dateold; $new_date = ''; if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $new_date = PluginMonitoringServiceevent::convert_datetime_timestamp($data['date']); $new_date += 24 * 3600; } else { $split = explode(" ", $lastdate); $a_date = explode("-", $split[0]); $new_date = date('U', mktime('0', '0', '0', $a_date[1], $a_date[2], $a_date[0])); } //get data in serviceevents for each 6 hours from this date to now while ($new_date + 12 * 3600 < date('U')) { $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `date` >= '" . date('Y-m-d H:i:m', $new_date - 12 * 3600) . "'\n AND`date` <= '" . date('Y-m-d H:i:m', $new_date + 12 * 3600) . "'\n ORDER BY `date`"; $result = $DB->query($query); // get data $mydatat = array(); $pmServiceevent = new PluginMonitoringServiceevent(); $pmService = new PluginMonitoringService(); $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template']); if (is_array($ret)) { $mydatat = $ret[0]; } $array_data = array(); foreach ($mydatat as $name => $a_values) { $valfloat = array_sum($a_values) / count($a_values); if ($valfloat > 2) { $array_data[$name] = round($valfloat); } else { $array_data[$name] = round($valfloat, 2); } } $input = array(); $input['plugin_monitoring_services_id'] = $plugin_monitoring_services_id; $input['date'] = date('Y-m-d H:i:s', $new_date); $input['data'] = exportArrayToDB($array_data); $input['type'] = '1d'; $this->add($input); $new_date = $new_date + 24 * 3600; } // *** 6m (x 5 days) $dateold = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m') - 6, date('d'), date('Y'))); $a_dateold['5d'] = $dateold; // Gest last value, and see if we must calculate new values $query = "SELECT * FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='5d'\n ORDER BY `date` DESC\n LIMIT 1"; $result = $DB->query($query); $lastdate = $dateold; $new_date = ''; if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $new_date = PluginMonitoringServiceevent::convert_datetime_timestamp($data['date']); $new_date += 5 * 24 * 3600; } else { $split = explode(" ", $lastdate); $a_date = explode("-", $split[0]); $new_date = date('U', mktime('0', '0', '0', $a_date[1], $a_date[2], $a_date[0])); } //get data in serviceevents for each 6 hours from this date to now while ($new_date + 5 * 24 * 3600 / 2 < date('U')) { $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `date` >= '" . date('Y-m-d H:i:m', $new_date - 5 * 24 * 3600 / 2) . "'\n AND`date` <= '" . date('Y-m-d H:i:m', $new_date + 5 * 24 * 3600 / 2) . "'\n ORDER BY `date`"; $result = $DB->query($query); // get data $mydatat = array(); $pmServiceevent = new PluginMonitoringServiceevent(); $pmService = new PluginMonitoringService(); $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template']); if (is_array($ret)) { $mydatat = $ret[0]; } $array_data = array(); foreach ($mydatat as $name => $a_values) { $valfloat = array_sum($a_values) / count($a_values); if ($valfloat > 2) { $array_data[$name] = round($valfloat); } else { $array_data[$name] = round($valfloat, 2); } } $input = array(); $input['plugin_monitoring_services_id'] = $plugin_monitoring_services_id; $input['date'] = date('Y-m-d H:i:s', $new_date); $input['data'] = exportArrayToDB($array_data); $input['type'] = '5d'; $this->add($input); $new_date = $new_date + 5 * 24 * 3600; } // *** 1y (x 10 days) $dateold = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m') - 10, date('d'), date('Y'))); $a_dateold['5d'] = $dateold; // Gest last value, and see if we must calculate new values $query = "SELECT * FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='10d'\n ORDER BY `date` DESC\n LIMIT 1"; $result = $DB->query($query); $lastdate = $dateold; $new_date = ''; if ($DB->numrows($result) == '1') { $data = $DB->fetch_assoc($result); $new_date = PluginMonitoringServiceevent::convert_datetime_timestamp($data['date']); $new_date += 10 * 24 * 3600; } else { $split = explode(" ", $lastdate); $a_date = explode("-", $split[0]); $new_date = date('U', mktime('0', '0', '0', $a_date[1], $a_date[2], $a_date[0])); } //get data in serviceevents for each 6 hours from this date to now while ($new_date + 5 * 24 * 3600 < date('U')) { $query = "SELECT * FROM `glpi_plugin_monitoring_serviceevents`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `date` >= '" . date('Y-m-d H:i:m', $new_date - 5 * 24 * 3600) . "'\n AND`date` <= '" . date('Y-m-d H:i:m', $new_date + 5 * 24 * 3600) . "'\n ORDER BY `date`"; $result = $DB->query($query); // get data $mydatat = array(); $pmServiceevent = new PluginMonitoringServiceevent(); $pmService = new PluginMonitoringService(); $ret = $pmServiceevent->getData($result, $pmComponent->fields['graph_template']); if (is_array($ret)) { $mydatat = $ret[0]; } $array_data = array(); foreach ($mydatat as $name => $a_values) { $valfloat = array_sum($a_values) / count($a_values); if ($valfloat > 2) { $array_data[$name] = round($valfloat); } else { $array_data[$name] = round($valfloat, 2); } } $input = array(); $input['plugin_monitoring_services_id'] = $plugin_monitoring_services_id; $input['date'] = date('Y-m-d H:i:s', $new_date); $input['data'] = exportArrayToDB($array_data); $input['type'] = '10d'; $this->add($input); $new_date = $new_date + 10 * 24 * 3600; } // *** Delete old values foreach ($a_dateold as $name => $date) { $query = "DELETE FROM `" . $this->getTable() . "`\n WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n AND `type`='" . $name . "'\n AND `date` < '" . $date . "'"; $result = $DB->query($query); } } }