import: Revamp parse_import_notification()
[nagios-reports-module.git] / verify_import.php
blob744f77e2f017ce3083967f4f67a1f632a139a079
1 #!/usr/bin/php
2 <?php
4 function crash($msg = false)
6 if ($msg) {
7 echo $msg . "\n";
10 exit(1);
14 $warnings = array();
15 function warn($msg)
17 global $warnings;
18 # echo $msg . "\n";
20 if (!isset($warnings[$msg]))
21 $warnings[$msg] = 0;
23 $warnings[$msg]++;
26 function get_count($condition = false)
28 $query = 'SELECT count(id) AS row_count FROM report_data';
29 if ($condition)
30 $query .= " WHERE $condition";
31 $result = mysql_query($query);
32 if (!$result)
33 crash("mysql_query($query) failed: " . mysql_error());
34 return mysql_result($result, 'row_count');
37 function human_time($stamp)
39 if (!$stamp)
40 return 0;
42 $days = sprintf("%.0f", $stamp / 86400);
43 $hours = sprintf("%.0f", ($stamp % 86400) / 3600);
44 $mins = sprintf("%.0f", ($stamp % 3600) / 60);
45 $secs = sprintf("%.0f", $stamp % 60);
47 $ret = "";
48 if ($days)
49 $ret .= $days . "d ";
50 if ($hours)
51 $ret .= $hours . "h ";
52 if ($mins)
53 $ret .= $mins . "m ";
54 $ret .= $secs . "s";
56 return $ret;
59 $db = mysql_connect('localhost', 'monitor', 'monitor');
60 if (!$db)
61 crash("Failed to connect to mysql: " . mysql_errno());
63 mysql_select_db('monitor_reports');
64 $result = mysql_query('SELECT count(id) as row_count FROM report_data');
65 $tot_rows = get_count();
66 $stop_events = get_count('event_type = 103');
67 $start_events = get_count('event_type = 100');
68 $dt_start_events = get_count('event_type = 1103');
69 $dt_stop_events = get_count('event_Type = 1104');
70 $other_events = get_count('event_type != 100 AND event_type != 103 AND event_type != 1104 AND event_type != 1103');
71 echo "Total rows: $tot_rows\n";
72 echo "Stop events: $stop_events\n";
73 echo "Start events: $start_events\n";
74 echo "Downtime start events: $dt_start_events\n";
75 echo "Downtime stop events: $dt_stop_events\n";
76 echo "Other events: $other_events\n";
78 $query = 'SELECT timestamp, event_type, id, host_name, service_description ' .
79 'FROM report_data ' .
80 'WHERE event_type IN (100, 103, 1103, 1104) ' .
81 'ORDER BY id';
82 $result = mysql_query($query);
83 if (!$result)
84 crash("mysql_query() failed: " . mysql_error());
86 $tot_rows = mysql_num_rows($result);
87 $running = false;
88 $rows = 0;
89 while ($row = mysql_fetch_array($result)) {
90 $when = $row['timestamp'];
91 $type = $row['event_type'];
92 $id = $row['id'];
93 $obj_name = $row['host_name'] . ';' . $row['service_description'];
94 $rows++;
96 switch ($type) {
97 case 100:
98 if ($running)
99 warn("Start when already running ($id)");
100 $running = true;
101 break;
103 case 103:
104 if (!$running)
105 warn("Stop when not running ($id)");
107 $running = false;
108 break;
110 case 1103:
111 if (!isset($dt[$obj_name]))
112 $dt[$obj_name] = $when;
113 $dt[$obj_name]++;
114 break;
115 case 1104:
116 if (!isset($dt[$obj_name]))
117 warn("Downtime stop without downtime start for $obj_name\n");
118 else {
119 $diff = $when - $dt[$obj_name];
120 if ($diff > 86400)
121 warn("Iffy downtime for $obj_name (".human_time($diff)."), " .
122 "started " . date("Y-m-d H:i:s", $dt[$obj_name]) .
123 " ($dt[$obj_name])");
125 unset($dt[$obj_name]);
127 break;
129 default:
130 if (!$running)
131 warn("Event when not running ($id)");
133 $running = true;
134 break;
137 if (posix_isatty(STDOUT))
138 printf("\rScanned $rows of $tot_rows rows (%.2f%%)",
139 ($rows / $tot_rows) * 100);
142 foreach ($dt as $obj_name => $then) {
143 $diff = $when - $then;
144 if ($diff > 7500)
145 warn("Unfinished downtime for $obj_name (" . human_time($diff) ."), " .
146 "started " . date("Y-m-d H:i:s", $then) . " ($then)");
148 echo "\n";
149 $tot_warnings = 0;
150 foreach ($warnings as $warn => $num) {
151 echo "$warn: $num\n";
152 $tot_warnings += $num;
154 echo "Total rows: $rows\nTotal warnings: $tot_warnings\n";