/** * Devolver la consulta a realizar en el socket para los servicios * * @param array $fields * @return string */ protected function getServicesFilter(array $fields) { if ($this->isAllHeaders() === false) { $filter = array('GET services', 'ResponseHeader: fixed16', 'Filter: host_checks_enabled = 1', 'Filter: state != ' . SERVICE_OK, 'Filter: last_hard_state_change > ' . (time() - Config::getConfig()->getNewItemTime() / 2), 'Filter: is_flapping = 1', 'Or: 3', 'Columns: ' . implode(' ', $fields), 'ColumnHeaders: off', 'OutputFormat: json'); $dataQuery = implode("\n", $filter) . "\n\n"; } else { $dataQuery = "GET services\nFilter: state != " . SERVICE_OK . "\nFilter: host_checks_enabled = 1\nColumnHeaders: off\nOutputFormat: json\n\n"; } return $dataQuery; }
/** * Cargar la configuración */ private static function loadConfig() { try { Config::loadConfig(new XmlHandler(XML_CONFIG_FILE)); } catch (\Exception $e) { error_log(Language::t($e->getMessage())); Session::setConfig(new ConfigData()); if (self::getCurrentScript() !== 'config.php') { header('Location: config.php'); } } }
/** * Función para obtener Información de la BD de NagiosQL * * @return array|bool */ public static function getHostsDBInfo() { $mysqli = new mysqli(Config::getConfig()->getDbServer(), Config::getConfig()->getDbUser(), Config::getConfig()->getDbUserPass(), Config::getConfig()->getDbName()); if ($mysqli->connect_errno) { error_log('(' . __FUNCTION__ . ') Fallo al conectar a MySQL: ' . $mysqli->connect_error); return false; } if (!($resQuery = $mysqli->query("SELECT host_name,alias FROM tbl_host"))) { error_log('(' . __FUNCTION__ . ') Fallo al obtener los registros: ' . $mysqli->connect_error); return false; } $result = array(); while ($row = $resQuery->fetch_assoc()) { $result[$row['host_name']] = $row['alias']; } // Devolvemos un array con los registros. // La clave es el nombre corto del host y el valor es el alias return $result; }
$ConfigData->setLanguage($siteLanguage); $ConfigData->setPageTitle($siteTitle); $ConfigData->setRefreshValue($eventRefresh); $ConfigData->setNewItemTime($eventNewItemTime); $ConfigData->setMaxDisplayItems($eventMaxItems); $ConfigData->setNewItemAudioEnabled($eventNewItemAudio); $ConfigData->setColLastcheck($showColLastCheck); $ConfigData->setColHost($showColHost); $ConfigData->setColService($showColService); $ConfigData->setColStatusInfo($showColInfo); $ConfigData->setColBackend($showColBackend); $ConfigData->setShowScheduled($showScheduled); $ConfigData->setRegexHostShow($regexHostShow); $ConfigData->setRegexServiceNoShow($regexServicesNoShow); $ConfigData->setCriticalItems(explode(',', $criticalItems)); $ConfigData->setBackend($Backends); $ConfigData->setClientURL($specialClientURL); $ConfigData->setRemoteServer($specialRemoteServerURL); $ConfigData->setMonitorServerUrl($specialMonitorServerUrl); $ConfigData->setAPIToken($specialAPIToken); if (!empty($specialConfigPass) && $specialConfigPass !== (string) Session::getConfig()->getConfigPassword()) { $ConfigData->setConfigPassword(sha1($specialConfigPass)); } else { $ConfigData->setConfigPassword($specialConfigPass); } try { Config::saveConfig(new XmlHandler(XML_CONFIG_FILE), $ConfigData); Response::printJSON('Configuración guardada', 0); } catch (Exception $e) { Response::printJSON('Error al guardar la configuración'); }
/** * Filtro para determinar qué rawItems devolver * * @param $item * @return bool */ private function checkFilter($item) { return $item['current_state'] != 0 || $item['last_hard_state_change'] > time() - Config::getConfig()->getNewItemTime() / 2 || $item['is_flapping'] === 1; }
/** * Comprobar si la configuración se ha actualizado * * @return bool */ public static function checkConfigRefresh() { return time() - Session::getConfigTime() <= Config::getConfig()->getRefreshValue(); }
/** * Función para mostrar los elementos del Dashboard * * @param EventInterface $item El elemento que contiene los datos. * @param bool $newItem Si es un nuevo elemento * @param bool $newItemUp Si es un nuevo elemento recuperado * @return EventInterface */ private function getHtmlItems(EventInterface $item, $newItem = false, $newItemUp = false) { $lastStateTime = date("m-d-Y H:i:s", $item->getLastHardStateChange()); $lastStateDuration = Util::timeElapsed(time() - $item->getLastHardStateChange()); $lastCheckDuration = Util::timeElapsed(time() - $item->getLastCheck()); $serviceDesc = $item->getDisplayName() ? $item->getDisplayName() : $item->getCheckCommand(); $hostname = $item->getHostDisplayName() ? $item->getHostDisplayName() : $item->getDisplayName(); $hostAlias = $item->getHostAlias() ? $item->getHostAlias() : ($item->getAlias() ? $item->getAlias() : $hostname); $scheduled = $item->getScheduledDowntimeDepth() >= 1 || $item->getHostScheduledDowntimeDepth() >= 1; $tdClass = ''; $trClass = EventState::getStateClass($item); $statusName = EventState::getStateName($item); $link = null; if ($item->getHostLastTimeUnreachable() > $item->getHostLastTimeUp() && !$newItemUp || $item->getLastTimeUnreachable() > $item->getLastCheck() && $item->getStateType() === 1) { $trClass = EventState::getStateClass($item, EventStateInterface::STATE_UNREACHABLE); $statusName = EventState::getStateName($item, EventStateInterface::STATE_UNREACHABLE); } if ($scheduled) { $trClass = EventState::getStateClass($item, EventStateInterface::STATE_SCHEDULED); $statusName = EventState::getStateName($item, EventStateInterface::STATE_SCHEDULED); } if ($newItem && !$item->isAcknowledged() && !$scheduled && !$newItemUp) { $tdClass = "new"; } elseif ($newItemUp && time() - $item->getLastHardStateChange() <= Config::getConfig()->getNewItemTime() / 2) { $trClass = EventState::getStateClass($item, EventStateInterface::STATE_RECOVER); $statusName = EventState::getStateName($item, EventStateInterface::STATE_RECOVER); } elseif ($item->isFlapping()) { $trClass = EventState::getStateClass($item, EventStateInterface::STATE_FLAPPING); $statusName = EventState::getStateName($item, EventStateInterface::STATE_FLAPPING); } elseif ($item->isAcknowledged()) { $trClass = EventState::getStateClass($item, EventStateInterface::STATE_ACKNOWLEDGED); $statusName = EventState::getStateName($item, EventStateInterface::STATE_ACKNOWLEDGED); } $line = '<tr class="item-data ' . $trClass . '" title="' . sprintf(Language::t('Estado %s desde %s'), $statusName, $lastStateTime) . '">' . PHP_EOL; $line .= '<td class="center">' . $statusName . '</td>'; if (Config::getConfig()->isColLastcheck()) { $line .= '<td title="' . sprintf('%s : %s', Language::t('Último check'), $lastCheckDuration) . '" class="center ' . $tdClass . '">' . $lastStateDuration . '</td>' . PHP_EOL; } if (Config::getConfig()->isColHost()) { if (!is_null($link)) { $line .= '<td><a href="' . $link . '" target="blank" title="' . $hostname . '">' . $hostAlias . '</a></td>' . PHP_EOL; } else { $line .= '<td>' . $hostAlias . '</td>' . PHP_EOL; } } if (Config::getConfig()->isColStatusInfo()) { if ($item->getFilterStatus() === '' || $newItem) { $line .= '<td class="statusinfo">' . $item->getPluginOutput() . '</td>' . PHP_EOL; } else { $line .= '<td class="statusinfo">' . $item->getPluginOutput() . '<br>Filter: ' . $item->getFilterStatus() . '</td>' . PHP_EOL; } } if (Config::getConfig()->isColService()) { $line .= '<td class="center">' . $serviceDesc . '</td>' . PHP_EOL; } if (Config::getConfig()->isColBackend()) { $line .= '<td class="center">' . $item->getBackendAlias() . '</td>' . PHP_EOL; } $line .= '</tr>' . PHP_EOL; return $line; }
?> </td> <td><?php echo date('d-m-Y H:i', $downtime->getStartTime()), ' → ', date('d-m-Y H:i', $downtime->getEndTime()); ?> </td> <td><?php echo $downtime->getAuthor(); ?> </td> <td><?php echo $downtime->getComment(); ?> </td> <?php if (Config::getConfig()->isColBackend()) { ?> <td><?php echo $downtime->getBackendAlias(); ?> </td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php
<input type="text" id="special_monitor_server_url" name="special_monitor_server_url" value="<?php echo Config::getConfig()->getMonitorServerUrl(); ?> " placeholder="http://cloud.foo.bar/icinga"/> </div> <div class="pure-control-group"> <label for="special_api_token"><?php echo Language::t('Token API'); ?> </label> <input type="text" id="special_api_token" name="special_api_token" value="<?php echo Config::getConfig()->getAPIToken(); ?> " placeholder=""/> <button class="btn-gen-token pure-button" type="button" title="<?php echo Language::t('Generar Token'); ?> " data-dst="special_api_token"> <i class="fa fa-refresh"></i> </button> </div> <div class="pure-control-group"> <label for="special_config_pass"><?php
/** * Comprobar el token de seguridad * * @param $token * @return bool */ public function checkToken($token) { return $token === Config::getConfig()->getAPIToken(); }
/** * Obtener los triggers en estado OK * * @return array|Event\EventInterface[] */ protected function getTriggersOk() { $this->getScheduledDowntimes(); $params = array('groupids' => null, 'hostids' => null, 'monitored' => true, 'filter' => array('value' => 0, 'lastChangeSince' => time() - Config::getConfig()->getNewItemTime() / 2), 'skipDependent' => true, 'expandDescription' => true, 'output' => array('triggerid', 'state', 'status', 'error', 'url', 'expression', 'description', 'priority', 'lastchange', 'value'), 'selectHosts' => array('hostid', 'name', 'maintenance_status', 'errors_from'), 'selectLastEvent' => array('eventid', 'acknowledged', 'objectid', 'clock', 'ns', 'value'), 'sortfield' => array('lastchange'), 'sortorder' => array('DESC'), 'limit' => Config::getConfig()->getMaxDisplayItems()); $triggers = $this->Zabbix->triggerGet($params); $events = array(); foreach ($triggers as $event) { foreach ($event->hosts as $host) { $Event = new Trigger(); $Event->setState($host->value); $Event->setStateType($event->state); $Event->setAcknowledged(intval($event->lastEvent->acknowledged)); $Event->setHostDisplayName($host->name); $Event->setDisplayName($host->name); $Event->setCheckCommand($event->triggerid); $Event->setPluginOutput($event->description); $Event->setLastCheck($event->lastchange); $Event->setLastTimeUnreachable($host->errors_from); $Event->setLastHardStateChange($event->lastchange); $Event->setLastHardState($event->lastchange); $Event->setActiveChecksEnabled($event->status); $Event->setScheduledDowntimeDepth($host->maintenance_status); $Event->setCurrentAttempt($event->value); $Event->setNotificationsEnabled(true); $Event->setBackendAlias($this->backend->getAlias()); $Event->setBackendUrl($this->backend->getUrl()); $Event->setBackendLevel($this->backend->getLevel()); $events[] = $Event; } } return $events; }
echo Config::getConfig()->getRemoteServer(); ?> '); config.setAjaxFile('<?php echo $ajaxFile; ?> '); config.setScroll(<?php echo $scroll ? 'true' : 'false'; ?> ); config.setTimeout(<?php echo $timeout; ?> ); config.setLang('<?php echo Language::t('Error al obtener los eventos de monitorización'); ?> '); config.setAudioEnabled(<?php echo Config::getConfig()->isNewItemAudioEnabled() ? 'true' : 'false'; ?> ); smd.setConfig(config); smd.startSMD(); smd.getUpdates(); }()); </script> </body> </html>
/** * Comprobar si es necesario enviar las cabeceras CORS */ public static function checkCORS() { $clientURL = Config::getConfig()->getClientURL(); if (!empty($clientURL)) { header('Access-Control-Allow-Origin: ' . $clientURL); header('Access-Control-Request-Method: GET'); Util::checkRefreshSession(); } }