#!/usr/bin/perl -w
#
# PURPOSE:
# Check disk+mem usage.  If disk or mem is over $output_at percent, print
# a line of information.
#---------------------------------------------------------------
# COPYRIGHT INFORMATION - DO NOT REMOVE
# "Portions Copyright (c) 2000-2001 LinuxMagic Inc. All Rights Reserved.
#
# This file contains Original Code and/or Modifications of Original Code as
# defined in and that are subject to the Free Source Code License Version
# 1.0 (the 'License'). You may not use this file except in compliance with
# the License. Please obtain a copy of the License at:
#
# http://www.linuxmagic.com/opensource/licensing/FSCL.txt
#
# and read it before using this file.
#
# The Original Code and all software distributed under the License are
# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, AND LINUXMAGIC HEREBY DISCLAIMS ALL SUCH WARRANTIES,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
# the License for the specific language governing rights and limitations
# under the License."
#
# Please read the terms of this license carefully. By using or downloading
# this software or file, you are accepting and agreeing to the terms of this
# license with LinuxMagic Inc. If you are agreeing to this license on behalf
# of a company, you represent that you are authorized to bind the company to
# such a license. If you do not meet this criterion or you do not agree to
# any of the terms of this license, do NOT download, distribute, use or alter
# this software or file in any way.
#
# DO NOT MODIFY WITHOUT CONSULTING THE LICENSE
#
#--------------------------------------------------------------
#
#   Authors: John Bratlien <john@wizard.ca>
#            Josh Wilsdon <josh@wizard.ca>
#
# $Id: check-resources.pl,v 1.3 2003/09/10 02:00:00 michael Exp $

$output_at = 90;     # min percent used we print at
@df_output = `df -PTh`;

# /proc/meminfo contains upto date memory info
open ($meminfo, "< /proc/meminfo") or die ("couldn't read meminfo");
@lines = <$meminfo>;

defined($lines[1]) or print "could not read lines 1 or 2";

#only need to get lines 2 & 3 for the memory utilization info 
for ($i=1; $i < 3; $i++) {
  	chomp($lines[$i]);
  	@fields = split(/[\s]+/, $lines[$i]);

  	$type = $fields[0];
        $total = $fields[1];
  	$used = $fields[2];
	$buffers = $fields[5];
	$cached = $fields[6];

	# disregard cache + buffers
	defined $cached and $used -= $cached;
	defined $buffers and $used -= $buffers;

  	$used =~ s/[^0-9]//;
  	if((($used/$total) * 100) >= $output_at) {
	   if ($type =~ /Mem:/) {
	 	   printf("%2d%% overall Mem usage.\n", $used/$total*100);
	   } else {		# $type == "Swap:" 
	  	   printf("%2d%% overall Swap usage.\n", $used/$total*100);
	   }
   }
}

LINE: foreach $line (@df_output) {
    chomp($line);
    @fields = split(' ', $line);
    $device = $fields[0];
    # ignore header line
    if ($device eq "Filesystem") {
        next LINE;
    }
    $type = $fields[1];
    $size = $fields[2];
    $used = $fields[5];
    $used =~ s/[^0-9]//;
    $mount = $fields[6];

    if($used >= $output_at) {
        printf("%2d%% of $size used on $mount ($type)\n", $used);
    }
}
