4 $table = 'report_data';
6 for ($i = 1; $i < $argc; $i++
) {
7 if ($argv[$i] === '--db-table') {
12 function crash($msg = false)
28 if (!isset($warnings[$msg]))
34 function get_count($condition = false)
38 $query = 'SELECT count(id) AS row_count FROM ' . $table;
40 $query .= " WHERE $condition";
41 $result = mysql_query($query);
43 crash("mysql_query($query) failed: " . mysql_error());
44 return mysql_result($result, 0, 'row_count');
47 function human_time($stamp)
52 $days = sprintf("%.0f", $stamp / 86400);
53 $hours = sprintf("%.0f", ($stamp %
86400) / 3600);
54 $mins = sprintf("%.0f", ($stamp %
3600) / 60);
55 $secs = sprintf("%.0f", $stamp %
60);
61 $ret .= $hours . "h ";
69 $db = mysql_connect('localhost', 'monitor', 'monitor');
71 crash("Failed to connect to mysql: " . mysql_errno());
73 mysql_select_db('monitor_reports');
74 $result = mysql_query('SELECT count(id) as row_count FROM ' . $table);
75 $tot_rows = get_count();
76 $stop_events = get_count('event_type = 103');
77 $start_events = get_count('event_type = 100');
78 $dt_start_events = get_count('event_type = 1103');
79 $dt_stop_events = get_count('event_Type = 1104');
80 $other_events = get_count('event_type != 100 AND event_type != 103 AND event_type != 1104 AND event_type != 1103');
81 echo "Total rows: $tot_rows\n";
82 echo "Stop events: $stop_events\n";
83 echo "Start events: $start_events\n";
84 echo "Downtime start events: $dt_start_events\n";
85 echo "Downtime stop events: $dt_stop_events\n";
86 echo "Other events: $other_events\n";
88 $query = 'SELECT timestamp, event_type, id, host_name, service_description ' .
89 'FROM ' . $table . ' ' .
90 'WHERE event_type IN (100, 103, 1103, 1104) ' .
92 $result = mysql_query($query);
94 crash("mysql_query() failed: " . mysql_error());
96 $tot_rows = mysql_num_rows($result);
99 while ($row = mysql_fetch_array($result)) {
100 $when = $row['timestamp'];
101 $type = $row['event_type'];
103 $obj_name = $row['host_name'] . ';' . $row['service_description'];
109 warn("Start when already running ($id)");
115 warn("Stop when not running ($id)");
121 if (!isset($dt[$obj_name]))
122 $dt[$obj_name] = $when;
126 if (!isset($dt[$obj_name]))
127 warn("Downtime stop without downtime start for $obj_name\n");
129 $diff = $when - $dt[$obj_name];
131 warn("Iffy downtime for $obj_name (".human_time($diff)."), " .
132 "started " . date("Y-m-d H:i:s", $dt[$obj_name]) .
133 " ($dt[$obj_name])");
135 unset($dt[$obj_name]);
141 warn("Event when not running ($id)");
147 if (posix_isatty(STDOUT
))
148 printf("\rScanned $rows of $tot_rows rows (%.2f%%)",
149 ($rows / $tot_rows) * 100);
152 foreach ($dt as $obj_name => $then) {
153 $diff = $when - $then;
155 warn("Unfinished downtime for $obj_name (" . human_time($diff) ."), " .
156 "started " . date("Y-m-d H:i:s", $then) . " ($then)");
160 foreach ($warnings as $warn => $num) {
161 echo "$warn: $num\n";
162 $tot_warnings +
= $num;
164 echo "Total rows: $rows\nTotal warnings: $tot_warnings\n";