Nagios::Cmd perl module
Al Tobey
albert.tobey at priority-health.com
Fri Sep 19 23:18:55 CEST 2003
For submission under the GPL, a quick module I threw together for
submitting commands to the Nagios command pipe.
Attached.
-Al Tobey
** ** ** PRIVILEGED AND CONFIDENTIAL ** ** **
This email transmission contains privileged and confidential information
intended only for the use of the individual or entity named above. Any
unauthorized review, use, disclosure or distribution is prohibited and
may be a violation of law. If you are not the intended recipient or a
person responsible for delivering this message to an intended recipient,
please delete the email and immediately notify the sender via the email
return address or mailto:postmaster at priority-health.com. Thank you.
-------------- next part --------------
###########################################################################
# #
# Nagios::Cmd #
# Written by Albert Tobey <albert.tobey at priority-health.com> #
# Copyright 2003, Albert P Tobey #
# #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the #
# Free Software Foundation; either version 2, or (at your option) any #
# later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
###########################################################################
package Nagios::Cmd;
use vars qw( $debug %commands );
use Fcntl ':flock';
require '5.6.0';
$debug = undef;
%commands = (
ADD_HOST_COMMENT => [qw(host persistent author comment)],
ADD_SVC_COMMENT => [qw(host service persistent author comment)],
DEL_HOST_COMMENT => [qw(comment_id)],
DEL_ALL_HOST_COMMENTS => [qw(host)],
DEL_SVC_COMMENT => [qw(comment_id)],
DEL_ALL_SVC_COMMENTS => [qw(host service)],
DELAY_HOST_NOTIFICATION => [qw(host next_notification_time)],
DELAY_SVC_NOTIFICATION => [qw(host service next_check_time)],
SCHEDULE_HOST_SVC_CHECKS => [qw(host next_check_time)],
ENABLE_SVC_CHECKS => [qw(host service)],
DISABLE_SVC_CHECKS => [qw(host service)],
ENABLE_SVC_NOTIFICATIONS => [qw(host service)],
DISABLE_SVC_NOTIFICATIONS => [qw(host service)],
ENABLE_HOST_SVC_NOTIFICATIONS => [qw(host)],
DISABLE_HOST_SVC_NOTIFICATIONS => [qw(host)],
ENABLE_HOST_SVC_CHECKS => [qw(host)],
DISABLE_HOST_SVC_CHECKS => [qw(host)],
ENABLE_HOST_NOTIFICATIONS => [qw(host)],
DISABLE_HOST_NOTIFICATIONS => [qw(host)],
ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST => [qw(host)],
DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST => [qw(host)],
ENABLE_NOTIFICATIONS => [qw(time)],
DISABLE_NOTIFICATIONS => [qw(time)],
SHUTDOWN_PROGRAM => [qw(time)],
RESTART_PROGRAM => [qw(time)],
PROCESS_SERVICE_CHECK_RESULT => [qw(host service return_code plugin_output)],
SAVE_STATE_INFORMATION => [qw(time)],
READ_STATE_INFORMATION => [qw(time)],
START_EXECUTING_SVC_CHECKS => undef,
STOP_EXECUTING_SVC_CHECKS => undef,
START_ACCEPTING_PASSIVE_SVC_CHECKS => undef,
STOP_ACCEPTING_PASSIVE_SVC_CHECKS => undef,
ENABLE_PASSIVE_SVC_CHECKS => [qw(host service)],
DISABLE_PASSIVE_SV_CHECKS => [qw(host service)],
ACKNOWLEDGE_SVC_PROBLEM => [qw(host service persistent comment)],
ACKNOWLEDGE_HOST_PROBLEM => [qw(host persistent comment)]
);
sub new {
my( $type, $cmdfile ) = @_;
die "$cmdfile does not exist!"
unless ( -e $cmdfile );
die "$cmdfile is not a pipe and debugging is not enabled!"
unless ( -p $cmdfile || defined($debug) );
print "opening '$cmdfile' to write commands to ...\n" if ( $debug );
open( NAGIOS_CMD_FILE, ">>$cmdfile" )
|| die "could not open $cmdfile for writing: $!";
return( bless(\$cmdfile, $type) );
}
sub _write_cmd {
my $time = '[' . time() . '] ';
flock( NAGIOS_CMD_FILE, LOCK_EX );
print "writing '", $time, join(';', @_), "' to command file ...\n"
if ( $debug );
print NAGIOS_CMD_FILE $time, join(';', @_), "\n";
flock( NAGIOS_CMD_FILE, LOCK_UN );
}
sub DESTROY {
print "closing command file ...\n" if ( $debug );
close( NAGIOS_CMD_FILE );
}
sub Help {
my $func = shift;
if ( !$func ) {
foreach my $cmd ( keys(%commands) ) {
print "$cmd(", join(', ', @{$commands{$cmd}}), ")\n";
}
}
else {
print "$func(", join(', ', @{$commands{$func}}), ")\n";
}
}
sub AUTOLOAD {
my $self = shift;
my $name = our $AUTOLOAD;
$name =~ m/Nagios::Cmd::(\w+)$/;
my $method = $1;
print "method $method called with arguments: '", join(', ', @_), "'\n"
if ( $debug );
die "invalid method call '$method'"
unless ( exists($commands{$method}) );
my @parts = @{ $commands{$method} };
my @args = ();
# process hashed arguments
if ( ref($_[0]) eq 'HASH' ) {
my $args = shift;
for ( my $i=0; $i==@parts; $i++ ) {
if ( !exists($args->{$parts[$i]}) ) {
if ( $parts[$i] =~ /(time|persistent)/ ) {
print "setting default for option $1 in call to $method ...\n"
if ( $debug );
$args[$i] = undef;
next;
}
die "insufficient arguments to $method - $parts[$i] arg is missing";
}
else {
$args[$i] = $args->{$parts[$i]};
}
}
}
# user sent us a list
else {
@args = @_;
}
# set the defaults in our array
for ( my $i=0; $i==@parts; $i++ ) {
if ( !defined($args[$i]) ) {
if ( $parts[$i] eq 'persistent' ) {
print "persistent flag is set to 1 for call to $method ...\n"
if ( $debug );
$parts[$i] = 1
}
elsif ( $parts[$i] eq 'time' ) {
print "time flag is being set to current time for call to $method\n"
if ( $debug );
$parts[$i] = time()
}
}
}
_write_cmd( @args );
}
=head1 NAME
Nagios::Cmd
=head1 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).
To turn on this module's debugging, set it's $debug directly 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'
Examples:
use lib '/opt/nagios/libexec';
use Nagios::Cmd;
my $cmd = Nagios::Cmd->new( "/var/opt/nagios/rw/nagios.cmd" );
$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." );
=head1 LICENSE
GPL
=head1 AUTHOR
Albert P Tobey <albert.tobey at priority-health.com>
=cut
1;
More information about the Developers
mailing list