| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Nagios::Plugin::Config; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 32 | use strict; | 
|  | 6 |  |  |  |  | 11 |  | 
|  | 6 |  |  |  |  | 229 |  | 
| 4 | 6 |  |  | 6 |  | 31 | use Carp; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 351 |  | 
| 5 | 6 |  |  | 6 |  | 32 | use File::Spec; | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 136 |  | 
| 6 | 6 |  |  | 6 |  | 26 | use base qw(Config::Tiny); | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 5589 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | my $FILENAME1 = 'plugins.ini'; | 
| 9 |  |  |  |  |  |  | my $FILENAME2 = 'nagios-plugins.ini'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # Config paths ending in nagios (search for $FILENAME1) | 
| 12 |  |  |  |  |  |  | my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios); | 
| 13 |  |  |  |  |  |  | # Config paths not ending in nagios (search for $FILENAME2) | 
| 14 |  |  |  |  |  |  | my @CONFIG_PATH = qw(/etc /usr/local/etc /etc/opt); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Override Config::Tiny::read to default the filename, if not given | 
| 17 |  |  |  |  |  |  | sub read | 
| 18 |  |  |  |  |  |  | { | 
| 19 | 13 |  |  | 13 | 1 | 18 | my $class = shift; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 13 | 50 |  |  |  | 36 | unless ($_[0]) { | 
| 22 |  |  |  |  |  |  | SEARCH: { | 
| 23 | 13 | 50 |  |  |  | 26 | if ($ENV{NAGIOS_CONFIG_PATH}) { | 
|  | 13 |  |  |  |  | 48 |  | 
| 24 | 13 |  |  |  |  | 54 | for (split /:/, $ENV{NAGIOS_CONFIG_PATH}) { | 
| 25 | 26 |  |  |  |  | 453 | my $file = File::Spec->catfile($_, $FILENAME1); | 
| 26 | 26 | 100 |  |  |  | 579 | unshift(@_, $file), last SEARCH if -f $file; | 
| 27 | 13 |  |  |  |  | 107 | $file = File::Spec->catfile($_, $FILENAME2); | 
| 28 | 13 | 50 |  |  |  | 138 | unshift(@_, $file), last SEARCH if -f $file; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 | 0 |  |  |  |  | 0 | for (@NAGIOS_CONFIG_PATH) { | 
| 32 | 0 |  |  |  |  | 0 | my $file = File::Spec->catfile($_, $FILENAME1); | 
| 33 | 0 | 0 |  |  |  | 0 | unshift(@_, $file), last SEARCH if -f $file; | 
| 34 |  |  |  |  |  |  | } | 
| 35 | 0 |  |  |  |  | 0 | for (@CONFIG_PATH) { | 
| 36 | 0 |  |  |  |  | 0 | my $file = File::Spec->catfile($_, $FILENAME2); | 
| 37 | 0 | 0 |  |  |  | 0 | unshift(@_, $file), last SEARCH if -f $file; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | # Use die instead of croak, so we can pass a clean message downstream | 
| 42 | 13 | 50 |  |  |  | 42 | die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0]; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 13 |  |  |  |  | 74 | $class->SUPER::read( @_ ); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | # Straight from Config::Tiny - only changes are repeated property key support | 
| 49 |  |  |  |  |  |  | # Would be nice if we could just override the per-line handling ... | 
| 50 |  |  |  |  |  |  | sub read_string | 
| 51 |  |  |  |  |  |  | { | 
| 52 | 13 | 50 |  | 13 | 1 | 1219 | my $class = ref $_[0] ? ref shift : shift; | 
| 53 | 13 |  |  |  |  | 44 | my $self  = bless {}, $class; | 
| 54 | 13 | 50 |  |  |  | 36 | return undef unless defined $_[0]; | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # Parse the file | 
| 57 | 13 |  |  |  |  | 20 | my $ns      = '_'; | 
| 58 | 13 |  |  |  |  | 18 | my $counter = 0; | 
| 59 | 13 |  |  |  |  | 1833 | foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) { | 
| 60 | 455 |  |  |  |  | 425 | $counter++; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Skip comments and empty lines | 
| 63 | 455 | 100 |  |  |  | 1194 | next if /^\s*(?:\#|\;|$)/; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # Handle section headers | 
| 66 | 351 | 100 |  |  |  | 1328 | if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) { | 
| 67 |  |  |  |  |  |  | # Create the sub-hash if it doesn't exist. | 
| 68 |  |  |  |  |  |  | # Without this sections without keys will not | 
| 69 |  |  |  |  |  |  | # appear at all in the completed struct. | 
| 70 | 104 |  | 50 |  |  | 585 | $self->{$ns = $1} ||= {}; | 
| 71 | 104 |  |  |  |  | 164 | next; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # Handle properties | 
| 75 | 247 | 50 |  |  |  | 1143 | if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) { | 
| 76 | 247 |  |  |  |  | 267 | push @{$self->{$ns}->{$1}}, $2; | 
|  | 247 |  |  |  |  | 997 |  | 
| 77 | 247 |  |  |  |  | 373 | next; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  | 0 | return $self->_error( "Syntax error at line $counter: '$_'" ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 13 |  |  |  |  | 138 | $self; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  | 0 | 1 |  | sub write { croak "Write access not permitted" } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | 1; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 NAME | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Nagios::Plugin::Config - read nagios plugin .ini style config files | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # Read given nagios plugin config file | 
| 97 |  |  |  |  |  |  | $Config = Nagios::Plugin::Config->read( '/etc/nagios/plugins.ini' ); | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # Search for and read default nagios plugin config file | 
| 100 |  |  |  |  |  |  | $Config = Nagios::Plugin::Config->read(); | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | # Access sections and properties (returns scalars or arrayrefs) | 
| 103 |  |  |  |  |  |  | $rootproperty =  $Config->{_}->{rootproperty}; | 
| 104 |  |  |  |  |  |  | $one = $Config->{section}->{one}; | 
| 105 |  |  |  |  |  |  | $Foo = $Config->{section}->{Foo}; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | Nagios::Plugin::Config is a subclass of the excellent Config::Tiny, | 
| 110 |  |  |  |  |  |  | with the following changes: | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =over 4 | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =item | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Repeated keys are allowed within sections, returning lists instead of scalars | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =item | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Write functionality has been removed i.e. access is read only | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =item | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Nagios::Plugin::Config searches for a default nagios plugins file if no explicit | 
| 125 |  |  |  |  |  |  | filename is given to C. The current standard locations checked are: | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =over 4 | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =item /etc/nagios/plugins.ini | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =item /usr/local/nagios/etc/plugins.ini | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =item /usr/local/etc/nagios /etc/opt/nagios/plugins.ini | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =item /etc/nagios-plugins.ini | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =item /usr/local/etc/nagios-plugins.ini | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =item /etc/opt/nagios-plugins.ini | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =back | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | To use a custom location, set a C environment variable | 
| 144 |  |  |  |  |  |  | to the set of directories that should be checked. The first C or | 
| 145 |  |  |  |  |  |  | C file found will be used. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =back | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | L, L | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 AUTHORS | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | This code is maintained by the Nagios Plugin Development Team: | 
| 158 |  |  |  |  |  |  | L. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 COPYRIGHT and LICENCE | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Copyright (C) 2006-2007 by Nagios Plugin Development Team | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 166 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =cut | 
| 169 |  |  |  |  |  |  |  |