/** * Agrupar las paradas programadas para aquellos hosts que tengan mas de X servicios programados. * * @return array Con los eventos programados */ public function getScheduledDowntimesGroupped() { $groupCount = 5; $hosts = array(); $out = array(); $downtimes = Util::arraySortByProperty($this->getScheduledDowntimes(), 'start_time', false); // Recorrer el array de eventos y contabilizar el número de veces que aparece cada host foreach ($downtimes as $downtime) { /** @var $downtime DowntimeInterface */ // Incrementar el contador y silenciar avisos @$hosts[$downtime->getHostHash()]['count']++; } // Recorrer el array de eventos y agrupar aquellos eventos de un host que se repitan más de $groupCount. // Se utiliza un nuevo array con la clave el nombre del host foreach ($downtimes as $downtime) { /** @var $downtime DowntimeInterface */ $hash = $downtime->getHostHash(); $hostCounter = $hosts[$hash]['count']; if ($hostCounter > $groupCount) { $downtime->setServiceDisplayName(sprintf(Language::t('Programado para %d servicios'), $hostCounter)); $out[$hash] = $downtime; } elseif (!isset($out[$hash])) { $out[$hash] = $downtime; } } return $out; }
/** * Cargar la configuración desde el archivo * * @param StorageInterface $Storage */ public static function loadConfig(StorageInterface $Storage) { if (Util::checkReload() || gettype(Session::getConfig()) !== 'object' || time() >= Session::getConfigTime() + self::TIMEOUT_REFRESH) { Session::setConfig(self::arrayMapper($Storage)); Session::setConfigTime(time()); } }
/** * Obtener los datos remotos desde la API de sysMonDash con CURL * * Devuelve los datos deserializados * * @param $url * @return mixed * @throws \Exception */ protected function getRemoteData($url) { $data = json_decode(Util::getDataFromUrl($url)); if (is_object($data) && isset($data->status) && $data->status === 1) { $msg = sprintf('%s (%s): %s', $this->getBackend()->getAlias(), 'SMD', $data->description); error_log($msg); throw new BackendException($msg); } elseif (!is_object($data)) { $msg = sprintf('%s (%s): %s', $this->getBackend()->getAlias(), 'SMD', Language::t('Error al acceder a la API')); error_log($msg); throw new BackendException($msg); } return unserialize(base64_decode($data->data)); }
use SMD\Api\Api; use SMD\Core\Init; use SMD\Http\Request; use SMD\Http\Response; use SMD\Util\Json; define('APP_ROOT', '.'); require APP_ROOT . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'Base.php'; Init::start(); $apiToken = Request::analyze('token'); $action = Request::analyze('action', 0); $Api = new Api(); if (!$Api->checkToken($apiToken)) { Response::printJSON('Token inválido'); } $data = null; switch ($action) { case Api::ACTION_EVENTS: $data = $Api->getEvents(); break; case Api::ACTION_DOWNTIMES: $data = $Api->getDowntimes(); break; case Api::ACTION_CHECK: Response::printJSON('V ' . implode('.', \SMD\Util\Util::getVersion(true)), 0); break; default: Response::printJSON('Petición inválida'); } header('Content-type: application/json'); $json = array('status' => 0, 'data' => $data, 'action' => $action); die(Json::getJson($json));
* */ use SMD\Backend\BackendInterface; use SMD\Core\Init; use SMD\Core\sysMonDash; use SMD\Util\Util; define('APP_ROOT', '.'); require APP_ROOT . DIRECTORY_SEPARATOR . 'Base.php'; Init::start(); $raw = \SMD\Http\Request::analyze('raw', 0); $allHeaders = \SMD\Http\Request::analyze('allheaders', false, false, true); echo '<pre>'; if ($raw === 1) { $SMD = new sysMonDash(); foreach ($SMD->getBackends() as $Backend) { try { /** @var BackendInterface $Backend */ $Backend->setAllHeaders($allHeaders); echo 'Backend: ', $Backend->getBackend()->getAlias(), PHP_EOL; echo 'Hosts', PHP_EOL; print_r(Util::arraySortByProperty($Backend->getHostsProblems(), 'lastHardStateChange')); echo 'Services', PHP_EOL; print_r(Util::arraySortByProperty($Backend->getServicesProblems(), 'lastHardStateChange')); echo 'Downtimes', PHP_EOL; print_r(Util::arraySortByProperty($Backend->getScheduledDowntimes(), 'startTime', false)); } catch (Exception $e) { echo 'ERROR: ' . $Backend->getBackend()->getAlias() . ': ' . $e->getMessage(); } } } echo '</pre>';
/** * 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; }
?> <div class="title"><?php echo Language::t('Errores'); ?> </div> <?php foreach ($SMD->getErrors() as $error) { ?> <div id="nomessages" class="full error"> <?php echo Language::t($error); ?> </div> <?php } } ?> <?php ob_end_flush(); ?> <?php if (Util::checkRefreshSession()) { ?> <script> console.info('RELOAD'); window.location.href = "index.php"; </script> <?php }
<?php } ?> <div id="help"> <i class="fa fa-info-circle" aria-hidden="true"></i> <?php printf(Language::t('Más información en %s'), Util::getAppInfo('appWiki')); ?> </div> </div> <footer> <div id="project"> <span id="updates"></span> <?php printf('%s :: %s :: %s', Util::getAppInfo('appVersion'), Util::getAppInfo('appCode'), Util::getAppInfo('appAuthor')); ?> </div> </footer> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/alertify.min.js"></script> <script type="text/javascript" src="js/functions.min.js"></script> <script> (function () { config.setLang('<?php echo Language::t('Seguro?'); ?> '); config.setLang('<?php echo Language::t('Conexión correcta');
Init::start(); $action = Request::analyze('action'); $data = Request::analyze('data'); if (!$action || !$data) { Response::printJSON('Petición inválida'); } else { $data = json_decode($data); if (json_last_error() !== 0) { Response::printJSON('Petición inválida'); } } try { switch ($action) { case 'smdBackend': $url = $data->url . '?action=' . $data->action . '&token=' . $data->token; $json = Util::getDataFromUrl($url); break; case 'zabbixBackend': $ZabbixLoader = new ZabbixApiLoader(); $Zabbix = $ZabbixLoader->getAPI($data->version); $Zabbix->setApiUrl($data->url); $Zabbix->userLogin(array('user' => $data->user, 'password' => $data->pass)); $version = $Zabbix->apiinfoVersion(); Response::printJSON('V ' . $version, 0); break; default: Response::printJSON('Petición inválida'); } } catch (Exception $e) { Response::printJSON($e->getMessage()); }
/** * Obtener el periodo de tiempo más cercano al tiempo actual * * @param array $timePeriods * @return int */ private function getTimePeriod(array $timePeriods) { $result = array(); foreach ($timePeriods as $timePeriod) { if ($timePeriod->timeperiod_type == 0) { $end = $timePeriod->start_date + $timePeriod->period; if (time() <= $end) { $result[] = array('type' => $timePeriod->timeperiod_type, 'start' => $timePeriod->start_date, 'end' => $end); } } else { $start = strtotime('today', time()) + $timePeriod->start_time; $end = $start + $timePeriod->period; $result[] = array('type' => $timePeriod->timeperiod_type, 'start' => $start, 'end' => $end); } } Util::arraySortByKey($result, 'end'); return count($result) > 0 ? $result[0] : false; }
* (at your option) any later version. * * sysMonDash is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with sysMonDash. If not, see <http://www.gnu.org/licenses/>. */ use SMD\Core\Init; use SMD\Core\Language; use SMD\Util\Util; define('APP_ROOT', '..'); require APP_ROOT . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'Base.php'; Init::start(); $updates = Util::checkUpdates(); if (is_array($updates)) { ?> <a href="<?php echo $updates['url']; ?> " target="_blank" title="<?php printf(Language::t('Nueva Versión %s'), $updates['version']); ?> "> <i class="fa fa-cloud-download"></i> </a> <?php }
/** * 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(); } }