forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.php
2733 lines (2358 loc) · 87.7 KB
/
utilities.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-2017 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('./include/auth.php');
include_once('./lib/api_data_source.php');
include_once('./lib/utility.php');
include_once('./lib/clog_webapi.php');
include_once('./lib/boost.php');
/* set default action */
set_default_action();
switch (get_request_var('action')) {
case 'clear_poller_cache':
/* obtain timeout settings */
$max_execution = ini_get('max_execution_time');
ini_set('max_execution_time', '0');
repopulate_poller_cache();
ini_set('max_execution_time', $max_execution);
header('Location: utilities.php?action=view_poller_cache');exit;
break;
case 'rebuild_resource_cache':
rebuild_resource_cache();
header('Location: utilities.php?header=false');exit;
break;
case 'view_snmp_cache':
top_header();
utilities_view_snmp_cache();
bottom_footer();
break;
case 'view_poller_cache':
top_header();
utilities_view_poller_cache();
bottom_footer();
break;
case 'view_logfile':
utilities_view_logfile();
break;
case 'clear_logfile':
utilities_clear_logfile();
utilities_view_logfile();
break;
case 'purge_logfile':
clog_purge_logfile();
utilities_view_logfile();
break;
case 'view_cleaner':
top_header();
utilities_view_cleaner();
bottom_footer();
break;
case 'view_user_log':
top_header();
utilities_view_user_log();
bottom_footer();
break;
case 'clear_user_log':
utilities_clear_user_log();
utilities_view_user_log();
break;
case 'view_tech':
$php_info = utilities_php_modules();
top_header();
utilities_view_tech($php_info);
bottom_footer();
break;
case 'view_boost_status':
top_header();
boost_display_run_status();
bottom_footer();
break;
case 'view_snmpagent_cache':
top_header();
snmpagent_utilities_run_cache();
bottom_footer();
break;
case 'rebuild_snmpagent_cache';
snmpagent_cache_rebuilt();
header('Location: utilities.php?action=view_snmpagent_cache');exit;
break;
case 'view_snmpagent_events':
top_header();
snmpagent_utilities_run_eventlog();
bottom_footer();
break;
case 'ajax_hosts':
get_allowed_ajax_hosts();
break;
case 'ajax_hosts_noany':
get_allowed_ajax_hosts(false);
break;
default:
if (!api_plugin_hook_function('utilities_action', get_request_var('action'))) {
top_header();
utilities();
bottom_footer();
}
break;
}
/* -----------------------
Utilities Functions
----------------------- */
function rebuild_resource_cache() {
db_execute('DELETE FROM settings WHERE name LIKE "md5dirsum%"');
db_execute('TRUNCATE TABLE poller_resource_cache');
raise_message('resource_cache_rebuild');
cacti_log('NOTE: Poller Resource Cache scheduled for rebuild by user ' . get_username($_SESSION['sess_user_id']), false, 'WEBUI');
}
function utilities_view_tech($php_info = '') {
global $database_default, $config, $rrdtool_versions, $poller_options, $input_types;
/* Get table status */
$tables = db_fetch_assoc_prepared('SELECT *
FROM information_schema.tables
WHERE table_schema = ?', array($database_default));
/* Get poller stats */
$poller_item = db_fetch_assoc('SELECT action, count(action) AS total
FROM poller_item
GROUP BY action');
/* Get system stats */
$host_count = db_fetch_cell('SELECT COUNT(*) FROM host');
$graph_count = db_fetch_cell('SELECT COUNT(*) FROM graph_local');
$data_count = db_fetch_assoc('SELECT i.type_id, COUNT(i.type_id) AS total
FROM data_template_data AS d, data_input AS i
WHERE d.data_input_id = i.id
AND local_data_id <> 0
GROUP BY i.type_id');
/* Get RRDtool version */
$rrdtool_version = 'Unknown';
if ((file_exists(read_config_option('path_rrdtool'))) && ((function_exists('is_executable')) && (is_executable(read_config_option('path_rrdtool'))))) {
$out_array = array();
exec(cacti_escapeshellcmd(read_config_option('path_rrdtool')), $out_array);
if (sizeof($out_array) > 0) {
if (preg_match('/^RRDtool ([1-9]\.[0-9])/', $out_array[0], $m)) {
$rrdtool_version = 'rrd-'. $m[1] .'.x';
}
}
}
/* Get SNMP cli version */
if ((file_exists(read_config_option('path_snmpget'))) && ((function_exists('is_executable')) && (is_executable(read_config_option('path_snmpget'))))) {
$snmp_version = shell_exec(cacti_escapeshellcmd(read_config_option('path_snmpget')) . ' -V 2>&1');
} else {
$snmp_version = "<span class='deviceDown'>" . __('NET-SNMP Not Installed or its paths are not set. Please install if you wish to monitor SNMP enabled devices.') . "</span>";
}
/* Check RRDtool issues */
$rrdtool_error = '';
if ($rrdtool_version != read_config_option('rrdtool_version')) {
$rrdtool_error .= "<br><span class='deviceDown'>" . __('ERROR: Installed RRDtool version does not match configured version.<br>Please visit the %s and select the correct RRDtool Utility Version.', "<a href='" . html_escape('settings.php?tab=general') . "'>" . __('Configuration Settings') . '</a>') . "</span><br>";
}
$graph_gif_count = db_fetch_cell('SELECT COUNT(*) FROM graph_templates_graph WHERE image_format_id = 2');
if ($graph_gif_count > 0) {
$rrdtool_error .= "<br><span class='deviceDown'>" . __('ERROR: RRDtool 1.2.x+ does not support the GIF images format, but %d" graph(s) and/or templates have GIF set as the image format.', $graph_gif_count) . '</span><br>';
}
/* Get spine version */
$spine_version = 'Unknown';
if ((file_exists(read_config_option('path_spine'))) && ((function_exists('is_executable')) && (is_executable(read_config_option('path_spine'))))) {
$out_array = array();
exec(read_config_option('path_spine') . ' --version', $out_array);
if (sizeof($out_array) > 0) {
$spine_version = $out_array[0];
}
}
/* ================= input validation ================= */
get_filter_request_var('tab', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z]+)$/')));
/* ==================================================== */
/* present a tabbed interface */
$tabs = array(
'summary' => __('Summary'),
'database' => __('Database'),
'phpinfo' => __('PHP Info'),
);
/* set the default tab */
load_current_session_value('tab', 'sess_ts_tabs', 'summary');
$current_tab = get_nfilter_request_var('tab');
$header_label = __('Technical Support [%s]', $tabs[get_request_var('tab')]);
if (sizeof($tabs)) {
$i = 0;
/* draw the tabs */
print "<div class='tabs'><nav><ul role='tablist'>\n";
foreach (array_keys($tabs) as $tab_short_name) {
print "<li class='subTab'><a class='tab" . (($tab_short_name == $current_tab) ? " selected'" : "'") .
" href='" . html_escape($config['url_path'] .
'utilities.php?action=view_tech' .
'&tab=' . $tab_short_name) .
"'>" . $tabs[$tab_short_name] . "</a></li>\n";
$i++;
}
api_plugin_hook('user_admin_tab');
print "</ul></nav></div>\n";
}
/* Display tech information */
html_start_box($header_label, '100%', '', '3', 'center', '');
if (get_request_var('tab') == 'summary') {
html_section_header(__('General Information'), 2);
form_alternate_row();
print '<td>' . __('Date') . "</td>\n";
print '<td>' . date('r') . "</td>\n";
form_end_row();
api_plugin_hook_function('custom_version_info');
form_alternate_row();
print '<td>' . __('Cacti Version') . "</td>\n";
print '<td>' . CACTI_VERSION . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Cacti OS') . "</td>\n";
print '<td>' . $config['cacti_server_os'] . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('RSA Fingerprint') . "</td>\n";
print '<td>' . read_config_option('rsa_fingerprint') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('NET-SNMP Version') . "</td>\n";
print '<td>' . $snmp_version . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('RRDtool Version') . "</td>\n";
print '<td>' . $rrdtool_versions[$rrdtool_version] . ' ' . $rrdtool_error . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Devices') . "</td>\n";
print '<td>' . number_format_i18n($host_count, -1) . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Graphs') . "</td>\n";
print '<td>' . number_format_i18n($graph_count, -1) . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Data Sources') . "</td>\n";
print '<td>';
$data_total = 0;
if (sizeof($data_count)) {
foreach ($data_count as $item) {
print $input_types[$item['type_id']] . ': ' . number_format_i18n($item['total'], -1) . '<br>';
$data_total += $item['total'];
}
print __('Total: %s', number_format_i18n($data_total, -1));
} else {
print "<span class='deviceDown'>0</span>";
}
print "</td>\n";
form_end_row();
html_section_header(__('Poller Information'), 2);
form_alternate_row();
print "<td>Interval</td>\n";
print '<td>' . read_config_option('poller_interval') . "</td>\n";
if (file_exists(read_config_option('path_spine')) && $poller_options[read_config_option('poller_type')] == 'spine') {
$type = $spine_version;
} else {
$type = $poller_options[read_config_option('poller_type')];
}
form_end_row();
form_alternate_row();
print '<td>' . __('Type') . "</td>\n";
print '<td>' . $type . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Items') . "</td>\n";
print '<td>';
$total = 0;
if (sizeof($poller_item)) {
foreach ($poller_item as $item) {
print __('Action[%s]', $item['action']) . ': ' . number_format_i18n($item['total'], -1) . '<br>';
$total += $item['total'];
}
print __('Total: %s', number_format_i18n($total, -1));
} else {
print "<span class='deviceDown'>" . __('No items to poll') . "</span>";
}
print "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Concurrent Processes') . "</td>\n";
print '<td>' . read_config_option('concurrent_processes') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Max Threads') . "</td>\n";
print '<td>' . read_config_option('max_threads') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('PHP Servers') . "</td>\n";
print '<td>' . read_config_option('php_servers') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Script Timeout') . "</td>\n";
print '<td>' . read_config_option('script_timeout') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Max OID') . "</td>\n";
print '<td>' . read_config_option('max_get_size') . "</td>\n";
form_end_row();
form_alternate_row();
print '<td>' . __('Last Run Statistics') . "</td>\n";
print '<td>' . read_config_option('stats_poller') . "</td>\n";
form_end_row();
/* Get System Memory */
$memInfo = utilities_get_system_memory();
if (sizeof($memInfo)) {
html_section_header(__('System Memory'), 2);
foreach($memInfo as $name => $value) {
if ($config['cacti_server_os'] == 'win32') {
form_alternate_row();
print "<td>$name</td>\n";
print '<td>' . number_format_i18n($value/1000, 2) . " MB</td>\n";
form_end_row();
} else {
switch($name) {
case 'SwapTotal':
case 'SwapFree':
case 'Cached':
case 'MemTotal':
case 'MemFree':
case 'Buffers':
case 'Active':
case 'Inactive':
form_alternate_row();
print "<td>$name</td>\n";
print '<td>' . number_format_i18n($value/1000/1000, 2) . " MB</td>\n";
form_end_row();
}
}
}
form_end_row();
}
html_section_header(__('PHP Information'), 2);
form_alternate_row();
print "<td>" . __('PHP Version') . "</td>\n";
if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
print '<td>' . PHP_VERSION . "</td>\n";
} else {
print '<td>' . PHP_VERSION . "</br><span class='deviceDown'>" . __('PHP Version 5.5.0+ is recommended due to strong password hashing support.') . "</span></td>\n";
}
form_end_row();
form_alternate_row();
print "<td>" . __('PHP OS') . "</td>\n";
print '<td>' . PHP_OS . "</td>\n";
form_end_row();
form_alternate_row();
print "<td>" . __('PHP uname') . "</td>\n";
print '<td>';
if (function_exists('php_uname')) {
print php_uname();
} else {
print __('N/A');
}
print "</td>\n";
form_end_row();
form_alternate_row();
print "<td>" . __('PHP SNMP') . "</td>\n";
print '<td>';
if (function_exists('snmpget')) {
print __('Installed');
} else {
print __('Not Installed');
}
print "</td>\n";
form_end_row();
form_alternate_row();
print "<td>max_execution_time</td>\n";
print '<td>' . ini_get('max_execution_time') . "</td>\n";
form_end_row();
form_alternate_row();
print "<td>memory_limit</td>\n";
print '<td>' . ini_get('memory_limit');
/* Calculate memory suggestion based off of data source count */
$memory_suggestion = $data_total * 32768;
/* Set minimum - 16M */
if ($memory_suggestion < 16777216) {
$memory_suggestion = 16777216;
}
/* Set maximum - 512M */
if ($memory_suggestion > 536870912) {
$memory_suggestion = 536870912;
}
/* Suggest values in 8M increments */
$memory_suggestion = round($memory_suggestion / 8388608) * 8388608;
if (memory_bytes(ini_get('memory_limit')) < $memory_suggestion) {
print "<br><span class='deviceDown'>";
if ((ini_get('memory_limit') == -1)) {
print __("You've set memory limit to 'unlimited'.") . "<br>";
}
print __('It is highly suggested that you alter you php.ini memory_limit to %s or higher.', memory_readable($memory_suggestion)) . ' <br/>' .
__('This suggested memory value is calculated based on the number of data source present and is only to be used as a suggestion, actual values may vary system to system based on requirements.');
print '</span><br>';
}
print "</td>\n";
form_end_row();
utilities_get_mysql_recommendations();
} elseif (get_request_var('tab') == 'database') {
html_section_header(__('MySQL Table Information - Sizes in KBytes'), 2);
form_alternate_row();
print " <td colspan='2' style='text-align:left;padding:0px'>";
if (sizeof($tables) > 0) {
print "<table id='tables' class='cactiTable' style='width:100%'>\n";
print "<thead>\n";
print "<tr class='tableHeader'>\n";
print " <th class='tableSubHeaderColumn'>" . __('Name') . "</th>\n";
print " <th class='tableSubHeaderColumn'>" . __('Engine') . "</th>\n";
print " <th class='tableSubHeaderColumn right'>" . __('Rows') . "</th>\n";
print " <th class='tableSubHeaderColumn right'>" . __('Avg Row Length') . "</th>\n";
print " <th class='tableSubHeaderColumn right'>" . __('Data Length') . "</th>\n";
print " <th class='tableSubHeaderColumn right'>" . __('Index Length') . "</th>\n";
print " <th class='tableSubHeaderColumn'>" . __('Collation') . "</th>\n";
print " <th class='tableSubHeaderColumn'>" . __('Comment') . "</th>\n";
print "</tr>\n";
print "</thead>\n";
foreach ($tables as $table) {
form_alternate_row();
print '<td>' . $table['TABLE_NAME'] . "</td>\n";
print '<td>' . $table['ENGINE'] . "</td>\n";
print '<td class="right">' . number_format_i18n($table['TABLE_ROWS'], -1) . "</td>\n";
print '<td class="right">' . number_format_i18n($table['AVG_ROW_LENGTH'], -1) . "</td>\n";
print '<td class="right">' . number_format_i18n($table['DATA_LENGTH'], -1) . "</td>\n";
print '<td class="right">' . number_format_i18n($table['INDEX_LENGTH'], -1) . "</td>\n";
print '<td>' . $table['TABLE_COLLATION'] . "</td>\n";
print '<td>' . $table['TABLE_COMMENT'] . "</td>\n";
form_end_row();
}
print "</table>\n";
} else {
print __('Unable to retrieve table status');
}
print "</td>\n";
form_end_row();
} else {
html_section_header(__('PHP Module Information'), 2);
form_alternate_row();
$php_info = str_replace(
array('width="600"', 'th colspan="2"', ','),
array('', 'th class="subHeaderColumn"', ', '),
$php_info
);
print "<td colspan='2'>" . $php_info . "</td>\n";
form_end_row();
}
html_end_box();
?>
<script type='text/javascript'>
$(function() {
$('#tables').tablesorter({
widgets: ['zebra'],
widgetZebra: { css: ['even', 'odd'] },
headerTemplate: '<div class="textSubHeaderDark">{content} {icon}</div>',
cssIconAsc: 'fa-sort-asc',
cssIconDesc: 'fa-sort-desc',
cssIconNone: 'fa-sort',
cssIcon: 'fa'
});
});
</script>
<?php
}
function utilities_view_user_log() {
global $auth_realms, $item_rows;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'filter' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'time',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
),
'username' => array(
'filter' => FILTER_CALLBACK,
'default' => '-1',
'options' => array('options' => 'sanitize_search_string')
),
'result' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
)
);
validate_store_request_vars($filters, 'sess_userlog');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
?>
<script type="text/javascript">
function clearFilter() {
strURL = urlPath+'utilities.php?action=view_user_log&clear=1&header=false';
loadPageNoHeader(strURL);
}
function purgeLog() {
strURL = urlPath+'utilities.php?action=clear_user_log&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#purge').click(function() {
purgeLog();
});
$('#form_userlog').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
function applyFilter() {
strURL = urlPath+'utilities.php?username=' + $('#username').val();
strURL += '&result=' + $('#result').val();
strURL += '&rows=' + $('#rows').val();
strURL += '&filter=' + $('#filter').val();
strURL += '&action=view_user_log';
strURL += '&header=false';
loadPageNoHeader(strURL);
}
</script>
<?php
html_start_box(__('User Login History'), '100%', '', '3', 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='form_userlog' action='utilities.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('User');?>
</td>
<td>
<select id='username' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('username') == '-1') {?> selected<?php }?>><?php print __('All');?></option>
<option value='-2'<?php if (get_request_var('username') == '-2') {?> selected<?php }?>><?php print __('Deleted/Invalid');?></option>
<?php
$users = db_fetch_assoc('SELECT DISTINCT username FROM user_auth ORDER BY username');
if (sizeof($users)) {
foreach ($users as $user) {
print "<option value='" . $user['username'] . "'"; if (get_request_var('username') == $user['username']) { print ' selected'; } print '>' . $user['username'] . "</option>\n";
}
}
?>
</select>
</td>
<td>
<?php print __('Result');?>
</td>
<td>
<select id='result' onChange='applyFilter()'>
<option value='-1'<?php if (get_request_var('result') == '-1') {?> selected<?php }?>><?php print __('Any');?></option>
<option value='1'<?php if (get_request_var('result') == '1') {?> selected<?php }?>><?php print __('Success - Pswd');?></option>
<option value='2'<?php if (get_request_var('result') == '2') {?> selected<?php }?>><?php print __('Success - Token');?></option>
<option value='0'<?php if (get_request_var('result') == '0') {?> selected<?php }?>><?php print __('Failed');?></option>
</select>
</td>
<td>
<?php print __('Attempts');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . html_escape($value) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input type='button' id='refresh' value='<?php print __esc_x('Button: use filter settings', 'Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' id='clear' value='<?php print __esc_x('Button: reset filter settings', 'Clear');?>' title='<?php print __esc('Clear Filters');?>'>
<input type='button' id='purge' value='<?php print __esc_x('Button: delete all table entries', 'Purge');?>' title='<?php print __esc('Purge User Log');?>'>
</span>
</td>
</tr>
</table>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search');?>
</td>
<td>
<input id='filter' type='text' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
</tr>
</table>
<input type='hidden' name='action' value='view_user_log'>
</form>
</td>
</tr>
<?php
html_end_box();
$sql_where = '';
/* filter by username */
if (get_request_var('username') == '-2') {
$sql_where = 'WHERE ul.username NOT IN (SELECT DISTINCT username FROM user_auth)';
} elseif (get_request_var('username') != '-1') {
$sql_where = "WHERE ul.username='" . get_request_var('username') . "'";
}
/* filter by result */
if (get_request_var('result') != '-1') {
if ($sql_where != '') {
$sql_where .= ' AND ul.result=' . get_request_var('result');
} else {
$sql_where = 'WHERE ul.result=' . get_request_var('result');
}
}
/* filter by search string */
if (get_request_var('filter') != '') {
if ($sql_where != '') {
$sql_where .= " AND (ul.username LIKE '%" . get_request_var('filter') . "%'
OR ul.time LIKE '%" . get_request_var('filter') . "%'
OR ua.full_name LIKE '%" . get_request_var('filter') . "%'
OR ul.ip LIKE '%" . get_request_var('filter') . "%')";
} else {
$sql_where = "WHERE (ul.username LIKE '%" . get_request_var('filter') . "%'
OR ul.time LIKE '%" . get_request_var('filter') . "%'
OR ua.full_name LIKE '%" . get_request_var('filter') . "%'
OR ul.ip LIKE '%" . get_request_var('filter') . "%')";
}
}
$total_rows = db_fetch_cell("SELECT
COUNT(*)
FROM user_auth AS ua
RIGHT JOIN user_log AS ul
ON ua.username=ul.username
$sql_where");
$user_log_sql = "SELECT ul.username, ua.full_name, ua.realm,
ul.time, ul.result, ul.ip
FROM user_auth AS ua
RIGHT JOIN user_log AS ul
ON ua.username=ul.username
$sql_where
ORDER BY " . get_request_var('sort_column') . ' ' . get_request_var('sort_direction') . '
LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
$user_log = db_fetch_assoc($user_log_sql);
$nav = html_nav_bar('utilities.php?action=view_user_log&username=' . get_request_var('username') . '&filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 6, __('User Logins'), 'page', 'main');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
'username' => array(__('User'), 'ASC'),
'full_name' => array(__('Full Name'), 'ASC'),
'realm' => array(__('Authentication Realm'), 'ASC'),
'time' => array(__('Date'), 'ASC'),
'result' => array(__('Result'), 'DESC'),
'ip' => array(__('IP Address'), 'DESC')
);
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), 1, 'utilities.php?action=view_user_log');
if (sizeof($user_log)) {
foreach ($user_log as $item) {
form_alternate_row('', true);
?>
<td class='nowrap'>
<?php print filter_value($item['username'], get_request_var('filter'));?>
</td>
<td class='nowrap'>
<?php if (isset($item['full_name'])) {
print filter_value($item['full_name'], get_request_var('filter'));
} else {
print __('(User Removed)');
}
?>
</td>
<td class='nowrap'>
<?php if (isset($auth_realms[$item['realm']])) {
print filter_value($auth_realms[$item['realm']], get_request_var('filter'));
} else {
print __('N/A');
}
?>
</td>
<td class='nowrap'>
<?php print filter_value($item['time'], get_request_var('filter'));?>
</td>
<td class='nowrap'>
<?php print ($item['result'] == 0 ? 'Failed':($item['result'] == 1 ? 'Success - Pswd':'Success - Token'));?>
</td>
<td class='nowrap'>
<?php print filter_value($item['ip'], get_request_var('filter'));?>
</td>
</tr>
<?php
}
}
html_end_box();
if (sizeof($user_log)) {
print $nav;
}
}
function utilities_clear_user_log() {
$users = db_fetch_assoc('SELECT DISTINCT username FROM user_auth');
if (sizeof($users)) {
/* remove active users */
foreach ($users as $user) {
$total_login_rows = db_fetch_cell_prepared('SELECT COUNT(username)
FROM user_log
WHERE username = ?
AND result IN (1)',
array($user['username']));
$total_token_rows = db_fetch_cell_prepared('SELECT COUNT(username)
FROM user_log
WHERE username = ?
AND result IN (2)',
array($user['username']));
if ($total_login_rows > 1) {
db_execute_prepared('DELETE
FROM user_log
WHERE username = ?
AND result IN(1)
ORDER BY time LIMIT ' . ($total_login_rows - 1),
array($user['username']));
}
if ($total_token_rows > 1) {
db_execute_prepared('DELETE
FROM user_log
WHERE username = ?
AND result IN(2)
ORDER BY time
LIMIT ' . ($total_token_rows - 1),
array($user['username']));
}
db_execute_prepared('DELETE
FROM user_log
WHERE username = ?
AND result = 0',
array($user['username']));
}
/* delete inactive users */
db_execute('DELETE
FROM user_log
WHERE user_id NOT IN (SELECT id FROM user_auth)
OR username NOT IN (SELECT username FROM user_auth)');
}
}
function utilities_view_logfile() {
global $log_tail_lines, $page_refresh_interval, $config;
$logfile = read_config_option('path_cactilog');
$logbase = basename($logfile);
if (isset_request_var('filename')) {
$requestedFile = dirname($logfile) . '/' . basename(get_nfilter_request_var('filename'));
if (file_exists($requestedFile)) {
$logfile = $requestedFile;
} else {
$logfile = read_config_option('path_cactilog');
}
}
if ($logfile == '') {
$logfile = $config['base_path'] . '/log/cacti.log';
}
if (get_nfilter_request_var('filename') != '') {
if (strpos(get_nfilter_request_var('filename'), $logbase) === false) {
raise_message('clog_invalid');
header('Location: utilities.php?action=view_logfile&filename=' . $logbase);
exit(0);
}
}
/* ================= input validation and session storage ================= */
$filters = array(
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'tail_lines' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'rfilter' => array(
'filter' => FILTER_VALIDATE_IS_REGEX,
'pageset' => true,
'default' => ''
),
'message_type' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'reverse' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'refresh' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('log_refresh_interval')
)
);
validate_store_request_vars($filters, 'sess_log');
/* ================= input validation ================= */
$page_nr = get_request_var('page');
$page = 'utilities.php?action=view_logfile&header=false';
$page .= '&filename=' . basename($logfile) . '&page=' . $page_nr;
$refresh = array(
'seconds' => get_request_var('refresh'),
'page' => $page,
'logout' => 'false'
);
set_page_refresh($refresh);
top_header();
?>
<script type='text/javascript'>
function purgeLog() {
strURL = urlPath+'utilities.php?action=purge_logfile&header=false&filename='+$('#filename').val();
loadPageNoHeader(strURL);
}
$(function() {
$('#refreshme').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#purge').click(function() {
purgeLog();
});
$('#form_logfile').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
function applyFilter() {
strURL = urlPath+'utilities.php?tail_lines=' + $('#tail_lines').val();
strURL += '&message_type=' + $('#message_type').val();
strURL += '&refresh=' + $('#refresh').val();
strURL += '&reverse=' + $('#reverse').val();
strURL += '&rfilter=' + $('#rfilter').val();
strURL += '&filename=' + $('#filename').val();
strURL += '&action=view_logfile';
strURL += '&header=false';
refreshMSeconds=$('#refresh').val()*1000;
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = urlPath+'utilities.php?clear=1';
strURL += '&action=view_logfile';
strURL += '&header=false';
loadPageNoHeader(strURL);
}
</script>