#!/usr/bin/perl -w

use warnings;

#############################
## Check for low disk space in LVM logical volumes and volume groups
## Copyright Chris Biltcliffe
## CB Services
## C-Box
## All Rights Reserved
#############################



##$configFile = "/etc/lvmcheck/lvmCheck.conf";


$\="\n";
my @vgResult = `vgs --units b --nosuffix`;

chomp @vgResult;

foreach my $line (@vgResult)
	{
	$line =~ s/^\s*//g;
	if (!($line =~ /^VG\s/))
		{
		@vgBits = split / {1,20}/, $line;
		
		if ($vgBits[6]/$vgBits[5]*100 < 15)
			{
			open (LOGFILE, '>>/var/log/lvm_check.log');
			print LOGFILE `date +%F_%T `." LESS THAN 15% free in volume group: ".$vgBits[0];
			print LOGFILE "Total: ".$vgBits[5]." bytes, Free: ".$vgBits[6]." bytes.";
			close (LOGFILE);
			############ need to send mail here!!!


		}					
#		print $line;
#		print "VG: ".$vgBits[0];
#		print "Total: ".$vgBits[5];
#		print "Free: ".$vgBits[6];
	}
}

my @fsResult = `df -k`;
chomp @fsResult;
foreach my $fsLine (@fsResult)
	{
	$fsLine =~ s/^\s*//g;
	if (($fsLine =~ /^\/dev\/mapper\//))
		{
		@fsBits = split / {1,20}/, $fsLine;
#		print $fsLine;
		$fsBits[3] =~ /([\d\.]+)/;
#		$fsFree = $1;
		
#		print "FS: ".$fsBits[0]."    Total: ".$fsBits[1]."      Free: ".$fsBits[3];

	####### if less than 15% free space, AND less than 800MB free space...
		if ($fsBits[3]/$fsBits[1]*100 < 15 && $fsBits[3] < 800000)
			{
			#print "FS needs expansion..";
			open (LOGFILE, '>>/var/log/lvm_check.log');
			print LOGFILE `date +%F_%T `." ".$fsBits[0]." expanded by 500MB";
			close (LOGFILE);
			system("lvextend -L +500MB ".$fsBits[0]);
			system("fsadm resize ".$fsBits[0]);
		}
#		print "Free size: ".$fsFree;
#		print "Free unitless: ".$fsFree;
	}
}	

