Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to notify by mailx if disk is almost full 3

Status
Not open for further replies.

IThead

MIS
Sep 6, 2002
102
US
Hi,
Does anyone has any script that can check the disk usage by percentage? if any disk that is >90% (using df -k) will email an alert.

Thanks
 
What have you so far ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hope this helps, may need to adjust to suit your OS, this works on RH Linux:

#!/bin/ksh
# ========================================================
#
# Check file system for disk full and sends messages
#
# ========================================================
# logs to /tmp
# ========================================================

# Parameters required

HOST=`hostname -s`
LIST="home var usr"
SYSTIME=`date +"%OH:%OM"`
LEVEL1=80
LEVEL2=90
LEVEL3=95
RESET=79

# Check for each defined FS in LIST

for i in $LIST
do

XXX=`df | grep $i|sed '/tmp/q'|sed -e 's/%//'|awk '{print $5}'`
LAST=`awk '{print $1}' /tmp/$i.last`

# send message level 1

if [ "$XXX" -ge "$LEVEL1" ] && [ "$LAST" -lt "$LEVEL1" ]
then
echo "$SYSTIME : $HOST /$i $LEVEL1% Full!"|mail -s $HOST name@domain.com
echo $LEVEL1 > /tmp/$i.last
fi

# send message level 2

if [ "$XXX" -ge "$LEVEL2" ] && [ "$LAST" -eq "$LEVEL1" ]
then
echo "$SYSTIME : $HOST /$i $LEVEL2% Full!"|mail -s $HOST name@domain.com
echo $LEVEL2 > /tmp/$i.last
fi

# send message level 3

if [ "$XXX" -ge "$LEVEL3" ] && [ "$LAST" -eq "$LEVEL2" ]

then
echo "$SYSTIME : URGENT! $HOST /$i $XXX% Full!"|mail -s $HOST name@domain.com
echo $LEVEL3 > /tmp/$i.last
fi
#
# reset last
#
if [ "$XXX" -lt "$RESET" ]
then
echo $XXX > /tmp/$i.last
fi

done

# Special hack for root FS checking below

ROOT=`df|grep /dev/sda7|sed -e 's/%//'|awk '{print $5}'`
LAST=`awk '{print $1}' /tmp/root.last`

# send message level 1

if [ "$ROOT" -ge "$LEVEL1" ] && [ "$LAST" -lt "$LEVEL1" ]
then
echo "$SYSTIME : $HOST Root FS $LEVEL1% Full!"|mail -s $HOST name@domain.com
echo $LEVEL1 > /tmp/root.last
fi

# send message level 2

if [ "$ROOT" -ge "$LEVEL2" ] && [ "$LAST" -eq "$LEVEL1" ]
then
echo "$SYSTIME : $HOST Root FS $LEVEL2% Full!!"|mail -s $HOST name@domain.com
echo $LEVEL2 > /tmp/root.last
fi
# send message level 3

if [ "$ROOT" -ge "$LEVEL3" ] && [ "$LAST" -eq "$LEVEL2" ]
then
echo "$SYSTIME : URGENT! Root FS $ROOT% Full!!"|mail -s $HOST name@domain.com
echo $ROOT > /tmp/root.last
fi

# reset last

if [ "$ROOT" -lt "$RESET" ]
then
echo $LEVEL3 > /tmp/root.last
fi



IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
MS Certified Windblows Rebooter
 
aixmurderer,
I try make it simple by doing the following code, but the $i did not work in xx=`df -k |awk '/\/$i/{print substr($5,1,length($5)-1)}'` any idea?

Thanks

#!/bin/ksh
LIST="arch redo backup"
for i in $LIST
do
xx=`df -k |awk '/\/$i/{print substr($5,1,length($5)-1)}'`
echo $xx
if test $xx -gt 60
then
echo "/oracle/arch is at $xx%" | mailx -r root@newport.com -s "Space alert!"
fi
done
 
it works for me:

Code:
df -k | grep ^/| sed 's/\%//g' | awk '{if ($5 > 90) {print $6","$5"%"}}' >> $Temp

# Check whether some filesystems meet the criteria:

fs_count="`cat $Temp | wc -l`"

if [ $fs_count -gt 0 ]
then
  echo " " >> $EMail
  echo "##-----------------------------------" >> $EMail
  echo "## Espacios en filesystems:" >> $EMail
  echo "##-----------------------------------" >> $EMail
  echo " " >> $EMail
  echo "The following filesystems are over 90%:" >> $EMail
  cat $Temp | sed -e 's/,/ /' |awk '{print "Filesystem \"" $1 "\" is " $2 }' >> $EMail
  
  cat $EMail | mailx -s "Alertas sobre servidor * $server *" $a
fi
rm $Temp
rm $EMail
 
IThead,

The example below is a slight variation of your script.

Code:
#!/usr/bin/ksh

LIST="/arch /redo /backup"

for i in $LIST
do
xx=`df -k $i |nawk '/^\//{print substr($5,1,length($5)-1)}'`
if test $xx -gt 60
then
echo "$i is at $xx - Please investigate" | mailx -r root@newport.com -s "Space alert! - $i on `uname -n` is at $xx %"  
fi
done

Thanks,

John
 
Thanks guys... all solutions work
but johngiggs solution work best for me, thanks again!
 
Hi,

I have tried PHV's FAQ219-2884 and also johngiggs solution.

----------------
I have the following error with PHV's faq script.
mfsys.sh[14]: allocation: The specified number is not valid for this command.

Which is
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:

in my script.
----------------

Next, I tried johngiggs' script. First of all, I don't have nawk, so I substituted awk for nawk. I am getting the following error

The flags you gave are used only when sending mail.
The flags you gave are used only when sending mail.

Code:
#!/usr/bin/sh

LIST="/arch /redo /backup"

for i in $LIST
do
xx=`df -k $i |nawk '/^\//{print substr($5,1,length($5)-1)}'`
if test $xx -gt 60
then
echo "$i is at $xx - Please investigate" | mailx -r xxx@yyy.com -s "Space alert! - $i on `uname -n` is at $xx %"  
fi
done

Can you help me plz? Thanks.

 
Sorry, this is the exact code

Code:
#!/usr/bin/sh
 
LIST="/ /home"
 
for i in $LIST
do
xx=`df -k $i |awk '/^\//{print substr($5,1,length($5)-1)}'`
if test $xx -gt 90
then
echo "$i is at $xx - Please investigate" | mailx -r xxx@yyy.com -s "Space alert! - $i on `uname -n` is at $xx %"
fi
done
 
replace:
mailx -r xxx@yyy.com -s "Space alert! - $i on `uname -n` is at $xx %"

with:
mailx -s "Space alert! - $i on `uname -n` is at $xx %" xxx@yyy.com
 
----------------
I have the following error with PHV's faq script.
mfsys.sh[14]: allocation: The specified number is not valid for this command.

Which is
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:

in my script.
----------------

Could someboy plz look into it?
 
The FAQ link in my sig isn't a script !
Can you post the script mfsys.sh ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Here's the script that I am using,

Code:
#!/usr/bin/sh
#
#@(#) Check filesystem space -
#@(#)    used to automate the monitoring of filesystems to
#@(#)    confirm that they do not pass a pre-defined threshold.
#
#  This job is set up in cron to run on a regular basis.
#  It monitors space in directories, making sure that free space
#  does not fall below pre-defined thresholds.
#

export DATE=`date +%m%d`
export TMP="/tmp/.chkfilspc.$DATE"
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:
export HOSTNAME=`hostname`
export PROGRAM=$0
export PAGE='/usr/local/bin/sendpage -n'

# If you are using this under AIX 3.2.5, leave DF='df', but on 
# an AIX 4 system you will have to change it to DF='df -k'
export DF='df -k'

function CHK_FILE_SPC
{
export FILESYS=$1

if [ "$3" = "%" ]
   then
      export AMOUNT=`$DF $FILESYS | tail -1 |          awk -v PERCENT=$2  '{print $2*PERCENT/100/1024}'`
   else
      export AMOUNT=$2
fi

integer FREE_SPACE LOW_WATER_MARK

FREE_SPACE=`$DF $FILESYS | tail -1 | awk '{print $3}'`
(( LOW_WATER_MARK = $AMOUNT * 1024 ))

if (( $FREE_SPACE < $LOW_WATER_MARK))
   then
      (( MB_FREE = $FREE_SPACE / 1024 ))
      FILE_NAME="$TMP`echo $FILESYS | sed 's/\//\./g'`"
      if [ ! -s $FILE_NAME ]
         then

#  If the log file doesn't exist, then check the filesystem one more 
#  time before paging the administrator
            RE_CHK_FILE_SPC
         else

#  If the log file exists, then just log that it was still full at this
#  time, but don't do any more (unless /tmp is the file that's full and
#  the log could not be created.
            date >> $FILE_NAME
      fi
fi
}

function RE_CHK_FILE_SPC
{
integer FREE_SPACE LOW_WATER_MARK
FREE_SPACE=`$DF $FILESYS | tail -1 | awk '{print $3}'`
(( LOW_WATER_MARK = $AMOUNT * 1024 ))

if (( $FREE_SPACE < $LOW_WATER_MARK))
   then
      (( MB_FREE = $FREE_SPACE / 1024 ))
      FILE_NAME="$TMP`echo $FILESYS | sed 's/\//\./g'`"
      MESSAGE="Only $MB_FREE MB free in filesystem - $FILESYS - on $HOSTNAME" 

#  Comment out the next line if you do not have any paging software 
#  installed.
#      echo "$MESSAGE" | $PAGE UNIX_CRITICAL

#  The following lines open the log file and save the message. This
#  file is used to check against each time the script is run. If
#  the log file exists then it will not page again, until after the
#  date changes
      date > $FILE_NAME
      echo "$MESSAGE" >> $FILE_NAME

#  The following lines will be sent to the mailbox of the root user.
#      echo "PROGRAM: \t $PROGRAM\n"
#      echo "LOG FILE:\t $HOSTNAME:$FILE_NAME\n"
#      echo "MESSAGE: \t $MESSAGE\n"
#      echo "PAGED:   \t UNIX_CRITICAL"
fi
}
############### MAIN
#
#  The format of the following lines - are as follows:
#     FUNCTION_NAME  filesystem  amount_free  [%]
#
#  Examples:
#
#     CHK_FILE_SPC /tmp 10  
#               - check /tmp for 10 MB of free space
#
     CHK_FILE_SPC / 10 %
#               - check / to see that there is 40 % of free space

 
You use ksh style of export and the first line of your script insists for sh.
Let run this script in ksh as many lines in it are written with korn syntax.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi, I had this question before also.... I have bourne shell on HP-UX. Can I run a script as ksh? I replaced sh with ksh. Still gives the same error. I don't have ksh available.
 
AFAIK, the HP-UX shell is POSIX compliant, so this may be OK.
Try to put a set -x before the export lines to figure out where the script fails.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Here is the error,

+ date +%m%d
+ export DATE=0325
+ export TMP=/tmp/.chkfilspc.0325
+ export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:
+ hostname
+ export HOSTNAME=fhnjdms
+ export PROGRAM=mfsys.sh
+ export PAGE=/usr/local/bin/sendpage -n
+ export DF=df -k
+ CHK_FILE_SPC / 10 %
mfsys.sh[14]: allocation: The specified number is not valid for this command.

Thanks
 
change the function call

from:
CHK_FILE_SPC / 10 %

to:
CHK_FILE_SPC '/' '10' '%'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi,

+ CHK_FILE_SPC / 10%
mfsys.sh[14]: allocation: The specified number is not valid for this command.

I am getting the same error
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top