4 function crash($msg = false)
20 if (!isset($warnings[$msg]))
26 function get_count($condition = false)
28 $query = 'SELECT count(id) AS row_count FROM report_data';
30 $query .= " WHERE $condition";
31 $result = mysql_query($query);
33 crash("mysql_query($query) failed: " . mysql_error());
34 return mysql_result($result, 'row_count');
37 function human_time($stamp)
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);
51 $ret .= $hours . "h ";
59 $db = mysql_connect('localhost', 'monitor', 'monitor');
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 ' .
80 'WHERE event_type IN (100, 103, 1103, 1104) ' .
82 $result = mysql_query($query);
84 crash("mysql_query() failed: " . mysql_error());
86 $tot_rows = mysql_num_rows($result);
89 while ($row = mysql_fetch_array($result)) {
90 $when = $row['timestamp'];
91 $type = $row['event_type'];
93 $obj_name = $row['host_name'] . ';' . $row['service_description'];
99 warn("Start when already running ($id)");
105 warn("Stop when not running ($id)");
111 if (!isset($dt[$obj_name]))
112 $dt[$obj_name] = $when;
116 if (!isset($dt[$obj_name]))
117 warn("Downtime stop without downtime start for $obj_name\n");
119 $diff = $when - $dt[$obj_name];
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]);
131 warn("Event when not running ($id)");
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;
145 warn("Unfinished downtime for $obj_name (" . human_time($diff) ."), " .
146 "started " . date("Y-m-d H:i:s", $then) . " ($then)");
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";