Пример #1
0
 /**
  * Convert the list of events for web output
  *
  * @param array $listEvents The list of events
  * @return array The datas for infinite scroll
  */
 private function convertListEventLogs($listEvents)
 {
     HostRepository::setObjectClass('\\CentreonConfiguration\\Models\\Host');
     /* Convert data for output */
     $lastDateCount = 0;
     $lastDate = null;
     $firstDate = null;
     $data = array();
     foreach ($listEvents as $log) {
         if ($lastDate != $log['datetime']) {
             $lastDate = $log['datetime'];
             $lastDateCount = 0;
         }
         if (is_null($firstDate)) {
             $firstDate = $log['datetime'];
         }
         $lastDateCount++;
         if (isset($log['host']) && $log['host'] !== "") {
             $log['host_logo'] = HostRepository::getIconImage($log['host']);
         }
         if (isset($log['service']) && $log['service'] !== "") {
             $log['service_logo'] = ServiceRepository::getIconImage($log['service']);
         }
         if ($log['type'] != '') {
             if ($log['type'] == 1) {
                 $log['type'] = 'HARD';
             } else {
                 $log['type'] = 'SOFT';
             }
         } else {
             $log['type'] = " ";
         }
         /* Translate the status id */
         if (isset($log['service_id']) && isset($log['host_id']) && $log['status'] !== '') {
             try {
                 $log['status_text'] = Status::numToString($log['status'], Status::TYPE_SERVICE, true);
             } catch (\OutOfBoundsException $e) {
                 $log['status_text'] = "";
             }
         } elseif (!isset($log['service_id']) && $log['status'] !== '') {
             try {
                 $log['status_text'] = Status::numToString($log['status'], Status::TYPE_HOST, true);
             } catch (\OutOfBoundsException $e) {
                 $log['status_text'] = "";
             }
         } else {
             if ($log['status'] !== '') {
                 try {
                     $log['status_text'] = Status::numToString($log['status'], Status::TYPE_EVENT, true);
                 } catch (\OutOfBoundsException $e) {
                     $log['status_text'] = "";
                 }
             }
         }
         if ($log['msg_type'] != 1 && $log['msg_type'] != 0) {
             $log['status_css'] = '';
             $log['border_color'] = 'centreon-border-info';
         } elseif ($log['msg_type'] == 1) {
             $log['status_css'] = 'centreon-status-h-' . $log['status'];
             $log['border_color'] = 'centreon-border-status-h-' . $log['status'];
         } else {
             $log['status_css'] = 'centreon-status-s-' . $log['status'];
             $log['border_color'] = 'centreon-border-status-s-' . $log['status'];
         }
         /* For test */
         $log['object_name'] = "";
         $object_name = array();
         if (isset($log['host']) && $log['host'] !== "") {
             $object_name[] = $log['host'];
         }
         if (isset($log['service']) && $log['service'] !== "") {
             $object_name[] = $log['service'];
         }
         if (count($object_name) > 0) {
             $log['object_name'] = join(' - ', $object_name);
         } else {
             $log['object_name'] = 'Other';
         }
         $log['logo'] = "<i class='fa fa-gear'></i>";
         if (isset($log['service_logo'])) {
             $log['logo'] = $log['service_logo'];
         } else {
             if (isset($log['host_logo'])) {
                 $log['logo'] = $log['host_logo'];
             }
         }
         if (isset($log['output'])) {
             $log['description'] = $log['output'];
         }
         $data[] = $log;
     }
     return array('data' => $data, 'lastTimeEntry' => $lastDate, 'nbEntryForLastTime' => $lastDateCount, 'recentTime' => $firstDate, 'facets' => array());
 }
Пример #2
0
 /**
  * 
  * @param type $domain
  * @param type $service
  * @param type $metricList
  * @return string
  */
 public static function normalizeMetricsForStorage($domain, $service, $metricList)
 {
     $normalizeMetricSet = array();
     if (!isset($metricList['used'])) {
         return array();
     }
     $metric = $metricList['used'];
     $newUnit = "";
     $memoryValue = HumanReadable::convertArray(array($metric['current_value'], $metric['max']), $metric['unit_name'], $newUnit, 2);
     $normalizeMetricSet['current'] = $memoryValue[0];
     $normalizeMetricSet['max'] = $memoryValue[1];
     if (!empty($newUnit)) {
         $metric['unit_name'] = $newUnit;
     }
     $normalizeMetricSet['unit'] = $metric['unit_name'];
     $normalizeMetricSet['status'] = strtolower(StatusUtils::numToString($service['state'], StatusUtils::TYPE_SERVICE));
     return $normalizeMetricSet;
 }
Пример #3
0
 /**
  * Transform raw data
  *
  * @param array $rawdata
  * @return array
  */
 protected function transformRawData($rawdata)
 {
     $data = array();
     /* Address */
     $data[] = array('label' => _('Address'), 'value' => $rawdata['host_address']);
     /* Instance */
     $data[] = array('label' => _('Poller'), 'value' => $rawdata['instance_name']);
     /* State */
     $data[] = array('label' => _('State'), 'value' => Status::numToString($rawdata['state'], Status::TYPE_HOST, true) . " (" . ($rawdata['state_type'] ? "HARD" : "SOFT") . ")");
     /* Command line */
     $data[] = array('label' => _('Command line'), 'value' => chunk_split($rawdata['command_line'], 80, "<br/>"));
     /* Output */
     $data[] = array('label' => _('Output'), 'value' => $rawdata['output']);
     /* Acknowledged */
     $data[] = array('label' => _('Acknowledged'), 'value' => $rawdata['acknowledged'] ? _('Yes') : _('No'));
     /* Downtime */
     $data[] = array('label' => _('In downtime'), 'value' => $rawdata['scheduled_downtime_depth'] ? _('Yes') : _('No'));
     /* Latency */
     $data[] = array('label' => _('Latency'), 'value' => $rawdata['latency'] . ' s');
     /* Check period */
     $data[] = array('label' => _('Check period'), 'value' => $rawdata['check_period']);
     /* Last check */
     $data[] = array('label' => _('Last check'), 'value' => $rawdata['last_check']);
     /* Next check */
     $data[] = array('label' => _('Next check'), 'value' => $rawdata['next_check']);
     return $data;
 }
Пример #4
0
 /**
  * Transform raw data
  *
  * @param array $rawdata
  * @return array
  */
 protected function transformRawData($rawdata)
 {
     $data = array();
     /* Instance */
     $data[] = array('label' => _('Poller'), 'value' => $rawdata['instance_name']);
     /* State */
     $data[] = array('label' => _('State'), 'value' => Status::numToString($rawdata['state'], Status::TYPE_SERVICE, true) . " (" . ($rawdata['state_type'] ? "HARD" : "SOFT") . ")");
     /* Command line */
     $data[] = array('label' => _('Command line'), 'value' => chunk_split($rawdata['command_line'], 80, "<br/>"));
     /* Output */
     $data[] = array('label' => _('Output'), 'value' => $rawdata['output']);
     /* Perfdata */
     if ($rawdata['perfdata']) {
         $perfdata = explode(' ', $rawdata['perfdata']);
         foreach ($perfdata as &$perf) {
             $perf = preg_replace("/([a-zA-Z0-9_-]+)(=)(.*)/i", '<span class="btn btn-xs btn-info perfdata">$1</span>
                 <span class="btn btn-xs btn-default perfdata">$3</span>', $perf);
         }
         $data[] = array('label' => _('Performance'), 'value' => implode("<br />", $perfdata));
     }
     /* Acknowledged */
     $data[] = array('label' => _('Acknowledged'), 'value' => $rawdata['acknowledged'] ? _('Yes') : _('No'));
     /* Downtime */
     $data[] = array('label' => _('In downtime'), 'value' => $rawdata['scheduled_downtime_depth'] ? _('Yes') : _('No'));
     /* Latency */
     $data[] = array('label' => _('Latency'), 'value' => $rawdata['latency'] . ' s');
     /* Check period */
     $data[] = array('label' => _('Check period'), 'value' => $rawdata['check_period']);
     /* Last check */
     $data[] = array('label' => _('Last check'), 'value' => $rawdata['last_check']);
     /* Next check */
     $data[] = array('label' => _('Next check'), 'value' => $rawdata['next_check']);
     return $data;
 }
Пример #5
0
 /**
  * 
  * @param type $hostId
  * @return type
  */
 public static function getHostShortInfo($hostId)
 {
     $finalInfo = array();
     // Initializing connection
     $di = Di::getDefault();
     $dbconn = $di->get('db_centreon');
     $stmt = $dbconn->prepare('SELECT ' . 'state as state, output as output, last_state_change as lastChange, ' . 'last_check as lastCheck, next_check as nextCheck ' . 'FROM rt_hosts WHERE host_id = ? LIMIT 1');
     $stmt->execute(array($hostId));
     $infos = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     if (count($infos) > 0) {
         $finalInfo['status'] = strtolower(UtilStatus::numToString($infos[0]['state'], UtilStatus::TYPE_HOST));
         $finalInfo['output'] = $infos[0]['output'];
         $finalInfo['lastChange'] = $infos[0]['lastChange'];
         $finalInfo['lastCheck'] = $infos[0]['lastCheck'];
         $finalInfo['nextCheck'] = $infos[0]['nextCheck'];
     } else {
         $finalInfo['status'] = -1;
         $finalInfo['output'] = '';
         $finalInfo['lastChange'] = '';
         $finalInfo['lastCheck'] = '';
         $finalInfo['nextCheck'] = '';
     }
     return $finalInfo;
 }
Пример #6
0
 /**
  * Get information for a incident for display graph
  *
  * @route /incident/graph
  * @method POST
  */
 public function getIncidentGraphInfoAction()
 {
     $di = Di::getDefault();
     $router = $di->get('router');
     $response = array();
     $action = $router->request()->param('action', null);
     $incidentId = $router->request()->param('incident_id', null);
     if (is_null($incidentId)) {
         $router->response()->code(400);
         return;
     }
     switch ($action) {
         case 'get_info':
             $incident = IncidentsRepository::getIncident($incidentId);
             $fullname = $incident['name'];
             if (false === is_null($incident['description'])) {
                 $fullname .= ' - ' . $incident['description'];
             }
             $response = array('id' => $incident['issue_id'], 'name' => $fullname, 'status' => self::getCssStatus($incident['state']), 'output' => $incident['output'], 'last_update' => Datetime::format($incident['last_state_change']), 'has_children' => $incident['nb_children'] > 0 ? true : false, 'has_parent' => $incident['nb_parents'] > 0 ? true : false, 'parents' => array_map(function ($values) {
                 $parent = array();
                 $parent['id'] = $values['issue_id'];
                 $fullname = $values['name'];
                 if (!is_null($values['description'])) {
                     $fullname .= ' - ' . $values['description'];
                 }
                 $parent['name'] = $fullname;
                 return $parent;
             }, $incident['parents']));
             break;
         case 'getChildren':
             $listChildren = IncidentsRepository::getChildren($incidentId);
             $response = array();
             foreach ($listChildren as $child) {
                 $fullname = $child['name'];
                 if (false === is_null($child['description'])) {
                     $fullname .= ' - ' . $child['description'];
                 }
                 $response[] = array('id' => $child['issue_id'], 'name' => $fullname, 'status' => self::getCssStatus($child['state']), 'output' => $child['output'], 'last_update' => Datetime::format($child['last_state_change']), 'has_children' => $child['nb_children'] > 0 ? true : false, 'has_parent' => $child['nb_parents'] > 0 ? true : false);
             }
             break;
         case 'get_extended_info':
             $status = IncidentsRepository::getListStatus($incidentId);
             $statusList = array();
             foreach ($status as $tmp) {
                 if (false === is_null($tmp['service_id'])) {
                     $statusType = Status::TYPE_SERVICE;
                 } else {
                     $statusType = Status::TYPE_HOST;
                 }
                 $statusList[] = array('id' => $tmp['state'], 'text' => Status::numToString($tmp['state'], $statusType, true), 'datetime' => $tmp['start_time']);
             }
             $response = array('status' => array(array('id' => 2, 'text' => 'Critical', 'datetime' => '2014-05-12 01:03:11'), array('id' => 2, 'text' => 'Critical', 'datetime' => '2014-05-12 01:03:11'), array('id' => 2, 'text' => 'Critical', 'datetime' => '2014-05-12 01:03:11'), array('id' => 2, 'text' => 'Critical', 'datetime' => '2014-05-12 01:03:11')));
             break;
         default:
             $router->response()->code(400);
             return;
     }
     $router->response()->json($response);
 }
Пример #7
0
 public function testStringToNumExceptionBadStatus()
 {
     $this->setExpectedException('\\OutOfBoundsException', "Status text does not exists");
     Status::stringToNum('BadStatus', Status::TYPE_HOST);
 }
Пример #8
0
 /**
  * Return the list of events
  *
  * @param $fromTime string The start time in date format Y-m-d H:i:s
  * @param $order string The order for getting events : DESC or ASC
  * @param $limit int The number of event to get
  * @param $filters array The list of fitlers for event
  * @return array
  */
 public static function getEventLogs($fromTime = null, $order = 'DESC', $limit = null, $filters = array())
 {
     $listFullsearch = array('output');
     $timeField = array('period');
     $di = Di::getDefault();
     $options = $di->get('config');
     /* Connection to the elastic search server */
     $params['hosts'] = array();
     $params['hosts'][] = $options->get('default', 'es_host');
     $esclient = new ESClient($params);
     $queryFilters = array();
     if (false === is_null($limit)) {
         $queryFilters["filter"] = array("limit" => array("value" => $limit));
     }
     $queryQueries = array();
     if (false === is_null($fromTime)) {
         $queryQueries[] = array("range" => array("@timestamp" => array("to" => $fromTime)));
     }
     foreach ($filters as $key => $value) {
         if (in_array($key, $listFullsearch)) {
             $queryQueries[] = array("term" => array("message" => $value));
         } elseif (in_array($key, $timeField)) {
             list($timeStart, $timeEnd) = explode(' - ', $value);
             $queryQueries[] = array("range" => array("@timestamp" => array("from" => $timeStart, "to" => $timeEnd)));
         } elseif ($key == 'host') {
             $queryQueries[] = array("or" => array(array("term" => array("host" => $value)), array("term" => array("centreon_hostname" => $value))));
         } elseif ($key == 'service') {
             $queryQueries[] = array("term" => array("centreon_service" => $value));
         } elseif ($key == 'eventtype') {
             $queryQueries[] = self::queryEventtype($value);
         } elseif ($key == 'status') {
             $queryQueries[] = array("or" => array("term" => array("centreon_status" => strtoupper(Status::numToString($value, Status::TYPE_SERVICE))), "term" => array("centreon_status" => strtoupper(Status::numToString($value, Status::TYPE_HOST))), "term" => array("centreon_status" => strtoupper(Status::numToString($value, Status::TYPE_EVENT)))));
         }
     }
     /* Add filter for only centreon-engine message */
     $queryQueries[] = array('exists' => array('field' => 'centreon_hostname'));
     if (count($queryQueries) > 0) {
         $queryFilters["filter"]["and"] = $queryQueries;
     }
     if (count($queryFilters) > 0) {
         $esQuery = array("query" => array("filtered" => $queryFilters));
     } else {
         $esQuery = array("query" => array("match_all" => array()));
     }
     $esQuery['sort'] = array(array('@timestamp' => 'desc'));
     file_put_contents('/tmp/query_es', json_encode($esQuery, JSON_PRETTY_PRINT));
     $results = $esclient->search(array("body" => $esQuery));
     $data = array();
     foreach ($results['hits']['hits'] as $result) {
         if (isset($result['_source']) && $result['_type'] == 'syslog') {
             $host = '';
             $service = '';
             $status = '';
             $type = '';
             if (isset($result['_source']['centreon_hostname'])) {
                 $host = $result['_source']['centreon_hostname'];
             } else {
                 $host = $result['_source']['host'];
             }
             if (isset($result['_source']['centreon_service'])) {
                 $service = $result['_source']['centreon_service'];
             }
             if (isset($result['_source']['centreon_status']) && isset($result['_source']['centreon_type'])) {
                 $type = $result['_source']['centreon_type'];
                 if (strstr('HOST', substr($type, 4))) {
                     $statusType = Status::TYPE_HOST;
                 } else {
                     if (strstr('SERVICE', substr($type, 7))) {
                         $statusType = Status::TYPE_SERVICE;
                     } else {
                         $statusType = Status::TYPE_EVENT;
                     }
                 }
                 try {
                     $textStatus = ucfirst(strtolower($result['_source']['centreon_status']));
                     $status = Status::stringToNum($textStatus, $statusType);
                 } catch (\OutOfBoundsException $e) {
                     $status = '';
                 }
             }
             $data[] = array('datetime' => strtotime($result['_source']['@timestamp']), 'host_id' => '', 'host' => $host, 'service_id' => '', 'service' => $service, 'instance' => '', 'output' => $result['_source']['message'], 'status' => $status, 'type' => $type, 'msg_type' => '');
         }
     }
     return $data;
 }