#! /usr/bin/perl -w # (c)2004 Kevin Hilscher, Hatsize Learning Corp. require 5.004; use POSIX; use strict; use Getopt::Long; use vars qw($opt_V $opt_h $opt_w $opt_c $verbose $PROGNAME); use lib "/usr/lib/nagios/plugins"; use utils qw($TIMEOUT %ERRORS &print_revision &support); $PROGNAME="check_vmfs"; sub parse_vmfs (); sub check_disk_space (); sub print_help (); sub print_usage (); $ENV{'PATH'}=''; $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; # defaults my $DEFAULT_WARN_PERCENT = 10; # 10% my $DEFAULT_CRIT_PERCENT = 5; # 5% my $VMFS_HBA_PREFIX = "vmhba"; my $VDF_PATH = "/usr/sbin/vdf"; my $NUM_OF_VMFS_PARTITIONS = 0; my $OUTPUT_STRING = ""; my $STATE = $ERRORS{'UNKNOWN'}; my @VMHBA_ARRAY; my @VMFS_STATS_ARRAY; Getopt::Long::Configure('bundling'); GetOptions ("V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "v" => \$verbose, "verbose" => \$verbose, "w=f" => \$opt_w, "warning=f" => \$opt_w, "c=f" => \$opt_c, "critical=f" => \$opt_c); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.0.0.0 $ '); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } ($opt_w) || ($opt_w = $DEFAULT_WARN_PERCENT); my $owarn = $1 if ($opt_w =~ /([0-9.]+)/); ($opt_c) || ($opt_c = $DEFAULT_CRIT_PERCENT); my $ocrit = $1 if ($opt_c =~ /([0-9.]+)/); if ($ocrit > $owarn ) { print "Critical value should be smaller than warning value\n"; print_usage(); exit $ERRORS{"UNKNOWN"}; } else { parse_vmfs(); check_disk_space(); foreach my $key (keys %ERRORS) { if ($STATE==$ERRORS{$key}) { print ("VMFS $key: $OUTPUT_STRING\n"); last; } } if ($verbose) { print "State: $STATE\n"; print "Warning Level: $owarn%\n"; print "Critical Level: $ocrit%\n"; } exit $STATE; } sub parse_vmfs () { open(FILEHANDLE, "$VDF_PATH |"); while () { #Locate vmhba volumes if($_ =~/^$VMFS_HBA_PREFIX/i) { #Save into an array $VMHBA_ARRAY[$NUM_OF_VMFS_PARTITIONS] = $_; $NUM_OF_VMFS_PARTITIONS++; } } close(FILEHANDLE); } sub check_disk_space () { my $x = 0; my $PREVIOUSSTATE = $ERRORS{'OK'}; for (my $i=0; $i < $NUM_OF_VMFS_PARTITIONS; $i++) { my $row = $VMHBA_ARRAY[$i]; chomp($row); #vmhba $VMFS_STATS_ARRAY[$x] = substr($row, 0,12); $x++; #used space $VMFS_STATS_ARRAY[$x] = substr($row, 52,2); #convert to free space my $freespace = 100 - $VMFS_STATS_ARRAY[$x]; if ($freespace < $owarn) { #generate warning $STATE = $ERRORS{'WARNING'}; if ($freespace < $ocrit) { #generate critical $STATE = $ERRORS{'CRITICAL'}; } } else { $STATE = $ERRORS{'OK'}; } #the following is in case multiple vmhbas are present if ($STATE == $ERRORS{'WARNING'}) { $PREVIOUSSTATE = $ERRORS{'WARNING'}; } if ($STATE == $ERRORS{'CRITICAL'}) { $PREVIOUSSTATE = $ERRORS{'CRITICAL'}; } $x++; } #check if there was a previous warning or critical message if ($STATE != $PREVIOUSSTATE) { $STATE = $PREVIOUSSTATE; } for (my $i=0; $i < $x; $i++) { my $VAL1 = 100 - $VMFS_STATS_ARRAY[$i+1]; $OUTPUT_STRING = "$OUTPUT_STRING$VAL1% free space on"; $OUTPUT_STRING = "$OUTPUT_STRING $VMFS_STATS_ARRAY[$i] "; $i++; } } sub print_usage () { print "Usage: $PROGNAME -w limit -c limit [--verbose] [-h | --help] [-V | --version]\n"; } sub print_help () { print_revision($PROGNAME,'$Revision: 1.0.0.0 $'); print "Copyright (c) 2004 Kevin Hilscher\n"; print "\n"; print_usage(); print " This plugin will check the percent of used disk space on a VMware VMFS file system and generates an alert if percentage is above one of the threshold values.\n -w = warning % free space (do not use % symbol). Default is 10\r -c = critical % free space (do not use % symbol). Default is 5\n "; }