forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_realtime.php
491 lines (399 loc) · 17.4 KB
/
graph_realtime.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
$guest_account = true;
include('./include/auth.php');
include_once('./lib/rrd.php');
$config['force_storage_location_local'] = true;
/* ================= input validation ================= */
get_filter_request_var('graph_start');
get_filter_request_var('graph_end');
get_filter_request_var('graph_height');
get_filter_request_var('graph_width');
get_filter_request_var('graph_nolegend', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '(true|false)')));
get_filter_request_var('local_graph_id');
get_filter_request_var('size');
get_filter_request_var('ds_step');
get_filter_request_var('count');
get_filter_request_var('top');
get_filter_request_var('left');
/* ==================================================== */
set_default_action();
switch (get_request_var('action')) {
case 'init':
case 'timespan':
case 'interval':
case 'countdown':
ob_start();
$guest_account = true;
switch (get_request_var('action')) {
case 'init':
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
load_current_session_value('size', 'sess_realtime_size', read_user_setting('realtime_size', 100));
load_current_session_value('graph_nolegend', 'sess_realtime_nolegend', read_user_setting('realtime_nolegend', 'false'));
break;
case 'timespan':
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
break;
case 'interval':
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
load_current_session_value('size', 'sess_realtime_size', read_user_setting('realtime_size', 100));
load_current_session_value('graph_nolegend', 'sess_realtime_nolegend', read_user_setting('realtime_nolegend', 'false'));
break;
case 'countdown':
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
load_current_session_value('size', 'sess_realtime_size', read_user_setting('realtime_size', 100));
load_current_session_value('graph_nolegend', 'sess_realtime_nolegend', read_user_setting('realtime_nolegend', 'false'));
break;
default:
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
load_current_session_value('size', 'sess_realtime_size', read_user_setting('realtime_size', 100));
load_current_session_value('graph_nolegend', 'sess_realtime_nolegend', read_user_setting('realtime_nolegend', 'false'));
break;
}
$graph_data_array = array();
/* ds */
$graph_data_array['ds_step'] = read_user_setting('realtime_interval', 10);
if (!isempty_request_var('ds_step')) {
$graph_data_array['ds_step'] = get_request_var('ds_step');
$_SESSION['sess_realtime_dsstep'] = get_request_var('ds_step');
}
/* override: graph height (in pixels) */
if (!isempty_request_var('graph_height') && get_request_var('graph_height') < 3000) {
$graph_data_array['graph_height'] = get_request_var('graph_height');
} else {
$graph_data_array['graph_height'] = 125;
}
/* override: graph width (in pixels) */
if (!isempty_request_var('graph_width') && get_request_var('graph_width') < 3000) {
$graph_data_array['graph_width'] = get_request_var('graph_width');
} else {
$graph_data_array['graph_width'] = 425;
}
/* override: skip drawing the legend? */
if (get_request_var('graph_nolegend') == 'true') {
$graph_data_array['graph_nolegend'] = 'true';
}
if (isset_request_var('size') && get_request_var('size') > 0) {
$_SESSION['sess_realtime_size'] = get_request_var('size');
$size = get_request_var('size');
} elseif (isset($_SESSION['sess_realtime_size']) && $_SESSION['sess_realtime_size'] != '') {
$size = $_SESSION['sess_realtime_size'];
} else {
$size = 100;
}
if (isset_request_var('local_graph_id')) {
$graph_data = db_fetch_row_prepared('SELECT width, height
FROM graph_templates_graph
WHERE local_graph_id = ?',
array(get_request_var('local_graph_id')));
if (cacti_sizeof($graph_data)) {
$graph_data_array['graph_height'] = $graph_data['height'];
$graph_data_array['graph_width'] = $graph_data['width'];
}
}
if (isset_request_var('size') && get_request_var('size') < 100) {
$graph_data_array['graph_height'] = $graph_data_array['graph_height'] * $size / 100;
$graph_data_array['graph_width'] = $graph_data_array['graph_width'] * $size / 100;
}
/* override: graph start */
if (!isempty_request_var('graph_start')) {
$graph_data_array['graph_start'] = get_request_var('graph_start');
if ($graph_data_array['graph_start'] < 0) {
$graph_data_array['graph_start'] = time() + $graph_data_array['graph_start'];
}
$_SESSION['sess_realtime_window'] = abs(get_request_var('graph_start'));
}
/* override: graph end */
if (!isempty_request_var('graph_end')) {
$graph_data_array['graph_end'] = get_request_var('graph_end');
} else {
$graph_data_array['graph_end'] = time();
}
/* print RRDtool graph source? */
if (!isempty_request_var('show_source')) {
$graph_data_array['print_source'] = get_request_var('show_source');
}
/* check ds */
if ($graph_data_array['ds_step'] < 1) {
$graph_data_array['ds_step'] = read_user_setting('realtime_interval', 10);
}
// Determine the graph type of the output
if (!isset_request_var('image_format')) {
$type = db_fetch_cell_prepared('SELECT image_format_id
FROM graph_templates_graph
WHERE local_graph_id = ?',
array(get_request_var('local_graph_id')));
switch($type) {
case '1':
$gtype = 'png';
break;
case '3':
$gtype = 'svg+xml';
break;
default:
$gtype = 'png';
break;
}
} else {
switch(strtolower(get_nfilter_request_var('image_format'))) {
case 'png':
$graph_data_array['image_format'] = 'png';
break;
case 'svg':
$gtype = 'svg+xml';
break;
default:
$gtype = 'png';
break;
}
}
$graph_data_array['image_format'] = $gtype;
/* call poller */
$graph_rrd = read_config_option('realtime_cache_path') . '/user_' . hash('sha256', session_id()) . '_lgi_' . get_request_var('local_graph_id') . '.png';
$command = read_config_option('path_php_binary');
$args = sprintf('poller_realtime.php --graph=%s --interval=%d --poller_id=' . hash('sha256',session_id()), get_request_var('local_graph_id'), $graph_data_array['ds_step']);
shell_exec("$command $args");
/* construct the image name */
$graph_data_array['export_realtime'] = $graph_rrd;
$graph_data_array['output_flag'] = RRDTOOL_OUTPUT_GRAPH_DATA;
$null_param = array();
$output = rrdtool_function_graph(get_request_var('local_graph_id'), '', $graph_data_array, '', $null_param, $_SESSION[SESS_USER_ID]);
$error = '';
if (file_exists($graph_rrd)) {
$graph_contents = file_get_contents($graph_rrd);
if (preg_match('/^ERROR/',$graph_contents)) {
$error = $graph_contents;
$output = '';
}
}
if (empty($output) && empty($error)) {
$graph_data_array['get_error'] = true;
$null_param = array();
rrdtool_function_graph(get_request_var('local_graph_id'), '', $graph_data_array, '', $null_param, $_SESSION[SESS_USER_ID]);
$error = ob_get_contents();
if (read_config_option('stats_poller') == '') {
$error = __('The Cacti Poller has not run yet.');
}
}
if (!empty($error)) {
$graph_data_array['get_error'] = true;
if (isset($graph_data_array['graph_width']) && isset($graph_data_array['graph_height'])) {
$graph_contents = rrdtool_create_error_image($error, $graph_data_array['graph_width'], $graph_data_array['graph_height']);
} else {
$graph_contents = rrdtool_create_error_image($error);
}
ob_end_clean();
if ($graph_contents === false) {
$graph_contents = file_get_contents(__DIR__ . '/images/cacti_error_image.png');
}
}
if (isset($graph_contents)) {
$data = base64_encode($graph_contents);
} else {
$data = '';
}
/* save user preferences */
set_user_setting('realtime_interval', get_request_var('ds_step'));
set_user_setting('realtime_gwindow', abs(get_request_var('graph_start')));
set_user_setting('realtime_size', get_request_var('size'));
set_user_setting('realtime_nolegend', get_request_var('graph_nolegend'));
$_SESSION['sess_realtime_ds_step'] = get_request_var('ds_step');
$_SESSION['sess_realtime_graph_start'] = get_request_var('graph_start');
$_SESSION['sess_realtime_size'] = get_request_var('size');
$_SESSION['sess_realtime_nolegend'] = get_request_var('graph_nolegend');
/* send text information back to browser as well as image information */
$return_array = array(
'local_graph_id' => get_request_var('local_graph_id'),
'top' => get_request_var('top'),
'left' => get_request_var('left'),
'ds_step' => html_escape(isset($_SESSION['sess_realtime_ds_step']) ? $_SESSION['sess_realtime_ds_step']:$graph_data_array['ds_step']),
'graph_start' => html_escape(isset($_SESSION['sess_realtime_graph_start']) ? $_SESSION['sess_realtime_graph_start']:$graph_data_array['graph_start']),
'size' => html_escape(isset($_SESSION['sess_realtime_size']) ? $_SESSION['sess_realtime_size']:read_user_setting('realtime_size', 100)),
'thumbnails' => html_escape(isset($_SESSION['sess_realtime_nolegend']) ? $_SESSION['sess_realtime_nolegend']:'false'),
'data' => (isset($data) ? $data:''),
'image_format' => $graph_data_array['image_format']
);
print json_encode($return_array);
exit;
break;
case 'view':
$graph_rrd = read_config_option('realtime_cache_path') . '/user_' . hash('sha256',session_id()) . '_lgi_' . get_request_var('local_graph_id') . '.png';
if (file_exists($graph_rrd)) {
print base64_encode(file_get_contents($graph_rrd));
}
exit;
break;
default:
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
load_current_session_value('size', 'sess_realtime_size', read_user_setting('realtime_size', 100));
load_current_session_value('graph_nolegend', 'sess_realtime_nolegend', read_user_setting('realtime_nolegend', 'false'));
break;
}
/* ================= input validation ================= */
get_filter_request_var('ds_step');
get_filter_request_var('local_graph_id');
get_filter_request_var('graph_start');
get_filter_request_var('size');
/* ==================================================== */
$init = '';
if (!isset($_SESSION['sess_realtime_ds_step'])) {
load_current_session_value('ds_step', 'sess_realtime_ds_step', read_user_setting('realtime_interval', 10));
} else {
set_request_var('ds_step', $_SESSION['sess_realtime_ds_step']);
}
if (!isset($_SESSION['sess_realtime_graph_start'])) {
load_current_session_value('graph_start', 'sess_realtime_graph_start', read_user_setting('realtime_gwindow', 60));
} else {
set_request_var('graph_start', $_SESSION['sess_realtime_graph_start']);
}
/* save user preferences */
set_user_setting('realtime_interval', get_request_var('ds_step'));
set_user_setting('realtime_gwindow', abs(get_request_var('graph_start')));
set_user_setting('realtime_size', get_request_var('size'));
set_user_setting('realtime_nolegend', get_request_var('graph_nolegend'));
if (read_config_option('realtime_enabled') == '') {
print "<html>\n";
print "<body>\n";
print ' <p><strong>' . __('Real-time has been disabled by your administrator.') . "</strong></p>\n";
print "</body>\n";
print "</html>\n";
exit;
}
if (!is_dir(read_config_option('realtime_cache_path'))) {
print "<html>\n";
print "<body>\n";
print ' <p><strong>' . __('The Image Cache Directory does not exist. Please first create it and set permissions and then attempt to open another Real-time graph.') . "</strong></p>\n";
print "</body>\n";
print "</html>\n";
exit;
}
if (!is_writable(read_config_option('realtime_cache_path'))) {
print "<html>\n";
print "<body>\n";
print ' <p><strong>' . __('The Image Cache Directory is not writable. Please set permissions and then attempt to open another Real-time graph.') . "</strong></p>\n";
print "</body>\n";
print "</html>\n";
exit;
}
$selectedTheme = get_selected_theme();
$sizes = array(
'100' => '100%',
'90' => '90%',
'80' => '80%',
'70' => '70%',
'60' => '60%',
'50' => '50%',
'40' => '40%'
);
?>
<html>
<head>
<?php html_common_header(__('Cacti Real-time Graphing'));?>
<?php include(CACTI_PATH_INCLUDE . '/global_session.php'); ?>
</head>
<body style='font-size:12px;'>
<form method='post' action='graph_realtime.php' id='gform'>
<div id='rtfilter' class='cactiTable center'>
<div class='filterTable even'>
<select id='graph_start' onChange='imageOptionsChanged("timespan")'>
<?php
foreach ($realtime_window as $interval => $text) {
printf('<option value="%d"%s>%s</option>',
$interval, $interval == abs(get_request_var('graph_start')) ? ' selected="selected"' : '', $text
);
}
?>
</select>
<select id='ds_step' onChange='imageOptionsChanged("interval")'>
<?php
$min_refresh = read_config_option('realtime_interval');
foreach ($realtime_refresh as $interval => $text) {
if ($interval >= $min_refresh) {
printf('<option value="%d"%s>%s</option>',
$interval, $interval == get_request_var('ds_step') ? ' selected="selected"' : '', $text
);
}
}
?>
</select>
<select id='size' onChange='imageOptionsChanged("interval")'>
<?php
foreach ($sizes as $key => $value) {
printf('<option value="%d"%s>%s</option>',
$key, $key == get_request_var('size') ? ' selected="selected"' : '', $value
);
}
?>
</select>
<input type='checkbox' id='thumbnails' onChange='imageOptionsChanged("interval")' <?php print get_request_var('graph_nolegend') == 'true' ? 'checked':'';?>>
<label for='thumbnails'><?php print __('Thumbnails');?></label>
</div>
</div>
<div class='cactiTable center'>
<span id='countdown'><?php print __('%d seconds left.', get_request_var('ds_step')); ?></span>
</div>
<div id='image' class='center' style='padding:2px;'></div>
<input type='hidden' id='url_path' name='url_path' value='<?php print CACTI_PATH_URL;?>'/>
<input type='hidden' id='local_graph_id' name='local_graph_id' value='<?php print get_request_var('local_graph_id'); ?>'/>
<script type='text/javascript'>
var url;
var ds_step = 0;
var sizeset = false;
var count = 0;
var realtimePopout = true;
var refreshIsLogout= false;
var refreshPage=urlPath+'/graph_realtime.php?action=countdown&size='+$('#size').val();
var refreshMSeconds=999999999;
var myCountdown = {};
var secondsLeft = '<?php print __(' seconds left.');?>';
function countdown_update() {
ds_step--;
if (ds_step < 0) {
ds_step = $('#ds_step').val();
imageOptionsChanged('countdown');
sizeset = false;
}
setRealtimeWindowSize();
$('#countdown').empty().html(ds_step + secondsLeft);
count++;
destroy(myCountdown);
myCountdown = setTimeout(function() {
countdown_update();
}, 1000);
}
$(function() {
imageOptionsChanged('init');
myCountdown = setTimeout(function() {
countdown_update();
}, 1000);
});
</script>
</form>
</body>
</html>