-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.pl
executable file
·1889 lines (1673 loc) · 59.3 KB
/
install.pl
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
#!/usr/bin/perl
# $Id$
# License: OSI Artistic License
# http://www.opensource.org/licenses/artistic-license.php
# Author: (c) 2008 Alan Brenner, Ithaka Harbors
# Author: (c) 2010 Matthew Wall
# Installation script for nagiosgraph. If you run this script manually it
# will prompt for all the information needed to do an install. Specify one
# or more environment variables to do automated or unattended installations.
# TODO: install logrotate file
# TODO: install ssi
# TODO: install graphed-service template
# TODO: install graph icon
# TODO: restart nagios and httpd services?
## no critic (ProhibitCascadingIfElse)
## no critic (ProhibitPostfixControls)
## no critic (RequireBriefOpen)
## no critic (RequireCheckedSyscalls)
## no critic (RegularExpressions)
## no critic (ProhibitConstantPragma)
## no critic (ProhibitMagicNumbers)
## no critic (ProhibitExcessComplexity)
use English qw(-no_match_vars);
use Fcntl ':mode';
use File::Copy qw(copy move);
use File::Path qw(mkpath);
use File::Temp qw(tempfile);
use IPC::Open3 qw(open3);
use POSIX qw(geteuid strftime);
use strict;
use warnings;
use vars qw($VERSION);
$VERSION = '1.2';
use constant EXIT_FAIL => 1;
use constant EXIT_OK => 0;
use constant PERMS_755 => oct 755;
use constant PERMS_644 => oct 644;
use constant LOG_FN => 'install-log';
use constant NAGIOS_CFG_STUB_FN => 'nagiosgraph-nagios.cfg';
use constant NAGIOS_CMD_STUB_FN => 'nagiosgraph-commands.cfg';
use constant APACHE_STUB_FN => 'nagiosgraph-apache.conf';
use constant STAG => '# begin nagiosgraph configuration';
use constant ETAG => '# end nagiosgraph configuration';
my @NAGIOS_USERS = qw(nagios icinga);
my @NAGIOS_GROUPS = qw(nagios icinga);
my @APACHE_USERS = qw(www-data www apache wwwrun webservd);
my @APACHE_GROUPS = qw(www-data www webservd);
my @NAGIOS_NAMES = qw(nagios3 nagios2 nagios icinga);
my @NAGIOS_BIN_DIRS = qw(/usr/bin /usr/sbin /bin /sbin /usr/local/APP/bin /opt/APP/bin);
my @NAGIOS_VAR_DIRS = qw(/var/APP /var/spool/APP /usr/local/APP/var /opt/APP/var);
my @NAGIOS_CFG_DIRS = qw(/etc/APP /usr/local/APP/etc /opt/APP/etc);
my $NAGIOS_CFG = '/path/to/nagios.cfg';
my $NAGIOS_EXEC = '/path/to/nagios';
my $NAGIOS_INIT = '/path/to/nagios-init-script';
my @APACHE_NAMES = qw(apache2 apache httpd);
my @APACHE_CFG_DIRS = qw(/etc/APP /usr/local/APP/etc /opt/APP/etc);
my @APACHE_BIN_DIRS = qw(/usr/bin /usr/sbin /bin /sbin /usr/local/APP/bin /opt/APP/bin);
my $APACHE_EXEC = 'path/to/httpd';
my $APACHE_INIT = 'path/to/apache-init-script';
# these are standard installation configurations.
my @PRESETS = (
{ ng_layout => 'standalone',
ng_url => '/nagiosgraph',
ng_etc_dir => 'etc',
ng_bin_dir => 'bin',
ng_cgi_dir => 'cgi',
ng_doc_dir => 'doc',
ng_examples_dir => 'examples',
ng_www_dir => 'share',
ng_util_dir => 'util',
ng_var_dir => 'var',
ng_rrd_dir => 'rrd',
ng_log_dir => 'log',
ng_log_file => 'nagiosgraph.log',
ng_cgilog_file => 'nagiosgraph-cgi.log',
ng_cgi_url => 'cgi-bin',
ng_css_url => 'nagiosgraph.css',
ng_js_url => 'nagiosgraph.js', },
{ ng_layout => 'overlay',
ng_url => '/nagios',
ng_etc_dir => 'etc/nagiosgraph',
ng_bin_dir => 'libexec',
ng_cgi_dir => 'sbin',
ng_doc_dir => 'docs/nagiosgraph',
ng_examples_dir => 'docs/nagiosgraph/examples',
ng_www_dir => 'share',
ng_util_dir => 'docs/nagiosgraph/util',
ng_var_dir => '/var/nagios',
ng_rrd_dir => 'rrd',
ng_log_dir => '/var/nagios',
ng_log_file => 'nagiosgraph.log',
ng_cgilog_file => 'nagiosgraph-cgi.log',
ng_cgi_url => 'cgi-bin',
ng_css_url => 'nagiosgraph.css',
ng_js_url => 'nagiosgraph.js', },
{ ng_layout => 'debian',
ng_prefix => q(/),
ng_url => '/nagiosgraph',
ng_etc_dir => '/etc/nagiosgraph',
ng_bin_dir => '/usr/lib/nagiosgraph',
ng_cgi_dir => '/usr/lib/cgi-bin/nagiosgraph',
ng_doc_dir => '/usr/share/nagiosgraph/doc',
ng_examples_dir => '/usr/share/nagiosgraph/examples',
ng_www_dir => '/usr/share/nagiosgraph/htdocs',
ng_util_dir => '/usr/share/nagiosgraph/util',
ng_var_dir => '/var/spool/nagiosgraph',
ng_rrd_dir => 'rrd',
ng_log_dir => '/var/log/nagiosgraph',
ng_log_file => 'nagiosgraph.log',
ng_cgilog_file => 'nagiosgraph-cgi.log',
ng_cgi_url => 'cgi-bin',
ng_css_url => 'nagiosgraph.css',
ng_js_url => 'nagiosgraph.js',
nagios_cgi_url => '/nagios3/cgi-url',
nagios_perfdata_file => '/tmp/perfdata.log',
nagios_user => 'nagios',
www_user => 'www-data',
nagios_config_file => '/etc/nagios3/nagios.cfg',
nagios_commands_file => '/etc/nagios3/commands.cfg',
apache_config_dir => '/etc/apache2/conf.d', },
{ ng_layout => 'redhat',
ng_prefix => q(/),
ng_url => '/nagiosgraph',
ng_etc_dir => '/etc/nagiosgraph',
ng_bin_dir => '/usr/libexec/nagiosgraph',
ng_cgi_dir => '/usr/lib/nagiosgraph/cgi-bin',
ng_doc_dir => '/usr/share/doc/nagiosgraph',
ng_examples_dir => '/usr/share/nagiosgraph/examples',
ng_www_dir => '/usr/share/nagiosgraph/htdocs',
ng_util_dir => '/usr/share/nagiosgraph/util',
ng_var_dir => '/var/spool/nagiosgraph',
ng_rrd_dir => 'rrd',
ng_log_dir => '/var/log/nagiosgraph',
ng_log_file => 'nagiosgraph.log',
ng_cgilog_file => 'nagiosgraph-cgi.log',
ng_cgi_url => 'cgi-bin',
ng_css_url => 'nagiosgraph.css',
ng_js_url => 'nagiosgraph.js',
nagios_perfdata_file => '/tmp/perfdata.log',
nagios_user => 'nagios',
www_user => 'apache',
nagios_config_file => '/etc/nagios/nagios.cfg',
nagios_commands_file => '/etc/nagios/objects/commands.cfg',
apache_config_dir => '/etc/httpd/conf.d', },
{ ng_layout => 'suse',
ng_prefix => q(/),
ng_url => '/nagios',
ng_etc_dir => '/etc/nagiosgraph',
ng_bin_dir => '/usr/lib/nagiosgraph',
ng_cgi_dir => '/usr/lib/nagios/cgi',
ng_doc_dir => '/usr/share/doc/packages/nagiosgraph',
ng_examples_dir => '/usr/share/nagiosgraph/examples',
ng_www_dir => '/usr/share/nagios',
ng_util_dir => '/usr/share/nagiosgraph/util',
ng_var_dir => '/var/spool/nagiosgraph',
ng_rrd_dir => '/var/lib/nagios/rrd/nagiosgraph',
ng_log_dir => '/var/log/nagios',
ng_log_file => 'nagiosgraph.log',
ng_cgilog_file => 'nagiosgraph-cgi.log',
ng_cgi_url => 'cgi-bin',
ng_css_url => 'stylesheets/nagiosgraph.css',
ng_js_url => 'nagiosgraph.js',
nagios_perfdata_file => '/var/log/nagios/service-perfdata.log',
nagios_user => 'nagios',
www_user => 'wwwrun',
nagios_config_file => '/etc/nagios/nagios.cfg',
nagios_commands_file => '/etc/nagios/objects/commands.cfg',
apache_config_dir => '/etc/apache2/conf.d', },
);
my @CONF =
( { key => 'ng_prefix',
msg => 'Destination directory (prefix)',
def => '/usr/local/nagiosgraph' },
{ key => 'ng_etc_dir',
msg => 'Location of configuration files (etc-dir)',
parent => 'ng_prefix' },
{ key => 'ng_bin_dir',
msg => 'Location of executables',
parent => 'ng_prefix' },
{ key => 'ng_cgi_dir',
msg => 'Location of CGI scripts',
parent => 'ng_prefix' },
{ key => 'ng_doc_dir',
msg => 'Location of documentation (doc-dir)',
parent => 'ng_prefix' },
{ key => 'ng_examples_dir',
msg => 'Location of examples',
parent => 'ng_prefix' },
{ key => 'ng_www_dir',
msg => 'Location of CSS and JavaScript files',
parent => 'ng_prefix' },
{ key => 'ng_util_dir',
msg => 'Location of utilities',
parent => 'ng_prefix' },
{ key => 'ng_var_dir',
msg => 'Location of state files (var-dir)',
parent => 'ng_prefix' },
{ key => 'ng_rrd_dir',
msg => 'Location of RRD files',
parent => 'ng_var_dir' },
{ key => 'ng_log_dir',
msg => 'Location of log files (log-dir)',
parent => 'ng_var_dir' },
{ key => 'ng_log_file',
msg => 'Path of log file',
parent => 'ng_log_dir' },
{ key => 'ng_cgilog_file',
msg => 'Path of CGI log file',
parent => 'ng_log_dir' },
{ key => 'ng_url',
msg => 'Base URL',
def => '/nagiosgraph' },
{ key => 'ng_cgi_url',
msg => 'URL of CGI scripts',
parent => 'ng_url' },
{ key => 'ng_css_url',
msg => 'URL of CSS file',
parent => 'ng_url' },
{ key => 'ng_js_url',
msg => 'URL of JavaScript file',
parent => 'ng_url' },
{ key => 'nagios_cgi_url',
msg => 'URL of Nagios CGI scripts',
def => '/nagios/cgi-bin' },
{ key => 'nagios_perfdata_file',
msg => 'Path of Nagios performance data file',
def => '/tmp/perfdata.log' },
{ key => 'nagios_user',
msg => 'username or userid of Nagios user',
def => 'nagios' },
{ key => 'www_user',
msg => 'username or userid of web server user',
def => 'www-data' },
{ key => 'modify_nagios_config' },
{ key => 'nagios_config_file' },
{ key => 'nagios_commands_file' },
{ key => 'modify_apache_config' },
{ key => 'apache_config_dir' },
{ key => 'apache_config_file' },
);
my $verbose = 1;
my $dryrun = 0;
my $action = 'install';
my %conf = qw(ng_layout standalone);
my $checkprereq = 1;
my $dochown = 1;
while ($ARGV[0]) {
my $arg = shift;
if($arg eq '--version') {
print 'nagiosgraph installer ' . $VERSION . "\n";
exit EXIT_OK;
} elsif($arg eq '--install') {
$action = 'install';
} elsif ($arg eq '--dry-run') {
$dryrun = 1;
} elsif ($arg eq '--silent' || $arg eq '--quiet') {
$verbose = 0;
} elsif ($arg eq '--verbose') {
$verbose = 1;
} elsif ($arg eq '--check-installation') {
$action = 'check-installation';
} elsif ($arg eq '--check-prereq') {
$action = 'check-prereq';
} elsif ($arg eq '--layout') {
$conf{ng_layout} = shift;
} elsif ($arg =~ /^--layout=(.+)/) {
$conf{ng_layout} = $1;
} elsif ($arg eq '--prefix') {
$conf{ng_prefix} = trimslashes(shift);
} elsif ($arg =~ /^--prefix=(.+)/) {
$conf{ng_prefix} = trimslashes($1);
} elsif ($arg eq '--var-dir') {
$conf{ng_var_dir} = trimslashes(shift);
} elsif ($arg =~ /^--var-dir=(.+)/) {
$conf{ng_var_dir} = trimslashes($1);
} elsif ($arg eq '--log-dir') {
$conf{ng_log_dir} = trimslashes(shift);
} elsif ($arg =~ /^--log-dir=(.+)/) {
$conf{ng_log_dir} = trimslashes($1);
} elsif ($arg eq '--etc-dir') {
$conf{ng_etc_dir} = trimslashes(shift);
} elsif ($arg =~ /^--etc-dir=(.+)/) {
$conf{ng_etc_dir} = trimslashes($1);
} elsif ($arg eq '--doc-dir') {
$conf{ng_doc_dir} = trimslashes(shift);
} elsif ($arg =~ /^--doc-dir=(.+)/) {
$conf{ng_doc_dir} = trimslashes($1);
} elsif ($arg eq '--nagios-cgi-url') {
$conf{nagios_cgi_url} = shift;
} elsif ($arg =~ /^--nagios-cgi-url=(.+)/) {
$conf{nagios_cgi_url} = $1;
} elsif ($arg eq '--nagios-perfdata-file') {
$conf{nagios_perfdata_file} = shift;
} elsif ($arg =~ /^--nagios-perfdata-file=(.+)/) {
$conf{nagios_perfdata_file} = $1;
} elsif ($arg eq '--nagios-user') {
$conf{nagios_user} = shift;
} elsif ($arg =~ /^--nagios-user=(.+)/) {
$conf{nagios_user} = $1;
} elsif ($arg eq '--www-user') {
$conf{www_user} = shift;
} elsif ($arg =~ /^--www-user=(.+)/) {
$conf{www_user} = $1;
} elsif ($arg eq '--no-check-prereq') {
$checkprereq = 0;
} elsif ($arg eq '--no-chown') {
$dochown = 0;
} elsif ($arg eq '--list-vars') {
print "recognized environment variables include:\n";
foreach my $v (envvars()) {
print " $v\n";
}
print "when building RPM or DEB package, use DESTDIR\n";
exit EXIT_OK;
} else {
my $code = EXIT_OK;
if ($arg ne '--help') {
$code = EXIT_FAIL;
print "unknown option $arg\n";
print "\n";
}
print "options include:\n";
print " --install do the installation\n";
print " --check-prereq check pre-requisites\n";
print " --check-installation check an existing installation\n";
print "\n";
print " --dry-run\n";
print " --verbose | --silent\n";
print " --list-vars list recognized environment variables\n";
print "\n";
print " --layout (overlay | standalone | debian | redhat | suse | custom)\n";
print " --prefix path\n";
print " --etc-dir path\n";
print " --var-dir path\n";
print " --log-dir path\n";
print " --doc-dir path\n";
print " --nagios-cgi-url url\n";
print " --nagios-perfdata-file path\n";
print " --nagios-user userid\n";
print " --www-user userid\n";
print "\n";
print "examples:\n";
print "to install at /usr/local/nagiosgraph:\n";
print " install.pl --prefix /usr/local/nagiosgraph\n";
print "to install at /opt/nagiosgraph:\n";
print " install.pl --prefix /opt/nagiosgraph\n";
exit $code;
}
}
if ($conf{ng_layout} ne 'standalone' &&
$conf{ng_layout} ne 'overlay' &&
$conf{ng_layout} ne 'debian' &&
$conf{ng_layout} ne 'redhat' &&
$conf{ng_layout} ne 'suse' &&
$conf{ng_layout} ne 'custom') {
print "unknown layout '$conf{ng_layout}'\n";
exit EXIT_FAIL;
}
my $LOG;
my $failure = 0;
if($action eq 'check-prereq') {
$failure |= checkprereq();
} elsif($action eq 'check-installation') {
$failure |= checkinstallation();
} elsif($action eq 'install') {
open $LOG, '>', LOG_FN ||
print 'cannot write to log file ' . LOG_FN . ": $OS_ERROR\n";
checkuserid();
$failure |= checkprereq() if $checkprereq;
$failure |= getconfig(\%conf);
if (checkconfig(\%conf)) {
logmsg('*** one or more missing configuration parameters!');
exit EXIT_FAIL;
}
printconfig(\%conf);
if (! isyes($conf{automated})) {
my $confirm = getanswer('Continue with this configuration', 'y');
if ($confirm !~ /y/) {
logmsg('installation aborted');
exit EXIT_OK;
}
}
$failure |= installfiles(\%conf, $dryrun ? 0 : 1);
$failure |= patchnagios(\%conf, $dryrun ? 0 : 1);
$failure |= patchapache(\%conf, $dryrun ? 0 : 1);
if (! isyes($conf{automated})) {
printinstructions(\%conf);
}
close $LOG || print "cannot close log file: $OS_ERROR\n";
undef $LOG;
}
if ($failure) {
logmsg(q());
logmsg('*** one or more problems were detected!');
logmsg(q());
exit EXIT_FAIL;
}
exit EXIT_OK;
sub logmsg {
my ($msg) = @_;
if ($verbose) {
print $msg . "\n";
}
if (${LOG}) {
my $ts = strftime '%d.%m.%Y %H:%M:%S', localtime time;
print ${LOG} "[$ts] $msg\n";
}
return;
}
sub getanswer {
my ($question, $default) = @_;
$default ||= q();
print $question . '? ';
if ($default ne q()) {
print '[' . $default . '] ';
}
my $answer = readline *STDIN;
chomp $answer;
return ($answer =~ /^\s*$/) ? $default : $answer;
}
# append to the specified file. do the append to a copy of the file, then
# move the original file to a time-stamped copy, then move the appended file
# to the original name.
sub appendtofile {
my ($ifn, $txt, $doit) = @_;
logmsg("append to $ifn");
return 0 if !$doit;
my $ofn = $ifn . '.tmp';
if (ng_copy($ifn, $ofn)) {
return 1;
}
my $ts = strftime '%Y%m%d.%H%M', localtime time;
my $fail = 0;
if (open my $FILE, '>>', $ofn) {
print ${FILE} "\n$txt\n";
if (close $FILE) {
my $bak = $ifn . q(-) . $ts;
if (ng_move($ifn, $bak) || ng_move($ofn, $ifn)) {
$fail = 1;
}
} else {
logmsg("*** cannot close $ofn: $OS_ERROR");
$fail = 1;
}
} else {
logmsg("*** cannot append to $ofn: $OS_ERROR");
$fail = 1;
}
return $fail;
}
sub prependpath {
my ($pfx, $path) = @_;
return $path =~ /^\//
? $path
: $pfx . ($pfx !~ /\/$/ ? q(/) : q()) . $path;
}
sub checkconfig {
my ($conf) = @_;
my $missing = 0;
foreach my $ii (@CONF) {
my $key = $ii->{key};
if (! defined $conf->{$key}) {
if($key eq 'nagios_config_file' &&
! isyes($conf->{modify_nagios_config})) {
next;
} elsif($key eq 'nagios_commands_file' &&
! isyes($conf->{modify_nagios_config})) {
next;
} elsif(($key eq 'apache_config_dir' ||
$key eq 'apache_config_file') &&
! isyes($conf->{modify_apache_config})) {
next;
} elsif($key eq 'apache_config_file' &&
defined $conf->{apache_config_dir} &&
isyes($conf->{modify_apache_config})) {
next;
} elsif($key eq 'apache_config_dir' &&
defined $conf->{apache_config_file} &&
isyes($conf->{modify_apache_config})) {
next;
} else {
logmsg('parameter ' . $key . ' is not defined');
$missing = 1;
}
}
}
return $missing;
}
sub getconfig {
my ($conf) = @_;
my $fail = 0;
if (readconfigenv($conf) > 0) {
$conf->{automated} = 'y';
return 0;
}
readconfigpresets($conf);
if (! defined $conf->{nagios_perfdata_file}) {
my $x = findfile('perfdata.log',
mkapppath(\@NAGIOS_NAMES, \@NAGIOS_VAR_DIRS));
$conf->{nagios_perfdata_file} = $x if $x ne q();
}
if (! defined $conf->{nagios_user}) {
my $x = finduser(@NAGIOS_USERS);
$conf->{nagios_user} = $x if $x ne q();
}
if (! defined $conf->{www_user}) {
my $x = finduser(@APACHE_USERS);
$conf->{www_user} = $x if $x ne q();
}
# prompt for each item in the configuration
foreach my $ii (@CONF) {
my $def = defined $conf->{$ii->{key}} ?
$conf->{$ii->{key}} :
defined $ii->{def} ? $ii->{def} : q();
if ($def ne q() &&
defined $ii->{parent} &&
defined $conf->{$ii->{parent}}) {
$def = prependpath($conf->{$ii->{parent}}, $def);
}
$conf->{$ii->{key}} = getanswer($ii->{msg}, $def) if defined $ii->{msg};
}
# find out if we should modify the nagios configuration. if there is a
# conf.d directory, then use it. if not, modify the .conf file.
$conf->{modify_nagios_config} =
getanswer('Modify the Nagios configuration', 'n');
if (isyes($conf->{modify_nagios_config})) {
my @cfgdirs = mkapppath(\@NAGIOS_NAMES, \@NAGIOS_CFG_DIRS);
my $x = defined $conf->{nagios_config_file} ?
$conf->{nagios_config_file} : q();
$x = findfile('nagios.cfg', @cfgdirs) if $x eq q();
$x = findfile('icinga.cfg', @cfgdirs) if $x eq q();
$conf->{nagios_config_file} =
getanswer('Path of Nagios configuration file', $x);
$NAGIOS_CFG = $conf->{nagios_config_file};
$x = defined $conf->{nagios_commands_file} ?
$conf->{nagios_commands_file} : q();
$x = findfile('commands.cfg', @cfgdirs) if $x eq q();
if ($x eq q()) {
my @cdirs;
foreach my $d (@cfgdirs) {
push @cdirs, $d . '/objects';
}
$x = findfile('commands.cfg', @cdirs);
}
$conf->{nagios_commands_file} =
getanswer('Path of Nagios commands file', $x);
}
# find out if we should modify the apache configuration. if there is a
# conf.d directory, then use it. if not, modify the .conf file.
$conf->{modify_apache_config} =
getanswer('Modify the Apache configuration', 'n');
if (isyes($conf->{modify_apache_config})) {
my @dirs = mkapppath(\@APACHE_NAMES, \@APACHE_CFG_DIRS);
my $x = defined $conf->{apache_config_dir} ?
$conf->{apache_config_dir} : q();
$x = finddir('conf.d', @dirs) if $x eq q();
$conf->{apache_config_dir} =
getanswer('Path of Apache configuration directory', $x);
if (! defined $conf->{apache_config_dir} ||
$conf->{apache_config_dir} eq q()) {
$x = defined $conf->{apache_config_file} ?
$conf->{apache_config_file} : q();
$x = findfile('apache2.conf', @dirs) if $x eq q();
$x = findfile('conf/apache2.conf', @dirs) if $x eq q();
$x = findfile('httpd.conf', @dirs) if $x eq q();
$x = findfile('conf/httpd.conf', @dirs) if $x eq q();
$conf->{apache_config_file} =
getanswer('Path of Apache configuration file', $x);
}
}
return $fail;
}
sub readconfigpresets {
my ($conf) = @_;
foreach my $p (@PRESETS) {
if (defined $conf->{ng_layout} &&
$conf->{ng_layout} eq $p->{ng_layout} &&
(! defined $p->{ng_prefix} ||
! defined $conf->{ng_prefix} ||
$p->{ng_prefix} eq $conf->{ng_prefix})) {
foreach my $ii (keys %{$p}) {
if (! defined $conf->{$ii}) {
$conf->{$ii} = $p->{$ii};
}
}
last;
}
}
return 0;
}
sub readconfigenv {
my ($conf) = @_;
my $cnt = 0;
if ($ENV{NG_LAYOUT}) {
$conf->{ng_layout} = $ENV{NG_LAYOUT};
$cnt += 1;
}
foreach my $ii (@CONF) {
my $name = mkenvname($ii->{key});
if ($ENV{$name}) {
$conf->{$ii->{key}} = $ENV{$name};
$cnt += 1;
}
}
return 0 if $cnt == 0;
my $n = mkenvname('modify_nagios_config');
$conf->{modify_nagios_config} = $ENV{$n} && $ENV{$n} eq 'y' ? 'y' : 'n';
$n = mkenvname('modify_apache_config');
$conf->{modify_apache_config} = $ENV{$n} && $ENV{$n} eq 'y' ? 'y' : 'n';
$n = mkenvname('nagios_config_file');
$conf->{nagios_config_file} = $ENV{$n} if $ENV{$n};
$n = mkenvname('nagios_commands_file');
$conf->{nagios_commands_file} = $ENV{$n} if $ENV{$n};
$n = mkenvname('apache_config_dir');
$conf->{apache_config_dir} = $ENV{$n} if $ENV{$n};
$n = mkenvname('apache_config_file');
$conf->{apache_config_file} = $ENV{$n} if $ENV{$n};
readconfigpresets($conf);
foreach my $ii (@CONF) {
my $def = defined $conf->{$ii->{key}} ?
$conf->{$ii->{key}} :
defined $ii->{def} ? $ii->{def} : q();
if ($def ne q() &&
defined $ii->{parent} &&
defined $conf->{$ii->{parent}}) {
$def = prependpath($conf->{$ii->{parent}}, $def);
}
$conf->{$ii->{key}} = $def;
}
return $cnt;
}
sub envvars {
my @vars;
push @vars, 'NG_LAYOUT';
foreach my $ii (@CONF) {
push @vars, mkenvname($ii->{key});
}
return @vars;
}
sub mkenvname {
my ($name) = @_;
$name =~ tr/a-z/A-Z/;
if ($name !~ /^NG_/) {
$name = 'NG_' . $name;
}
return $name;
}
sub printconfig {
my ($conf) = @_;
logmsg('configuration:');
if ($ENV{DESTDIR}) {
logmsg(' DESTDIR=' . $ENV{DESTDIR});
}
foreach my $ii (@CONF) {
my $key = $ii->{key};
logmsg(sprintf ' %-20s %s', $key, defined $conf->{$key} ? $conf->{$key} : q());
}
return;
}
# check pre-requisites. return 0 if everything is ok, 1 otherwise.
sub checkprereq {
my $fail = 0;
logmsg('checking required PERL modules');
$fail |= checkmodule('Carp');
$fail |= checkmodule('CGI');
$fail |= checkmodule('Data::Dumper');
$fail |= checkmodule('Digest::MD5');
$fail |= checkmodule('File::Basename');
$fail |= checkmodule('File::Find');
$fail |= checkmodule('MIME::Base64');
$fail |= checkmodule('POSIX');
$fail |= checkmodule('RRDs');
$fail |= checkmodule('Time::HiRes');
logmsg('checking optional PERL modules');
checkmodule('GD', 1);
checkmodule('Nagios::Config', 1);
my $found;
my @dirs;
logmsg('checking nagios installation');
@dirs = mkapppath(\@NAGIOS_NAMES, \@NAGIOS_BIN_DIRS);
$found = checkexec('nagios3', @dirs);
$found = checkexec('nagios2', @dirs) if $found eq q();
$found = checkexec('nagios', @dirs) if $found eq q();
$found = checkexec('icinga', @dirs) if $found eq q();
if ($found ne q()) {
logmsg(" found nagios exectuable at $found");
$NAGIOS_EXEC = $found;
} else {
my $dlist;
foreach my $d (@dirs) {
$dlist .= "\n $d";
}
logmsg(" nagios not found in any of:$dlist");
$fail = 1;
}
@dirs = ('/etc/init.d');
$found = checkexec('nagios3', @dirs);
$found = checkexec('nagios2', @dirs) if $found eq q();
$found = checkexec('nagios', @dirs) if $found eq q();
$found = checkexec('icinga', @dirs) if $found eq q();
if ($found ne q()) {
logmsg(" found nagios init script at $found");
$NAGIOS_INIT = $found;
}
logmsg('checking web server installation');
@dirs = mkapppath(\@APACHE_NAMES, \@APACHE_BIN_DIRS);
$found = checkexec('apache2', @dirs);
$found = checkexec('apache', @dirs) if $found eq q();
$found = checkexec('httpd', @dirs) if $found eq q();
if ($found ne q()) {
logmsg(" found apache executable at $found");
$APACHE_EXEC = $found;
} else {
my $dlist;
foreach my $d (@dirs) {
$dlist .= " $d\n";
}
logmsg(" apache not found in any of:\n$dlist");
$fail = 1;
}
@dirs = ('/etc/init.d');
$found = checkexec('apache2', @dirs);
$found = checkexec('apache', @dirs) if $found eq q();
$found = checkexec('httpd', @dirs) if $found eq q();
if ($found ne q()) {
logmsg(" found apache init script at $found");
$APACHE_INIT = $found;
}
return $fail;
}
# return 0 if ok, 1 if failure
sub checkmodule {
my ($func, $optional) = @_;
my $rval = eval "{ require $func; }"; ## no critic (ProhibitStringyEval)
my $status = 'fail';
if (defined $rval && $rval == 1) {
$status = $func->VERSION;
}
logmsg(" $func..." . ($status eq 'fail' ? ' ***FAIL***' : $status));
return $status eq 'fail' ? 1 : 0;
}
# return the dir/app, empty string if not found
sub checkexec {
my ($app, @dirs) = @_;
my $found = q();
foreach my $d (@dirs) {
my $a = "$d/$app";
if (-f $a) {
$found = $a;
last;
}
}
return $found;
}
# check for things that are often broken
sub checkinstallation {
my $fail = 0;
my $pd = getanswer('Path of perfdata file', '/var/nagios/perfdata.log');
my $mapfn = getanswer('Path of map file', '/etc/nagiosgraph/map');
my $rrddir = getanswer('Path of RRD directory', '/var/nagiosgraph/rrd');
my $logdir = getanswer('Path of log directory', '/var/log/nagiosgraph');
my $nuser = finduser(@NAGIOS_USERS);
my $ngroup = findgroup(@NAGIOS_GROUPS);
my $auser = finduser(@APACHE_USERS);
my $agroup = findgroup(@APACHE_GROUPS);
$nuser = getanswer('nagios user', $nuser);
$ngroup = getanswer('nagios group', $ngroup);
$auser = getanswer('apache user', $auser);
$agroup = getanswer('apache group', $agroup);
logmsg('checking RRDs');
my $rval = eval { require RRDs; };
if (defined $rval && $rval == 1) {
my ($fh,$fn) = tempfile();
RRDs::create("$fn",'-s 60',
'DS:temp:GAUGE:600:0:100',
'RRA:AVERAGE:0.5:1:576',
'RRA:AVERAGE:0.5:6:672',
'RRA:AVERAGE:0.5:24:732',
'RRA:AVERAGE:0.5:144:1460');
my $err = RRDs::error();
if (! $err) {
logmsg(' RRDs::create: ok');
RRDs::update("$fn", '-t', 'temp', 'N:50');
$err = RRDs::error();
if (! $err) {
logmsg(' RRDs::update: ok');
} else {
logmsg('*** RRDs::update failed: ' . $err);
$fail = 1;
}
} else {
logmsg('*** RRDs::create failed: ' . $err);
$fail = 1;
}
if (-f $fn) {
unlink $fn;
}
} else {
logmsg('*** RRDs is not installed');
$fail = 1;
}
logmsg('checking GD');
$rval = eval { require GD; };
if (defined $rval && $rval == 1) {
my $img = new GD::Image(5,5);
if ($img) {
logmsg(' GD:Image ok');
} else {
logmsg('*** GD::Image: failed');
$fail = 1;
}
} else {
logmsg(' GD is not installed (GD is recommended but not required)');
}
logmsg('checking map file');
if (-f $mapfn) {
logmsg('checking map file with perl');
my ($CHILD_IN, $CHILD_OUT, $CHILD_ERR);
open3($CHILD_IN, $CHILD_OUT, $CHILD_ERR, "perl -c $mapfn");
if (! $CHILD_ERR) {
logmsg(' no errors detected in map file.');
} else {
logmsg(' one or more problems with map file');
my $result = q();
while( <$CHILD_ERR> ) {
$result .= $_;
}
logmsg($result);
$fail = 1;
}
logmsg('checking ability to load map file');
if (open my $FH, '<', $mapfn) {
my @rules;
while(<$FH>) {
push @rules, $_;
}
close $FH or logmsg("close failed for $mapfn");
# this code must match the code in ngshared
## no critic (RequireInterpolationOfMetachars)
my $code = 'sub evalrules {' . "\n" .
' $_ = $_[0];' . "\n" .
' my ($d, @s) = ($_);' . "\n" .
' no strict "subs";' . "\n" .
join(q(), @rules) .
' use strict "subs";' . "\n" .
' return () if ($#s > -1 && $s[0] eq "ignore");' . "\n" .
' return @s;' . "\n" .
'} 1' . "\n";
my $rc = eval $code; ## no critic (ProhibitStringyEval)
if (defined $rc && $rc) {
logmsg(' map file loaded successfully');
} else {
logmsg('*** map file eval error!');
$fail = 1;
}
} else {
logmsg("*** cannot open map file $mapfn: $OS_ERROR");
$fail = 1;
}
} else {
logmsg("*** no mapfile at $mapfn");
}
logmsg('checking ability to write perfdata files');
my $pddir = $pd;
$pddir =~ s/[^\/]+$//g;
if (-d $pddir) {
if (canwrite($pddir, $nuser, $ngroup)) {
logmsg(" writeable by nagios user $nuser");
} else {
logmsg("*** not writeable by nagios user $nuser:$ngroup");
$fail = 1;
}
} else {
logmsg("*** no directory for performance data at $pddir");
$fail = 1;
}
logmsg('checking RRD directory permissions');
if (-d $rrddir) {
if (canwrite($rrddir, $nuser, $ngroup)) {
logmsg(" writeable by nagios user $nuser");
} else {
logmsg("*** not writeable by nagios user $nuser:$ngroup");
$fail = 1;
}
if (canread($rrddir, $auser, $agroup)) {
logmsg(" readable by apache user $auser");
} else {
logmsg("*** not readable by apache user $auser:$agroup");
$fail = 1;
}
} else {
logmsg("*** no RRD directory at $rrddir");
}
logmsg('checking log directory permissions');
if (-d $logdir) {
if (canwrite($logdir, $nuser, $ngroup)) {
logmsg(" writeable by nagios user $nuser");
} else {
logmsg("*** not writeable by nagios user $nuser:$ngroup");
$fail = 1;
}
if (canwrite($logdir, $auser, $agroup)) {
logmsg(" writeable by apache user $auser");
} else {
logmsg("*** not writeable by apache user $auser:$agroup");
$fail = 1;
}
} else {
logmsg("*** no log directory at $logdir");
$fail = 1;
}
return $fail;
}
sub checkuserid {
if ($dochown && geteuid() != 0) {
logmsg('***');
logmsg('*** Warning! root privileges are required for installation');
logmsg('***');
}
return;
}
sub finduser {
my @users = @_;
foreach my $u (@users) {
my $uid = getpwnam $u;
if (defined $uid) {
return $u;
}