Home > Nagios, Perl > Perl – A nagios script for checking disk space by percentage or size

Perl – A nagios script for checking disk space by percentage or size

There is currently an .exe script for linux/aix machines that run nagios to check disk size usage by size. However, the script for Windows only allows a percentage input. The perl script below has been created to allow for checking either percentage of disk used or when the available disk size is lower than the threshold.

#!/usr/bin/perl
################################################################################
# Job Name : check_disk
# System : AIX, Windows, Linux
# Author : Kerri Robberts
# Date : 14-Jun-2011
# Version : 1.0
#
# Description : Script to check disk size either by % used or MB threshold, written for Nagios
#
# Preceding Job : None
#
# Following Job :
#
# Parameters : Command line parameters available within the script.
# -d Disk name
# -cp Critical percentage
# -cs Critical size
# -wp Warning percentage
# -ws Warning size
#
# Exit Codes : 0 OK
# 1 Warning threshold reached
# 2 Critical threshold reached
#
# Nagios check_command: # 'check_disk command definition
# define command{
# command_name check_disk
# command_line $USER1$/check_disk --d $ARG1$ -wp $ARG2$ -ws $ARG3$ -cp $ARG4$ -cs $ARG5$
# }
#
# Example host.cfg entry:
# define service{
# use IC-local-service ; Name of service template to use
# host_name ln1dev09
# service_description DISK: Drive {C}
# check_command check_disk!C!95!500!99!300
# }
################################################################################
# Change History
################################################################################
# Version Author Date Change & Reason
# ------- ------ --------- ---------------
# 1.0 Krobbe 14-Jun-11 Initial Version
################################################################################
use warnings;
use strict;
use Getopt::Long;
my $DISC => undef;
my $WARNS => undef;
my $WARNP => undef;
my $CRITS => undef;
my $CRITP => undef;
my $AVAILABLE => undef;
my $TOTAL => undef;
my $USED => undef;
my $USEDP => undef;
my $USEDS => undef;
my $OK = 0;
my $WARNING = 1;
my $CRITICAL = 2;
my $UNKNOWN = 3;
GetOptions (
'disc|d=s' => \$DISC,
'warnsize|ws:s' => \$WARNS,
'warnper|wp=s' => \$WARNP,
'critsize|cs:s' => \$CRITS,
'critper|cp=s' => \$CRITP);
if (defined (($WARNP || $WARNS)&&($CRITP || $CRITS))) {
check();
}
sub check {
my $OSNAME = `uname|awk '{printf \$1}'`;
if ($OSNAME eq 'AIX') {
my $DFCMD = "df -k | grep $DISC | grep -v $DISC/ ";
$AVAILABLE = `$DFCMD | awk '{ printf \$3}'`;
$USED = `$DFCMD | awk '{ printf \$4}'`;
$TOTAL = `$DFCMD | awk '{ printf \$2}'`;
}
elsif ($OSNAME eq 'Linux') {
my $DFCMD = "df -k | grep $DISC | grep -v $DISC/ ";
$AVAILABLE = `$DFCMD | awk '{ printf \$3}'`;
$USED = `$DFCMD | awk '{ printf \$4}'`;
$TOTAL = `$DFCMD | awk '{ printf \$1}'`;
}
else {
my $DFCMD = "df -k | grep $DISC: | grep -v $DISC:/ ";
$AVAILABLE = `$DFCMD | awk '{ printf \$4}'`;
$USED = `$DFCMD | awk '{ printf \$5}'`;
$TOTAL = `$DFCMD | awk '{ printf \$2}'`;
}
$TOTAL = sprintf("%.2f",($TOTAL / 1024));
$USEDS = sprintf("%.2f",($TOTAL - ($AVAILABLE/1024)));
my $CRITPER = sprintf("%.2f",($TOTAL) * ($CRITP/100));
my $CRITSIZE = sprintf("%.2f",(($TOTAL) - $CRITS));
my $WARNPER = sprintf("%.2f",($TOTAL) * ($WARNP/100));
my $WARNSIZE = sprintf("%.2f",(($TOTAL) - $WARNS));
if (($CRITSIZE >= $CRITPER) && ($CRITSIZE <= $USEDS)) {
print "DISC CRITICAL - usage: $DISC: $USEDS MB ($USED of total $TOTAL MB)\n";
exit $CRITICAL;
}
elsif (($CRITPER >=$CRITSIZE) && ($CRITPER <= $USEDS)) {
print "DISC CRITICAL - usage: $DISC: $USEDS MB ($USED of total $TOTAL MB)\n";
exit $CRITICAL;
}
elsif (($WARNSIZE >= $WARNPER)&&($WARNSIZE <= $USEDS)) {
print "DISC WARNING - usage: $DISC: $USEDS MB ($USED of total $TOTAL MB)\n";
exit $WARNING;
}
elsif (($WARNPER >= $WARNSIZE) &&( $WARNPER <= $USEDS)) {
print "DISC WARNING - usage: $DISC: $USEDS MB ($USED of total $TOTAL MB)\n";
exit $WARNING;
}
else {
print "DISC OK - usage: $DISC: $USEDS MB ($USED of total $TOTAL MB)\n\n";
exit $OK;
}
};

Advertisements
  1. February 23, 2014 at 3:41 PM

    Please let me know if you’re looking for a writer for your blog.

    You have some really great articles and I think I would be a good asset.
    If you ever want to take some of the load off, I’d absolutely
    love to write some content for your blog in exchange for
    a link back to mine. Please blast me an e-mail
    if interested. Regards!

  2. August 24, 2014 at 1:31 PM

    What’s up, this weekend is pleasant designed
    for me, because this moment i am reading this enormous educational piece of writing
    here at my residence.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: