Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nagios perl template plugin
#!/usr/bin/perl -w
use strict;
use lib "/usr/lib/nagios/plugins"; # Debian
use lib "/usr/local/nagios/libexec"; # something else
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); # Requires utils.pm to be in one of the two directories above
use Getopt::Long qw(:config no_ignore_case);
# Predeclare subroutines
sub print_usage ();
sub print_version ();
sub print_help ();
# Initialise variables
my $plugin_revision = '0.1';
my $warning = '5';
my $critical = '10';
my $version;
my $help;
# If invoked with a path, strip the path from our name
my $prog_dir;
my $prog_name = $0;
if ($0 =~ s/^(.*?)[\/\\]([^\/\\]+)$//) {
$prog_dir = $1;
$prog_name = $2;
}
# Grab options from command line
my $good_options = GetOptions (
"w|warning=s" => \$warning,
"c|critical=s" => \$critical,
"v|version" => \$version,
"help" => \$help,
);
print_version() if $version;
print_help() if $help;
print_help() unless $good_options;
# Look up something here
my $thing = 16; # This is the thing you are testing and will be used to determine the return status
if($thing > $critical)
{
print "CRITICAL: Some text here";
exit $ERRORS{'CRITICAL'};
}
elsif($thing > $warning)
{
print "WARNING: Some text here";
exit $ERRORS{'WARNING'};
}
print "OK - Some text here";
exit $ERRORS{'OK'};
# Version number
sub print_version()
{
print "$prog_name $plugin_revision\n";
exit $ERRORS{'OK'};
}
# Long usage info
sub print_help()
{
print "./$prog_name [--warning|-w <value>] [--critical|-c <value]
./$prog_name --version|-v
./$prog_name --help
--warning, -w Override the default warning value of $warning
--critical, -c Override the default critical value of $critical
--version, -v Print the version of this plugin
--help Print this
";
exit $ERRORS{'OK'};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.