*** 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 () { + 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;