1<?php
2
3/**
4 * Observium
5 *
6 *   This file is part of Observium.
7 *
8 * @package    observium
9 * @subpackage web
10 * @copyright  (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
11 *
12 */
13
14/**
15 * Display events.
16 *
17 * Display pages with device/port/system events on some formats.
18 * Examples:
19 * print_events() - display last 10 events from all devices
20 * print_events(array('pagesize' => 99)) - display last 99 events from all device
21 * print_events(array('pagesize' => 10, 'pageno' => 3, 'pagination' => TRUE)) - display 10 events from page 3 with pagination header
22 * print_events(array('pagesize' => 10, 'device' = 4)) - display last 10 events for device_id 4
23 * print_events(array('short' => TRUE)) - show small block with last events
24 *
25 * @param array $vars
26 * @return none
27 *
28 */
29function print_events($vars)
30{
31  global $config;
32
33  // Get events array
34  $events = get_events_array($vars);
35
36  if (!$events['count'])
37  {
38    // There have been no entries returned. Print the warning.
39    print_warning('<h4>No eventlog entries found!</h4>');
40  } else {
41    // Entries have been returned. Print the table.
42    $list = array('device' => FALSE, 'port' => FALSE);
43    if (!isset($vars['device']) || empty($vars['device']) || $vars['page'] == 'eventlog') { $list['device'] = TRUE; }
44    if ($events['short'] || !isset($vars['port']) || empty($vars['port'])) { $list['entity'] = TRUE; }
45
46    $string = generate_box_open($vars['header']);
47
48    $string .= '<table class="'.OBS_CLASS_TABLE_STRIPED_MORE.'">' . PHP_EOL;
49    if (!$events['short'])
50    {
51      $string .= '  <thead>' . PHP_EOL;
52      $string .= '    <tr>' . PHP_EOL;
53      $string .= '      <th class="state-marker"></th>' . PHP_EOL;
54      $string .= '      <th>Date</th>' . PHP_EOL;
55      if ($list['device']) { $string .= '      <th>Device</th>' . PHP_EOL; }
56      if ($list['entity']) { $string .= '      <th>Entity</th>' . PHP_EOL; }
57      $string .= '      <th>Message</th>' . PHP_EOL;
58      $string .= '    </tr>' . PHP_EOL;
59      $string .= '  </thead>' . PHP_EOL;
60    }
61    $string   .= '  <tbody>' . PHP_EOL;
62
63    foreach ($events['entries'] as $entry)
64    {
65      switch ($entry['severity'])
66      {
67        case "0": // Emergency
68        case "1": // Alert
69        case "2": // Critical
70        case "3": // Error
71            $entry['html_row_class'] = "error";
72            break;
73        case "4": // Warning
74            $entry['html_row_class'] = "warning";
75            break;
76        case "5": // Notification
77            $entry['html_row_class'] = "recovery";
78            break;
79        case "6": // Informational
80            $entry['html_row_class'] = "up";
81            break;
82        case "7": // Debugging
83            $entry['html_row_class'] = "suppressed";
84            break;
85        default:
86      }
87
88      $string .= '  <tr class="'.$entry['html_row_class'].'">' . PHP_EOL;
89      $string .= '<td class="state-marker"></td>' . PHP_EOL;
90
91      if ($events['short'])
92      {
93        $string .= '    <td class="syslog" style="white-space: nowrap">';
94        $timediff = $GLOBALS['config']['time']['now'] - strtotime($entry['timestamp']);
95        $string .= generate_tooltip_link('', format_uptime($timediff, "short-3"), format_timestamp($entry['timestamp']), NULL) . '</td>' . PHP_EOL;
96      } else {
97        $string .= '    <td style="width: 160px">';
98        $string .= format_timestamp($entry['timestamp']) . '</td>' . PHP_EOL;
99      }
100
101      if ($entry['device_id'] == 0 && $entry['entity_type'] == '')
102      {
103        // Compatability for global events
104        $entry['entity_type'] = 'global';
105      }
106
107      if ($list['device'])
108      {
109        if ($entry['entity_type'] == 'global')
110        {
111          // Global events
112          $string .= '    <td class="entity"><span class="label">GLOBAL</span></td>' . PHP_EOL;
113        } else {
114          $dev = device_by_id_cache($entry['device_id']);
115          $device_vars = array('page'    => 'device',
116                               'device'  => $entry['device_id'],
117                               'tab'     => 'logs',
118                               'section' => 'eventlog');
119          $string .= '    <td class="entity">' . generate_device_link($dev, short_hostname($dev['hostname']), $device_vars) . '</td>' . PHP_EOL;
120        }
121      }
122      if ($list['entity'])
123      {
124        if ($entry['entity_type'] == 'device' && !$entry['entity_id']) { $entry['entity_id'] = $entry['device_id']; }
125        if ($entry['entity_type'] == 'port')
126        {
127          $this_if = get_port_by_id_cache($entry['entity_id']);
128          $entry['link'] = '<span class="entity"><i class="' . $config['entities']['port']['icon'] . '"></i> ' . generate_port_link($this_if, $this_if['port_label_short']) . '</span>';
129        }
130        else if ($entry['entity_type'] == 'global')
131        {
132          $entry['link'] = '<i class="' . $config['icon']['info'] . '"></i>';
133        } else {
134          if (!empty($config['entities'][$entry['entity_type']]['icon']))
135          {
136            $entry['link'] = '<i class="' . $config['entities'][$entry['entity_type']]['icon'] . '"></i> <span class="entity">'.generate_entity_link($entry['entity_type'], $entry['entity_id']).'</span>';
137          } else {
138            $entry['link'] = nicecase($entry['entity_type']);
139          }
140
141        }
142        if (!$events['short']) { $string .= '    <td style="white-space: nowrap">' . $entry['link'] . '</td>' . PHP_EOL; }
143      }
144      if ($events['short'])
145      {
146        $string .= '    <td class="syslog">';
147        if (strpos($entry['message'], $entry['link']) !== 0)
148        {
149          $string .= $entry['link'] . ' ';
150        }
151      } else {
152        $string .= '    <td>';
153      }
154      $string .= escape_html($entry['message']) . '</td>' . PHP_EOL;
155      //$string .= $entry['message'] . '</td>' . PHP_EOL;
156      $string .= '  </tr>' . PHP_EOL;
157    }
158
159    $string .= '  </tbody>' . PHP_EOL;
160    $string .= '</table>';
161
162    $string .= generate_box_close();
163
164    // Print pagination header
165    if ($events['pagination_html']) { $string = $events['pagination_html'] . $string . $events['pagination_html']; }
166
167    // Print events
168    echo $string;
169  }
170}
171
172/**
173 * Display short events.
174 *
175 * This is use function:
176 * print_events(array('short' => TRUE))
177 *
178 * @param array $vars
179 * @return none
180 *
181 */
182function print_events_short($var)
183{
184  $var['short'] = TRUE;
185  print_events($var);
186}
187
188/**
189 * Params:
190 * short
191 * pagination, pageno, pagesize
192 * device_id, entity_id, entity_type, message, timestamp_from, timestamp_to
193 */
194function get_events_array($vars)
195{
196  $array = array();
197
198  // Short events? (no pagination, small out)
199  $array['short'] = (isset($vars['short']) && $vars['short']);
200  // With pagination? (display page numbers in header)
201  $array['pagination'] = (isset($vars['pagination']) && $vars['pagination']);
202  pagination($vars, 0, TRUE); // Get default pagesize/pageno
203  $array['pageno']   = $vars['pageno'];
204  $array['pagesize'] = $vars['pagesize'];
205  $start    = $array['pagesize'] * $array['pageno'] - $array['pagesize'];
206  $pagesize = $array['pagesize'];
207
208  // Begin query generate
209  $param = array();
210  $where = ' WHERE 1 ';
211  foreach ($vars as $var => $value)
212  {
213    if ($value != '')
214    {
215      switch ($var)
216      {
217        case 'device':
218        case 'device_id':
219          $where .= generate_query_values($value, 'device_id');
220          break;
221        case 'port':
222        case 'entity':
223        case 'entity_id':
224          $where .= generate_query_values($value, 'entity_id');
225          break;
226        case 'severity':
227          $where .= generate_query_values($value, 'severity');
228          break;
229        case 'type':
230        case 'entity_type':
231          $where .= generate_query_values($value, 'entity_type');
232          break;
233        case 'message':
234          $where .= generate_query_values($value, 'message', '%LIKE%');
235          break;
236        case 'timestamp_from':
237          $where .= ' AND `timestamp` >= ?';
238          $param[] = $value;
239          break;
240        case 'timestamp_to':
241          $where .= ' AND `timestamp` <= ?';
242          $param[] = $value;
243          break;
244        case "group":
245        case "group_id":
246          $values = get_group_entities($value);
247          $where .= generate_query_values($values, 'entity_id');
248          $where .= generate_query_values(get_group_entity_type($value), 'entity_type');
249          break;
250      }
251    }
252  }
253
254  // Show events only for permitted devices
255  $query_permitted = generate_query_permitted();
256
257  $query = 'FROM `eventlog` ';
258  $query .= $where . $query_permitted;
259  $query_count = 'SELECT COUNT(*) '.$query;
260  $query_updated = 'SELECT MAX(`timestamp`) '.$query;
261
262  $query = 'SELECT * '.$query;
263  $query .= ' ORDER BY `event_id` DESC ';
264  $query .= "LIMIT $start,$pagesize";
265
266  // Query events
267  $array['entries'] = dbFetchRows($query, $param);
268
269  // Query events count
270  if ($array['pagination'] && !$array['short'])
271  {
272    $array['count'] = dbFetchCell($query_count, $param);
273    $array['pagination_html'] = pagination($vars, $array['count']);
274  } else {
275    $array['count'] = count($array['entries']);
276  }
277
278  // Query for last timestamp
279  //$array['updated'] = dbFetchCell($query_updated, $param);
280
281  return $array;
282}
283
284function generate_eventlog_form_values($form_filter = FALSE, $column = NULL)
285{
286  //global $cache;
287  global $config;
288
289  $form_items = array();
290  $filter = is_array($form_filter); // Use filer or not
291
292  switch ($column)
293  {
294    case 'severities':
295    case 'severity':
296      // These complete same as syslog priorities
297      return generate_form_values('syslog', $form_filter, 'priorities');
298      break;
299    case 'entity_type':
300    case 'type':
301    case 'types':
302      // Use filter as items
303      if ($filter)
304      {
305        if (in_array('device', $form_filter))
306        {
307          // Device always first
308          $form_filter = array_unique(array_merge(array('device'), $form_filter));
309        }
310        if (in_array('global', $form_filter))
311        {
312          // Global always first
313          $form_filter = array_unique(array_merge(array('global'), $form_filter));
314        }
315      }
316      foreach ($form_filter as $type)
317      {
318        if (strlen($type) == 0) { continue; }
319        $form_items[$type]['name'] = nicecase($type);
320
321        if ($type == 'global')
322        {
323          $form_items[$type]['icon'] = $config['icon']['info'];
324        }
325        else if (!isset($config['entities'][$type]['icon']))
326        {
327          $form_items[$type]['icon'] = $config['entity_default']['icon'];
328        } else {
329          $form_items[$type]['icon'] = $config['entities'][$type]['icon'];
330        }
331      }
332      break;
333  }
334  return $form_items;
335}
336
337// EOF
338