Author image Richard Lippmann

NAME

Nagios::Clientstatus - Framework for Nagios check-service programs

SYNOPSIS

    use Nagios::Clientstatus;
    # This is needed for logging
    use Log::Log4perl qw/:easy/;
    Log::Log4perl->easy_init($ERROR);
    my $logger = Log::Log4perl->get_logger;
    
    # Arguments to the program are:
    # --critical=40 --warning=35 --hostname=server1.zdf --sensor_nr=4
    my $version = "0.01";
    my $ncli    = Nagios::Clientstatus->new(
        help_subref    => \&help,
        version        => $version,
        # default is that the module checks commandline
        dont_check_commandline_args => 0,  # default
        mandatory_args => [ "hostname", "sensor_nr", "critical", "warning" ],
    );

    # ask only one time, because it's expensive
    my $temperature = &get_temperature_of_sensor(
        hostname  => $ncli->get_given_arg('hostname'),
        sensor_nr => $ncli->get_given_arg('sensor_nr'),
    );

    # Message for the user to read
    my $msg;
    my $status;

    # strange case
    if (   ( !defined $temperature )
        || ( defined $temperature && $temperature eq "" ) )
    {
        $status = "unknown";
        $msg    = "Could not get temperature from sensor";
    }
    else {

        # We got a temperature
        # worst case first
        if ( $temperature > $ncli->get_given_arg('critical') ) {
            $status = "critical";
        }
        elsif ( $temperature > $ncli->get_given_arg('warning') ) {
            $status = "warning";
        }
        else {
            $status = "ok";
        }
        $msg = sprintf "Temperature is %s degrees Celsius", $temperature;
    }
    printf "%s - %s", uc($status), $msg;
    exit $ncli->exitvalue($status);

    sub help {
        print "Usage:\n";
        print "$0 --critical=40 --warning=35"
          . " --hostname=server1.zdf --sensor_nr=4";

        # When supplying help you should exit, use class-method
        # because we don't have an object
        exit Nagios::Clientstatus::exitvalue( 'unknown' );
    }

    sub get_temperature_of_sensor {
        my(%args) = @_;
        print "You should supply something useful here.\n";
        printf "Hostname: %s, sensor: %s\n", 
            $args{hostname}, $args{sensor_nr};
        print "Please enter a temperature: ";
        my $temperature = <STDIN>;
        chomp $temperature;
        return $temperature;

    };

DESCRIPTION

Create a program to check the function of some service or device for Nagios. This module helps you to check the mandatory and optional arguments. It helps you to send the right output so that Nagios can check wether the service works ok or not.

METHODS

new

Create the object. Immediately check commandline arguments which are mandatory for every Nagios command.

Usage:

    my $ncli = Nagios::Clientstatus->new(
        help_subref => \&help,
        version => $version,
        dont_check_commandline_args => 0, # default
        # mandatory_args is optional, maybe you don't need any
        mandatory_args => [ 'url' ],
        # optional_args is optional, maybe you don't need any
        optional_args => [ 'carsize' ],
    );

get_given_arg

Object-creator can ask for the value of an argument given to the program. This can be a mandatory or an optional argument. Not given optional arguments return undef.

When you create the object like this:

    my $ncli = Nagios::Clientstatus->new(
        help_subref => \&help,
        version => $version,
        mandatory_args => [ 'url' ],
        optional_args => [ 'carsize' ],
    );

If program is called: checkme --url=xx --carsize=medium

    # $value -> 'medium'
    $value = $nc->get_given_arg('carsize');
    
    # $value -> 'xx'
    $value = $nc->get_given_arg('url');

    # $value -> undef
    $value = $nc->get_given_arg('carpoolnotgiven');

exitvalue

Return the value the Nagios-command must return to Nagios. This is the only value which is important for the Nagios state.

Use it like this:

    exit $ncli->exitvalue( $status );

or without object as class-method:

    exit Nagios::Clientstatus::exitvalue( $status );

Returnvalue can be a string of these:

    OK|WARNING|CRITICAL|UNKNOWN

help_example

Give the user a hint how to use this programm.

AUTHOR

    Richard Lippmann
    CPAN ID: HORSHACK
    horshack@lisa.franken.de
    http://lena.franken.de

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.