-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
133 lines (92 loc) · 3.44 KB
/
parser.php
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
<?php
//Check Inputs
$cmdOptions = getopt('f:', array('file:'));
if (empty($cmdOptions))
exit("\nError: Input file is missing. \n\n Usage: $argv[0] --file filename\n\n");
echo "Initializing the engine \n";
$file = isset($cmdOptions['file']) ? $cmdOptions['file'] : $cmdOptions['f'];
//Validate the file
if(file_exists($file))
if(is_readable($file))
echo "Reading File.\n";
else
exit( "Error: Cannot Read file: $file . Exiting. \n");
else
exit("Error: File Does not Exist. Exiting. \n");
//Types to track
$types = array('studioLoad', 'TOMCAT', 'generateWar', 'CLOUD_JEE', 'WAR', 'EAR', 'CLOUD_FOUNDRY');
$data = array();
$months = array(
'Jan' => array( 'counts' => array()),
'Feb' => array( 'counts' => array()),
'Mar' => array( 'counts' => array()),
'Apr' => array( 'counts' => array()),
'May' => array( 'counts' => array()),
'Jun' => array( 'counts' => array()),
'Jul' => array( 'counts' => array()),
'Aug' => array( 'counts' => array()),
'Sep' => array( 'counts' => array()),
'Oct' => array( 'counts' => array()),
'Nov' => array( 'counts' => array()),
'Dec' => array( 'counts' => array())
);
//Parse the Log File
echo "Parsing the File\n";
$handle = popen("grep '/img/blank.gif?op=' " .$file , "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
//Find Year, Month
preg_match('/\[(.+?)\/(.+?)\/(.+?):.*\]/', $line, $matches);
// Insert the Year in to the Result Array
if( ! array_key_exists($matches[3], $data))
$data[$matches[3]] = $months;
//Find the type
preg_match('/op=(.*?)&/', $line, $type);
//Add the Data to the curresponding Month.
//array_push($data[$matches[3]][$matches[2]]['raw'], $line);
if(in_array($type[1], $types))
{
if( ! array_key_exists($type[1], $data[$matches[3]][$matches[2]]['counts']))
$data[$matches[3]][$matches[2]]['counts'][$type[1]] = 1;
else
$data[$matches[3]][$matches[2]]['counts'][$type[1]]++;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
pclose($handle);
}
//Display The Result
echo "Preparing the result\n\n";
echo "\t\t============================================== Analytics Report =====================================================\n"
."\t\t=====================================================================================================================";
foreach ($data as $year => $months) {
echo "\n\n\t\tYear : $year \n\n\n\t\t\t";
foreach ($months as $monthName => $value) {
echo "\t $monthName ";
}
echo "\tTotal";
echo "\n\t\t\t\t --- \t --- \t --- \t --- \t --- \t --- \t --- \t --- \t --- \t --- \t --- \t ---\t-----\n\t";
foreach ($types as $type) {
$totalForYear = 0;
printf("\n\n\t\t%-10s", $type);
foreach ($months as $month => $value) {
if(isset($value['counts'][$type]))
{
echo "\t " . $value['counts'][$type];
$totalForYear += $value['counts'][$type];
}
else
{
echo "\t 0";
}
}
echo "\t$totalForYear";
echo "\n\t";
}
}
echo "\n\n\t\t-------------------------------------------------------------------------------------------------------------------";
echo "\n\t\t============================================== Analytics Ends =====================================================\n"
."\t\t===================================================================================================================\n\n";
?>