The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Nagios::Cmd

DESCRIPTION

Nagios::Cmd is a module to take care of the logistics involved in submitting a command to Nagios's command pipe. flock(2) is used to insure that parallel calls to this module don't corrupt each other (unlikely in any case).

SYNOPSIS

To turn on this module's debugging, set $Nagios::Cmd::debug to a value greater than 0 before calling any methods: $Nagios::Cmd::debug = 1;

To get a list of valid commands and their arguments, run the following command: perl -MNagios::Cmd -e 'Nagios::Cmd::Help' perl -MNagios::Cmd -e 'Nagios::Cmd::Help(ADD_HOST_COMMENT)'

You might need to specify an include path for Nagios::Cmd since it most likely won't be in your standard perl include directories: perl -I/opt/nagios/libexec -MNagios::Cmd -e 'Nagios::Cmd::Help'

EXAMPLE

 use lib '/opt/nagios/libexec';
 use Nagios::Cmd;
 my $cmd = Nagios::Cmd->new( "/var/opt/nagios/rw/nagios.cmd" );
 # -- OR --
 $cmd = Nagios::Cmd->new_anyfile( "/var/tmp/test_file.txt" );

 my $cmd_args = {
    host => $host,
    persistent => 1,
    author => "Al Tobey",
    comment => "This host is very stable."
 };
 $cmd->ADD_HOST_COMMENT( $cmd_args );

 $cmd->ADD_HOST_COMMENT(
    host => $host,
    persistent => 1,
    author => "Al Tobey",
    comment => "This host is very stable."
 );

 $cmd->ADD_HOST_COMMENT( $host, 1, "Al Tobey", "This host is very stable." );

 # -- OR --

 use lib '/opt/nagios/libexec';
 use Nagios::Cmd;
 my $time = CORE::time(); # use CORE:: if you have Time::HiRes overriding time()

 # submit a custom command to the pipe
 $cmd->nagios_cmd( "[$time] DEL_ALL_HOST_COMMENTS;localhost" );

METHODS

new()

Initiate a Nagios::Cmd object. It takes ony one argument, the full path to the nagios command file. If you want to test this module out, without submitting all kinds of noise to Nagios, set $Nagios::Cmd::debug = 1, which will allow the command file to be a regular file instead of a pipe. You can also create a test command file with the mknod(1) command.

 mknod -m 600 /var/tmp/nagios_cmd p

The cat(1) command works well as a reader on a fifo.

 my $cmd = Nagios::Cmd->new( "/usr/local/nagios/var/rw/cmd.pipe" );
new_anyfile()

Same thing as new, but does not check to see if the target file is a fifo.

 my $cmd = Nagios::Cmd->new_anyfile( "/dev/null" );
 my $cmd = Nagios::Cmd->new_anyfile( "/tmp/commands.txt" );
service_check(), host_check()

These are (essentially) hard-coded aliases for PROCESS_SERVICE_CHECK_RESULT and PROCESS_HOST_CHECK_RESULT.

Passive host checks don't work with Nagios 1.0, but should be available for 2.0, so keep that in mind if you're thinking about using host_check. **WARNING** It's mainly here to play with. Your code may need twiddling with to work with future releases of this module.

 $cmd->service_check( "PING", "localhost", 1, "PING CRITICAL - Packet loss = 100%" );
 $cmd->host_check( "localhost", 1, "CRITICAL - Host Down!" );
nagios_cmd()

Use this method if you need to use a command that is not defined in this module. Adding commands to this module is pretty trivial, so you may want to look at the %commands hash at the top of the Cmd.pm file.

 $cmd->nagios_cmd( "[".time()."] " . join(";", $COMMAND_NAME, @ARGS) );
 $cmd->nagios_cmd( "[1063919882] DISABLE_HOST_SVC_NOTIFICATIONS;localhost" );

LICENSE

GPL

AUTHOR

Albert P Tobey <albert.tobey@priority-health.com>