SUMMARY: Script to notify users of a disk filling up?

Maccy maccy at maccomms.co.uk
Thu Nov 29 14:48:55 EST 2001


My original query
------------------
> Does anyone have a nice script that automatically emails the users who are
> taking up the majority of space on a disk which is close to capacity?
> Currently I have to run :-
> mymachine> du -ks /home/ermintrude/* | sort -nr | head
> and email the users manually. I'm using the general threshold of
> 95% before I warn users to clean-up.

My thanks go to the following for their contributions
----------------------------------------------------
Peter Duncan, Ron Dinwiddie, Bertrand Hutin, David Glass, Randy Romero,
Thomas Knox, Jim M., Gary Mulder, Robert Reynolds, Jim Lang, Suresh
Vuthamaraju + anybody else that may contribute after this summary.

The scripts
-----------
A few asked whether I was using quotas on my systems, the answer is no, and
we are unlikely to in the future. I intend to play with the scripts received
over the next few days and then settle on the one. Note that some will just
mail the admin, not the users themselves (latter preferred). The ones
included below look the most interesting ;-)

Script 1
--------

# Set up variable for who is to be mailed
ADMIN_MAIL="unixadmin at globalfarmers.com"

# The maximum percent that the disk slices are allowed to grow to
MAXSIZE=95

# The Command to check the disks and strip out unwanted variables
DISKSIZES=`df -k |grep / |awk '{print $5}' |sed -e 's/\%//'`

# Take the input from the df command above
for percent in $DISKSIZES

do

# If the percent is greater than the maximum percent that the disk slices
are allowed to grow to
if [ $percent -gt $MAXSIZE ]
then


# Mail a df -k to the specified person(s)
df -k | mailx -s "Disk Space Problem on 'nodename' " $ADMIN_MAIL

fi

done

Script 2
--------

#! /usr/bin/ksh
DIR=/ADC/sys
HOST=`hostname`
/usr/bin/df -k | /usr/bin/awk '{print $6 " " $5}' | sed '/%/s// /g' >
$DIR/fs_usage
echo "The following file system/s is/are getting dangerously low on disk
space on system: " $HOST > $DIR/$HOST.usagemail
for size in $DIR/fs_usage
  do
    cat $size | \
while read line
  do
    /usr/bin/awk '$2 >= 90' >> $DIR/$HOST.usagemail
  done
  done
cat $DIR/$HOST.usagemail | /usr/bin/mailx -s "Filesystem Usage"
whoever at email-address.com,another-user-if-you-want at email-address.com

Script 3
--------

for i in `du -ks /home/ermintrude/* | sort -nr | head | awk '{print $2}'`
do
mailx -s "Clean up your directory" $i
done

Script 4
--------

#!/usr/bin/ksh

# This script will check a list of drives to
determine if they are below
# a specified threshold. If they are, it
will send an email alert.
# This script is designed to be called by
cron, and execute quickly.

# Uncomment the next line for debugging
info.
#set -o xtrace

# Which machine am I running on?
HOSTNAME=`uname -n`

# Whom do I send alerts to?
MAILTO="me at mycompany.com
you at yourcompany.com"

# What file contains the drive thresholds?
THRESHOLD=/usr/local/etc/threshold_list

# Read the list of drives to check, one at a
time.
while read DRIVE LLIMIT
do
    # Determine if we're looking for a % or
a fixed number
    DRVPCT=${LLIMIT%'%'}
    if [ "$LLIMIT" = "$DRVPCT" ]; then
        # Get the number of Kilobytes free
on the drive:
        let NUMFREE=`df -k | grep $DRIVE |
nawk '{ print $4 }'`
        MYDRIVE=`df -k | grep $DRIVE | nawk
'{ print $6 }'`
        if [ "$NUMFREE" -lt "$LLIMIT" ];
then
            echo There is only $NUMFREE K
free on $HOSTNAME:$MYDRIVE | \
            mailx -s "Urgent: freespace on
$HOSTNAME" $MAILTO
        fi
    else
        # Get the % used of the drive
        let NUMFREE=`df -k | grep $DRIVE |
nawk '{ print $5 }' | cut -f1 -d\%`
        MYDRIVE=`df -k | grep $DRIVE | nawk
'{ print $6 }'`
        if [ "$DRVPCT" -lt "$NUMFREE" ];
then
            let TOTFREE="100-$NUMFREE"
            echo There is only $TOTFREE %
free on $HOSTNAME:$MYDRIVE | \
            mailx -s "Urgent: freespace on
$HOSTNAME" $MAILTO
        fi
    fi

# Check the next drive in the list
done < $THRESHOLD

/usr/local/etc/threshold_list:
/ 95%
/var 131282

Script 5
--------

#!/bin/sh

# temporary working file
USERLIST=/tmp/userlist$$

# WARNSIZE is the high water mark  in KB before we send an e-mail
# in this case 1GB
WARNSIZE=`expr 1024 \* 1024`

# check whether we're using NIS or /etc/passwd
# needs to be modified to use niscat if NIS+ is being used
/usr/bin/ypwhich > /dev/null 2>&1
if [ $? -eq 0 ]
then
        /usr/bin/ypcat passwd | /usr/bin/awk -F\: '{print $1 ":" $6}' >
$USERLIST
else
        awk -F\: '{print $1 ":" $6}' /etc/passwd > $USERLIST
fi

# loop through each user in $USERLIST and check his or her disk usage
for USER in `cat $USERLIST`
do
        NAME=`echo $USER | /usr/bin/awk -F\: '{print $1}'`
        HOMEDIR=`echo $USER | /usr/bin/awk -F\: '{print $2}'`
        SIZE=`/usr/bin/du -ks $HOMEDIR | /usr/bin/awk '{print $1}'`
        if [ $SIZE -gt $WARNSIZE ]
        then
                /usr/bin/mailx -s "You have reached $WARNSIZE Kilobytes of
disk
usage" $NAME << !!
Dear ${NAME},

Please delete unneccesary files as you are using $SIZE Kilobytes of disk
space
in $HOMEDIR.

Thanks,
System Admin
!!
        fi
done
/usr/bin/rm -f $USERLIST

Script 6
--------

#!/bin/sh
#
# Check various system parameters and send an e-mail if there are problems #
Dave Tarbatt 23/07/2000 # Version 1.0 # Version 1.1 -- added check for
inodes # Version 1.2 -- added autodetect non-Linux nodes (for df -i) # #
Global parameters
EMAIL="steven.briggs at autotrader.co.uk, robert.reynolds at autotrader.co.uk"
# where to send errors to
HOSTNAME=`/bin/hostname` # what host am I
UNAME=`uname` # what type of system am I
ERRORMESSAGE= # variable to hold e-mail body

# CHECK#1 parameters -- low disk/inode space check
# Define list of partitions and their upper percentage threshold
PARTITIONSPACE="/dev/dsk/c0t0d0s0=10"
INODESPACE="/dev/dsk/c0t0d0s0=80"
CHECK1ERROR= # variable to hold check#1 e-mail body

# CHECK#2 parameters -- monitored process check
# Define list of processess that should always be running PROCESSES="sshd
httpd"
CHECK2ERROR= # variable to hold check#2 e-mail body

############################################################################
###
# CHECK#1 -- low disk/inode space check
############################################################################
###
DATE=`/bin/date` # start time of check
for DFDATA in `/bin/df -k|grep -v "Filesystem|proc"|sed "s/%//g"|awk '{print
$1"="$4"="$5"="$6}'` ; do
    DFDEVICE=`echo $DFDATA | cut -f1 -d=` # get device name
    DFKSPACE=`echo $DFDATA | cut -f2 -d=` # get space in KB
    DFPSPACE=`echo $DFDATA | cut -f3 -d=` # get space in %
    DFMOUNT=`echo $DFDATA | cut -f4 -d=` # get the mount point

    for PARTITION in $PARTITIONSPACE ; do
        DEVICE=`echo $PARTITION | cut -f1 -d=` # get device name to
check
        DEVICETHRESH=`echo $PARTITION | cut -f2 -d=` # get device
threshold
        if [ $DFDEVICE = $DEVICE ] ; then
            if [ $DFPSPACE -ge $DEVICETHRESH ] ; then
                CHECK1ERROR=$CHECK1ERROR`echo "\n$DFDEVICE mounted on
$DFMOUNT only $DFKSPACE KB free ($DFPSPACE% full)"`
            fi
        fi
    done
done

for DFDATA in `/bin/df -k|grep -v "Filesystem|proc"|sed "s/%//g"|if [ $UNAME
= "SunOS" ] ; then awk '{print $1"="$4"="$5"="$6}' ; else awk '{print
$1"="$7"="$8"="$9}' ; fi` ; do
    DFDEVICE=`echo $DFDATA | cut -f1 -d=`       # get device name
    DFISPACE=`echo $DFDATA | cut -f2 -d=`       # get inodes available
    DFPSPACE=`echo $DFDATA | cut -f3 -d=`       # get inodes in %
    DFMOUNT=`echo $DFDATA | cut -f4 -d=`        # get the mount point

    for INODE in $INODESPACE ; do
        DEVICE=`echo $INODE | cut -f1 -d=`          # get device name to
check
        DEVICETHRESH=`echo $INODE | cut -f2 -d=`    # get device threshold
        if [ $DFDEVICE = $DEVICE ] ; then
            if [ $DFPSPACE -ge $DEVICETHRESH ] ; then
                CHECK1ERROR=$CHECK1ERROR`echo  "\n$DFDEVICE mounted on
$DFMOUNT only $DFISPACE free inodes ($DFPSPACE% used)"`
            fi
        fi
    done
done

if [ "$CHECK1ERROR" != "" ] ; then
    ERRORMESSAGE=$ERRORMESSAGE`echo  "\n\nLOW DISK SPACE WARNING ($DATE):
$CHECK1ERROR"` fi

############################################################################
###
# CHECK#2 -- monitored process check
############################################################################
###
DATE=`/bin/date` # start time of check
for PROCESS in $PROCESSES ; do
    /bin/ps -aefuwww|grep -v grep|grep $PROCESS > /dev/null
    if [ $? -ne 0 ] ; then
        CHECK2ERROR=$CHECK2ERROR`echo  "\nMonitored process $PROCESS is not
running"`
    fi
done

if [ "$CHECK2ERROR" != "" ] ; then
    ERRORMESSAGE=$ERRORMESSAGE`echo  "\n\nMONITORED PROCESSES ERROR ($DATE):
$CHECK2ERROR"` fi

############################################################################
###
# End of checks. If there were any problems, e-mail them
############################################################################
###
if [ "$ERRORMESSAGE" != "" ] ; then
    echo  "$ERRORMESSAGE"|mail "HealthCheck: $HOSTNAME problems" $EMAIL
fi

----------

Regards
----
Mark .I. Mahabir (XMM SSC Computer Operator)
Dept of Physics & Astronomy,
University of Leicester,            Phone  +44 116 252 5652
Leicester, LE1 7RH,  U.K.           Fax    +44 116 252 3311
_______________________________________________
sunmanagers mailing list
sunmanagers at sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



More information about the summaries mailing list