Browse Source

Initial import

pull/1/head
Chris Smith 15 years ago
commit
20b0402f48
8 changed files with 315 additions and 0 deletions
  1. 44
    0
      FileParser.php
  2. 40
    0
      Formats.php
  3. 45
    0
      ObjectFileParser.php
  4. 38
    0
      StatusFileParser.php
  5. 3
    0
      index.php
  6. 34
    0
      script.js
  7. 63
    0
      status.php
  8. 48
    0
      style.css

+ 44
- 0
FileParser.php View File

@@ -0,0 +1,44 @@
1
+<?PHP
2
+
3
+ abstract class FileParser {
4
+
5
+  protected abstract function parse_line($line);
6
+
7
+  protected function parse_blockname($block) { return $block; }
8
+  protected function analyse() {}
9
+
10
+  public function __construct($file) {
11
+   $contents = file($file);
12
+
13
+   $block = '';
14
+   $lines = array();
15
+   foreach ($contents as $line) {
16
+    $tline = trim($line);
17
+
18
+    // Comments and empty lines
19
+    if (empty($tline) || $tline[0] == '#') {
20
+     continue;
21
+    }
22
+
23
+    // Close braces
24
+    if ($tline[0] == '}') {
25
+     call_user_func(array(&$this, 'parse_' . $block), $lines);
26
+     continue;
27
+    }
28
+
29
+    // Open braces
30
+    if (substr($tline, -1) == '{') {
31
+     $block = $this->parse_blockname(trim(substr($tline, 0, -1)));
32
+     $lines = array();
33
+     continue;
34
+    }
35
+
36
+    // Other lines
37
+    $lines = array_merge($lines, $this->parse_line($tline));
38
+   }
39
+
40
+   $this->analyse();
41
+  }
42
+ } 
43
+
44
+?>

+ 40
- 0
Formats.php View File

@@ -0,0 +1,40 @@
1
+<?PHP
2
+
3
+ function translateStatus($status) {
4
+  switch ($status) {
5
+   case  0: return 'OK';
6
+   case  1: return 'Warning';
7
+   case  2: return 'Critical';
8
+   default: return 'Unknown';
9
+  }
10
+ }
11
+
12
+ function formatDate($date, $scheduled = false, $id) {
13
+  if ($date == 0) {
14
+   return '<td class="date never update" id="' . $id . '">Never</td>';
15
+  } else if ($date < strtotime('-30 seconds') && $scheduled) {
16
+   return '<td class="date overdue update" id="' . $id . '">' . date('d/m/Y H:i', $date) . '</td>';
17
+  } else {
18
+   return '<td class="date update" id="' . $id . '">' . date('d/m/Y H:i', $date) . '</td>';
19
+  }
20
+ }
21
+
22
+ function trunc($string) {
23
+  if (strlen($string) > 60) {
24
+   $string = substr($string, 0, 59).'…';
25
+  }
26
+  return $string;
27
+ }
28
+
29
+ function knatsort(&$karr){
30
+   $kkeyarr = array_keys($karr);
31
+   natsort($kkeyarr);
32
+   $ksortedarr = array();
33
+   foreach($kkeyarr as $kcurrkey){
34
+    $ksortedarr[$kcurrkey] = $karr[$kcurrkey];
35
+   }
36
+   $karr = $ksortedarr;
37
+   return true;
38
+ }
39
+
40
+?>

+ 45
- 0
ObjectFileParser.php View File

@@ -0,0 +1,45 @@
1
+<?PHP
2
+
3
+ require_once(dirname(__FILE__) . '/FileParser.php');
4
+
5
+ class ObjectFileParser extends FileParser {
6
+
7
+  private $hosts = array();
8
+  private $services = array();
9
+
10
+  protected function parse_line($line) {
11
+   $index = strpos($line, "\t");
12
+   return array(substr($line, 0, $index) => substr($line, 1 + $index));
13
+  }
14
+
15
+  protected function parse_blockname($name) {
16
+   return substr($name, 7);
17
+  }
18
+
19
+  protected function parse_command($data) {}
20
+  protected function parse_service($data) { $this->services[$data['host_name']][$data['service_description']] = $data; }
21
+  protected function parse_servicegroup($data) {}
22
+  protected function parse_host($data) { $this->hosts[$data['host_name']] = $data; }
23
+  protected function parse_hostextinfo($data) {}
24
+  protected function parse_hostgroup($data) {}
25
+  protected function parse_contact($data) {}
26
+  protected function parse_contactgroup($data) {} 
27
+  protected function parse_timeperiod($data) {}
28
+
29
+  protected function analyse() {
30
+   foreach ($this->hosts as $hostname => $data) {
31
+    $this->hosts[$hostname]['services'] = $this->services[$hostname];
32
+   }
33
+
34
+   unset($this->services);
35
+  }
36
+
37
+  public function getHosts() { return $this->hosts; }
38
+
39
+  public function __construct($file = '/var/cache/nagios3/objects.cache') {
40
+   parent::__construct($file);
41
+  }
42
+
43
+ } 
44
+
45
+?>

+ 38
- 0
StatusFileParser.php View File

@@ -0,0 +1,38 @@
1
+<?PHP
2
+
3
+ require_once(dirname(__FILE__) . '/FileParser.php');
4
+
5
+ class StatusFileParser extends FileParser {
6
+
7
+  private $hosts = array();
8
+  private $services = array();
9
+
10
+  protected function parse_line($line) {
11
+   $index = strpos($line, '=');
12
+   return array(substr($line, 0, $index) => substr($line, 1 + $index));
13
+  }
14
+
15
+  protected function parse_servicestatus($data) { $this->services[$data['host_name']][$data['service_description']] = $data; }
16
+  protected function parse_hoststatus($data) { $this->hosts[$data['host_name']] = $data; }
17
+  protected function parse_info($data) {}
18
+  protected function parse_programstatus($data) { }
19
+  protected function parse_contactstatus($data) { }
20
+  protected function parse_servicecomment($data) { }
21
+
22
+  protected function analyse() {
23
+   foreach ($this->hosts as $hostname => $data) {
24
+    $this->hosts[$hostname]['services'] = $this->services[$hostname];
25
+   }
26
+
27
+   unset($this->services);
28
+  }
29
+
30
+  public function getHosts() { return $this->hosts; }
31
+
32
+  public function __construct($file = '/var/cache/nagios3/status.dat') {
33
+   parent::__construct($file);
34
+  }
35
+
36
+ } 
37
+
38
+?>

+ 3
- 0
index.php View File

@@ -0,0 +1,3 @@
1
+<?php
2
+	header('Location: status.php');
3
+?>

+ 34
- 0
script.js View File

@@ -0,0 +1,34 @@
1
+function update() {
2
+ new Ajax.Request(document.location.href, {method: 'get', parameters: {xml: 'true', rand: Math.random() * 1000}, onSuccess: updateResult});
3
+}
4
+
5
+function updateResult(transport) {
6
+ var xml = transport.responseXML;
7
+ var updated = false;
8
+
9
+ $$('.update').each(function(el) {
10
+  try {
11
+   var updatable = xml.getElementById(el.id);
12
+
13
+   if (el.innerHTML != updatable.innerHTML || el.className != updatable.className) {
14
+    el.innerHTML = updatable.innerHTML;
15
+    el.className = updatable.className + ' updated';
16
+    updated = true;
17
+   }
18
+  } catch (e) {
19
+   //alert('Something broke: ' + e);
20
+  }
21
+ });
22
+
23
+ if (updated) {
24
+  setTimeout(updateCancel, 3000);
25
+ }
26
+}
27
+
28
+function updateCancel() {
29
+ $$('.updated').each(function(el) {
30
+  el.removeClassName('updated');
31
+ });
32
+}
33
+
34
+new PeriodicalExecuter(update, 10);

+ 63
- 0
status.php View File

@@ -0,0 +1,63 @@
1
+<?PHP
2
+
3
+if (isset($_GET['xml'])) {
4
+ header('Content-type: text/xml');
5
+} else {
6
+ header('Content-type: application/xhtml+xml');
7
+}
8
+
9
+echo '<?xml version="1.0" encoding="UTF-8"?>', "\n";
10
+
11
+?>
12
+<html xmlns="http://www.w3.org/1999/xhtml">
13
+ <head>
14
+  <title>Nagios status</title>
15
+  <link rel="stylesheet" href="style.css" type="text/css"/>
16
+  <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6/prototype.js" type="text/javascript"></script>
17
+ </head>
18
+ <body>
19
+<?PHP
20
+
21
+ require_once(dirname(__FILE__) . '/Formats.php');
22
+ require_once(dirname(__FILE__) . '/ObjectFileParser.php');
23
+ require_once(dirname(__FILE__) . '/StatusFileParser.php');
24
+
25
+ $op = new ObjectFileParser();
26
+ $sp = new StatusFileParser();
27
+ $sh = $sp->getHosts();
28
+
29
+ echo '<table class="status">';
30
+ echo '<tr><th>Host</th><th>Service</th><th>Status</th><th>Last check</th><th>Next check</th><th>Comment</th></tr>';
31
+
32
+ $hosts = $op->getHosts();
33
+ knatsort($hosts);
34
+
35
+ foreach ($hosts as $hn => $host) {
36
+  echo '<tr><td rowspan="', count($host['services']), '">', $host['alias'], '</td>';
37
+  $first = true; 
38
+
39
+  foreach ($host['services'] as $sn => $service) {
40
+   if ($first) {
41
+    $first = false;
42
+   } else {
43
+    echo '<tr>';
44
+   }
45
+
46
+   $services = $sh[$hn]['services'][$sn];
47
+   $status = translateStatus($services['current_state']);
48
+   $prefix = $host['host_name'] . '_' . str_replace(' ', '_', $service['service_description']) . '_';
49
+   echo '<td>', $service['service_description'], '</td>';
50
+   echo '<td class="update status ', $status, '" id="', $prefix, '_status">', $status, '</td>';
51
+   echo formatDate($services['last_check'], false, $prefix . 'last');
52
+   echo formatDate($services['next_check'],  true, $prefix . 'next');
53
+   echo '<td class="update output" id="', $prefix, '_output">', trunc($services['plugin_output']), '</td>';
54
+   echo '</tr>';
55
+  }
56
+ }
57
+
58
+ echo '</table>';
59
+
60
+?>
61
+  <script src="script.js" type="text/javascript"></script>
62
+ </body>
63
+</html>

+ 48
- 0
style.css View File

@@ -0,0 +1,48 @@
1
+body {
2
+}
3
+
4
+td, th {
5
+ border: 1px solid black;
6
+ padding: 2px 3px;
7
+}
8
+
9
+table {
10
+ border-collapse: collapse;
11
+}
12
+
13
+td.status, td.date {
14
+ text-align: center;
15
+}
16
+
17
+td.status {
18
+ width: 100px;
19
+}
20
+
21
+td.status.OK {
22
+ background-color: green;
23
+ color: white;
24
+}
25
+
26
+td.status.Unknown {
27
+ background-color: yellow;
28
+ color: black;
29
+}
30
+
31
+td.status.Warning, td.date.overdue {
32
+ background-color: orange;
33
+ color: black;
34
+}
35
+
36
+td.status.Critical, td.date.never {
37
+ background-color: red;
38
+ color: white;
39
+}
40
+
41
+.updated {
42
+ text-decoration: blink; 
43
+}
44
+
45
+td.output {
46
+ font-family: monospace;
47
+ font-style: italic;
48
+}

Loading…
Cancel
Save