import: Fix progress output ETA when skipping files
[nagios-reports-module.git] / verify_import.php
blobf59fe142b1d870166a2d3eb8f33b0bbacb171c90
1 #!/usr/bin/php
2 <?php
4 $table = 'report_data';
6 for ($i = 1; $i < $argc; $i++) {
7 if ($argv[$i] === '--db-table') {
8 $table = $argv[++$i];
12 function crash($msg = false)
14 if ($msg) {
15 echo $msg . "\n";
18 exit(1);
22 $warnings = array();
23 function warn($msg)
25 global $warnings;
26 # echo $msg . "\n";
28 if (!isset($warnings[$msg]))
29 $warnings[$msg] = 0;
31 $warnings[$msg]++;
34 function get_count($condition = false)
36 global $table;
38 $query = 'SELECT count(id) AS row_count FROM ' . $table;
39 if ($condition)
40 $query .= " WHERE $condition";
41 $result = mysql_query($query);
42 if (!$result)
43 crash("mysql_query($query) failed: " . mysql_error());
44 return mysql_result($result, 0, 'row_count');
47 function human_time($stamp)
49 if (!$stamp)
50 return 0;
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);
57 $ret = "";
58 if ($days)
59 $ret .= $days . "d ";
60 if ($hours)
61 $ret .= $hours . "h ";
62 if ($mins)
63 $ret .= $mins . "m ";
64 $ret .= $secs . "s";
66 return $ret;
69 $db = mysql_connect('localhost', 'monitor', 'monitor');
70 if (!$db)
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) ' .
91 'ORDER BY id';
92 $result = mysql_query($query);
93 if (!$result)
94 crash("mysql_query() failed: " . mysql_error());
96 $tot_rows = mysql_num_rows($result);
97 $running = false;
98 $rows = 0;
99 while ($row = mysql_fetch_array($result)) {
100 $when = $row['timestamp'];
101 $type = $row['event_type'];
102 $id = $row['id'];
103 $obj_name = $row['host_name'] . ';' . $row['service_description'];
104 $rows++;
106 switch ($type) {
107 case 100:
108 if ($running)
109 warn("Start when already running ($id)");
110 $running = true;
111 break;
113 case 103:
114 if (!$running)
115 warn("Stop when not running ($id)");
117 $running = false;
118 break;
120 case 1103:
121 if (!isset($dt[$obj_name]))
122 $dt[$obj_name] = $when;
123 $dt[$obj_name]++;
124 break;
125 case 1104:
126 if (!isset($dt[$obj_name]))
127 warn("Downtime stop without downtime start for $obj_name\n");
128 else {
129 $diff = $when - $dt[$obj_name];
130 if ($diff > 86400)
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]);
137 break;
139 default:
140 if (!$running)
141 warn("Event when not running ($id)");
143 $running = true;
144 break;
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;
154 if ($diff > 7500)
155 warn("Unfinished downtime for $obj_name (" . human_time($diff) ."), " .
156 "started " . date("Y-m-d H:i:s", $then) . " ($then)");
158 echo "\n";
159 $tot_warnings = 0;
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";