-
Notifications
You must be signed in to change notification settings - Fork 16
/
syslog.php
2199 lines (1919 loc) · 74.7 KB
/
syslog.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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. |
+-------------------------------------------------------------------------+
| Originally released as aloe by: sidewinder at shitworks.com |
| Modified by: Harlequin <[email protected]> |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/* allow guest account to see this page */
$guest_account = true;
/* initialize cacti environment */
chdir('../../');
include('./include/auth.php');
include_once('./lib/html_tree.php');
include_once('./plugins/syslog/functions.php');
include_once('./plugins/syslog/database.php');
syslog_connect();
set_default_action();
if (get_request_var('action') == 'ajax_programs') {
return get_ajax_programs(true);
} elseif (get_request_var('action') == 'ajax_programs_wnone') {
return get_ajax_programs(true, true);
} elseif (get_request_var('action') == 'ajax_hosts') {
print get_ajax_hosts();
exit;
} elseif (get_request_var('action') == 'save') {
save_settings();
exit;
}
$title = __('Syslog Viewer', 'syslog');
$trimvals = array(
'1024' => __('All Text', 'syslog'),
'30' => __('%d Chars', 30, 'syslog'),
'50' => __('%d Chars', 50, 'syslog'),
'75' => __('%d Chars', 75, 'syslog'),
'100' => __('%d Chars', 100, 'syslog'),
'150' => __('%d Chars', 150, 'syslog'),
'300' => __('%d Chars', 300, 'syslog')
);
/* set the default tab */
get_filter_request_var('tab', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z]+)$/')));
load_current_session_value('tab', 'sess_syslog_tab', 'syslog');
$current_tab = get_request_var('tab');
/* validate the syslog post/get/request information */;
if ($current_tab != 'stats') {
syslog_request_validation($current_tab);
}
if (isset_request_var('refresh')) {
$refresh['seconds'] = get_request_var('refresh');
$refresh['page'] = $config['url_path'] . 'plugins/syslog/syslog.php?header=false&tab=' . $current_tab;
$refresh['logout'] = 'false';
set_page_refresh($refresh);
}
/* draw the tabs */
/* display the main page */
if (isset_request_var('export')) {
syslog_export($current_tab);
/* clear output so reloads wont re-download */
unset_request_var('output');
} else {
general_header();
syslog_display_tabs($current_tab);
if ($current_tab == 'current') {
syslog_view_alarm();
} elseif ($current_tab == 'stats') {
syslog_statistics();
} else {
syslog_messages($current_tab);
}
bottom_footer();
}
$_SESSION['sess_nav_level_cache'] = array();
function get_ajax_hosts() {
global $syslogdb_default;
$ac_rows = read_config_option('autocomplete_rows');
if ($ac_rows <= 0) {
$ac_rows = 100;
}
$term = '%' . get_nfilter_request_var('term') . '%';
if (syslog_db_table_exists('host', false)) {
$hosts = syslog_db_fetch_assoc_prepared("SELECT DISTINCT sh.host_id, sh.host, h.id
FROM `" . $syslogdb_default . "`.`syslog_hosts` AS sh
LEFT JOIN host AS h
ON sh.host = h.hostname
OR sh.host = h.description
OR sh.host LIKE substring_index(h.hostname, '.', 1)
OR sh.host LIKE substring_index(h.description, '.', 1)
WHERE sh.host LIKE ?
OR h.description LIKE ?
ORDER BY host
LIMIT $ac_rows",
array($term, $term));
} else {
$hosts = syslog_db_fetch_assoc_prepared("SELECT DISTINCT sh.host_id, sh.host, '0' AS id
FROM `" . $syslogdb_default . "`.`syslog_hosts` AS sh
WHERE sh.host LIKE ?
ORDER BY host
LIMIT $ac_rows",
array($term));
}
if (cacti_sizeof($hosts)) {
foreach ($hosts as $host) {
if (!empty($host['id'])) {
$class = get_device_leaf_class($host['id']);
} else {
$class = 'deviceUp';
}
$rhosts[$host['host_id']] = array(
'host' => $host['host'],
'host_id' => $host['id'],
'class' => $class
);
}
return json_encode($rhosts);
} else {
return json_encode(array());
}
}
function syslog_display_tabs($current_tab) {
global $config;
/* present a tabbed interface */
$tabs_syslog['syslog'] = __('System Logs', 'syslog');
if (read_config_option('syslog_statistics') == 'on') {
$tabs_syslog['stats'] = __('Statistics', 'syslog');
}
$tabs_syslog['alerts'] = __('Alert Logs', 'syslog');
/* if they were redirected to the page, let's set that up */
if (!isempty_request_var('id') || $current_tab == 'current') {
$current_tab = 'current';
}
load_current_session_value('id', 'sess_syslog_id', '0');
if (!isempty_request_var('id') || $current_tab == 'current') {
$tabs_syslog['current'] = __('Selected Alert', 'syslog');
}
/* draw the tabs */
print "<div class='tabs'><nav><ul>\n";
if (cacti_sizeof($tabs_syslog)) {
foreach (array_keys($tabs_syslog) as $tab_short_name) {
print '<li><a class="tab ' . (($tab_short_name == $current_tab) ? 'selected"':'"') . " href='" . html_escape($config['url_path'] .
'plugins/syslog/syslog.php?' .
'tab=' . $tab_short_name) .
"'>" . $tabs_syslog[$tab_short_name] . "</a></li>\n";
}
}
print "</ul></nav></div>\n";
}
function syslog_view_alarm() {
global $config;
global $syslogdb_default;
print "<table class='cactiTable'>";
print "<tr class='tableHeader'><td class='textHeaderDark'>" . __('Syslog Alert View', 'syslog') . "</td></tr>";
print "<tr><td class='odd'>\n";
$html = syslog_db_fetch_cell('SELECT html FROM `' . $syslogdb_default . '`.`syslog_logs` WHERE seq=' . get_request_var('id'));
print trim($html, "' ");
print '</td></tr></table>';
exit;
}
/** function syslog_statistics()
* This function paints a table of summary statistics for syslog
* messages by host, facility, priority, and time range.
*/
function syslog_statistics() {
global $title, $rows, $config;
global $syslogdb_default;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1',
),
'refresh' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('syslog_refresh'),
),
'timespan' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '300',
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'rfilter' => array(
'filter' => FILTER_VALIDATE_IS_REGEX,
'pageset' => true,
'default' => ''
),
'host' => array(
'filter' => FILTER_VALIDATE_IS_NUMERIC_LIST,
'pageset' => true,
'default' => '',
),
'facility' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '',
),
'priority' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '',
),
'program' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'host',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_syslogs');
/* ================= input validation ================= */
html_start_box(__('Syslog Statistics Filter', 'syslog'), '100%', '', '3', 'center', '');
syslog_stats_filter();
html_end_box();
$sql_where = '';
$sql_groupby = '';
if (get_request_var('rows') == -1) {
$rows = read_config_option('num_rows_table');
} elseif (get_request_var('rows') == -2) {
$rows = 999999;
} else {
$rows = get_request_var('rows');
}
$records = get_stats_records($sql_where, $sql_groupby, $rows);
$rows_query_string = "SELECT COUNT(*)
FROM `" . $syslogdb_default . "`.`syslog_statistics` AS ss
$sql_where
$sql_groupby";
$total_rows = syslog_db_fetch_cell('SELECT COUNT(*) FROM ('. $rows_query_string . ') as temp');
$nav = html_nav_bar('syslog.php?tab=stats', MAX_DISPLAY_PAGES, get_request_var_request('page'), $rows, $total_rows, 4, __('Messages', 'syslog'), 'page', 'main');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
'host' => array(
'display' => __('Device Name', 'syslog'),
'sort' => 'ASC',
'align' => 'left'
),
'facility' => array(
'display' => __('Facility', 'syslog'),
'sort' => 'ASC',
'align' => 'left'
),
'priority' => array(
'display' => __('Priority', 'syslog'),
'sort' => 'ASC',
'align' => 'left'
),
'program' => array(
'display' => __('Program', 'syslog'),
'sort' => 'ASC',
'align' => 'left'
),
'insert_time' => array(
'display' => __('Date', 'syslog'),
'sort' => 'DESC',
'align' => 'right'
),
'records' => array(
'display' => __('Records', 'syslog'),
'sort' => 'DESC',
'align' => 'right'
)
);
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'));
if (get_request_var('timespan') < 3600) {
$date_format = 'Y-m-d H:i';
} elseif (get_request_var('timespan') < 86400) {
$date_format = 'Y-m-d H:00';
} else {
$date_format = 'Y-m-d 00:00';
}
if (cacti_sizeof($records)) {
$i = 0;
foreach ($records as $r) {
$time = date($date_format, strtotime($r['insert_time']));
form_alternate_row('line' . $i);
print '<td>' . (get_request_var('host') != '-2' ? $r['host']:'-') . '</td>';
print '<td>' . (get_request_var('facility') != '-2' ? ucfirst($r['facility']):'-') . '</td>';
print '<td>' . (get_request_var('priority') != '-2' ? ucfirst($r['priority']):'-') . '</td>';
print '<td>' . (get_request_var('program') != '-2' ? ucfirst($r['program']):'-') . '</td>';
//print '<td class="right">' . $r['insert_time'] . '</td>';
print '<td class="right">' . $time . '</td>';
print '<td class="right">' . number_format_i18n($r['records'], -1) . '</td>';
form_end_row();
$i++;
}
} else {
print "<tr><td colspan='4'><em>" . __('No Syslog Statistics Found', 'syslog') . "</em></td></tr>";
}
html_end_box(false);
if (cacti_sizeof($records)) {
print $nav;
}
}
function get_stats_records(&$sql_where, &$sql_groupby, $rows) {
global $syslogdb_default;
/* form the 'where' clause for our main sql query */
if (!isempty_request_var('rfilter')) {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') .
"sh.host RLIKE '" . get_request_var('rfilter') . "'
OR spr.program RLIKE '" . get_request_var('rfilter') . "'";
}
if (get_request_var('host') == '-2') {
// Do nothing
} elseif (get_request_var('host') != '-1' && get_request_var('host') != '') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'ss.host_id=' . get_request_var('host');
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'host_id';
} else {
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'host_id';
}
if (get_request_var('facility') == '-2') {
// Do nothing
} elseif (get_request_var('facility') != '-1' && get_request_var('facility') != '') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'ss.facility_id=' . get_request_var('facility');
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'facility_id';
} else {
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'facility_id';
}
if (get_request_var('priority') == '-2') {
// Do nothing
} elseif (get_request_var('priority') != '-1' && get_request_var('priority') != '') {
$sql_where .= ($sql_where == '' ? 'WHERE ': ' AND ') . 'ss.priority_id=' . get_request_var('priority');
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'priority_id';
} else {
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'priority_id';
}
if (get_request_var('program') == '-2') {
// Do nothing
} elseif (get_request_var('program') != '-1' && get_request_var('program') != '') {
$sql_where .= ($sql_where == '' ? 'WHERE ': ' AND ') . 'ss.program_id=' . get_request_var('program');
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'program_id';
} else {
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . 'program_id';
}
if (get_request_var('timespan') != '-1') {
$sql_groupby .= ($sql_groupby != '' ? ', ':'') . ' UNIX_TIMESTAMP(insert_time) DIV ' . get_request_var('timespan');
}
$sql_order = get_order_string();
if (!isset_request_var('export')) {
$sql_limit = ' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
} else {
$sql_limit = ' LIMIT 10000';
}
if ($sql_groupby != '') {
$sql_groupby = 'GROUP BY ' . $sql_groupby;
}
$time = 'FROM_UNIXTIME(TRUNCATE(UNIX_TIMESTAMP(insert_time)/' . get_request_var('timespan') . ',0)*' . get_request_var('timespan') . ') AS insert_time';
$query_sql = "SELECT sh.host, sf.facility, sp.priority, spr.program, records, insert_time
FROM (
SELECT host_id, facility_id, priority_id, program_id, sum(records) AS records, $time
FROM `" . $syslogdb_default . "`.`syslog_statistics` AS ss
$sql_where
$sql_groupby
) AS ss
LEFT JOIN `" . $syslogdb_default . "`.`syslog_facilities` AS sf
ON ss.facility_id=sf.facility_id
LEFT JOIN `" . $syslogdb_default . "`.`syslog_priorities` AS sp
ON ss.priority_id=sp.priority_id
LEFT JOIN `" . $syslogdb_default . "`.`syslog_programs` AS spr
ON ss.program_id=spr.program_id
LEFT JOIN `" . $syslogdb_default . "`.`syslog_hosts` AS sh
ON ss.host_id=sh.host_id
$sql_order
$sql_limit";
//cacti_log(str_replace("\n", "", $query_sql));
return syslog_db_fetch_assoc($query_sql);
}
function syslog_stats_filter() {
global $config, $item_rows;
global $syslogdb_default;
?>
<tr class='even'>
<td>
<form id='stats_form' action='syslog.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Device', 'syslog');?>
</td>
<td>
<select id='host' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('host') == '-1') { ?> selected<?php } ?>><?php print __('All', 'syslog');?></option>
<option value='-2'<?php if (get_request_var('host') == '-2') { ?> selected<?php } ?>><?php print __('None', 'syslog');?></option>
<?php
$ac_rows = read_config_option('autocomplete_rows');
if ($ac_rows <= 0) {
$ac_rows = 100;
}
if (syslog_db_table_exists('host', false)) {
$hosts = syslog_db_fetch_assoc("SELECT DISTINCT sh.host_id, sh.host, h.id
FROM `" . $syslogdb_default . "`.`syslog_hosts` AS sh
LEFT JOIN host AS h
ON sh.host = h.hostname
OR sh.host = h.description
OR sh.host LIKE substring_index(h.hostname, '.', 1)
OR sh.host LIKE substring_index(h.description, '.', 1)
ORDER BY host
LIMIT $ac_rows");
} else {
$hosts = syslog_db_fetch_assoc("SELECT DISTINCT sh.host_id, sh.host, '0' AS id
FROM `" . $syslogdb_default . "`.`syslog_hosts` AS sh
ORDER BY host
LIMIT $ac_rows");
}
if (cacti_sizeof($hosts)) {
foreach ($hosts as $host) {
if (!empty($host['id'])) {
$class = get_device_leaf_class($host['id']);
} else {
$class = 'deviceUp';
}
print '<option class="' . $class . '" value="' . $host['host_id'] . '"'; if (get_request_var('host') == $host['host_id']) { print ' selected'; } print '>' . $host['host'] . '</option>';
}
}
?>
</select>
</td>
<td>
<?php print __('Facility', 'syslog');?>
</td>
<td>
<select id='facility' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('facility') == '-1') { ?> selected<?php } ?>><?php print __('All', 'syslog');?></option>
<option value='-2'<?php if (get_request_var('facility') == '-2') { ?> selected<?php } ?>><?php print __('None', 'syslog');?></option>
<?php
$facilities = syslog_db_fetch_assoc('SELECT DISTINCT facility_id, facility
FROM `' . $syslogdb_default . '`.`syslog_facilities` AS sf
ORDER BY facility');
if (cacti_sizeof($facilities)) {
foreach ($facilities as $r) {
print '<option value="' . $r['facility_id'] . '"'; if (get_request_var('facility') == $r['facility_id']) { print ' selected'; } print '>' . ucfirst($r['facility']) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<?php print __('Priority', 'syslog');?>
</td>
<td>
<select id='priority' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('priority') == '-1') { ?> selected<?php } ?>><?php print __('All', 'syslog');?></option>
<option value='-2'<?php if (get_request_var('priority') == '-2') { ?> selected<?php } ?>><?php print __('None', 'syslog');?></option>
<?php
$priorities = syslog_db_fetch_assoc('SELECT DISTINCT priority_id, priority
FROM `' . $syslogdb_default . '`.`syslog_priorities` AS sp
ORDER BY priority');
if (cacti_sizeof($priorities)) {
foreach ($priorities as $r) {
print '<option value="' . $r['priority_id'] . '"'; if (get_request_var('priority') == $r['priority_id']) { print ' selected'; } print '>' . ucfirst($r['priority']) . "</option>\n";
}
}
?>
</select>
</td>
<?php print html_program_filter(get_request_var('program'), true, 'ajax_programs_wnone');?>
<td>
<span>
<input id='go' type='button' value='<?php print __esc('Go', 'syslog');?>'>
<input id='clear' type='button' value='<?php print __esc('Clear', 'syslog');?>'>
</span>
</td>
</tr>
</table>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search', 'syslog');?>
</td>
<td>
<input type='text' id='rfilter' size='30' value='<?php print html_escape_request_var('rfilter');?>' onChange='applyFilter()'>
</td>
<td>
<?php print __('Time Range', 'syslog');?>
</td>
<td>
<select id='timespan' onChange='applyFilter()'>
<?php
$timespans = array(
60 => __('%d Minute', 1, 'syslog'),
120 => __('%d Minutes', 2, 'syslog'),
300 => __('%d Minutes', 5, 'syslog'),
600 => __('%d Minutes', 10, 'syslog'),
1800 => __('%d Minutes', 30, 'syslog'),
3600 => __('%d Hour', 1, 'syslog'),
7200 => __('%d Hours', 2, 'syslog'),
14400 => __('%d Hours', 4, 'syslog'),
28880 => __('%d Hours', 8, 'syslog'),
86400 => __('%d Day', 1, 'syslog')
);
foreach($timespans as $time => $span) {
print '<option value="'. $time . '"' . (get_request_var('timespan') == $time ? ' selected':'') . '>' . $span . '</option>';
}
?>
</select>
</td>
<td>
<?php print __('Entries', 'syslog');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('rows') == '-1') { ?> selected<?php } ?>><?php print __('Default', 'syslog');?></option>
<?php
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print '<option value="' . $key . '"'; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . $value . "</option>\n";
}
}
?>
</select>
</td>
</tr>
</table>
<input type='hidden' id='page' value='<?php print get_filter_request_var('page');?>'>
</form>
</td>
<script type='text/javascript'>
function clearFilter() {
strURL = 'syslog.php?tab=stats&clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#go').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#host').selectmenu({
open: function() {
$('div.ui-selectmenu-menu li.ui-menu-item').each(function(idx){
$(this).addClass( $('#host option').eq(idx).attr('class') )
})
}
});
});
function applyFilter() {
strURL = 'syslog.php?header=false';
strURL += '&none=true';
strURL += '&facility=' + $('#facility').val();
strURL += '&host=' + $('#host').val();
strURL += '&priority=' + $('#priority').val();
strURL += '&program=' + $('#eprogram').val();
strURL += '×pan=' + $('#timespan').val();
strURL += '&rfilter=' + base64_encode($('#rfilter').val());
strURL += '&rows=' + $('#rows').val();
loadPageNoHeader(strURL);
}
</script>
</tr>
<?php
}
/** function syslog_request_validation()
* This is a generic function for this page that makes sure that
* we have a good request. We want to protect against people who
* like to create issues with Cacti.
*/
function syslog_request_validation($current_tab, $force = false) {
global $title, $rows, $config, $reset_multi;
include_once($config['base_path'] . '/lib/time.php');
if ($current_tab != 'alerts' && isset_request_var('host') && get_nfilter_request_var('host') == -1) {
kill_session_var('sess_syslog_' . $current_tab . '_hosts');
unset_request_var('host');
}
$shift_span = false;
if (isset_request_var('predefined_timespan')) {
$shift_span = 'span';
} elseif (isset_request_var('predefined_timeshift')) {
$shift_span = 'shift';
} elseif (isset_request_var('date1') && isset_request_var('date2')) {
$shift_span = 'custom';
}
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => read_user_setting('syslog_rows', '-1', $force)
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'id' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => ''
),
'removal' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('syslog_removal', '1', $force)
),
'predefined_timespan' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => read_user_setting('default_timespan', GT_LAST_DAY, $force)
),
'predefined_timeshift' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => read_user_setting('default_timeshift', GTS_1_DAY, $force)
),
'refresh' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('syslog_refresh', read_config_option('syslog_refresh'), $force)
),
'trimval' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('syslog_trimval', '75', $force)
),
'enabled' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'host' => array(
'filter' => FILTER_VALIDATE_IS_NUMERIC_LIST,
'pageset' => true,
'default' => '',
),
'efacility' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => read_user_setting('syslog_efacility', '-1', $force),
'options' => array('options' => 'sanitize_search_string')
),
'epriority' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => read_user_setting('syslog_epriority', '-1', $force),
'options' => array('options' => 'sanitize_search_string')
),
'eprogram' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => read_user_setting('syslog_eprogram', '-1', $force),
),
'rfilter' => array(
'filter' => FILTER_VALIDATE_IS_REGEX,
'pageset' => true,
'default' => ''
),
'date1' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'date2' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'logtime',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'DESC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_sl_' . $current_tab);
/* ================= input validation ================= */
// Modify session and request variables based upon span/shift/settings
set_shift_span($shift_span, 'sess_sl_' . $current_tab);
api_plugin_hook_function('syslog_request_val');
if (isset_request_var('host')) {
$_SESSION['sess_syslog_' . $current_tab . '_hosts'] = get_nfilter_request_var('host');
} elseif (isset($_SESSION['sess_syslog_' . $current_tab . '_hosts'])) {
set_request_var('host', $_SESSION['sess_syslog_' . $current_tab . '_hosts']);
} else {
set_request_var('host', '-1');
}
}
function set_shift_span($shift_span, $session_prefix) {
global $graph_timeshifts;
if ($shift_span == 'span' || $shift_span === false) {
$span = array();
// Calculate the timespan
$first_weekdayid = read_user_setting('first_weekdayid');
get_timespan($span, time(), get_request_var('predefined_timespan'), $first_weekdayid);
// Save the settings for next page refresh
set_request_var('date1', date('Y-m-d H:i:s', $span['begin_now']));
set_request_var('date2', date('Y-m-d H:i:s', $span['end_now']));
// We don't want any date saved in the session
kill_session_var($session_prefix . '_date1');
kill_session_var($session_prefix . '_date2');
set_request_var('custom', false);
} elseif ($shift_span == 'shift') {
$span = array();
$span['current_value_date1'] = get_request_var('date1');
$span['current_value_date2'] = get_request_var('date2');
$span['begin_now'] = strtotime(get_request_var('date1'));
$span['end_now'] = strtotime(get_request_var('date2'));
if (isset_request_var('shift_right')) {
$direction = '+';
} elseif (isset_request_var('shift_left')) {
$direction = '-';
} else {
$direction = '+';
}
$timeshift = $graph_timeshifts[get_request_var('predefined_timeshift')];
// Calculate the new date1 and date2
shift_time($span, $direction, $timeshift);
// Save the settings for next page refresh
set_request_var('date1', date('Y-m-d H:i:s', $span['begin_now']));
set_request_var('date2', date('Y-m-d H:i:s', $span['end_now']));
// Save the dates in the session variable for page refresh
$_SESSION[$session_prefix . '_date1'] = get_request_var('date1');
$_SESSION[$session_prefix . '_date2'] = get_request_var('date2');
set_request_var('custom', true);
} elseif ($shift_span == 'custom') {
set_request_var('custom', true);
}
}
function get_syslog_messages(&$sql_where, $rows, $tab) {
global $sql_where, $hostfilter, $hostfilter_log, $current_tab, $syslog_incoming_config;
global $syslogdb_default;
$sql_where = '';
if ($tab == 'alerts') {
if (get_request_var('host') == 0) {
// Show all hosts
} else {
$hosts = explode(',', get_request_var('host'));
$thold_pos = array_search('-1', $hosts, true);
if ($thold_pos !== false) {
unset($hosts[$thold_pos]);
}
if (sizeof($hosts)) {
sql_hosts_where($tab);
if ($hostfilter_log != '') {
$sql_where .= 'WHERE ' . $hostfilter_log;
}
}
if ($thold_pos !== false) {
$ids = array_rekey(
syslog_db_fetch_assoc('SELECT id
FROM `' . $syslogdb_default . '`.`syslog_alert`
WHERE method = 1'),
'id', 'id'
);
if (cacti_sizeof($ids)) {
$sql_where .= ($sql_where == '' ? 'WHERE ':' OR ') . 'alert_id IN (' . implode(', ', $ids) . ')';
} elseif ($sql_where == '') {
$sql_where .= 'WHERE 0 = 1';
}
}
}
} elseif ($tab == 'syslog') {
if (!isempty_request_var('host')) {
sql_hosts_where($tab);
if ($hostfilter != '') {
$sql_where .= 'WHERE ' . $hostfilter;
}
}
}
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') .
"logtime BETWEEN '" . get_request_var('date1') . "'
AND '" . get_request_var('date2') . "'";
if (isset_request_var('id') && $current_tab == 'current') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') .
'sa.id=' . get_request_var('id');
}
if (!isempty_request_var('rfilter')) {
if ($tab == 'syslog') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "message RLIKE '" . get_request_var('rfilter') . "'";
} else {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "logmsg RLIKE '" . get_request_var('rfilter') . "'";
}
}
if (get_request_var('eprogram') != '-1') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'syslog.program_id = ' . db_qstr(get_request_var('eprogram'));
}
if (get_request_var('efacility') != '-1') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'syslog.facility_id = ' . db_qstr(get_request_var('efacility'));
}
if (isset_request_var('epriority') && get_request_var('epriority') != '-1') {
$priorities = '';
switch(get_request_var('epriority')) {
case '0':
$priorities = ' = 0';
break;
case '1o':
$priorities = ' = 1';
break;
case '1':
$priorities = ' <= 1';
break;
case '2o':
$priorities = ' = 2';
break;
case '2':
$priorities = ' <= 2';
break;
case '3o':
$priorities = ' = 3';
break;
case '3':
$priorities = ' <= 3';
break;
case '4o':
$priorities = ' = 4';
break;
case '4':
$priorities = ' <= 4';
break;
case '5o':
$priorities = ' = 5';
break;
case '5':
$priorities = ' <= 5';
break;
case '6o':
$priorities = ' = 6';
break;
case '6':
$priorities = ' <= 6';
break;
case '7':
$priorities = ' = 7';
break;
}
$sql_where .= ($sql_where == '' ? 'WHERE ': ' AND ') . 'syslog.priority_id ' . $priorities;
}
$sql_where = api_plugin_hook_function('syslog_sqlwhere', $sql_where);
$sql_order = get_order_string();
if (!isset_request_var('export')) {
$sql_limit = ' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
} else {
$sql_limit = ' LIMIT 10000';
}
if ($tab == 'syslog') {
if (get_request_var('removal') == '-1') {
$query_sql = "SELECT syslog.*, syslog_programs.program, 'main' AS mtype