| 1 | package Nagios::Clientstatus; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 13 | 3 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 16 | 3 | @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 17 | 3 | %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Nagios::Clientstatus - Framework for Nagios check-service programs | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Nagios::Clientstatus; | ||||||||||||||||||||||||||||
| 27 | # This is needed for logging | ||||||||||||||||||||||||||||
| 28 | use Log::Log4perl qw/:easy/; | ||||||||||||||||||||||||||||
| 29 | Log::Log4perl->easy_init($ERROR); | ||||||||||||||||||||||||||||
| 30 | my $logger = Log::Log4perl->get_logger; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Arguments to the program are: | ||||||||||||||||||||||||||||
| 33 | # --critical=40 --warning=35 --hostname=server1.zdf --sensor_nr=4 | ||||||||||||||||||||||||||||
| 34 | my $version = "0.01"; | ||||||||||||||||||||||||||||
| 35 | my $ncli = Nagios::Clientstatus->new( | ||||||||||||||||||||||||||||
| 36 | help_subref => \&help, | ||||||||||||||||||||||||||||
| 37 | version => $version, | ||||||||||||||||||||||||||||
| 38 | # default is that the module checks commandline | ||||||||||||||||||||||||||||
| 39 | dont_check_commandline_args => 0, # default | ||||||||||||||||||||||||||||
| 40 | mandatory_args => [ "hostname", "sensor_nr", "critical", "warning" ], | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # ask only one time, because it's expensive | ||||||||||||||||||||||||||||
| 44 | my $temperature = &get_temperature_of_sensor( | ||||||||||||||||||||||||||||
| 45 | hostname => $ncli->get_given_arg('hostname'), | ||||||||||||||||||||||||||||
| 46 | sensor_nr => $ncli->get_given_arg('sensor_nr'), | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Message for the user to read | ||||||||||||||||||||||||||||
| 50 | my $msg; | ||||||||||||||||||||||||||||
| 51 | my $status; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # strange case | ||||||||||||||||||||||||||||
| 54 | if ( ( !defined $temperature ) | ||||||||||||||||||||||||||||
| 55 | || ( defined $temperature && $temperature eq "" ) ) | ||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | $status = "unknown"; | ||||||||||||||||||||||||||||
| 58 | $msg = "Could not get temperature from sensor"; | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # We got a temperature | ||||||||||||||||||||||||||||
| 63 | # worst case first | ||||||||||||||||||||||||||||
| 64 | if ( $temperature > $ncli->get_given_arg('critical') ) { | ||||||||||||||||||||||||||||
| 65 | $status = "critical"; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | elsif ( $temperature > $ncli->get_given_arg('warning') ) { | ||||||||||||||||||||||||||||
| 68 | $status = "warning"; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | $status = "ok"; | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | $msg = sprintf "Temperature is %s degrees Celsius", $temperature; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | printf "%s - %s", uc($status), $msg; | ||||||||||||||||||||||||||||
| 76 | exit $ncli->exitvalue($status); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub help { | ||||||||||||||||||||||||||||
| 79 | print "Usage:\n"; | ||||||||||||||||||||||||||||
| 80 | print "$0 --critical=40 --warning=35" | ||||||||||||||||||||||||||||
| 81 | . " --hostname=server1.zdf --sensor_nr=4"; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # When supplying help you should exit, use class-method | ||||||||||||||||||||||||||||
| 84 | # because we don't have an object | ||||||||||||||||||||||||||||
| 85 | exit Nagios::Clientstatus::exitvalue( 'unknown' ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub get_temperature_of_sensor { | ||||||||||||||||||||||||||||
| 89 | my(%args) = @_; | ||||||||||||||||||||||||||||
| 90 | print "You should supply something useful here.\n"; | ||||||||||||||||||||||||||||
| 91 | printf "Hostname: %s, sensor: %s\n", | ||||||||||||||||||||||||||||
| 92 | $args{hostname}, $args{sensor_nr}; | ||||||||||||||||||||||||||||
| 93 | print "Please enter a temperature: "; | ||||||||||||||||||||||||||||
| 94 | my $temperature = <STDIN>; | ||||||||||||||||||||||||||||
| 95 | chomp $temperature; | ||||||||||||||||||||||||||||
| 96 | return $temperature; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Create a program to check the function of some service or device | ||||||||||||||||||||||||||||
| 104 | for Nagios. This module helps you to check the mandatory and | ||||||||||||||||||||||||||||
| 105 | optional arguments. It helps you to send the right output so that | ||||||||||||||||||||||||||||
| 106 | Nagios can check wether the service works ok or not. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 METHODS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 new | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Create the object. Immediately check commandline arguments which | ||||||||||||||||||||||||||||
| 115 | are mandatory for every Nagios command. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Usage: | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | my $ncli = Nagios::Clientstatus->new( | ||||||||||||||||||||||||||||
| 120 | help_subref => \&help, | ||||||||||||||||||||||||||||
| 121 | version => $version, | ||||||||||||||||||||||||||||
| 122 | dont_check_commandline_args => 0, # default | ||||||||||||||||||||||||||||
| 123 | # mandatory_args is optional, maybe you don't need any | ||||||||||||||||||||||||||||
| 124 | mandatory_args => [ 'url' ], | ||||||||||||||||||||||||||||
| 125 | # optional_args is optional, maybe you don't need any | ||||||||||||||||||||||||||||
| 126 | optional_args => [ 'carsize' ], | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 14 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 133 | my $new_object = bless( | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | help_subref => \&help_example, | ||||||||||||||||||||||||||||
| 136 | version => $args{version}, | ||||||||||||||||||||||||||||
| 137 | 14 | ▶ | mandatory_args => [], | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 138 | # help, version, debug: but no arguments here | ||||||||||||||||||||||||||||
| 139 | # like --help=specialvalue, only --help | ||||||||||||||||||||||||||||
| 140 | optional_default_args => [], | ||||||||||||||||||||||||||||
| 141 | # more optional values, all must have a value | ||||||||||||||||||||||||||||
| 142 | # like --carsize=medium | ||||||||||||||||||||||||||||
| 143 | optional_additional_args => [], | ||||||||||||||||||||||||||||
| 144 | given_args => {}, | ||||||||||||||||||||||||||||
| 145 | dont_check_commandline_args => 0, | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | ref($class) || $class | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 14 | ▶ | if ( exists $args{dont_check_commandline_args} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | $new_object->{dont_check_commandline_args} = | ||||||||||||||||||||||||||||
| 152 | 3 | $args{dont_check_commandline_args}; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # help_subref | ||||||||||||||||||||||||||||
| 156 | 14 | ▶ | unless ( ( exists $args{help_subref} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"help_subref"} and ref $args{"help_subref"} eq "CODE"
MC/DC100%2/2
exists $args{"help_subref"} and ref $args{"help_subref"} eq "CODE"
exists $args{"help_subref"}
ref $args{"help_subref"} eq "CODE"
Truth table100%3/3
A and B
A= exists $args{"help_subref"}B= ref $args{"help_subref"} eq "CODE"
| |||||||||||||||||||||||||||||
| 157 | && ( ref $args{help_subref} eq "CODE" ) ) | ||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 2 | print STDERR | |||||||||||||||||||||||||||
| 160 | "Missing ref to help-subroutine. This sub could output this:\n"; | ||||||||||||||||||||||||||||
| 161 | 2 | $new_object->help_example; | |||||||||||||||||||||||||||
| 162 | my $msg = | ||||||||||||||||||||||||||||
| 163 | sprintf "Mandatory argument help_subref must point" | ||||||||||||||||||||||||||||
| 164 | . " to a help-subroutine, but it is a '%s'", | ||||||||||||||||||||||||||||
| 165 | 2 | ref( $args{help_subref} ); | |||||||||||||||||||||||||||
| 166 | 2 | die $msg; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 12 | $new_object->{help_subref} = $args{help_subref}; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # which optional args could be at the commandline? | ||||||||||||||||||||||||||||
| 171 | # The usual ones | ||||||||||||||||||||||||||||
| 172 | 12 | $new_object->{optional_default_args} = [ $new_object->_get_optional_default_args ]; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # The other one the user wants | ||||||||||||||||||||||||||||
| 175 | # These arguments must be supplied like this: | ||||||||||||||||||||||||||||
| 176 | # --carsize=medium, not valid: --carsize | ||||||||||||||||||||||||||||
| 177 | 12 | ▶ | if ( ( exists $args{optional_args} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"optional_args"} and ref $args{"optional_args"} eq "ARRAY"
MC/DC50%1/2
exists $args{"optional_args"} and ref $args{"optional_args"} eq "ARRAY"
exists $args{"optional_args"}
ref $args{"optional_args"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $args{"optional_args"}B= ref $args{"optional_args"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 178 | && ( ref $args{optional_args} eq "ARRAY" ) ) | ||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 3 | foreach my $optarg ( @{$args{optional_args}} ) { | |||||||||||||||||||||||||||
| 181 | 3 | push @{$new_object->{optional_additional_args}}, $optarg; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # are there mandatory arguments? | ||||||||||||||||||||||||||||
| 186 | 12 | ▶ | if ( ( exists $args{mandatory_args} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"mandatory_args"} and ref $args{"mandatory_args"} eq "ARRAY"
MC/DC50%1/2
exists $args{"mandatory_args"} and ref $args{"mandatory_args"} eq "ARRAY"
exists $args{"mandatory_args"}
ref $args{"mandatory_args"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $args{"mandatory_args"}B= ref $args{"mandatory_args"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 187 | && ( ref $args{mandatory_args} eq "ARRAY" ) ) | ||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||
| 189 | 9 | $new_object->_set_mandatory_args( @{ $args{mandatory_args} } ); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # don't set mandatory args, sometimes it's critical when | ||||||||||||||||||||||||||||
| 193 | # the service does not run -> very simple | ||||||||||||||||||||||||||||
| 194 | # $new_object->_set_mandatory_args( "critical", "warning" ); | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 12 | $new_object->_check_commandline_args; | |||||||||||||||||||||||||||
| 197 | 6 | return $new_object; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 12 | ▶ | sub _logger { | ||||||||||||||||||||||||||
|
Subroutine
_logger: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 201 | 12 | return Log::Log4perl->get_logger('Nagios.Clientstatus'); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 12 | ▶ | sub _dont_check_commandline_args { | ||||||||||||||||||||||||||
|
Subroutine
_dont_check_commandline_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # shall any commandline-arg be checked by Getopt::Long? | ||||||||||||||||||||||||||||
| 207 | 12 | shift->{dont_check_commandline_args}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | #=head2_set_mandatory_args | ||||||||||||||||||||||||||||
| 211 | # | ||||||||||||||||||||||||||||
| 212 | #Remind arguments which user must supply when calling the program. | ||||||||||||||||||||||||||||
| 213 | #Can be called several times. | ||||||||||||||||||||||||||||
| 214 | # | ||||||||||||||||||||||||||||
| 215 | #=cut | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 9 | ▶ | sub _set_mandatory_args { | ||||||||||||||||||||||||||
|
Subroutine
_set_mandatory_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 218 | 9 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 219 | 9 | push @{ $self->{mandatory_args} }, @args; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | #=head2 _get_mandatory_args | ||||||||||||||||||||||||||||
| 223 | # | ||||||||||||||||||||||||||||
| 224 | #Which args MUST be given to the programm? Each argument must have a value, too. | ||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||
| 226 | #=cut | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 24 | ▶ | sub _get_mandatory_args { | ||||||||||||||||||||||||||
|
Subroutine
_get_mandatory_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 229 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 230 | 24 | @{ $self->{mandatory_args} }; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | #=head2 _set_given_args | ||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||
| 235 | #Which arguments where given to the program? | ||||||||||||||||||||||||||||
| 236 | # | ||||||||||||||||||||||||||||
| 237 | #=cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub _set_given_args { | ||||||||||||||||||||||||||
|
Subroutine
_set_given_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 240 | 0 | my ( $self, $name, $value ) = @_; | |||||||||||||||||||||||||||
| 241 | 0 | $self->{given_args}->{name} = $value; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 get_given_arg | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Object-creator can ask for the value of an argument | ||||||||||||||||||||||||||||
| 247 | given to the program. This can be a mandatory or | ||||||||||||||||||||||||||||
| 248 | an optional argument. Not given optional arguments | ||||||||||||||||||||||||||||
| 249 | return undef. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | When you create the object like this: | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | my $ncli = Nagios::Clientstatus->new( | ||||||||||||||||||||||||||||
| 254 | help_subref => \&help, | ||||||||||||||||||||||||||||
| 255 | version => $version, | ||||||||||||||||||||||||||||
| 256 | mandatory_args => [ 'url' ], | ||||||||||||||||||||||||||||
| 257 | optional_args => [ 'carsize' ], | ||||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | If program is called: checkme --url=xx --carsize=medium | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # $value -> 'medium' | ||||||||||||||||||||||||||||
| 263 | $value = $nc->get_given_arg('carsize'); | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # $value -> 'xx' | ||||||||||||||||||||||||||||
| 266 | $value = $nc->get_given_arg('url'); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # $value -> undef | ||||||||||||||||||||||||||||
| 269 | $value = $nc->get_given_arg('carpoolnotgiven'); | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 3 | ▶ | sub get_given_arg { | ||||||||||||||||||||||||||
|
Subroutine
get_given_arg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 274 | 3 | my ( $self, $name ) = @_; | |||||||||||||||||||||||||||
| 275 | return exists $self->{given_args}->{$name} | ||||||||||||||||||||||||||||
| 276 | 3 | ▶ | ? $self->{given_args}->{$name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | : undef; | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | #=head2 _check_commandline_args | ||||||||||||||||||||||||||||
| 281 | # | ||||||||||||||||||||||||||||
| 282 | #There are arguments which must exist when calling a Nagios-checker. | ||||||||||||||||||||||||||||
| 283 | #warning|critcal are mandatory, other mandatory were given by new. | ||||||||||||||||||||||||||||
| 284 | # | ||||||||||||||||||||||||||||
| 285 | #=cut | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 12 | ▶ | sub _check_commandline_args { | ||||||||||||||||||||||||||
|
Subroutine
_check_commandline_args: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 288 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 289 | 12 | my $logger = $self->_logger; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 12 | my %getopt_long_arg; | |||||||||||||||||||||||||||
| 292 | my %got_this_option; | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # shall any commandline-arg be checked by Getopt::Long? | ||||||||||||||||||||||||||||
| 295 | 12 | ▶ | if ( $self->_dont_check_commandline_args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | $logger->info("Do not check any commandline arguments"); | |||||||||||||||||||||||||||
| 297 | 0 | return; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Build up the argument hash for Getopt::Long | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # Build the hash for Getopt::Long | ||||||||||||||||||||||||||||
| 303 | 12 | foreach ( $self->_get_optional_default_args ) { | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # Getopt::Long wants a ref to a scalar where value is stored in | ||||||||||||||||||||||||||||
| 306 | 36 | $getopt_long_arg{$_} = $got_this_option{$_}; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | # Maybe there are optional args supplied by new | ||||||||||||||||||||||||||||
| 310 | # Must be all like: --carsize=medium (with value) | ||||||||||||||||||||||||||||
| 311 | 12 | foreach ($self->_get_optional_additional_args) { | |||||||||||||||||||||||||||
| 312 | 3 | $getopt_long_arg{"$_=s"} = $got_this_option{$_}; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 12 | foreach ( $self->_get_mandatory_args ) { | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # Tell Getopt::Long that there must be an argument | ||||||||||||||||||||||||||||
| 318 | # Getopt::Long wants a ref to a scalar where value is stored in | ||||||||||||||||||||||||||||
| 319 | 9 | $getopt_long_arg{"$_=s"} = $got_this_option{$_}; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # Unusual syntax for daily life in GetOptions | ||||||||||||||||||||||||||||
| 323 | # Look in manpage of Getopt::Long for this: | ||||||||||||||||||||||||||||
| 324 | # Storing option values in a hash | ||||||||||||||||||||||||||||
| 325 | # All given options are stored in hash given as first argument | ||||||||||||||||||||||||||||
| 326 | GetOptions( | ||||||||||||||||||||||||||||
| 327 | 12 | \%got_this_option, | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # all possible options are here: | ||||||||||||||||||||||||||||
| 330 | keys %getopt_long_arg, | ||||||||||||||||||||||||||||
| 331 | ); | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # Now all arguments given to the program are in %got_this_option | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # Do mandatory args exist? | ||||||||||||||||||||||||||||
| 336 | 12 | my @mand_forgotten; | |||||||||||||||||||||||||||
| 337 | 12 | foreach ( $self->_get_mandatory_args ) { | |||||||||||||||||||||||||||
| 338 | 9 | ▶ | unless ( exists $got_this_option{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 6 | push @mand_forgotten, $_; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 12 | ▶ | if ( scalar @mand_forgotten > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 6 | $logger->debug('%getopt_long_arg was: '); | |||||||||||||||||||||||||||
| 344 | 6 | $logger->debug( Dumper( \%getopt_long_arg ) ); | |||||||||||||||||||||||||||
| 345 | 6 | $logger->debug('%got_this_option was: '); | |||||||||||||||||||||||||||
| 346 | 6 | $logger->debug( Dumper( \%got_this_option ) ); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 6 | printf STDERR "Mandatory arguments not given: %s\n", | |||||||||||||||||||||||||||
| 349 | join( ", ", @mand_forgotten ); | ||||||||||||||||||||||||||||
| 350 | 6 | $self->{help_subref}->(); | |||||||||||||||||||||||||||
| 351 | 6 | $self->_exit; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # all arguments where checked, now put them into given_args | ||||||||||||||||||||||||||||
| 355 | 6 | $self->{given_args} = \%got_this_option; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head2 exitvalue | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Return the value the Nagios-command must return to Nagios. | ||||||||||||||||||||||||||||
| 361 | This is the only value which is important for the Nagios state. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Use it like this: | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | exit $ncli->exitvalue( $status ); | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | or without object as class-method: | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | exit Nagios::Clientstatus::exitvalue( $status ); | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | Returnvalue can be a string of these: | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | OK|WARNING|CRITICAL|UNKNOWN | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 9 | ▶ | sub exitvalue { | ||||||||||||||||||||||||||
|
Subroutine
exitvalue: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 379 | 9 | my $first_arg = shift; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # Class-method or object-method? | ||||||||||||||||||||||||||||
| 382 | 9 | ▶ | my $status = ref($first_arg) ? shift: $first_arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 9 | $status = uc $status; | |||||||||||||||||||||||||||
| 385 | 9 | my %nagios_returnvalue = ( | |||||||||||||||||||||||||||
| 386 | 'OK' => 0, | ||||||||||||||||||||||||||||
| 387 | 'WARNING' => 1, | ||||||||||||||||||||||||||||
| 388 | 'CRITICAL' => 2, | ||||||||||||||||||||||||||||
| 389 | 'UNKNOWN' => 3, | ||||||||||||||||||||||||||||
| 390 | ); | ||||||||||||||||||||||||||||
| 391 | 9 | ▶ | unless ( exists $nagios_returnvalue{$status} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 1 | die "Wrong status '$status' to return, status can only be: " . join ",", | |||||||||||||||||||||||||||
| 393 | sort keys %nagios_returnvalue; | ||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | 8 | return $nagios_returnvalue{$status}; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head2 help_example | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | Give the user a hint how to use this programm. | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =cut | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 2 | ▶ | sub help_example { | ||||||||||||||||||||||||||
|
Subroutine
help_example: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 405 | 2 | shift; | |||||||||||||||||||||||||||
| 406 | 2 | print <<"EOUSAGE"; | |||||||||||||||||||||||||||
| 407 | This is $0 | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Usage: | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | $0 --warning 60 \\ | ||||||||||||||||||||||||||||
| 412 | --critical 130 \\ | ||||||||||||||||||||||||||||
| 413 | --your_argument_here_1 xx \\ | ||||||||||||||||||||||||||||
| 414 | --your_argument_here_2 xx \\ | ||||||||||||||||||||||||||||
| 415 | [--version] | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | Tell the user what this programm does | ||||||||||||||||||||||||||||
| 418 | EOUSAGE | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 24 | ▶ | sub _get_optional_default_args { | ||||||||||||||||||||||||||
|
Subroutine
_get_optional_default_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 422 | 24 | shift; | |||||||||||||||||||||||||||
| 423 | 24 | qw{version help debug}; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | #=head2 _get_optional_additional_args | ||||||||||||||||||||||||||||
| 427 | # | ||||||||||||||||||||||||||||
| 428 | #Get a list of args which could be given to the program. | ||||||||||||||||||||||||||||
| 429 | #These are the optional args given in new, but not the | ||||||||||||||||||||||||||||
| 430 | #default optional args 'help','debug','version' | ||||||||||||||||||||||||||||
| 431 | # | ||||||||||||||||||||||||||||
| 432 | #=cut | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 12 | ▶ | sub _get_optional_additional_args { | ||||||||||||||||||||||||||
|
Subroutine
_get_optional_additional_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 435 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 436 | 12 | @{$self->{optional_additional_args}}; | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # for testing only, I can overwrite exit | ||||||||||||||||||||||||||||
| 440 | # to let the program run after "exiting" | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | sub _exit { | ||||||||||||||||||||||||||
|
Subroutine
_exit: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 443 | 2 | exit; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | Richard Lippmann | ||||||||||||||||||||||||||||
| 449 | CPAN ID: HORSHACK | ||||||||||||||||||||||||||||
| 450 | horshack@lisa.franken.de | ||||||||||||||||||||||||||||
| 451 | http://lena.franken.de | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 456 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 459 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =cut | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 1 | 1; | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||