blob: cb2ea12b7d24206350391cb12ff84e5614321121 (
plain)
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
|
*** utils.pm.in.orig Wed Jan 22 15:39:22 2003
--- utils.pm.in Wed Jan 22 16:39:06 2003
***************
*** 19,25 ****
require Exporter;
@ISA = qw(Exporter);
! @EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage);
#use strict;
#use vars($TIMEOUT %ERRORS);
--- 19,25 ----
require Exporter;
@ISA = qw(Exporter);
! @EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage &config_value &config_values);
#use strict;
#use vars($TIMEOUT %ERRORS);
***************
*** 27,34 ****
--- 27,38 ----
sub usage;
sub support();
sub is_hostname;
+ sub config_value ($);
+ sub config_values ($);
## updated by autoconf
+ $prefix = "@prefix@";
+ $sysconfdir = "@sysconfdir@";
$PATH_TO_RPCINFO = "@PATH_TO_RPCINFO@" ;
$PATH_TO_NTPDATE = "@PATH_TO_NTPDATE@" ;
$PATH_TO_NTPDC = "@PATH_TO_NTPDC@" ;
***************
*** 72,75 ****
--- 76,124 ----
}
}
+ ## subroutines to read nagios.cfg and return values of config items.
+ $parsedConfig = 0;
+ %configItems = {};
+
+ sub read_config {
+ my $configFile = "$sysconfdir/nagios.cfg";
+
+ return 1 if $parsedConfig;
+
+ if (! open CONFIG, "<$configFile") {
+ print "$configFile: $!\n";
+ return 0;
+ }
+
+ while (<CONFIG>) {
+ next unless /^\s*([^=\s]+)=(.*)$/;
+
+ if (defined(@{$configItems{$1}})) {
+ $configItems{$1} = [@{$configItems{$1}}, $2];
+ } else {
+ $configItems{$1} = [$2];
+ }
+ }
+ close CONFIG;
+
+ $parsedConfig = 1;
+ return 1;
+ }
+
+ ## Returns the value/values of a config item. If called in array context,
+ ## a list of values is returned, otherwise the first value of the item
+ ## is returned. (Most Nagios config items have only one value, but some
+ ## such as cfg_file can be specified more than once.)
+ ##
+ ## ex: @cfg_files = &config_value('cfg_file');
+ ## ex: $resource_file = &config_value('resource_file');
+ sub config_value ($) {
+ my $key = shift;
+ return undef unless &read_config;
+ if (wantarray) {
+ return @{$configItems{$key}};
+ }
+ return $configItems{$key}->[0];
+ }
+
1;
|