-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreport.php
More file actions
139 lines (113 loc) · 2.12 KB
/
report.php
File metadata and controls
139 lines (113 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
date_default_timezone_set("Africa/Johannesburg");
global $__PROGRAM__;
function processFile($filename)
{
$fp = fopen($filename, "r");
$data = array();
$key = "";
while ($line = fgets($fp))
{
if (strpos($line, "Start:") === 0)
{
$date = str_replace("Start: ", "", trim($line));
$date = strtotime($date);
$key = $date;
}
else if (strpos($line, "HOST:") === FALSE)
{
$data[$key][] = trim($line);
}
}
$result = processData($data);
ksort($result);
return $result;
}
function processLine($line)
{
$array = preg_split("/[\s]+/", $line);
return $array[5];
}
function processBlock($block)
{
return processLine($block[1]);
}
function processData($data)
{
$result = array();
foreach ($data as $date=>$day)
{
$ping = processBlock($day);
$result[$date] = $ping;
}
return $result;
}
function dumpData($data)
{
foreach ($data as $date=>$ping)
{
echo "\"".date("Y-m-d H:i:s", $date)."\",".round($ping)."\n";
}
}
function avg($data)
{
$total = 0;
foreach ($data as $item)
{
$total += $item;
}
return round($total / count($data));
}
function getHourlyAvg($data)
{
// Sort the date into hour blocks
$tmpoutput = array();
foreach ($data as $date=>$ping)
{
$hour = date("H:i", $date);
$day = date("Y-m-d", $date);
$tmpoutput[$hour][$day] = round($ping);
}
ksort($tmpoutput);
$output = array();
foreach ($tmpoutput as $hour=>$pings)
{
$avg = avg($pings);
echo "\"$hour\",$avg\n";
$output[$hour] = $avg;
}
}
function printUsage()
{
global $__PROGRAM__;
echo <<<HEREDOC
Usage: $__PROGRAM__ [--] [options] <file>
If the program is being invoked from the commandline with PHP
you need to use -- to prevent PHP from trying to parse more
command line options.
-h Group by hours
HEREDOC;
}
function main($argc, $argv)
{
global $__PROGRAM__;
$__PROGRAM__ = $argv[0];
$showHourly = false;
$filename = $argv[1];
if ($argc == 1)
{
printUsage();
exit(1);
}
else if ($argc == 3 && $argv[1] == "-h")
{
$showHourly = true;
$filename = $argv[2];
}
$data = processFile($filename);
if ($showHourly)
getHourlyAvg($data);
else
dumpData($data);
}
main(count($argv), $argv);