-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.php
170 lines (130 loc) · 4.14 KB
/
analyzer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
// Dependency
require_once(dirname(__FILE__) . '/includes/functions.php');
// Generate file selection from logs directory
$target_dir = dirname(__FILE__) . '/logs';
$file_selection = scandir($target_dir);
// Filenames to exclude
$exclude_names = array('.', '..', '.DS_Store');
$valid_names = array();
foreach($file_selection as $name) {
if(!in_array($name, $exclude_names))
$valid_names[$name] = $target_dir . '/' . $name;
}
// Query
$line_no = @(int)$_GET['line_no'] ? (int)$_GET['line_no'] : false;
$search_term = @!empty($_GET['search_term']) ? $_GET['search_term'] : false;
// Open requested file
$target_file = array_key_exists(@$_GET['f'], $valid_names) ? $valid_names[$_GET['f']] : false;
$target_name = $target_file ? basename($target_file) : false;
$f = new LogAnalyzer($target_file);
// Benchmarks
$l_start = benchmark_start();
$line = $f->stream_file_get_line($line_no);
$l_benchmark = benchmark($l_start);
$c_start = benchmark_start();
$data = $f->stream_file_search($search_term);
$c_benchmark = benchmark($c_start);
$fields = Aggregator::line_to_fields_array($line, Aggregator::$FIELDS, Aggregator::$FIELDS_REGEX);
// Aggregation
if($data) {
$a_start = benchmark_start();
$agg = Aggregator::aggregate_data($data);
$agg['user_agent'] = aggregate_user_agents($data);
$a_benchmark = benchmark($a_start);
}
?>
<style type="text/css">
.clear {
clear: both;
}
.error {
color: #e00;
}
.left-col {
width: 422px;
float: left;
}
.right-col {
width: 600px;
float: right;
}
#file-selection {
height: 140px;
width: 400px;
overflow: auto;
border: 1px solid #eee;
padding: 10px;
margin: 0;
}
</style>
<h1>Log Analyzer</h1>
<?php if($f->error) : ?>
<div class="error"><?php echo $f->error; ?></div>
<?php endif; ?>
<div class="left-col">
<h2>File Selection</h2>
<p>Found <?php echo count($valid_names); ?> files in <?php echo $target_dir; ?>:</p>
<ul id="file-selection">
<?php foreach($valid_names as $name => $file_path) : ?>
<li><a href="?f=<?php echo $name; ?>"><?php echo $name; ?></a></li>
<?php endforeach; ?>
</ul>
<?php if($target_name): ?>
<form action="" method="get">
<p>
Get line no.: <input type="text" name="line_no" size="5" value="<?php echo @$_GET['line_no']; ?>" />
<input type="hidden" name="f" value="<?php echo $target_name; ?>" />
<input type="Submit" />
</p>
</form>
<?php endif; ?>
<?php if($line) : ?>
<h3>stream_file_get_line(<?php echo $line_no; ?>)</h3>
<p><pre><?php print_r($fields); ?></pre></p>
<p>Finished in <?php echo $l_benchmark ?> seconds</p>
<?php endif; ?>
</div>
<div class="right-col">
<?php if($target_name): ?>
<form action="" method="get">
<p>
Search term:<br /><input type="text" name="search_term" value="<?php echo @$_GET['search_term']; ?>" />
<small>(Limit <?php echo number_format($f::$SEARCH_LIMIT, 0, '', ','); ?> results)</small>
</p>
<p>
<input type="hidden" name="f" value="<?php echo $target_name; ?>" />
<input type="Submit" />
</p>
</form>
<?php endif; ?>
<?php if($data) : ?>
<h3>stream_file_search("<?php echo $search_term; ?>")</h3>
<p>
<?php echo number_format($f->results_found, 0, '', ','); ?> results <?php echo "in {$c_benchmark} seconds" ?><br />
Aggregated user agents in <?php echo $a_benchmark; ?> seconds
</p>
<table class="results" border="1" width="95%">
<?php foreach($agg as $section => $data) : ?>
<tr>
<th colspan="2"><?php echo $section; ?></th>
</tr>
<?php foreach($data as $field => $score) : ?>
<tr>
<td width="50%"><?php echo $field; ?></td>
<td>
<?php if(is_array($score)) : ?>
<?php foreach($score as $k => $v) : ?>
<?php echo $k; ?> (<?php echo $v; ?>)<br />
<?php endforeach; ?>
<?php else : ?>
<?php echo $score; ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
<div class="clear"></div>