Skip to content

Instantly share code, notes, and snippets.

@realflash
Created December 29, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realflash/60b3df6aa2525908cef8ccd7c7a34da9 to your computer and use it in GitHub Desktop.
Save realflash/60b3df6aa2525908cef8ccd7c7a34da9 to your computer and use it in GitHub Desktop.
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