-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
563 lines (496 loc) · 15.6 KB
/
functions.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?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/ |
+-------------------------------------------------------------------------+
*/
include_once(__DIR__ . '/setup.php');
$graphs_ppage = array(
1 => __('%d Graph', 1, 'cycle'),
2 => __('%d Graphs', 2, 'cycle'),
4 => __('%d Graphs', 4, 'cycle'),
6 => __('%d Graphs', 6, 'cycle'),
8 => __('%d Graphs', 8, 'cycle'),
9 => __('%d Graphs', 9, 'cycle'),
10 => __('%d Graphs', 10, 'cycle'),
12 => __('%d Graphs', 12, 'cycle'),
15 => __('%d Graphs', 15, 'cycle'),
16 => __('%d Graphs', 16, 'cycle'),
18 => __('%d Graphs', 18, 'cycle'),
20 => __('%d Graphs', 20, 'cycle'),
24 => __('%d Graphs', 24, 'cycle'),
25 => __('%d Graphs', 25, 'cycle'),
28 => __('%d Graphs', 28, 'cycle'),
30 => __('%d Graphs', 30, 'cycle'),
32 => __('%d Graphs', 32, 'cycle'),
35 => __('%d Graphs', 35, 'cycle'),
36 => __('%d Graphs', 36, 'cycle'),
40 => __('%d Graphs', 40, 'cycle'),
42 => __('%d Graphs', 42, 'cycle'),
48 => __('%d Graphs', 48, 'cycle'),
50 => __('%d Graphs', 50, 'cycle'),
60 => __('%d Graphs', 60, 'cycle'),
70 => __('%d Graphs', 70, 'cycle'),
80 => __('%d Graphs', 80, 'cycle'),
90 => __('%d Graphs', 90, 'cycle'),
100 => __('%d Graphs', 100, 'cycle')
);
$graph_cols = array(
1 => __('%d Column', 1, 'cycle'),
2 => __('%d Columns', 2, 'cycle'),
3 => __('%d Columns', 3, 'cycle'),
4 => __('%d Columns', 4, 'cycle'),
5 => __('%d Columns', 5, 'cycle'),
6 => __('%d Columns', 6, 'cycle'),
7 => __('%d Columns', 7, 'cycle'),
8 => __('%d Columns', 8, 'cycle')
);
function save_settings() {
validate_request_vars();
if (sizeof($_REQUEST)) {
foreach($_REQUEST as $var => $value) {
switch($var) {
case 'timespan':
set_user_setting('cycle_timespan', get_request_var('timespan'));
break;
case 'refresh':
set_user_setting('cycle_delay', get_request_var('refresh'));
break;
case 'graphs':
set_user_setting('cycle_graphs', get_request_var('graphs'));
break;
case 'cols':
set_user_setting('cycle_columns', get_request_var('cols'));
break;
case 'height':
set_user_setting('cycle_height', get_request_var('height'));
break;
case 'width':
set_user_setting('cycle_width', get_request_var('width'));
break;
case 'legend':
if ($value == 'true') {
$value = 'on';
} else {
$value = '';
}
set_user_setting('cycle_legend', $value);
break;
case 'rfilter':
set_user_setting('cycle_filter', get_request_var('rfilter'));
break;
}
}
}
validate_request_vars(true);
}
function validate_request_vars($force = false) {
cycle_config_settings(true);
/* ================= input validation and session storage ================= */
$filters = array(
'id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '-1'
),
'tree_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('cycle_custom_graphs_tree', $force),
),
'leaf_id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '-2'
),
'graphs' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_graphs', read_config_option('cycle_graphs'), $force)
),
'cols' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_columns', read_config_option('cycle_columns'), $force)
),
'width' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_width', read_config_option('cycle_width'), $force)
),
'height' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_height', read_config_option('cycle_height'), $force)
),
'timespan' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_timespan', read_config_option('cycle_timespan'), $force)
),
'delay' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('cycle_delay', read_config_option('cycle_delay'), $force)
),
'legend' => array(
'filter' => FILTER_CALLBACK,
'default' => read_user_setting('cycle_legend', read_config_option('cycle_legend'), $force),
'options' => array('options' => 'sanitize_search_string')
),
'rfilter' => array(
'filter' => FILTER_VALIDATE_IS_REGEX,
'pageset' => true,
'default' => read_user_setting('cycle_filter', '', $force),
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_cycle');
/* ================= input validation ================= */
}
function cycle_set_defaults() {
$user = $_SESSION['sess_user_id'];
if (!isset($_SESSION['sess_cycle_defaults'])) {
$defaults = array(
'cycle_delay' => '60',
'cycle_timespan' => '5',
'cycle_columns' => '2',
'cycle_graphs' => '4',
'cycle_height' => '100',
'cycle_width' => '400',
'cycle_font_size' => '8',
'cycle_font_face' => '',
'max_length' => '100',
'cycle_filter' => '',
'cycle_font_color' => '1',
'cycle_legend' => '',
'cycle_custom_graphs_type' => '2'
);
foreach($defaults as $name => $value) {
$current = db_fetch_cell_prepared('SELECT value
FROM settings_user
WHERE name = ?
AND user_id = ?',
array($name, $user));
if ($current === false) {
db_execute_prepared('REPLACE INTO settings_user
(user_id, name, value)
VALUES (?, ?, ?)',
array($user, $name, $value));
}
}
$_SESSION['sess_cycle_defaults'] = true;
}
}
function get_next_graphid($graphpp, $filter, $graph_tree, $leaf_id) {
global $id, $graph_id, $graphs, $next_graph_id, $prev_graph_id;
/* if no default graph list has been specified, default to 0 */
$type = read_config_option('cycle_custom_graphs_type');
$list = read_config_option('cycle_custom_graphs_list');
if ($type == 1 && $list == '') {
$type = 0;
}
switch($type) {
case '0':
case '1':
case '2':
$graph_id = $id;
if ($graph_id <= 0) {
$graph_id = 0;
}
$sql_where = "WHERE gl.id>=$graph_id";
if ($filter != '') {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . " gtg.title_cache RLIKE '$filter'";
}
if ($type == 1) {
$cases = explode(',', read_config_option('cycle_custom_graphs_list'));
sort($cases);
$newcase = '';
foreach($cases as $case) {
$newcase .= (is_numeric($case) ? (strlen($newcase) ? ',':'') . $case:'');
}
if (strlen($newcase)) {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . " gl.id IN($newcase)";
}
}elseif ($type == 2) {
$newcase = '';
$graph_data = get_tree_graphs($graph_tree, $leaf_id);
$local_graph_ids = array();
if (sizeof($graph_data)) {
$local_graph_ids = array_keys($graph_data);
sort($local_graph_ids);
}
if (sizeof($local_graph_ids)) {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . ' gl.id IN(' . implode(',', $local_graph_ids) . ')';
}else{
break;
}
}
$done = false;
$next_found = false;
$start = 0;
$next_graph_id = 0;
$prev_graph_id = 0;
$title = '';
$graphs = array();
$i = 0;
/* Build a graphs array of the number of graphs requested
* this graph array will be used for rendering. In addition
* when the user hit's next, or the graphs cycle, we need
* to know the next graph id to display. Calculate that
* based upon the offset $graphpp. If we overflow, start
* from the beginning, which is the second section until
* we either run out of rows, or reach the $graphpp limit.
*
* Finally, don't try to grap all graphs at once. It takes
* too much memory on big systems.
*/
/* first pass is moving up in ids */
while (!$done) {
$sql = "SELECT
gl.id,
gtg.title_cache
FROM graph_local AS gl
INNER JOIN graph_templates_graph AS gtg
ON (gtg.local_graph_id=gl.id)
$sql_where
ORDER BY gl.id ASC
LIMIT $start, $graphpp";
$rows = db_fetch_assoc($sql);
if ($graph_id > 0) {
$curr_found = true;
}else{
$curr_found = false;
}
if (sizeof($rows)) {
foreach ($rows as $row) {
if (is_graph_allowed($row['id'])) {
if (!$curr_found) {
$graph_id = $row['id'];
$title = $row['title_cache'];
$curr_found = true;
$graphs[$graph_id]['graph_id'] = $graph_id;
$i++;
}else{
if (sizeof($graphs) < $graphpp) {
$graphs[$row['id']]['graph_id'] = $row['id'];
$i++;
}else{
$next_graph_id = $row['id'];
$next_found = true;
break;
}
}
}
}
}
if ($next_graph_id > 0) {
$done = true;
}elseif (sizeof($rows) == 0) {
$done = true;
}else{
$start += $graphpp;
}
}
/* If we did not find all the graphs requested,
* move backwards from lowest graph id until the
* array is fully populated or we run out of graphs.
*/
if (sizeof($graphs) < $graphpp || $next_graph_id == 0) {
$sql_where = '';
/* setup the standard filters less the starting range, in other words start from the first graph */
if ($filter != '') {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . " gtg.title_cache RLIKE '$filter'";
}
if (isset($local_graph_ids) && sizeof($local_graph_ids)) {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . ' gl.id IN(' . implode(',', $local_graph_ids) . ')';
}
$start = 0;
$done = false;
$next_found = false;
while (!$done) {
$sql = "SELECT
gl.id,
gtg.title_cache
FROM graph_local AS gl
INNER JOIN graph_templates_graph AS gtg
ON (gtg.local_graph_id=gl.id)
$sql_where
ORDER BY gl.id ASC
LIMIT $start, $graphpp";
$rows = db_fetch_assoc($sql);
if (sizeof($rows)) {
foreach ($rows as $row) {
if (is_graph_allowed($row['id'])) {
if (!$curr_found) {
$graph_id = $row['id'];
$title = $row['title_cache'];
$curr_found = true;
$graphs[$graph_id]['graph_id'] = $graph_id;
$i++;
}else{
if (sizeof($graphs) < $graphpp) {
$graphs[$row['id']]['graph_id'] = $row['id'];
$i++;
}else{
$next_graph_id = $row['id'];
$next_found = true;
break;
}
}
}
}
}
if ($next_graph_id > 0) {
$done = true;
}elseif (sizeof($rows) == 0) {
$done = true;
}else{
$start += $graphpp;
}
}
}
/* When a user hits the 'Prev' button, we have to go backwards.
* Therefore, find the graph_id that would have to be used as
* the starting point if the user were to hit the 'Prev' button.
*
* Just like the 'Next' button, we need to scan the database until
* we reach the $graphpp variable or until we run out of rows. We
* also have to adjust for underflow in this case.
*/
$sql_where = "WHERE gl.id < $graph_id";
/* setup the standard filters less the starting range, in other words start from the first graph */
if ($filter != '') {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . " gtg.title_cache RLIKE '$filter'";
}
if (isset($local_graph_ids) && sizeof($local_graph_ids)) {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . ' gl.id IN(' . implode(',', $local_graph_ids) . ')';
}
$done = false;
$start = 0;
$pgraphs = array();
while (!$done) {
$sql = "SELECT gl.id,
gtg.title_cache
FROM graph_local AS gl
INNER JOIN graph_templates_graph AS gtg
ON (gtg.local_graph_id=gl.id)
$sql_where
ORDER BY id DESC
LIMIT $start, $graphpp";
$rows = db_fetch_assoc($sql);
if (sizeof($rows)) {
foreach ($rows as $row) {
if (is_graph_allowed($row['id'])) {
if (sizeof($pgraphs) < ($graphpp-1)) {
$pgraphs[] = $row['id'];
}else{
$prev_graph_id = $row['id'];
break;
}
}
}
}
if ($prev_graph_id > 0) {
$done = true;
}elseif (sizeof($rows) == 0) {
$done = true;
}else{
$start += $graphpp;
}
}
/* Now handle the underflow case, when we have not
* completed building the $pgraphs array to the
* correct size.
*/
if ($prev_graph_id == 0) {
$sql_where = '';
if ($filter != '') {
$sql_where .= (strlen($sql_where) ? ' AND':'WHERE') . " gtg.title_cache RLIKE '$filter'";
}
$start = 0;
$done = false;
while (!$done) {
$sql = "SELECT gl.id,
gtg.title_cache
FROM graph_local AS gl
INNER JOIN graph_templates_graph AS gtg
ON (gtg.local_graph_id=gl.id)
$sql_where
ORDER BY id DESC
LIMIT $start, $graphpp";
$rows = db_fetch_assoc($sql);
if (sizeof($rows)) {
foreach ($rows as $row) {
if (is_graph_allowed($row['id'])) {
if (sizeof($pgraphs) < ($graphpp-1)) {
$pgraphs[] = $row['id'];
}else{
$prev_graph_id = $row['id'];
break;
}
}
}
}
if ($prev_graph_id > 0) {
$done = true;
}elseif (sizeof($rows) == 0) {
$done = true;
}else{
$start += $graphpp;
}
}
}
break;
}
}
function get_tree_graphs($tree_id, $leaf_id) {
$graphs = array();
$hosts = array();
$outArray = array();
if (is_tree_allowed($tree_id)) {
if ($leaf_id == -2) {
$sql_leaf = 'parent=0 AND';
}elseif ($leaf_id > 0) {
$sql_leaf = 'parent=' . $leaf_id . ' AND';
}else{
$sql_leaf = '';
}
$sql = 'SELECT *
FROM graph_tree_items AS gti
WHERE ' . $sql_leaf . ' graph_tree_id=' . $tree_id;
$items = db_fetch_assoc($sql);
if (sizeof($items)) {
foreach($items as $i) {
if ((empty($i['title'])) && ($i['local_graph_id'] > 0 )) {
$graphs[$i['local_graph_id']] = $i['local_graph_id'];
} elseif ($i['host_id'] > 0 && is_device_allowed($i['host_id'])) {
$hosts[$i['host_id']] = $i['host_id'];
}elseif ($leaf_id > -2) {
$outArray += get_tree_graphs($tree_id, $i['id']);
}
}
}
if (sizeof($hosts) && sizeof($graphs)) {
$graphs = get_allowed_graphs('(h.id IN(' . implode(',', $hosts) . ') OR gl.id IN(' . implode(',', $graphs) . '))');
}elseif(sizeof($hosts)) {
$graphs = get_allowed_graphs('(h.id IN(' . implode(',', $hosts) . '))');
}elseif(sizeof($graphs)) {
$graphs = get_allowed_graphs('(gl.id IN(' . implode(',', $graphs) . '))');
}
}
if (isset($graphs) && sizeof($graphs)) {
foreach($graphs as $i) {
$outArray[$i['local_graph_id']] = $i['title_cache'];
}
}
return($outArray);
}