forked from nidebr/as-stats-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gengraph.php
227 lines (186 loc) · 6.38 KB
/
gengraph.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/*
* $Id$
*
* written by Manuel Kasper <[email protected]> for Monzoon Networks AG
*/
require_once('func.inc');
$as = $_GET['as'];
if (!preg_match("/^[0-9a-zA-Z]+$/", $as))
die("Invalid AS");
header("Content-Type: image/png");
$width = $default_graph_width;
$height = $default_graph_height;
if (isset($_GET['width']))
$width = (int)$_GET['width'];
if (isset($_GET['height']))
$height = (int)$_GET['height'];
$v6_el = "";
if (@$_GET['v'] == 6)
$v6_el = "v6_";
if(isset($_GET['peerusage']) && $_GET['peerusage'] == '1')
$peerusage = 1;
else
$peerusage = 0;
$knownlinks = getknownlinks();
if(isset($_GET['selected_links'])){
$reverse = array();
foreach($knownlinks as $link)
$reverse[$link['tag']] = array('color' => $link['color'], 'descr' => $link['descr']);
$links = array();
foreach(explode(',', $_GET['selected_links']) as $tag){
if (preg_match('/[^a-zA-Z0-9_]/', $tag))
continue;
$link = array(
'tag' => $tag,
'color' => $reverse[$tag]['color'],
'descr' => $reverse[$tag]['descr']
);
$links[] = $link;
}
$knownlinks = $links;
}
$rrdfile = getRRDFileForAS($as, $peerusage);
if (!isset($_GET['keep_all_links'])) {
exec(sprintf("/usr/bin/rrdtool info '%s' | grep '\.last_ds'", $rrdfile), $rrdinfo, $res);
if ($res == 0) {
$links_to_remove = array();
foreach ($knownlinks as $key => $link) {
$needed = false;
foreach (array('in', 'out', 'v6_in', 'v6_out') as $e) {
if (!in_array(sprintf('ds[%s_%s].last_ds = "U"', $link['tag'], $e), $rrdinfo)) {
$needed = true;
}
}
if (!$needed) {
$links_to_remove[] = $key;
}
}
foreach ($links_to_remove as $link_key) {
unset($knownlinks[$link_key]);
}
}
}
$knownlinks = update_palette($knownlinks);
if (!isset($_GET['keep_link_descr'])) {
foreach ($knownlinks as $key => $link) {
$knownlinks[$key]['descr'] = trim(preg_replace('/\{([^{}]*+|(?R))*\}/', '', $knownlinks[$key]['descr']));
$knownlinks[$key]['descr'] = trim(preg_replace('/\[([^\[\]]*+|(?R))*\]/', '', $knownlinks[$key]['descr']));
$knownlinks[$key]['descr'] = trim(preg_replace('!\s+!', ' ', $knownlinks[$key]['descr']));
}
}
if ($compat_rrdtool12) {
/* cannot use full-size-mode - must estimate height/width */
$height -= 65;
$width -= 81;
if ($vertical_label)
$width -= 16;
}
$cmd = "$rrdtool graph - " .
"--slope-mode --alt-autoscale -u 0 -l 0 --imgformat=PNG --base=1000 --height=$height --width=$width " .
"--color BACK#ffffffff --color SHADEA#ffffff00 --color SHADEB#ffffff00 ";
if (!$compat_rrdtool12)
$cmd .= "--full-size-mode ";
if ($vertical_label) {
if($outispositive)
$cmd .= "--vertical-label '<- IN | OUT ->' ";
else
$cmd .= "--vertical-label '<- OUT | IN ->' ";
}
if($showtitledetail && @$_GET['dname'] != "")
$cmd .= "--title " . escapeshellarg($_GET['dname']) . " ";
else
if (isset($_GET['v']) && is_numeric($_GET['v']))
$cmd .= "--title IPv" . $_GET['v'] . " ";
if (isset($_GET['nolegend']))
$cmd .= "--no-legend ";
if (isset($_GET['start']) && is_numeric($_GET['start']))
$cmd .= "--start " . $_GET['start'] . " ";
if (isset($_GET['end']) && is_numeric($_GET['end']))
$cmd .= "--end " . $_GET['end'] . " ";
/* geneate RRD DEFs */
foreach ($knownlinks as $link) {
$cmd .= "DEF:{$link['tag']}_{$v6_el}in=\"$rrdfile\":{$link['tag']}_{$v6_el}in:AVERAGE ";
$cmd .= "DEF:{$link['tag']}_{$v6_el}out=\"$rrdfile\":{$link['tag']}_{$v6_el}out:AVERAGE ";
}
if ($compat_rrdtool12) {
/* generate a CDEF for each DEF to multiply by 8 (bytes to bits), and reverse for outbound */
foreach ($knownlinks as $link) {
if ($outispositive) {
$cmd .= "CDEF:{$link['tag']}_{$v6_el}in_bits={$link['tag']}_{$v6_el}in,-8,* ";
$cmd .= "CDEF:{$link['tag']}_{$v6_el}out_bits={$link['tag']}_{$v6_el}out,8,* ";
} else {
$cmd .= "CDEF:{$link['tag']}_{$v6_el}in_bits={$link['tag']}_{$v6_el}in,8,* ";
$cmd .= "CDEF:{$link['tag']}_{$v6_el}out_bits={$link['tag']}_{$v6_el}out,-8,* ";
}
}
} else {
$tot_in_bits = "CDEF:tot_in_bits=0";
$tot_out_bits = "CDEF:tot_out_bits=0";
/* generate a CDEF for each DEF to multiply by 8 (bytes to bits), and reverse for outbound */
foreach ($knownlinks as $link) {
$cmd .= "CDEF:{$link['tag']}_{$v6_el}in_bits_pos={$link['tag']}_{$v6_el}in,8,* ";
$cmd .= "CDEF:{$link['tag']}_{$v6_el}out_bits_pos={$link['tag']}_{$v6_el}out,8,* ";
$tot_in_bits .= ",{$link['tag']}_{$v6_el}in_bits_pos,ADDNAN";
$tot_out_bits .= ",{$link['tag']}_{$v6_el}out_bits_pos,ADDNAN";
}
$cmd .= "$tot_in_bits ";
$cmd .= "$tot_out_bits ";
$cmd .= "VDEF:tot_in_bits_95th_pos=tot_in_bits,95,PERCENT ";
$cmd .= "VDEF:tot_out_bits_95th_pos=tot_out_bits,95,PERCENT ";
if ($outispositive) {
$cmd .= "CDEF:tot_in_bits_95th=tot_in_bits,POP,tot_in_bits_95th_pos,-1,* ";
$cmd .= "CDEF:tot_out_bits_95th=tot_out_bits,POP,tot_out_bits_95th_pos,1,* ";
} else {
$cmd .= "CDEF:tot_in_bits_95th=tot_in_bits,POP,tot_in_bits_95th_pos,1,* ";
$cmd .= "CDEF:tot_out_bits_95th=tot_out_bits,POP,tot_out_bits_95th_pos,-1,* ";
}
foreach ($knownlinks as $link) {
if ($outispositive) {
$cmd .= "CDEF:{$link['tag']}_{$v6_el}in_bits={$link['tag']}_{$v6_el}in_bits_pos,-1,* ";
$cmd .= "CDEF:{$link['tag']}_{$v6_el}out_bits={$link['tag']}_{$v6_el}out_bits_pos,1,* ";
} else {
$cmd .= "CDEF:{$link['tag']}_{$v6_el}out_bits={$link['tag']}_{$v6_el}out_bits_pos,-1,* ";
$cmd .= "CDEF:{$link['tag']}_{$v6_el}in_bits={$link['tag']}_{$v6_el}in_bits_pos,1,* ";
}
}
}
/* generate graph area/stack for inbound */
$i = 0;
foreach ($knownlinks as $link) {
if ($outispositive && $brighten_negative)
$col = $link['color'] . "BB";
else
$col = $link['color'];
$descr = str_replace(':', '\:', $link['descr']); # Escaping colons in description
$cmd .= "AREA:{$link['tag']}_{$v6_el}in_bits#{$col}:\"{$descr}\"";
if ($i > 0)
$cmd .= ":STACK";
$cmd .= " ";
$i++;
}
/* generate graph area/stack for outbound */
$i = 0;
foreach ($knownlinks as $link) {
if ($outispositive || !$brighten_negative)
$col = $link['color'];
else
$col = $link['color'] . "BB";
$cmd .= "AREA:{$link['tag']}_{$v6_el}out_bits#{$col}:";
if ($i > 0)
$cmd .= ":STACK";
$cmd .= " ";
$i++;
}
$cmd .= "COMMENT:' \\n' ";
if ($show95th && !$compat_rrdtool12) {
$cmd .= "LINE1:tot_in_bits_95th#FF0000 ";
$cmd .= "LINE1:tot_out_bits_95th#FF0000 ";
$cmd .= "GPRINT:tot_in_bits_95th_pos:'95th in %6.2lf%s' ";
$cmd .= "GPRINT:tot_out_bits_95th_pos:'/ 95th out %6.2lf%s\\n' ";
}
# zero line
$cmd .= "HRULE:0#00000080";
passthru($cmd);
exit;
?>