-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsetup.php
1618 lines (1383 loc) · 56.9 KB
/
setup.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. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
function plugin_syslog_install() {
global $config, $syslog_upgrade;
static $bg_inprocess = false;
syslog_determine_config();
if (defined('SYSLOG_CONFIG')) {
include(SYSLOG_CONFIG);
} else {
raise_message('syslog_info', __('Please rename either your config.php.dist or config_local.php.dist files in the syslog directory, and change setup your database before installing.', 'syslog'), MESSAGE_LEVEL_ERROR);
return false;
}
syslog_connect();
$syslog_exists = sizeof(syslog_db_fetch_row('SHOW TABLES FROM `' . $syslogdb_default . "` LIKE 'syslog'"));
/* ================= input validation ================= */
get_filter_request_var('days');
/* ==================================================== */
api_plugin_register_hook('syslog', 'config_arrays', 'syslog_config_arrays', 'setup.php');
api_plugin_register_hook('syslog', 'draw_navigation_text', 'syslog_draw_navigation_text', 'setup.php');
api_plugin_register_hook('syslog', 'config_settings', 'syslog_config_settings', 'setup.php');
api_plugin_register_hook('syslog', 'top_header_tabs', 'syslog_show_tab', 'setup.php');
api_plugin_register_hook('syslog', 'top_graph_header_tabs', 'syslog_show_tab', 'setup.php');
api_plugin_register_hook('syslog', 'top_graph_refresh', 'syslog_top_graph_refresh', 'setup.php');
api_plugin_register_hook('syslog', 'poller_bottom', 'syslog_poller_bottom', 'setup.php');
api_plugin_register_hook('syslog', 'graph_buttons', 'syslog_graph_buttons', 'setup.php');
api_plugin_register_hook('syslog', 'config_insert', 'syslog_config_insert', 'setup.php');
api_plugin_register_hook('syslog', 'utilities_list', 'syslog_utilities_list', 'setup.php');
api_plugin_register_hook('syslog', 'utilities_action', 'syslog_utilities_action', 'setup.php');
/* hook for table replication */
api_plugin_register_hook('syslog', 'replicate_out', 'syslog_replicate_out', 'setup.php');
api_plugin_register_realm('syslog', 'syslog.php', 'Syslog User', 1);
api_plugin_register_realm('syslog', 'syslog_alerts.php,syslog_removal.php,syslog_reports.php', 'Syslog Administration', 1);
if (isset_request_var('install')) {
if (!$bg_inprocess) {
syslog_execute_update($syslog_exists, $_REQUEST);
$bg_inprocess = true;
return true;
}
} elseif (isset($syslog_install_options) && cacti_sizeof($syslog_install_options)) {
/* hack for syslog so IBM Spectrum LSF RTM can install syslog without user interaction with preset defaults */
if (!$bg_inprocess) {
syslog_execute_update($syslog_exists, $syslog_install_options);
$bg_inprocess = true;
}
} elseif (isset_request_var('cancel')) {
header('Location:' . $config['url_path'] . 'plugins.php?mode=uninstall&id=syslog&uninstall&uninstall_method=all');
exit;
} else {
syslog_install_advisor($syslog_exists);
exit;
}
}
function syslog_execute_update($syslog_exists, $options) {
global $config;
if (isset($options['cancel'])) {
header('Location:' . $config['url_path'] . 'plugins.php?mode=uninstall&id=syslog&uninstall&uninstall_method=all');
exit;
} elseif (isset($options['return'])) {
db_execute('DELETE FROM plugin_config WHERE directory="syslog"');
db_execute('DELETE FROM plugin_realms WHERE plugin="syslog"');
db_execute('DELETE FROM plugin_db_changes WHERE plugin="syslog"');
db_execute('DELETE FROM plugin_hooks WHERE name="syslog"');
} elseif (isset($options['upgrade_type'])) {
if ($options['upgrade_type'] == 'truncate') {
syslog_setup_table_new($options);
}
} else {
syslog_setup_table_new($options);
}
set_config_option('syslog_retention', $options['days']);
}
function plugin_syslog_uninstall() {
global $config, $syslogdb_default;
syslog_determine_config();
syslog_connect();
if (isset_request_var('cancel') || isset_request_var('return')) {
header('Location:' . $config['url_path'] . 'plugins.php?header=false');
exit;
} elseif (isset_request_var('uninstall_method')) {
if (get_nfilter_request_var('uninstall_method') == 'all') {
/* do the big tables first */
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_removed`');
/* do the settings tables last */
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_incoming`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_alert`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_remove`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_reports`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_facilities`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_statistics`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_host_facilities`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_priorities`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_logs`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_hosts`');
} else {
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog`');
syslog_db_execute('DROP TABLE IF EXISTS `' . $syslogdb_default . '`.`syslog_removed`');
}
} elseif (function_exists('syslog_uninstall_advisor')) {
syslog_uninstall_advisor();
exit;
}
}
function plugin_syslog_check_config() {
/* Here we will check to ensure everything is configured */
if (!file_exists(dirname(__FILE__) . '/config_local.php') && !file_exists(dirname(__FILE__) . '/config.php')) {
raise_message('syslog_info', __('Please rename either your config.php.dist or config_local.php.dist files in the syslog directory, and change setup your database before installing.', 'syslog'), MESSAGE_LEVEL_ERROR);
return false;
}
if (api_plugin_installed('flowview')) {
syslog_check_upgrade();
}
return true;
}
function plugin_syslog_upgrade() {
/* Here we will upgrade to the newest version */
syslog_check_upgrade();
return false;
}
function syslog_connect() {
global $config, $syslog_cnn, $syslogdb_default, $local_db_cnn_id, $remote_db_cnn_id;
syslog_determine_config();
// Handle remote syslog processing
if (defined('SYSLOG_CONFIG')) {
include(SYSLOG_CONFIG);
}
include_once(dirname(__FILE__) . '/functions.php');
include_once(dirname(__FILE__) . '/database.php');
$connect_remote = false;
$connected = true;
/* Connect to the Syslog Database */
if (empty($syslog_cnn)) {
if ($config['poller_id'] == 1) {
if ($use_cacti_db == true) {
$syslog_cnn = $local_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
} elseif (isset($config['syslog_remote_db'])) {
if ($use_cacti_db == true) {
$syslog_cnn = $local_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
} else {
if ($use_cacti_db == true) {
$syslog_cnn = $remote_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
}
if ($connect_remote) {
if (!isset($syslogdb_port)) {
$syslogdb_port = '3306';
}
if (!isset($syslogdb_retries)) {
$syslogdb_retries = '5';
}
if (!isset($syslogdb_ssl)) {
$syslogdb_ssl = false;
}
if (!isset($syslogdb_ssl_key)) {
$syslogdb_ssl_key = '';
}
if (!isset($syslogdb_ssl_cert)) {
$syslogdb_ssl_cert = '';
}
if (!isset($syslogdb_ssl_ca)) {
$syslogdb_ssl_ca = '';
}
$syslog_cnn = syslog_db_connect_real($syslogdb_hostname, $syslogdb_username, $syslogdb_password, $syslogdb_default, $syslogdb_type, $syslogdb_port, $syslogdb_retries, $syslogdb_ssl, $syslogdb_ssl_key, $syslogdb_ssl_cert, $syslogdb_ssl_ca);
if ($syslog_cnn == false) {
print "FATAL Can not connect\n";
$connected = false;
}
}
if ($connected && !syslog_db_table_exists('syslog') && api_plugin_is_enabled('syslog')) {
cacti_log('Setting Up Database Tables Since they do not exist', false, 'SYSLOG');
if (!isset($syslog_install_options)) {
$syslog_install_options = array();
}
syslog_setup_table_new($syslog_install_options);
}
}
return $connected;
}
function syslog_check_upgrade() {
global $config, $syslogdb_default, $syslog_levels, $syslog_upgrade;
syslog_connect();
// Let's only run this check if we are on a page that actually needs the data
$files = array('plugins.php', 'syslog.php', 'syslog_removal.php', 'syslog_alerts.php', 'syslog_reports.php');
if (substr($_SERVER['SCRIPT_FILENAME'], -18) != 'syslog_process.php' && !in_array(get_current_page(), $files)) {
return;
}
/* don't let this script timeout */
ini_set('max_execution_time', 0);
if (function_exists('api_plugin_upgrade_register')) {
if (!api_plugin_upgrade_register('syslog')) {
// No upgrade required
return;
}
} else {
$version = plugin_syslog_version();
$current = $version['version'];
$old = db_fetch_cell("SELECT version FROM plugin_config WHERE directory='syslog'");
if ($current != $old) {
api_plugin_register_hook('syslog', 'replicate_out', 'syslog_replicate_out', 'setup.php', 1);
db_execute_prepared("UPDATE plugin_config SET
version = ?, name = ?, author = ?, webpage = ?
WHERE directory = ?",
array(
$version['version'],
$version['longname'],
$version['author'],
$version['homepage'],
$version['name']
)
);
} else {
// No upgrade required
return;
}
}
if (!db_column_exists('syslog_statistics', 'id')) {
syslog_db_execute('ALTER TABLE syslog_statistics
ADD COLUMN id BIGINT UNSIGNED auto_increment FIRST,
DROP PRIMARY KEY,
ADD PRIMARY KEY(id),
ADD UNIQUE INDEX (`host_id`,`facility_id`,`priority_id`,`program_id`,`insert_time`)');
}
if (!syslog_db_column_exists('syslog_alert', 'hash')) {
syslog_db_add_column('syslog_alert', array(
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id')
);
syslog_db_add_column('syslog_remove', array(
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id')
);
syslog_db_add_column('syslog_reports', array(
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id')
);
}
if (syslog_db_column_exists('syslog_incoming', 'date')) {
syslog_db_execute("ALTER TABLE syslog_incoming
DROP COLUMN date,
CHANGE COLUMN `time` logtime timestamp default '0000-00-00';");
}
$alerts = syslog_db_fetch_assoc('SELECT *
FROM syslog_alert
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($alerts)) {
foreach($alerts as $a) {
$hash = get_hash_syslog($a['id'], 'syslog_alert');
syslog_db_execute_prepared('UPDATE syslog_alert
SET hash = ?
WHERE id = ?',
array($hash, $a['id']));
}
}
$removes = syslog_db_fetch_assoc('SELECT *
FROM syslog_remove
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($removes)) {
foreach($removes as $r) {
$hash = get_hash_syslog($r['id'], 'syslog_remove');
syslog_db_execute_prepared('UPDATE syslog_remove
SET hash = ?
WHERE id = ?',
array($hash, $r['id']));
}
}
$reports = syslog_db_fetch_assoc('SELECT *
FROM syslog_reports
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($reports)) {
foreach($reports as $r) {
$hash = get_hash_syslog($r['id'], 'syslog_reports');
syslog_db_execute_prepared('UPDATE syslog_reports
SET hash = ?
WHERE id = ?',
array($hash, $r['id']));
}
}
if (!syslog_db_column_exists('syslog_alert', 'level')) {
syslog_db_add_column('syslog_alert', array(
'name' => 'level',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'method')
);
}
if (!syslog_db_column_exists('syslog_alert', 'notify')) {
syslog_db_add_column('syslog_alert', array(
'name' => 'notify',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'email')
);
}
if (!syslog_db_column_exists('syslog_alert', 'body')) {
syslog_db_add_column('syslog_alert', array(
'name' => 'body',
'type' => 'varchar(8192)',
'NULL' => false,
'default' => '',
'after' => 'message')
);
}
if (!syslog_db_column_exists('syslog_reports', 'notify')) {
syslog_db_add_column('syslog_reports', array(
'name' => 'notify',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'email')
);
}
syslog_db_execute('ALTER TABLE syslog_reports MODIFY column body VARCHAR(8192) NOT NULL default ""');
}
function syslog_create_partitioned_syslog_table($engine = 'InnoDB', $days = 30) {
global $config, $syslogdb_default, $syslog_levels;
syslog_connect();
if (stripos($engine, 'aria') !== false) {
$row_format = 'ROW_FORMAT=Page';
} else {
$row_format = 'ROW_FORMAT=Dynamic';
}
$sql = "CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog` (
facility_id int(10) unsigned default NULL,
priority_id int(10) unsigned default NULL,
program_id int(10) unsigned default NULL,
host_id int(10) unsigned default NULL,
logtime DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
message varchar(1024) NOT NULL default '',
seq bigint unsigned NOT NULL auto_increment,
PRIMARY KEY(seq, logtime),
INDEX `seq` (`seq`),
INDEX logtime (logtime),
INDEX program_id (program_id),
INDEX host_id (host_id),
INDEX priority_id (priority_id),
INDEX facility_id (facility_id))
ENGINE=$engine
$row_format
PARTITION BY RANGE (TO_DAYS(logtime))\n";
$now = time();
$parts = '';
for($i = $days; $i >= -1; $i--) {
$timestamp = $now - ($i * 86400);
$date = date('Y-m-d', $timestamp);
$format = date('Ymd', $timestamp - 86400);
$parts .= ($parts != '' ? ",\n":"(") . " PARTITION d" . $format . " VALUES LESS THAN (TO_DAYS('" . $date . "'))";
}
$parts .= ",\nPARTITION dMaxValue VALUES LESS THAN MAXVALUE);";
syslog_db_execute($sql . $parts);
}
function syslog_setup_table_new($options) {
global $config, $settings, $syslogdb_default, $syslog_levels;
syslog_connect();
$tables = array();
$syslog_levels = array(
0 => 'emerg',
1 => 'crit',
2 => 'alert',
3 => 'err',
4 => 'warn',
5 => 'notice',
6 => 'info',
7 => 'debug',
8 => 'other'
);
// Set default if they are not set.
if (!cacti_sizeof($options)) {
$options['upgrade_type'] = read_config_option('syslog_install_upgrade_type');
$options['engine'] = read_config_option('syslog_install_engine');
$options['db_type'] = read_config_option('syslog_install_db_type');
$options['days'] = read_config_option('syslog_install_days');
if (empty($options['upgrade_type'])) {
$options['upgrade_type'] = 'upgrade';
}
if (empty($options['engine'])) {
$options['engine'] = 'InnoDB';
}
if (empty($options['db_type'])) {
$options['db_type'] = 'part';
}
if (empty($options['days'])) {
$options['days'] = 30;
}
}
/* validate some simple information */
$truncate = isset($options['upgrade_type']) && $options['upgrade_type'] == 'truncate' ? true:false;
$engine = isset($options['engine']) && $options['engine'] == 'innodb' ? 'InnoDB':$options['engine'];
$partitioned = isset($options['db_type']) && $options['db_type'] == 'part' ? true:false;
$syslogexists = sizeof(syslog_db_fetch_row("SHOW TABLES FROM `" . $syslogdb_default . "` LIKE 'syslog'"));
/* set table construction settings for the remote pollers */
set_config_option('syslog_install_upgrade_type', empty($options['upgrade_type'])?'':$options['upgrade_type'], true);
set_config_option('syslog_install_engine', empty($options['engine']) ?'':$options['engine'], true);
set_config_option('syslog_install_db_type', empty($options['db_type']) ?'':$options['db_type'], true);
set_config_option('syslog_install_days', empty($options['days']) ?'':$options['days'], true);
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog`");
}
if (stripos($engine, 'aria') !== false) {
$row_format = 'ROW_FORMAT=Page';
} else {
$row_format = 'ROW_FORMAT=Dynamic';
}
if (!$partitioned) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog` (
facility_id int(10) unsigned default NULL,
priority_id int(10) unsigned default NULL,
program_id int(10) unsigned default NULL,
host_id int(10) unsigned default NULL,
logtime TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
message varchar(2048) NOT NULL default '',
seq bigint unsigned NOT NULL auto_increment,
PRIMARY KEY (seq, logtime),
INDEX `seq` (`seq`),
INDEX logtime (logtime),
INDEX program_id (program_id),
INDEX host_id (host_id),
INDEX priority_id (priority_id),
INDEX facility_id (facility_id))
ENGINE=$engine
$row_format");
} else {
syslog_create_partitioned_syslog_table($engine, $options['days']);
}
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_alert`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_alert` (
`id` int(10) NOT NULL auto_increment,
`hash` varchar(32) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`severity` int(10) UNSIGNED NOT NULL default '0',
`method` int(10) unsigned NOT NULL default '0',
`level` int(10) unsigned NOT NULL default '0',
`num` int(10) unsigned NOT NULL default '1',
`type` varchar(16) NOT NULL default '',
`enabled` CHAR(2) default 'on',
`repeat_alert` int(10) unsigned NOT NULL default '0',
`open_ticket` CHAR(2) default '',
`message` VARCHAR(2048) NOT NULL default '',
`body` VARCHAR(8192) NOT NULL default '',
`user` varchar(32) NOT NULL default '',
`date` int(16) NOT NULL default '0',
`email` varchar(255) default NULL,
`notify` int(10) unsigned NOT NULL default '0',
`command` varchar(255) default NULL,
`notes` varchar(255) default NULL,
PRIMARY KEY (id))
ENGINE=InnoDB");
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_incoming`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_incoming` (
facility_id int(10) unsigned default NULL,
priority_id int(10) unsigned default NULL,
program varchar(40) default NULL,
logtime TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
host varchar(64) default NULL,
message varchar(2048) NOT NULL DEFAULT '',
seq bigint unsigned NOT NULL auto_increment,
`status` tinyint(4) NOT NULL default '0',
PRIMARY KEY (seq),
INDEX program (program),
INDEX `status` (`status`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_remove`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_remove` (
id int(10) NOT NULL auto_increment,
`hash` varchar(32) NOT NULL default '',
name varchar(255) NOT NULL default '',
`type` varchar(16) NOT NULL default '',
enabled CHAR(2) DEFAULT 'on',
method CHAR(5) DEFAULT 'del',
message VARCHAR(2048) NOT NULL default '',
`user` varchar(32) NOT NULL default '',
`date` int(16) NOT NULL default '0',
notes varchar(255) default NULL,
PRIMARY KEY (id))
ENGINE=$engine
$row_format");
$present = syslog_db_fetch_row("SHOW TABLES FROM `" . $syslogdb_default . "` LIKE 'syslog_reports'");
if (cacti_sizeof($present)) {
$newreport = sizeof(syslog_db_fetch_row("SHOW COLUMNS FROM `" . $syslogdb_default . "`.`syslog_reports` LIKE 'body'"));
} else {
$newreport = true;
}
if ($truncate || !$newreport) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_reports`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_reports` (
id int(10) NOT NULL auto_increment,
`hash` varchar(32) NOT NULL default '',
name varchar(255) NOT NULL default '',
`type` varchar(16) NOT NULL default '',
enabled CHAR(2) DEFAULT 'on',
timespan int(16) NOT NULL default '0',
timepart char(5) NOT NULL default '00:00',
lastsent int(16) NOT NULL default '0',
body varchar(8192) NOT NULL default '0',
message varchar(2048) default NULL,
`user` varchar(32) NOT NULL default '',
`date` int(16) NOT NULL default '0',
email varchar(255) default NULL,
notify int(10) unsigned NOT NULL default '0',
notes varchar(255) default NULL,
PRIMARY KEY (id))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_hosts`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_programs` (
`program_id` int(10) unsigned NOT NULL auto_increment,
`program` VARCHAR(40) NOT NULL,
`last_updated` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`program`),
INDEX host_id (`program_id`),
INDEX last_updated (`last_updated`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic
COMMENT='Contains all programs currently in the syslog table'");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_hosts` (
`host_id` int(10) unsigned NOT NULL auto_increment,
`host` VARCHAR(64) NOT NULL,
`last_updated` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`host`),
INDEX host_id (`host_id`),
INDEX last_updated (`last_updated`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic
COMMENT='Contains all hosts currently in the syslog table'");
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_facilities`");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `". $syslogdb_default . "`.`syslog_facilities` (
`facility_id` int(10) unsigned NOT NULL,
`facility` varchar(10) NOT NULL,
`last_updated` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`facility_id`),
INDEX last_updated (`last_updated`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
syslog_db_execute("INSERT INTO `" . $syslogdb_default . "`.`syslog_facilities` (facility_id, facility) VALUES
(0,'kern'), (1,'user'), (2,'mail'), (3,'daemon'), (4,'auth'), (5,'syslog'), (6,'lpd'), (7,'news'),
(8,'uucp'), (9,'crond'), (10,'authpriv'), (11,'ftpd'), (12,'ntpd'), (13,'logaudit'), (14,'logalert'),
(15,'crond'), (16,'local0'), (17,'local1'), (18,'local2'), (19,'local3'), (20,'local4'), (21,'local5'),
(22,'local6'), (23,'local7');");
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_priorities`");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `". $syslogdb_default . "`.`syslog_priorities` (
`priority_id` int(10) unsigned NOT NULL,
`priority` varchar(10) NOT NULL,
`last_updated` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`priority_id`),
INDEX last_updated (`last_updated`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
syslog_db_execute("INSERT INTO `" . $syslogdb_default . "`.`syslog_priorities` (priority_id, priority) VALUES
(0,'emerg'), (1,'alert'), (2,'crit'), (3,'err'), (4,'warning'), (5,'notice'), (6,'info'), (7,'debug'), (8,'other');");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `". $syslogdb_default . "`.`syslog_host_facilities` (
`host_id` int(10) unsigned NOT NULL,
`facility_id` int(10) unsigned NOT NULL,
`last_updated` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`host_id`,`facility_id`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
if ($truncate) {
syslog_db_execute("DROP TABLE IF EXISTS `" . $syslogdb_default . "`.`syslog_removed`");
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_removed` LIKE `" . $syslogdb_default . "`.`syslog`");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_logs` (
alert_id int(10) unsigned not null default '0',
logseq bigint unsigned NOT NULL,
logtime TIMESTAMP NOT NULL default '0000-00-00 00:00:00',
logmsg varchar(1024) default NULL,
host varchar(64) default NULL,
facility_id int(10) unsigned default NULL,
priority_id int(10) unsigned default NULL,
program_id int(10) unsigned default NULL,
count integer unsigned NOT NULL default '0',
html blob default NULL,
seq bigint unsigned NOT NULL auto_increment,
PRIMARY KEY (seq),
INDEX `logseq` (`logseq`),
INDEX `program_id` (`program_id`),
INDEX `alert_id` (`alert_id`),
INDEX `host` (`host`),
INDEX `logtime` (`logtime`),
INDEX `priority_id` (`priority_id`),
INDEX `facility_id` (`facility_id`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
syslog_db_execute("CREATE TABLE IF NOT EXISTS `" . $syslogdb_default . "`.`syslog_statistics` (
`id` bigint UNSIGNED auto_increment,
`host_id` int(10) UNSIGNED NOT NULL,
`facility_id` int(10) UNSIGNED NOT NULL,
`priority_id` int(10) UNSIGNED NOT NULL,
`program_id` int(10) unsigned default NULL,
`insert_time` TIMESTAMP NOT NULL,
`records` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_pk` (`host_id`, `facility_id`, `priority_id`, `program_id`, `insert_time`),
INDEX `host_id`(`host_id`),
INDEX `facility_id`(`facility_id`),
INDEX `priority_id`(`priority_id`),
INDEX `program_id` (`program_id`),
INDEX `insert_time`(`insert_time`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic
COMMENT='Maintains High Level Statistics';");
if (!isset($settings['syslog'])) {
syslog_config_settings();
}
foreach($settings['syslog'] AS $name => $values) {
if (isset($values['default'])) {
set_config_option($name, $values['default']);
}
}
}
function syslog_replicate_out($data) {
syslog_connect();
if (read_config_option('syslog_remote_enabled') == 'on' && read_config_option('syslog_remote_sync_rules') == 'on') {
$remote_poller_id = $data['remote_poller_id'];
$rcnn_id = $data['rcnn_id'];
$class = $data['class'];
cacti_log('INFO: Replicating for the Syslog Plugin', false, 'REPLICATE');
if ($class == 'all') {
$tdata = syslog_db_fetch_assoc('SELECT * FROM syslog_alert');
replicate_out_table($rcnn_id, $tdata, 'syslog_alert', $remote_poller_id);
$tdata = syslog_db_fetch_assoc('SELECT * FROM syslog_remove');
replicate_out_table($rcnn_id, $tdata, 'syslog_remove', $remote_poller_id);
$tdata = syslog_db_fetch_assoc('SELECT * FROM syslog_reports');
replicate_out_table($rcnn_id, $tdata, 'syslog_reports', $remote_poller_id);
}
}
return $data;
}
function syslog_replicate_in() {
syslog_connect();
if (read_config_option('syslog_remote_enabled') == 'on' && read_config_option('syslog_remote_sync_rules') == 'on') {
$data = db_fetch_assoc('SELECT * FROM syslog_alert');
syslog_replace_data('syslog_alert', $data);
$data = db_fetch_assoc('SELECT * FROM syslog_remove');
syslog_replace_data('syslog_remove', $data);
$data = db_fetch_assoc('SELECT * FROM syslog_reports');
syslog_replace_data('syslog_reports', $data);
}
}
function syslog_replace_data($table, &$data) {
if (cacti_sizeof($data)) {
$sqlData = array();
$sqlQuery = array();
$columns = array_keys($data[0]);
$create = db_fetch_row('SHOW CREATE TABLE ' . $table);
if (isset($create["CREATE TABLE `$table`"]) || isset($create['Create Table'])) {
if (isset($create["CREATE TABLE `$table`"])) {
$create_sql = $create["CREATE TABLE `$table`"];
} else {
$create_sql = $create['Create Table'];
}
}
if (!syslog_db_table_exists($table)) {
syslog_db_execute($create);
syslog_db_execute("TRUNCATE TABLE $table");
}
// Make the prefix
$sql_prefix = "INSERT INTO $table (`" . implode('`,`', $columns) . '`) VALUES ';
// Make the suffix
$sql_suffix = ' ON DUPLICATE KEY UPDATE ';
foreach($columns as $c) {
$sql_suffix .= " $c = VALUES($c),";
}
$sql_suffix = trim($sql_suffix, ',');
// Construct the prepared statement
foreach($data as $row) {
$sqlQuery[] = '(' . trim(str_repeat('?, ', cacti_sizeof($columns)), ', ') . ')';
foreach($row as $col) {
$sqlData[] = $col;
}
}
$sql = implode(', ', $sqlQuery);
syslog_db_execute_prepared($sql_prefix . $sql . $sql_suffix, $sqlData);
}
}
function plugin_syslog_version() {
global $config;
$info = parse_ini_file($config['base_path'] . '/plugins/syslog/INFO', true);
return $info['info'];
}
function syslog_check_dependencies() {
return true;
}
function syslog_poller_bottom() {
global $config;
if (syslog_config_safe()) {
$command_string = read_config_option('path_php_binary');
$extra_args = ' -q ' . $config['base_path'] . '/plugins/syslog/syslog_process.php';
exec_background($command_string, $extra_args);
} else {
cacti_log('WARNING: You have installed the Syslog plugin, but you have not properly set a config.php or config_local.php', false, 'POLLER');
}
}
function syslog_install_advisor($syslog_exists) {
global $config, $syslog_retentions;
top_header();
syslog_config_arrays();
$fields_syslog_update = array(
'upgrade_type' => array(
'method' => 'drop_array',
'friendly_name' => __('What upgrade/install type do you wish to use', 'syslog'),
'description' => __('When you have very large tables, performing a Truncate will be much quicker. If you are concerned about archive data, you can choose either Inline, which will freeze your browser for the period of this upgrade, or background, which will create a background process to bring your old syslog data from a backup table to the new syslog format. Again this process can take several hours.', 'syslog'),
'value' => 'truncate',
'array' => array(
'truncate' => __('Truncate Syslog Table', 'syslog'),
'inline' => __('Inline Upgrade', 'syslog'),
'background' => __('Background Upgrade', 'syslog')
)
),
'engine' => array(
'method' => 'drop_array',
'friendly_name' => __('Database Storage Engine', 'syslog'),
'description' => __('You have the option to make this a partitioned table by days.', 'syslog'),
'value' => 'innodb',
'array' => array(
'myisam' => __('MyISAM Storage', 'syslog'),
'innodb' => __('InnoDB Storage', 'syslog'),
'aria' => __('Aria Storage', 'syslog')
)
),
'db_type' => array(
'method' => 'drop_array',
'friendly_name' => __('Database Architecture', 'syslog'),
'description' => __('You have the option to make this a partitioned table by days. You can create multiple partitions per day.', 'syslog'),
'value' => 'part',
'array' => array(
'trad' => __('Traditional Table', 'syslog'),
'part' => __('Partitioned Table', 'syslog')
)
),
'days' => array(
'method' => 'drop_array',
'friendly_name' => __('Retention Policy', 'syslog'),
'description' => __('Choose how many days of Syslog values you wish to maintain in the database.', 'syslog'),
'value' => '30',
'array' => $syslog_retentions
),
'mode' => array(
'method' => 'hidden',
'value' => 'install'
),
'install' => array(
'method' => 'hidden',
'value' => 'true'
),
'id' => array(
'method' => 'hidden',
'value' => 'syslog'
)
);
$fields_syslog_update['dayparts'] = array(
'method' => 'drop_array',
'friendly_name' => __('Partitions per Day', 'syslog'),
'description' => __('Select the number of partitions per day that you wish to create.', 'syslog'),
'value' => '1',
'array' => array(
'1' => __('%d Per Day', 1, 'syslog'),
'2' => __('%d Per Day', 2, 'syslog'),
'4' => __('%d Per Day', 4, 'syslog'),
'6' => __('%d Per Day', 6, 'syslog'),
'12' => __('%d Per Day', 12, 'syslog')
)
);
if ($syslog_exists) {
$type = __('Upgrade', 'syslog');
} else {
$type = __('Install', 'syslog');
}
$database = db_fetch_row('SHOW GLOBAL VARIABLES LIKE "version"');
/* remove Aria as a storage enging if this is mysql */
if (stripos($database['Value'], 'mariadb') == false) {
unset($fields_syslog_update['engine']['array']['aria']);
} else {
$fields_syslog_update['engine']['value'] = 'aria';
}
print "<table align='center' width='80%'><tr><td>";
html_start_box(__('Syslog %s Advisor', $type, 'syslog') . '<', '100%', '', '3', 'center', '');
print "<tr><td>";
if ($syslog_exists) {
print "<h2 style='color:red;'>" . __('WARNING: Syslog Upgrade is Time Consuming!!!', 'syslog') . "</h2>";
print "<p>" . __('The upgrade of the \'main\' syslog table can be a very time consuming process. As such, it is recommended that you either reduce the size of your syslog table prior to upgrading, or choose the background option</p> <p>If you choose the background option, your legacy syslog table will be renamed, and a new syslog table will be created. Then, an upgrade process will be launched in the background. Again, this background process can quite a bit of time to complete. However, your data will be preserved</p> <p>Regardless of your choice, all existing removal and alert rules will be maintained during the upgrade process.</p> <p>Press <b>\'Upgrade\'</b> to proceed with the upgrade, or <b>\'Cancel\'</b> to return to the Plugins menu.', 'syslog') . "</p></td></tr>";
} else {
unset($fields_syslog_update['upgrade_type']);
print "<p>" . __('You have several options to choose from when installing Syslog. The first is the Database Architecture. You should elect to utilize Table Partitioning to prevent the size of the tables from becoming excessive thus slowing queries.', 'syslog') . '</p><p>' . __('You can also set the MySQL storage engine for the analytical tables syslog and syslog_remove. If you have not tuned you system for InnoDB storage properties and using MySQL, it is strongly recommended that you utilize the MyISAM storage engine. If using MariaDB, we recommend the Aria Storage Engine as it\'s designed for analytical queries', 'syslog') . '</p><p>' . __('You can also select the retention duration. Please keep in mind that if you have several hosts logging to syslog, this table can become quite large. So, if not using partitioning, you might want to keep the size smaller.', 'syslog') . "</p></td></tr>";
}
html_end_box();
print "<form id='syslog_install' action='plugins.php' method='get'>";
html_start_box(__('Syslog %s Settings', $type, 'syslog'), '100%', '', '3', 'center', '');
draw_edit_form(array(
'config' => array(),
'fields' => inject_form_variables($fields_syslog_update, array()))
);
html_end_box();
syslog_confirm_button('install', 'plugins.php', $syslog_exists);
print "</td></tr></table>";
bottom_footer();
exit;
}