Friday, 8 July 2011

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;
}
};

No comments:

Post a Comment

Please feel free to leave a comment