#!/usr/bin/perl
# hdiskmon, Boone, 02/15/01
# Show disk space total, free for each normal file system
#
# Modifications:
# 02/15/01 Boone      Initial coding
# 05/30/01 Boone      File::Df vanished from CPAN; converted to use
#                     Filesys::Df
# 03/07/05 Boone      Don't force version any more; use hostname, it
#                     gives back the full name unlike the stupid perl
#                     module; add ext3, reiserfs as types of fs to watch
# 06/15/09 Boone      New password for new systems regime
# End Modifications

# Libraries

	use Filesys::Df;
	use Mon::Client;
#	use Sys::Hostname;

# Initialize

	$monsrv = "netmon.matrix.msu.edu";
	$sys = `uname`;
	chop($sys);
	if		($sys eq "SunOS")
	{
		$host = `hostname`;
	}
	elsif	($sys eq "Linux")
	{
		$host = `hostname -f`;
	}
	chop($host);
	@brief = ();
	@detail = ();

# Command line

	$free = shift(@ARGV) || 100.0;
	$pct = shift(@ARGV) / 100.0 || 0.20;

# Check disk space

	if		($^O eq "linux")			# mount list comes from /etc/mtab
	{
		&dflinux;
	}
	elsif	($^O eq "aix")				# info comes from "listmount"
	{
		&dfaix;
	}

	if (@brief > 0)
	{
		$mon = new Mon::Client
		(
			host => $monsrv,
			port => 2583,
			username => "mon",
			password => "onmtmbiw",
		) ||
			die "unable to create Mon::Client object: $!";
#		$mon -> prot("0.30.0");
		$mon -> connect ||
			die "unable to connect to mon server: " . $mon -> error;
		$mon -> login ||
			die "unable to login to mon server: " . $mon -> error;
		$mon -> send_trap
		(
			group => "diskservers",
			service => "diskfree",
			retval => 1,
			opstatus => "fail",
			summary => "$host-", join(",", @brief),
			detail => "$host:\n", join("\n ", @detail),
		);
		$mon -> quit;
		$mon -> disconnect;
	}
	else
	{
		$mon = new Mon::Client
		(
			host => $monsrv,
			port => 2583,
			username => "mon",
			password => "ZaStU3uG",
		) ||
			die "unable to create Mon::Client object: $!";
#		$mon -> prot("0.30.0");
		$mon -> connect ||
			die "unable to connect to mon server: " . $mon -> error;
		$mon -> login ||
			die "unable to login to mon server: " . $mon -> error;
		$mon -> send_trap
		(
			group => "diskservers",
			service => "diskfree",
			retval => 1,
			opstatus => "ok",
		);
		$mon -> quit;
		$mon -> disconnect;
	}

# Linux checker

	sub dflinux
	{
		open(MTAB, "/etc/mtab") ||
			die "unable to open /etc/mtab: #!";
		while (<MTAB>)
		{
			chop;
			($dev, $point, $type, $opt, $x, $y) = split(/ /);
			if		(($type eq "ext2") ||
					($type eq "ext3") ||
					($type eq "reiserfs"))
			{
				$ref = df($point, 1024);
				$used = $ref -> {"used"};
				$avail = $ref -> {"bavail"};
#				($x, $desc, $used, $avail, $fused, $favail) = df($point);
				$total = ($used + $avail) / 1024;
				$used /= 1024;
				$avail /= 1024;
				if ($total > (2 * $free))
				{
					if ($avail < $free)
					{
						push(@brief, "$point:$avail MB(S)");
						push(@detail, "$avail MB remain on $fs");
					}
				}
				else
				{
					if ($avail/($total) < $pct) 
					{
						push(@brief, "$point:$avail MB(S)");
						push(@detail, "$avail MB remain on $fs (small fs)");
					}
				}
			}
		}
	}

# AIX checker

	sub dfaix
	{
		open(INFO, "listmount |") ||
			die "unable to open listmount pipe: #!";
		while (<INFO>)
		{
			chop;
			s/^\s+|\s+$//;
			s/\s+/ /g;
			($fs, $total, $avail) = split(/ /);
			$total /= 1024;
			$avail /= 1024;
			if ($total > (2 * $free))
			{
				if ($avail < $free)
				{
					push(@brief, "$fs:$avail MB (S)");
					push(@detail, "$avail MB remain on $fs");
				}
			}
			else
			{
				if ($avail/($total) < $pct) 
				{
					push(@brief, "$fs:$avail MB (S)");
					push(@detail, "$avail MB remain on $fs (small fs)");
				}
			}
		}
	}
