/
status_rrd_summary.php
188 lines (168 loc) · 6.62 KB
/
status_rrd_summary.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* status_rrd_summary.php
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2010-2023 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once("guiconfig.inc");
// Using too small a divisor for traffic sums will likely exceed PHP_MAX_INT
$unitlist = array("MiB" => 2, "GiB" => 3, "TiB" => 4, "PiB" => 5);
// validate interface
$iflist = get_configured_interface_with_descr();
$interface = filter_input(INPUT_POST, "interface", FILTER_SANITIZE_STRING);
$interface = array_key_exists($interface, $iflist) ? $interface : "wan";
// get rrd bounds for interface
$rrd = "{$interface}-traffic.rrd";
$data = fetch_rrd_summary($rrd, "-10y", "now");
$firstyear = date("Y", ($data["first"] > 0) ? $data["first"] : time() );
$lastyear = date("Y", ($data["last"] > 0) ? $data["last"] : time() );
// validate year
$startyear = filter_input(INPUT_POST, "startyear", FILTER_VALIDATE_INT,
array("options" => array("min_range" => 0, "max_range" => $lastyear)) );
$startyear = ($startyear >= $firstyear || $startyear == "0") ? $startyear : $lastyear;
// validate startday
$startday = filter_input(INPUT_POST, "startday", FILTER_VALIDATE_INT,
array("options" => array("min_range" => 1, "max_range" => 28)) );
$startday = ($startday > 0) ? $startday : 1;
// validate units
$units = filter_input(INPUT_POST, "units", FILTER_VALIDATE_INT,
array("options" => array("min_range" => min(array_values($unitlist)), "max_range" => max(array_values($unitlist)) )) );
$units = in_array($units, $unitlist) ? $units : $unitlist["GiB"];
// 1:timestamp 2:inpass 3:outpass 4:inblock 5:outblock 6:inpass6 7:outpass6 8:inblock6 9:outblock6
// total_in = $2 + $4 + $6 + $8; total_out = $3 + $7 (blocked outbound traffic is excluded)
function fetch_rrd_summary($rrd, $start, $end, $units=2, $resolution=24*60*60) {
$traffic = array();
$rrd = escapeshellarg("/var/db/rrd/{$rrd}");
$start = escapeshellarg($start);
$end = escapeshellarg($end);
exec("/usr/local/bin/rrdtool fetch {$rrd} AVERAGE -r {$resolution} -s {$start} -e {$end}", $traffic);
$divisor = 1024 ** $units;
$t_keys = preg_split("/\s+/", $traffic[0]); // grab keys
//print "time=$t_first st=$start end=$end div=$divisor res=$resolution";
$traffic = preg_grep("/^[0-9]+:/", $traffic); // select data rows
$traffic = preg_grep("/nan/", $traffic, PREG_GREP_INVERT); // filter nan rows
$data = array( "first" => time() );
foreach ( $traffic as $t ) {
$t = preg_split("/[\s:]+/", $t);
if (count($t) != count($t_keys)) continue; // error: field mismatch
$data["first"] = min($t[0], $data["first"]);
$data["last"] = max($t[0], $data["last"]);
foreach ($t_keys as $i => $k) { $data[$k] += ($t[$i] / $divisor) * $resolution; }
}
// Adjust for resolution
foreach ($t_keys as $k) {
if (preg_match("/^outblock/", $k)) continue;
$data["total_in"] += preg_match("/^in/", $k) ? $data[$k] : 0;
$data["total_out"] += preg_match("/^out/", $k) ? $data[$k] : 0;
}
return $data;
}
function print_rrd_summary($rrd, $units, $startyear, $startday) {
$data = fetch_rrd_summary($rrd, "-10y", "now");
$first = $data["first"];
$last = $data["last"];
global $unitlist; $u = array_flip($unitlist);
foreach (range(date("Y", $last), date("Y", $first), 1) as $year) {
if ($startyear > 0 && $startyear != $year) continue;
foreach (range(12, 1, -1) as $month) {
$start = strtotime(date("{$month}/{$startday}/{$year}"));
$end = strtotime("+1 month", $start);
/* End time should be one second less than the start of the
* next month otherwise RRDtool will include the first day
* of the next month in the summary. */
$end = strtotime("-1 second", $end);
if ($start > $last || $end < $first) continue;
if ($start < $first) $start = $first;
/* Use at-time format as unix timestamps can be problematic in various ways. */
$data = fetch_rrd_summary($rrd, date('m/d/Y H:i', $start), date('m/d/Y H:i', $end), $units, 24*60*60);
?>
<tr>
<td><?=date("Y-m-d", $start); ?> to <?=date("Y-m-d", $end); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_in"], $u[$units]); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_out"], $u[$units]); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_in"] + $data["total_out"], $u[$units]); ?></td>
</tr>
<?php
}
}
}
$pgtitle = array("Status", "RRD Summary");
include_once("head.inc");
$form = new Form(false);
$section = new Form_Section('Select RRD Parameters');
$if_options = array();
foreach ($iflist as $i => $ifdesc) {
$if_options[$i] = $ifdesc . " (" . $i . ") ";
}
$section->addInput(new Form_Select(
'interface',
'Interface',
$interface,
$if_options
));
$section->addInput(new Form_Select(
'units',
'Units',
$units,
array_flip($unitlist)
));
$section->addInput(new Form_Select(
'startyear',
'Year',
$startyear,
array("0" => "All") + array_combine(range($lastyear, $firstyear, -1), range($lastyear, $firstyear, -1))
));
$section->addInput(new Form_Select(
'startday',
'Start Day',
$startday,
array_combine(range(1, 28, 1), range(1, 28, 1))
));
$form->add($section);
print($form);
?>
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title"><?=gettext("RRD Summary")?></h2></div>
<div class="panel-body">
<div class="table-responsive">
<strong>Estimated monthly traffic for given time period beginning on day <?=$startday; ?> of each month</strong>
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable id="rrdsummary">
<thead>
<tr>
<th data-sortable-type="alpha"><?=gettext("Period")?></th>
<th data-sortable-type="numeric"><?=gettext("Avg Traffic In")?></th>
<th data-sortable-type="numeric"><?=gettext("Avg Traffic Out")?></th>
<th data-sortable-type="numeric"><?=gettext("Est Total Traffic")?></th>
</tr>
</thead>
<tbody>
<?php print_rrd_summary($rrd, $units, $startyear, $startday); ?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
//<![CDATA[
events.push(function(){
$('#interface, #units, #startyear, #startday').on('change', function(){
$(this).parents('form').submit();
});
});
//]]>
</script>
<?php include_once("foot.inc"); ?>