-
Notifications
You must be signed in to change notification settings - Fork 167
/
monitorix
executable file
·966 lines (870 loc) · 29.5 KB
/
monitorix
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
#!/usr/bin/env perl
#
# Monitorix - A lightweight system monitoring tool.
#
# Copyright (C) 2005-2022 by Jordi Sanfeliu <[email protected]>
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
require 5.006;
use strict;
use warnings;
use FindBin qw($Bin);
use lib $Bin . "/lib", "/usr/lib/monitorix";
use Monitorix;
use POSIX qw(WNOHANG LC_TIME setlocale uname pause setsid);
use Config::General;
use Getopt::Std;
use Cwd qw(abs_path);
use Fcntl qw(:flock);
# Force a standard locale
$ENV{LANG} = "";
setlocale(LC_TIME, "C");
$SIG{'INT' } = 'INT_handler';
$SIG{'ABRT'} = 'INT_handler';
$SIG{'QUIT'} = 'INT_handler';
$SIG{'TRAP'} = 'INT_handler';
$SIG{'STOP'} = 'INT_handler';
$SIG{'TERM'} = 'INT_handler';
$SIG{'HUP' } = 'HUP_handler';
use constant VERSION => "3.15.0";
use constant RELDATE => "07-Dec-2022";
my @suppsys = ("Linux", "FreeBSD", "OpenBSD", "NetBSD");
our %config;
our %options;
sub INT_handler {
my ($signal) = @_;
logger("SIG$signal caught.");
flush_accounting_rules(\%config, $options{d}) unless $options{u};
if(lc($config{httpd_builtin}->{enabled} || "") eq "y") {
kill(15, $config{httpd_pid});
}
logger("Exiting.");
exit(0);
}
sub HUP_handler {
my ($signal) = @_;
my $myself = (caller(0))[3];
return if $options{n}; # only continue if it's a daemon
my (undef, undef, $uid) = getpwnam($config{httpd_builtin}->{user});
my (undef, undef, $gid) = getgrnam($config{httpd_builtin}->{group});
logger("SIG$signal caught.");
# upon receiving SIGHUP a new logfile is opened
if($config{log_file}) {
close(STDOUT);
close(STDERR);
open(STDOUT, ">> $config{log_file}") || logger("Can't write to LOG: $!");
open(STDERR, ">> $config{log_file}") || logger("Can't write to LOG: $!");
chmod(0600, $config{log_file});
logger("$myself: opening a new log file.");
}
# restart HTTP built-in
if(lc($config{httpd_builtin}->{enabled} || "") eq "y") {
if(defined($config{httpd_pid})) {
require HTTPServer;
kill(15, $config{httpd_pid});
kill(9, $config{httpd_pid});
httpd_setup(\%config, $options{u});
logger("Restarted HTTP built-in server (pid $config{httpd_pid}).") if (defined($config{httpd_pid}));
}
}
}
sub daemonize {
if($config{log_file}) {
open(STDIN, "< /dev/null") || die "Can't read /dev/null: $!";
open(STDOUT, ">> $config{log_file}") || die "Can't write to LOG: $!";
}
umask(022) || die "Unable to umask 022: $!";
exit if fork(); # parent exits
(setsid() != -1) || die "Can't start a new session: $!";
if($config{log_file}) {
open(STDERR, ">> $config{log_file}") || die "Can't write to LOG: $!";
}
}
sub usage {
print(STDERR << "EOF");
Usage: monitorix -c configfile [-p pidfile] [-d none | graph[,graph] | all ] [-v] [-n] [-u] [-s splitpolicy] [-e report=<timeframe>,graphs=<graph>[,<graph>,...],to=<email>]
EOF
exit(1);
}
sub create_index {
my $myself = (caller(0))[3];
my $n;
my $gname;
my $piwik_code = "";
my ($piwik_url, $piwik_sid, $piwik_img);
my $text_when_all;
my $value_when_all;
my $when_all_code = "";
# keep backwards compatibility for v3.2.1 and less
if(ref($config{theme}) ne "HASH") {
logger("$myself: WARNING: the <theme> option is not valid. Please consider upgrading your configuration file. Defaulting to white theme.");
delete($config{theme});
$config{theme}->{white}->{main_bg} = "FFFFFF";
$config{theme}->{white}->{main_fg} = "000000";
$config{theme}->{white}->{title_bg} = "777777";
$config{theme}->{white}->{title_fg} = "CCCC00";
$config{theme}->{white}->{graph_bg} = "CCCCCC";
$config{theme_color} = "white";
}
my $theme = $config{theme_color} || "white";
if(!$config{theme}->{$theme}) {
logger("$myself: ERROR: invalid value in 'theme_color' option. Defaulting to white theme.");
$theme = $config{theme_color} = "white";
}
# Default option in 'Graph' list when 'All' is selected in 'Hostname' list.
#
# The following small JavaScript function is intended to avoid to select to view
# accidentally (unless explicitly selected by the user) a huge amount of remote
# images that will hang the browser for a while.
$text_when_all = $config{multihost}->{default_option_when_all} || "";
$text_when_all = "System load" if !$text_when_all;
my @match = grep { $config{graphs}{$_} eq $text_when_all } keys %{$config{graphs}};
$value_when_all = $match[0] || "";
if(!$value_when_all) {
logger("$myself: ERROR: invalid value in 'default_option_when_all' option ('$text_when_all').");
$value_when_all = "_system1";
}
$when_all_code = <<"EOF";
<!-- Default option code when 'All' is selected in 'Hostname' list -->
<script type="text/javascript">
function auto_select() {
var host = document.getElementById("hostname");
var h = host.options[host.selectedIndex].text;
var graph = document.getElementById("graph");
var g = graph.options[graph.selectedIndex].text;
if(h == "All" && g == "All graphs") {
document.getElementById("graph").value = "$value_when_all";
}
return true;
}
</script>
<!-- End Default option code when 'All' is selected in 'Hostname' list -->
EOF
# Piwik tracking code
if(lc($config{piwik_tracking}->{enabled} || "") eq "y") {
$piwik_url = $config{piwik_tracking}->{url} || "";
$piwik_sid = $config{piwik_tracking}->{sid} || "";
$piwik_img = $config{piwik_tracking}->{img} || "";
$piwik_code = <<"EOF";
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "$piwik_url";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', $piwik_sid]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
g.defer=true; g.async=true; g.src=u+'piwik.js';
s.parentNode.insertBefore(g,s);
})();
</script>
<noscript>
<p><img src="$piwik_img" style="border:0;" alt=""/></p>
</noscript>
<!-- End Piwik Code -->
EOF
}
# force to only one trailing slash
(my $base_url = $config{base_url}) =~ s/\/*$/\//;
(my $base_cgi = $config{base_cgi}) =~ s/\/*$/\//;
if(!open(OUT, "> $config{base_dir}/index.html")) {
die "Unable to create '${config{base_dir}}index.html': $!";
}
my $css = sprintf("%scss/%s.css", $base_url, $theme);
print(OUT <<EOF);
<!DOCTYPE html "-//W3C//DTD HTML 4.01 Final//EN">
<html>
<head>
<title>$config{title}</title>
<link rel="shortcut icon" href="$base_url$config{favicon}">
<link href="$css" rel="stylesheet">
</head>
<body class="index-body">
$piwik_code
$when_all_code
<center>
<p>
<br>
<table class='startpage-header-table'>
<tr>
<td class='startpage-header-table'>
<a href="$config{logo_top_url}"><img src="$base_url$config{logo_top}" border="0"></a>
</td>
</tr>
<tr>
<td class='startpage-header-table'>
<h2 align="right">v@{[VERSION]}</h2>
</td>
</tr>
</table>
<p>
<form action="${base_cgi}monitorix.cgi" method="get">
<table>
<tr>
<td class='td-title'>
<b> Hostname </b>
</td>
<td class='td-title'>
<b> Graph </b>
</td>
</tr>
<tr>
EOF
print(OUT " <td>\n");
print(OUT " <select name='mode' size='1' id='hostname' onchange='auto_select()'>\n");
print(OUT " <optgroup label='Local Host'>\n");
print(OUT " <option value='localhost'>localhost</option>\n");
print(OUT " </optgroup>\n");
if(scalar(my @remotehost_list = split(',', $config{multihost}->{remotehost_list})) && lc($config{multihost}->{enabled} || "") eq "y") {
print(OUT " <optgroup label='Multihost'>\n");
print(OUT " <option value='multihost.all'>All</option>\n");
for($n = 0; $n < scalar(@remotehost_list); $n++) {
print(OUT " <option value='multihost.$n'>" . trim($remotehost_list[$n]) . "</option>\n");
}
print(OUT " </optgroup>\n");
if(lc($config{multihost}->{groups} || "") eq "y" ) {
my @remotegroup_list = split(',', $config{multihost}->{remotegroup_list});
print(OUT " <optgroup label='Multihost-Groups'>\n");
print(OUT " <option value='multihost.group'>All Groups</option>\n");
for($n = 0; $n < scalar(@remotegroup_list); $n++) {
print(OUT " <option value='multihost.group$n'>" . trim($remotegroup_list[$n]) . "</option>\n");
}
print(OUT " </optgroup>\n");
}
}
if(scalar(my @tal = split(',', $config{traffacct}->{list})) && lc($config{traffacct}->{enabled} || "") eq "y") {
print(OUT " <optgroup label='Traffic Accounting'>\n");
print(OUT " <option value='traffacct.all'>All</option>\n");
for($n = 0; $n < scalar(@tal); $n++) {
print(OUT " <option value='traffacct.$n'>" . trim($tal[$n]) . "</option>\n");
}
print(OUT " </optgroup>\n");
}
print(OUT " </select>\n");
print(OUT " </td>\n");
print(OUT " <td>\n");
print(OUT " <select name='graph' size='1' id='graph'>\n");
print(OUT " <option value='all'>All graphs</option>\n");
foreach (split(',', $config{graph_name})) {
my $g = trim($_);
if(lc($config{graph_enable}->{$g} || "") eq "y") {
print(OUT " <optgroup label='" . $config{graph_title}->{$g} . "'>\n");
if($g eq "proc") {
for($n = 0; $n < $config{proc}->{max}; $n++) {
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $config{graphs}->{$gname} . " " . $n . "</option>\n");
}
next;
}
if($g eq "gensens") {
for($n = 0; $n < keys %{$config{gensens}->{list}}; $n++) {
my $name = trim($config{gensens}->{title}->{$n});
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
if($g eq "ipmi") {
for($n = 0; $n < scalar(my @ipmi_list = split(',', $config{ipmi}->{list})); $n++) {
my $name = trim($ipmi_list[$n]);
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
if($g eq "ambsens") {
for($n = 0; $n < scalar(my @ambsens_list = split(',', $config{ambsens}->{list})); $n++) {
my $name = trim($ambsens_list[$n]);
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
if($g eq "fs" || $g eq "process") {
$n = 0;
foreach my $k (sort keys %{$config{graphs}}) {
if($k =~ m/$g/) {
$gname = "_" . $g . ++$n;
my $gname2 = "_" . $g . "0" . $n;
if($config{graphs}->{$gname}) {
print(OUT " <option value='" . $gname2 ."'>" . $config{graphs}->{$gname} . "</option>\n");
}
}
}
next;
}
if($g eq "du") {
for($n = 0; $n < scalar(my @du_list = split(',', $config{du}->{list})); $n++) {
my $name = trim($du_list[$n]);
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
if($g eq "net") {
my $n2;
for($n = 0; $n < scalar(my @nl = split(',', $config{net}->{list})); $n++) {
$gname = "_" . $g;
for($n2 = 1; $n2 <= 3; $n2++) {
my $str = trim($nl[$n]) . " " . $config{graphs}->{$gname . $n2};
print(OUT " <option value='" . $gname . $n . $n2 . "'>" . $str . "</option>\n");
}
}
next;
}
if($g eq "serv") {
for($n = 0; $n < scalar(my @serv_list = (sort keys %{$config{serv}->{list}})); $n++) {
my $name = trim($serv_list[$n]);
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
if($g eq "port") {
for($n = 0; $n < $config{port}->{max} && $n < scalar(my @port_list = split(',', $config{port}->{list})); $n++) {
my $num = trim($port_list[$n]);
if(!$config{port}->{desc}->{$num}) {
logger("$myself: port number '$num' listed but not defined.");
next;
}
my $name = trim((split(',', $config{port}->{desc}->{$num}))[0]);
my $pcon = trim((split(',', $config{port}->{desc}->{$num}))[2]);
$gname = "_" . $g;
foreach my $c (split('/', $pcon)) {
$c = uc(trim($c));
print(OUT " <option value='" . $gname . $n . "'>" . $config{graphs}->{$gname} . " " . $num . " (" . $c . "-" . $name . ")" . "</option>\n");
}
}
next;
}
if($g eq "fail2ban") {
for($n = 0; $n < scalar(my @fail2ban_list = split(',', $config{fail2ban}->{list})); $n++) {
my $name = trim($fail2ban_list[$n]);
$gname = "_" . $g;
print(OUT " <option value='" . $gname . $n . "'>" . $name . "</option>\n");
}
next;
}
$n = 0;
foreach my $k (sort keys %{$config{graphs}}) {
if($k =~ m/$g/) {
$gname = "_" . $g . ++$n;
if($config{graphs}->{$gname}) {
print(OUT " <option value='" . $gname ."'>" . $config{graphs}->{$gname} . "</option>\n");
}
}
}
print(OUT " </optgroup>\n");
}
}
print(OUT " </select>\n");
print(OUT " </td>\n");
print(OUT <<EOF);
</tr>
</table>
<p>
<table>
<tr>
EOF
if(lc($config{enable_hourly_view} || "") eq "y" ) {
print(OUT <<EOF);
<td class='td-title'>
<label>
<input type="radio" name="when" value="1hour">
<b>Hourly </b>
</label>
</td>
EOF
}
print(OUT <<EOF);
<td class='td-title'>
<label>
<input type="radio" checked name="when" value="1day">
<b>Daily </b>
</label>
</td>
<td class='td-title'>
<label>
<input type="radio" name="when" value="1week">
<b>Weekly </b>
</label>
</td>
<td class='td-title'>
<label>
<input type="radio" name="when" value="1month">
<b>Monthly </b>
</label>
</td>
<td class='td-title'>
<label>
<input type="radio" name="when" value="1year">
<b>Yearly </b>
</label>
</td>
</tr>
EOF
if($config{max_historic_years} > 1) {
print(OUT " <tr>\n");
}
for($n = 2; $n <= $config{max_historic_years}; $n++) {
if($n > 2 && (($n - 2) % 4) == 0) {
print(OUT " </tr>\n");
print(OUT " <tr>\n");
}
print(OUT <<EOF);
<td class='td-title'>
<label>
<input type="radio" name="when" value="${n}year">
<b>$n Years </b>
</label>
</td>
EOF
}
if($config{max_historic_years} > 1) {
print(OUT " </tr>\n");
}
print(OUT <<EOF);
</table>
<p>
<input type="hidden" name="color" value="$theme">
<input type="submit" value=" Ok ">
</form>
</center>
</body>
</html>
EOF
close(OUT);
}
# Main
# ----------------------------------------------------------------------------
getopts("c:p:d:vnus:e:", \%options) || usage();
if($options{v}) {
print("Monitorix version " . VERSION . " (" . RELDATE . ")\n");
print("by Jordi Sanfeliu <jordi\@fibranet.cat>\n");
print("https://www.monitorix.org/\n\n");
exit(0);
}
if(!$options{c}) {
usage();
exit(1);
}
$options{c} = (abs_path($options{c}) || $options{c}) unless $^V lt 5.6.2;
if(!stat($options{c})) {
die "Can't open file '$options{c}': $!";
}
# load main configuration file
$options{s} = "guess" if !defined($options{s});
my $conf = new Config::General(
-ConfigFile => $options{c},
-SplitPolicy => $options{s},
);
%config = $conf->getall;
$config{debug} = ();
$config{func_update} = ();
# get the current OS and kernel version and check its support
my $release;
($config{os}, undef, $release) = uname();
if(!($release =~ m/^(\d+)\.(\d+)/)) {
die "FATAL: unable to get the kernel version.";
}
$config{kernel} = "$1.$2";
if(!grep {$_ eq $config{os}} @suppsys) {
die "FATAL: your operating system ($config{os}) is not supported.";
}
if(grep {$_ eq $config{os}} ("FreeBSD", "OpenBSD", "NetBSD")) {
$SIG{'CHLD'} = 'DEFAULT';
}
$0 = sprintf("%s %s%s%s%s%s%s",
$^V lt 5.6.2 ? "monitorix" : abs_path($0),
$options{c} ? "-c $options{c}" : "",
$options{p} ? " -p $options{p}" : "",
$options{d} ? " -d $options{d}" : "",
$options{v} ? " -v" : "",
$options{n} ? " -n" : "",
$options{u} ? " -u" : "");
daemonize() unless $options{n};
logger("Starting Monitorix version " . VERSION . " (pid $$).");
logger("Loaded main configuration file '$options{c}'.");
# save the pidfile
if($options{p}) {
$options{p} = abs_path($options{p});
open(OUT, "> $options{p}")
|| die "Could not open '$options{p}' for writing: $!";
print(OUT "$$");
close(OUT);
}
# change to a safety directory
unless(chdir("/tmp")) {
die "Can't chdir to /tmp: $!";
}
if($options{d}) {
if($options{d} ne "none" && $options{d} ne "all") {
@{$config{debug}} = split(',', $options{d});
foreach my $t (@{$config{debug}}) {
if(!grep {trim($_) eq $t} (split(',', $config{graph_name} . ", traffacct, emailreports"))) {
die "Invalid debug key '$t'";
}
}
}
logger("Entering in debug mode.");
logger("Changed process name to '$0'.");
}
# load additional configuration files
if($config{include_dir} && opendir(DIR, $config{include_dir})) {
my @files = grep { !/^[.]/ } readdir(DIR);
closedir(DIR);
foreach my $c (sort @files) {
next unless -f $config{include_dir} . "/$c";
next unless $c =~ m/\.conf$/;
logger("Loading extra configuration file '$config{include_dir}/$c'.");
my $conf_inc = new Config::General(
-ConfigFile => $config{include_dir} . "/$c",
-SplitPolicy => $options{s},
);
my %config_inc = $conf_inc->getall;
while(my ($key, $val) = each(%config_inc)) {
if(ref($val) eq "HASH") {
# two level options (a subsection)
while(my ($key2, $val2) = each(%{$val})) {
# delete first this whole subsection
delete($config{$key}->{$key2});
if(ref($val2) eq "HASH") {
# three level options (a subsubsection)
while(my ($key3, $val3) = each(%{$val2})) {
$config{$key}->{$key2}->{$key3} = $val3;
delete $config_inc{$key}->{$key2}->{$key3};
}
next;
}
$config{$key}->{$key2} = $val2;
delete $config_inc{$key}->{$key2};
}
next;
}
# graph_name option is special
#if($key eq "graph_name") {
# $config{graph_name} .= ", $val";
# delete $config_inc{$key};
# next;
#}
if($key eq "additional_graph_name") {
$config{graph_name} .= ", $val";
delete $config_inc{$key};
next;
}
# one level options
$config{$key} = $val;
delete $config_inc{$key};
}
}
} else {
logger("Can't read directory '$config{include_dir}'. $!")
if $config{include_dir};
}
# make sure that some options are correctly defined
die "ERROR: 'base_dir' option does not exist.\n" if(!defined($config{base_dir}));
die "ERROR: 'base_lib' option does not exist.\n" if(!defined($config{base_lib}));
die "ERROR: 'base_url' option does not exist.\n" if(!defined($config{base_url}));
die "ERROR: 'base_cgi' option does not exist.\n" if(!defined($config{base_cgi}));
if(!$config{max_historic_years}) {
logger("WARNING: the 'max_historic_years' option doesn't exist. Please consider upgrading your configuration file.");
$config{max_historic_years} = 1;
}
if(!$config{net}->{max}) {
logger("WARNING: the 'max' option in 'net.pm' doesn't exist. Please consider upgrading your configuration file.");
$config{net}->{max} = 10;
}
if(!$config{global_zoom}) {
logger("WARNING: the 'global_zoom' option is not valid or doesn't exist. Please consider upgrading your configuration file.");
# it's set in 'monitorix.cgi'
}
if(!$config{image_format}) {
logger("WARNING: the 'image_format' option is not valid or doesn't exist. Please consider upgrading your configuration file.");
# it's set in 'monitorix.cgi'
}
if(!defined($config{logo_top_url})) {
logger("WARNING: the 'logo_top_url' option doesn't exist. Please consider upgrading your configuration file.");
$config{logo_top_url} = "https://www.monitorix.org/";
}
if(lc($config{httpd_builtin}->{enabled} || "") eq "y" && !$config{httpd_builtin}->{autocheck_responsiveness}) {
logger("WARNING: the 'autocheck_responsiveness' option is not valid or doesn't exist. Please consider upgrading your configuration file.");
$config{httpd_builtin}->{autocheck_responsiveness} = "y";
}
if(lc($config{multihost}->{enabled} || "") eq "y" && !$config{multihost}->{default_option_when_all}) {
logger("WARNING: the 'default_option_when_all' option is not valid or doesn't exist. Please consider upgrading your configuration file.");
$config{multihost}->{default_option_when_all} = "System load";
}
if(!$config{use_external_firewall}) {
$config{use_external_firewall} = "n";
}
# one-shot for the 'emailreports' module
if($options{e}) {
my $em_report;
my $em_graphs;
my $em_to;
my $em_when;
foreach (split(',', $options{e})) {
my $em = trim($_);
if($em =~ m/^report=(hourly|daily|weekly|monthly|yearly)$/) {
$em_report=$1;
}
if($em =~ m/^graphs=(\S+)$/) {
$em_graphs=$1;
$em_graphs =~ s/\+/\,/g;
}
if($em =~ m/^to=(\S+)$/) {
$em_to=$1;
}
}
die "Invalid or not defined time frame in 'report='." unless $em_report;
die "Unspecified graph names in 'graphs='." unless $em_graphs;
die "Unspecified destination email address in 'to='." unless $em_to;
my $emailreports = $config{emailreports};
$emailreports->{$em_report}->{graphs} = $em_graphs;
$emailreports->{$em_report}->{to} = $em_to;
my $d = "emailreports";
undef($d) if(!grep {trim($_) eq $d} (@{$config{debug}}));
eval "use emailreports qw(emailreports_send)";
if($@) {
die "WARNING: unable to load module 'emailreports'. $@";
}
$em_when = "1hour" if $em_report eq "hourly";
$em_when = "1day" if $em_report eq "daily";
$em_when = "1week" if $em_report eq "weekly";
$em_when = "1month" if $em_report eq "monthly";
$em_when = "1year" if $em_report eq "yearly";
eval { emailreports::emailreports_send(\%config, $em_report, $em_when, $d); };
if($@) {
logger("emailreports::emailreports_send(): $@");
}
logger("Done.");
exit(0);
}
# save the path of the configuration file
if(open(OUT, "> " . $config{base_dir} . "/cgi/monitorix.conf.path")) {
print(OUT "$options{c}\n");
print(OUT "$options{s}\n");
close(OUT);
} else {
logger("WARNING: unable to create the file '$config{base_dir}/cgi/monitorix.conf.path'. $!");
}
if($config{os} eq "Linux" && !$options{u}) {
# make sure that 'ip_default_table' option has a consistent value
$config{ip_default_table} = "filter" if !$config{ip_default_table};
# check 'iptables' version to include the new wait lock option '--wait'
my $ipv = `iptables --version`;
chomp($ipv);
$ipv =~ s/^iptables v//;
$ipv = sprintf("%03s.%03s.%03s", split('\.', $ipv));
if($ipv gt "001.004.020") { # 1.4.20
$config{iptables_wait_lock} = " --wait ";
} else {
$config{iptables_wait_lock} = "";
}
}
# make sure that there aren't residual Monitorix iptables rules
flush_accounting_rules(\%config, $options{d}) unless $options{u};
logger("Initializing graphs.");
# check for inconsistencies between enabled graphs and defined graphs
foreach my $g (sort keys %{$config{graph_enable}}) {
if(lc($config{graph_enable}->{$g} || "") eq "y") {
if(!grep {trim($_) eq $g} (split(',', $config{graph_name}))) {
logger("WARNING: inconsitency between '<graph_enable>' and 'graph_name' options; '$g' doesn't exist.");
}
}
}
# initialize all enabled graphs
foreach (split(',', ($config{graph_name} || "") . ", traffacct")) {
my $g = trim($_);
my $e = "n";
if($g eq "traffacct") {
$e = lc($config{$g}->{enabled} || "");
} else {
$e = lc($config{graph_enable}->{$g} || "");
}
if($e eq "y") {
my $init = $g . "_init";
my $d = $g;
undef($d) if(!grep {trim($_) eq $d} (@{$config{debug}}));
if(defined($options{d}) && $options{d} eq "all") {
$d = $g;
}
eval "use $g qw(" . $init . " " . $g . "_update)";
if($@) {
logger("WARNING: unable to load module '$g'. $@");
next;
}
{
no strict "refs";
eval { &$init($g, \%config, $d); };
}
logger("WARNING: unexpected errors in function $init().") if($@);
}
}
if(!scalar($config{func_update})) {
logger("Nothing to do, exiting.");
exit(0);
}
logger("Generating the 'index.html' file.");
create_index();
# start the HTTP built-in (if enabled)
if(lc($config{httpd_builtin}->{enabled} || "") eq "y") {
if(!$options{u}) {
logger("Setting owner/group and permission bits for the imgs/ directory.") if defined($options{d});
my (undef, undef, $uid) = getpwnam($config{httpd_builtin}->{user});
my (undef, undef, $gid) = getgrnam($config{httpd_builtin}->{group});
chown($uid, $gid, $config{base_dir} . "/" . $config{imgs_dir});
chmod(0755, $config{base_dir} . "/" . $config{imgs_dir});
}
require HTTPServer;
httpd_setup(\%config, $options{u});
logger("Started HTTP built-in server (pid $config{httpd_pid}).") if (defined($config{httpd_pid}));
}
setpriority(0, 0, $config{priority} || 0);
logger("Ok, ready.");
# main loop
while(1) {
select undef, undef, undef, 1.0;
my ($sec, $min, $hour, $mday, $mon, undef, $wday) = localtime(time);
# call to all enabled graphs on every minute
if($sec == 0) {
my $lockfile_handler = lockfile_handler(\%config);
global_flock($lockfile_handler, LOCK_EX);
foreach my $f (@{$config{func_update}}) {
my $update = $f . "_update";
my $d = $f;
logger("Calling $update()") unless !$options{d};
undef($d) if(!grep {trim($_) eq $d} (@{$config{debug}}));
if(defined($options{d}) && $options{d} eq "all") {
$d = $f;
}
{
no strict "refs";
eval { &$update($f, \%config, $d); };
if($@) {
logger("$update(): $@");
}
}
}
global_flock($lockfile_handler, LOCK_UN);
# TRAFFACCT graph daily reports
if(lc($config{traffacct}->{enabled} || "") eq "y") {
my $d = "traffacct";
undef($d) if(!grep {trim($_) eq $d} (@{$config{debug}}));
# at 00:00h
if($min == 0 && $hour == 0) {
# collect traffic accounting every day
eval { traffacct::traffacct_getcounters(\%config, $d); };
if($@) {
logger("traffacct::traffacct_getcounters(): $@");
}
# send reports every first day of a month
if(lc($config{traffacct}->{reports}->{enabled} || "") eq "y") {
if($mday == 1) {
eval { traffacct::traffacct_sendreports(\%config, $d); };
if($@) {
logger("traffacct::traffacct_sendreports(): $@");
}
}
}
}
}
# Scheduled Email Reports
if(lc($config{emailreports}->{enabled} || "") eq "y") {
my $emailreports = $config{emailreports};
my $m = $emailreports->{minute} || 0;
my $h = $emailreports->{hour} || 0;
my $d = "emailreports";
undef($d) if(!grep {trim($_) eq $d} (@{$config{debug}}));
if($min == $m && $hour == $h) {
eval "use emailreports qw(emailreports_send)";
if($@) {
logger("WARNING: unable to load module 'emailreports'. $@");
next;
}
# daily
if(lc($emailreports->{daily}->{enabled} || "") eq "y") {
eval { emailreports::emailreports_send(\%config, "daily", "1day", $d); };
if($@) {
logger("emailreports::emailreports_send(): $@");
}
}
# weekly (send reports on every Monday)
if($wday == 1) {
if(lc($emailreports->{weekly}->{enabled} || "") eq "y") {
eval { emailreports::emailreports_send(\%config, "weekly", "1week", $d); };
if($@) {
logger("emailreports::emailreports_send(): $@");
}
}
}
# monthly (send reports every first day of each month)
if($mday == 1) {
if(lc($emailreports->{monthly}->{enabled} || "") eq "y") {
eval { emailreports::emailreports_send(\%config, "monthly", "1month", $d); };
if($@) {
logger("emailreports::emailreports_send(): $@");
}
}
}
# yearly (send reports every first day of each year)
if($mon == 0 && $mday == 1) {
if(lc($emailreports->{yearly}->{enabled} || "") eq "y") {
eval { emailreports::emailreports_send(\%config, "yearly", "1year", $d); };
if($@) {
logger("emailreports::emailreports_send(): $@");
}
}
}
}
}
# check if the HTTP built-in server is responsive
if(lc($config{httpd_builtin}->{enabled} || "") eq "y") {
if(lc($config{httpd_builtin}->{autocheck_responsiveness} || "") eq "y") {
use LWP::UserAgent;
my $pid = fork();
if(!$pid) {
my $host = $config{httpd_builtin}->{host} ? $config{httpd_builtin}->{host} : "localhost";
my $url = "http://" . $host . ":" . $config{httpd_builtin}->{port} . $config{base_url};
my $ua = LWP::UserAgent->new(timeout => 30);
my $response = $ua->request(HTTP::Request->new('GET', $url));
if(!$response->is_success) {
if($response->status_line ne "401 Access Denied") {
logger("WARNING: HTTP built-in server not responding at '$url'.");
exit(1);
}
}
exit(0);
}
waitpid($pid, 0);
# waitpid sets the child's status in $?
if($? != 0) {
# restart HTTP built-in server
if(defined($config{httpd_pid})) {
require HTTPServer;
kill(15, $config{httpd_pid});
kill(9, $config{httpd_pid});
httpd_setup(\%config, $options{u});
logger("Restarted HTTP built-in server (pid $config{httpd_pid}).") if defined($config{httpd_pid});
}
}
}
}
}
waitpid(-1, WNOHANG);
}