-
Notifications
You must be signed in to change notification settings - Fork 0
/
guifi_graphs.inc.php
377 lines (344 loc) · 14.3 KB
/
guifi_graphs.inc.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* @file guifi_graphs.inc.php
*/
/**
* guifi_graph_detail
* outputs a page with node detailed graphs
*/
function guifi_graph_detail() {
$type = $_GET['type'];
if (isset($_GET['device']))
$device_id=$_GET['device'];
else if (isset($_GET['radio']))
$device_id=$_GET['radio'];
if (isset($device_id)) {
$query = db_query(
"SELECT r.id, r.nick, n.title, r.nid, l.zone_id " .
"FROM {guifi_devices} r, {node} n, {guifi_location} l " .
"WHERE r.id=%d " .
" AND n.nid=r.nid " .
" AND n.nid = l.id",
$device_id);
$radio = db_fetch_object($query);
$zid = $radio->zone_id;
}
if ($type=='supernode') {
$node = node_load(array('nid' => $_GET['node']));
if ($node->graph_server == -1) {
$rows[] = array(t('This node has the graphs disabled.'));
return array_merge($rows);
}
if (!empty($node->graph_server))
$gs = node_load(array('nid' => $node->graph_server));
else
$gs = node_load(array('nid' => guifi_graphs_get_server($node->id,'node')));
} else {
if ($radio->graph_server == -1) {
$rows[] = array(t('This device has the graphs disabled.'));
return array_merge($rows);
}
if (!empty($radio->graph_server))
$gs = node_load(array('nid' => $radio->graph_server));
else
$gs = node_load(array('nid' => guifi_graphs_get_server($radio->id,'device')));
}
$help = t('Here you have a detailed view of the available information for several periods of time (daily, weekly, monthly and yearly). You can obtain a detailed graph for a given period of time by entering the period in the boxes below.');
$args = array('type' => $type,
'node' => $_GET['node'],
'device' => $device_id
);
if (isset($_GET['direction']))
$args['direction']=$_GET['direction'];
switch ($type) {
case 'clients':
$title = '<a href="'.base_path().'guifi/device/'.$radio->id.'">'.$radio->nick.'</a> '.t('at').' '.'<a href='.base_path().'node/'.$radio->nid.'>'.$radio->title.'</a>';
$help .= '<br />'.t('The clients graph displays the top clients by transit.');
break;
case 'supernode':
$zid = $node->zone_id;
$title = '<a href='.base_path().'node/'.$_GET['node'].'>'.$node->title.'</a>';
$help .= '<br />'.t('Supernode graph displays the transit of each radio.');
break;
case 'radio':
case 'device':
$help= '<br />'.t('The radio graph show in & out transit.');
case 'pings':
if ($type != 'radio')
$help= '<br />'.t('The ping graph displays the latency and availability. High latency usually means bad connection. Yellow means % of failed pings, could be some yellow on the graphs, but must not reach value of 100, if the value reaches 100, that means that the radio is offline.');
$title = $radio->nick.' '.t('at').' '.'<a href='.base_path().'node/'.$radio->nid.'>'.$radio->title.'</a>';
break;
}
$secs_day = 60*60*24;
drupal_set_breadcrumb(guifi_zone_ariadna($zid));
$output = '<div id="guifi">';
// $rows[] = array(t('enter a timeframe to graph a customized period'));
$output .= '<h3>'.$type.'</h3>'.$help;
switch ($type) {
}
if (isset($_POST['date1']))
$date1 = $_POST['date1'];
else
$date1 = date('d-m-Y H:i',time()-60*60*2);
if (isset($_POST['date2']))
$date2 = $_POST['date2'];
else
$date2 = date('d-m-Y H:i',time()-300);
$str = '<form name="form_timespan_selector" method="post"><strong> '.t('From:');
$str .= ' </strong><input type="text" name="date1" id=\'date1\' size=\'14\' value="'.$date1.'"> <input type="image"
src="'.base_path(). drupal_get_path('module', 'guifi').'/contrib/calendar.gif" alt="Start date selector" onclick="return showCalendar(\'date1\');"> ';
$str .= '<strong>'.t('To:').' </strong> <input type="text" name="date2" id=\'date2\' size="14" value="'.$date2.'"> ';
$str .= '<input type="image" src="'.base_path(). drupal_get_path('module', 'guifi').'/contrib/calendar.gif" alt="End date selector" align="absmiddle" onclick="return showCalendar(\'date2\');"> ';
$str .= '<input type="submit" name="button_refresh" action="submit" value="refresh">';
$rows[] = array($str);
if (isset($_POST['date1'])) {
list($day,$month,$year,$hour,$min) = sscanf($_POST['date1'],'%d-%d-%d %d:%d');
$start = mktime($hour, $min, 0, $month, $day, $year);
list($day,$month,$year,$hour,$min) = sscanf($_POST['date2'],'%d-%d-%d %d:%d');
$end = mktime($hour, $min, 0, $month, $day, $year);
$rows[] = array(t('customized graph'));
$rows[] = array('<img src="'.guifi_cnml_call_service($gs->var['url'],'graph',$args,sprintf('start=%d&end=%d">',$start,$end)));
}
$rows[] = array(t('day'));
$rows[] = array('<img src="'.guifi_cnml_call_service($gs->var['url'],'graph',$args,sprintf('start=-%d&end=%d">',$secs_day,-300)));
$rows[] = array(t('week'));
$rows[] = array('<img src="'.guifi_cnml_call_service($gs->var['url'],'graph',$args,sprintf('start=-%d&end=%d">',$secs_day * 7,-300)));
$rows[] = array(t('month'));
$rows[] = array('<img src="'.guifi_cnml_call_service($gs->var['url'],'graph',$args,sprintf('start=-%d&end=%d">',$secs_day * 31,-300)));
$rows[] = array(t('year'));
$rows[] = array('<img src="'.guifi_cnml_call_service($gs->var['url'],'graph',$args,sprintf('start=-%d&end=%d">',$secs_day * 365,-300)));
$output .= theme('table', NULL, array_merge($rows));
$output .= "</div>"._guifi_script_calendar();
drupal_set_html_head('<script type="text/javascript" src="'.base_path(). drupal_get_path('module', 'guifi').'/contrib/calendar.js"></script> <script type="text/javascript" src="'.base_path(). drupal_get_path('module', 'guifi').'/contrib/lang/calendar-ca.js"></script></script> <script type="text/javascript" src="'.base_path(). drupal_get_path('module', 'guifi').'/contrib/calendar-setup.js"></script>');
drupal_set_title(t('graph details for').' '.$title);
return print theme('page', $output, t('graph details for').' '.$title);
}
function get_SSID_radio($radio) {
$querySSID = db_query("SELECT r.ssid FROM {guifi_radios} r WHERE r.id=%d",$radio);
$SSID = db_fetch_object($querySSID);
return $SSID->ssid;
}
/**
*
* @param $id
*
* @param $type
* Possible values are 'device' (default), 'node', 'zone', 'radio'
*
* @todo 'device' and 'radio' are the same. Unify.
*/
function guifi_graphs_get_server($id, $type='device') {
switch ($type) {
case 'node':
$n = node_load(array('nid' => $id));
if ($n->graph_server)
return $n->graph_server;
else
return guifi_graphs_get_server($n->zone_id,'zone');
case 'zone':
$z = node_load(array('nid' => $id));
if ($z->graph_server)
return $z->graph_server;
else
if ($z->master)
return guifi_graphs_get_server($z->master,'zone');
else
return FALSE;
case 'device':
case 'radio':
$d = db_fetch_object(db_query('SELECT nid, graph_server FROM {guifi_devices} WHERE id=%d',$id));
if ($d->graph_server)
return $d->graph_server;
$countRadios = db_fetch_object(db_query(
"SELECT count(*) c " .
"FROM {guifi_radios} " .
"WHERE nid=%d " .
" AND mode='ap'",$d->nid));
if ($countRadios->c > 0)
// node has APs, inherits node graph server
return guifi_graphs_get_server($d->nid,'node');
// client: finding an ap/client link for this node, inherits from remote node
$link = db_fetch_object(db_query(
"SELECT " .
" l2.device_id ".
"FROM {guifi_links} l1, " .
" {guifi_links} l2 " .
"WHERE l1.id=l2.id " .
" AND l1.nid != l2.nid " .
" AND l1.link_type='ap/client' " .
" AND l1.nid=%d ",
$d->nid));
if ($link)
return guifi_graphs_get_server($link->device_id,'device');
else
return guifi_graphs_get_server($d->nid,'node');
}
return FALSE;
}
/**
* guifi_device_graph_overview
* outputs an overiew graph of the device
**/
function guifi_device_graph_overview($radio) {
guifi_log(GUIFILOG_TRACE,'guifi_device_graph_overview()',$radio);
if (isset($radio['mode']))
$radio['type'] = 'radio';
if ($radio['graph_server'] == -1) {
$rows[] = array(t('This device has the graphs disabled.'));
return array_merge($rows);
}
if (empty($radio['graph_server']))
$gs = guifi_service_load(guifi_graphs_get_server($radio['id'],'device'));
else
$gs = guifi_service_load($radio['graph_server']);
if (substr($server_mrtg,0,3) == "fot")
{
$ssid=get_SSID_radio($radio['id']);
$ssid=strtolower($ssid);
$mrtg_url=substr($server_mrtg,3);
$rows[] = array('<a href="'.$mrtg_url.'/14all.cgi?log='.$ssid.'_6&cfg=mrtg.cfg" target="_blank" > <img src="'.$mrtg_url.'/14all.cgi?log='.$ssid.'_6&cfg=mrtg.cfg&png=weekly"></a>');
$rows[] = array('<a href="'.$mrtg_url.'/14all.cgi?log='.$ssid.'_ping&cfg=mrtg.cfg" target="_blank" > <img src="'.$mrtg_url.'/14all.cgi?log='.$ssid.'_ping&cfg=mrtg.cfg&png=weekly"></a>');
return array_merge($rows);
}
else
{
$clients = db_fetch_object(db_query(
"SELECT count(c.id) count " .
"FROM {guifi_links} c " .
"WHERE c.device_id=%d " .
" AND c.link_type IN ('wds','ap/client','bridge')",
$radio['id']));
$args = array('type' => 'clients',
'node' => $radio['nid'],
'device' => $radio['id']);
if ($clients->count > 1) // several clients, Totals In & Out
{
$rows[] = array(array(
'data'=> '<a href='.base_path().'guifi/graph_detail?'.
guifi_cnml_args($args,'direction=in').
'><img src="'.
guifi_cnml_call_service($gs->var['url'],'graph',$args,'direction=in').
'"></a>',
'align' => 'center'));
$rows[] = array(array(
'data'=> '<a href='.base_path().'guifi/graph_detail?'.
guifi_cnml_args($args,'direction=out').
'><img src="'.
guifi_cnml_call_service($gs->var['url'],'graph',$args,'direction=out').
'"></a>',
'align' => 'center'));
} else if (($radio['type']=='radio') or
($radio['variable']['mrtg_index']!='')) {
$args['type'] = 'device';
$rows[] = array(array(
'data'=> '<a href='.base_path().'guifi/graph_detail?'.
guifi_cnml_args($args).
'><img src="'.
guifi_cnml_call_service($gs->var['url'],'graph',$args).
'"></a>',
'align' => 'center'));
}
$args['type'] = 'pings';
$rows[] = array(array(
'data'=> '<a href='.base_path().'guifi/graph_detail?'.
guifi_cnml_args($args).
'><img src="'.
guifi_cnml_call_service($gs->var['url'],'graph',$args).
'"></a>',
'align' => 'center'));
return array_merge($rows);
}
}
/**
* guifi_get_availability
**/
function guifi_graphs_get_pings($hostname, $start = NULL, $end = NULL) {
$var = array();
$var['max_latency'] = 0;
$var['min_latency'] = NULL;
$var['last'] = NULL;
$var['avg_latency'] = 0;
$var['succeed'] = 0;
$var['samples'] = 0;
if ($start == NULL)
$start = time() - 60*60*24*7;
if ($end == NULL)
$end = time() - 300;
// print 'Start/end: '.$start.' '.$end."\n<br />";
$fn = variable_get('rrddb_path','/home/comesfa/mrtg/logs/').guifi_rrdfile($hostname)."_ping.rrd";
// print $fn."\n<br />";
if (file_exists($fn)) {
$cmd = sprintf("%s fetch %s AVERAGE --start=%d --end=%d",variable_get('rrdtool_path','/usr/bin/rrdtool'),$fn,$start,$end);
// print $cmd."\n<br />";
$fp = popen($cmd, "r");
if (isset($fp)) {
while (!feof($fp)) {
$failed = 'nan';
$n = sscanf(fgets($fp),"%d: %f %f",$interval,$failed,$latency);
if (is_numeric($failed) && ($n == 3)) {
$var['succeed'] += $failed;
$last_suceed = $failed;
if ($latency > 0) {
// print $interval.' '.$failed.' '.$latency."\n<br />";
$var['avg_latency'] += $latency;
if ($var['max_latency'] < $latency)
$var['max_latency'] = $latency;
if (($var['min_latency'] > $latency) || ($var['min_latency'] == NULL))
$var['min_latency'] = $latency;
}
$var['last'] = $interval;
$var['samples']++;
}
}
}
pclose($fp);
}
if ($var['samples'] > 0) {
$var['succeed'] = 100 - ($var['succeed'] / $var['samples']);
$var['avg_latency'] = $var['avg_latency'] / $var['samples'];
$var['last_sample'] = date('H:i',$var['last']);
$var['last_succeed'] = 100 - $last_suceed;
}
return $var;
}
function _guifi_script_calendar() {
return "<script type='text/javascript'>
// Initialize the calendar
calendar=NULL;
// This function displays the calendar associated to the input field 'id'
function showCalendar(id) {
var el = document.getElementById(id);
if (calendar != NULL) {
// we already have some calendar created
calendar.hide(); // so we hide it first.
} else {
// first-time call, create the calendar.
var cal = new Calendar(TRUE, NULL, selected, closeHandler);
cal.weekNumbers = FALSE; // Do not display the week number
cal.showsTime = TRUE; // Display the time
cal.time24 = TRUE; // Hours have a 24 hours format
cal.showsOtherMonths = FALSE; // Just the current month is displayed
calendar = cal; // remember it in the global var
cal.setRange(1900, 2070); // min/max year allowed.
cal.create();
}
calendar.setDateFormat('%d-%m-%Y %H:%M'); // set the specified date format
calendar.parseDate(el.value); // try to parse the text in field
calendar.sel = el; // inform it what input field we use
// Display the calendar below the input field
calendar.showAtElement(el, \"Br\"); // show the calendar
return FALSE;
}
// This function update the date in the input field when selected
function selected(cal, date) {
cal.sel.value = date; // just update the date in the input field.
}
// This function gets called when the end-user clicks on the 'Close' button.
// It just hides the calendar without destroying it.
function closeHandler(cal) {
cal.hide(); // hide the calendar
calendar = NULL;
}
</script>";
}
?>