LinHES Forums
http://forum.linhes.org/

Email me when root disk usage exceeds a specified percentage
http://forum.linhes.org/viewtopic.php?f=3&t=18153
Page 1 of 1

Author:  Kirk [ Tue Apr 08, 2008 12:07 am ]
Post subject:  Email me when root disk usage exceeds a specified percentage

A script to send yourself an email if the remaining root disk space falls below your comfort level. Typically one would set this up as a cronjob. Uses sendEmail, which has been discussed here before.

Code:
#!/bin/bash

# Email me when root disk usage exceeds a specified percentage.

SENDER=mythtv@my.house
RECIPIENT=emailme@my.work
SMTP_SERVER=mail.my.isp
ALERT_PERCENTAGE=90        # 90% disk used, ~500mb remaining on stock install

HOSTNAME=`hostname`

REMAINING=`df / |awk '{print $5}' |tail -1 |sed s/%//`

if [ $ALERT_PERCENTAGE -lt $REMAINING ]; then
        df -h | sendEmail -f $SENDER -t $RECIPIENT -s $SMTP_SERVER -u "$HOSTNAME has ${REMAINING}% root disk space remaining."
fi

Author:  mjl [ Tue Apr 08, 2008 6:41 am ]
Post subject: 

Hi,

Handy idea but to save postage, an osd alert works also. I have found any "anomaly" perceived or real is soon followed with a phone call from my wife :) I am sure an osd to the fact of something needs attention would invoke a similar phone call!

Mike

Author:  Kirk [ Tue Apr 08, 2008 5:31 pm ]
Post subject: 

Not a bad idea. I'll add that in shortly.

Author:  mythedoff [ Tue Apr 08, 2008 9:37 pm ]
Post subject: 

Thanks for the script.

Perhaps I'm misunderstanding, but by root usage, do you mean /.

If so then I'm getting an incorrect response from the from the df ... portion.

The tail command gives the last result only which in my case sda1 is for /myth/video.

I changed it a bit:

Code:
df /dev/[h,s]da1 | sed -n '/\/$/p' | awk '{print $5}' | sed s/%//

Author:  Kirk [ Tue Apr 08, 2008 11:02 pm ]
Post subject: 

Thanks mythedoff,

Yeah, I mean /

Looking back I don't know why I didn't just do 'df /' That should fix all cases.

Regards,
Kirk.

Author:  mjl [ Wed Apr 09, 2008 9:56 am ]
Post subject: 

Hi,

Taking the email sample, with a "couple of small changes", this works for an alert. Maybe have cron run it once an hour..
Code:
#!/bin/sh
# Drive space warning
#----------- adjust for monitor or tv--------------
lsmod |grep "ivtvfb "|cut -b1-6 >/tmp/tv
if [ "$(cat /tmp/tv)" = "ivtvfb" ]; then
fontsize="25"
fontcolor="lightyellow"
else
fontsize="34"
fontcolor="yellow"
fi
export DISPLAY=:0
export FONT="-adobe-helvetica-bold-*-*-*-$fontsize-*-*-*-*-*-*-*"
#-------------------------------------------------------------------------------
ALERT_PERCENTAGE=70        # 90% disk used, ~500mb remaining on stock install

HOSTNAME=`hostname`

REMAINING=`df / |awk '{print $5}' |tail -1 |sed s/%//`

if [ $ALERT_PERCENTAGE -lt $REMAINING ]; then
        date > /tmp/tts
        echo "$HOSTNAME has ${REMAINING}% root disk space remaining." >>/tmp/tts
        cat /tmp/tts | osd_cat --l=8 --delay=6 --font=$FONT --shadow=3  --color=$fontcolor  --pos=bottom$
fi


I just borrowed pieces from some of my other osd scripts.
Mike

Author:  Kirk [ Wed Apr 09, 2008 2:50 pm ]
Post subject: 

Ah, beat me to it.

Good one mjl :D

Author:  Too Many Secrets [ Wed Apr 09, 2008 3:11 pm ]
Post subject: 

Personally, I like the email... to each...

Author:  mjl [ Wed Apr 09, 2008 4:47 pm ]
Post subject: 

Hi,

We can make it optional :) I think this should work as I just borrowed some of Kirk's code and only re-arranged it a little.

Code:
#!/bin/sh
# Drive space warning script
#-------------------------------------------------------------
lsmod |grep "ivtvfb "|cut -b1-6 >/tmp/tv
if [ "$(cat /tmp/tv)" = "ivtvfb" ]; then
fontsize="25"
fontcolor="lightyellow"
else
fontsize="34"
fontcolor="yellow"
fi
export DISPLAY=:0
export FONT="-adobe-helvetica-bold-*-*-*-$fontsize-*-*-*-*-*-*-*"
#----------------------------------------------------------------
email=0                    # 1 or greater to enable email
show=1                     # 1 to show osd
date > /tmp/tts             # Nice to know when things happen
ALERT_PERCENTAGE=90        # 90% disk used, ~500mb remaining on stock install?
HOSTNAME=`hostname`
RREMAINING=`df / |awk '{print $5}' |tail -1 |sed s/%//`     # root status
MREMAINING=`df /myth |awk '{print $5}' |tail -1 |sed s/%//` # myth status

# root
if [ $ALERT_PERCENTAGE -lt $RREMAINING ]; then
        echo "WARNING, $HOSTNAME has ${RREMAINING}% root disk space remaining." >>/tmp/tts
        # bump flag
        email="$(($email + 1 ))"
        show="$(($show + 1 ))"
fi

ALERT_PERCENTAGE=85        # 85% myth partition used
REMAINING=`df /myth |awk '{print $5}' |tail -1 |sed s/%//`

# myth
if [ $ALERT_PERCENTAGE -lt $MREMAINING ]; then
        echo "$HOSTNAME has ${MREMAINING}%  disk space remaining for shows." >>/tmp/tts
        # bump flag
        #email="$(($email + 1 ))"
        #show="$(($show + 1 ))"
fi

# show it from here if flagged
if [ $show -gt "1" ]; then
        cat /tmp/tts | osd_cat --l=8 --delay=6 --font=$FONT --shadow=3  --color=$fontcolor  --pos=bottom --align=centre &
fi

# email it from here if flagged
if [ $email -gt "1" ]; then
#       Email me when root disk usage exceeds a specified percentage.
        SENDER=mythtv@my.house
        RECIPIENT=emailme@my.work
        SMTP_SERVER=mail.my.isp
        df -h | sendEmail -f $SENDER -t $RECIPIENT -s $SMTP_SERVER -u "$HOSTNAME has ${RREMAINING}% root disk space remaining."
        email=0
fi

#done

Just by setting switches you can have either, both or none.

Mike
Fixed a typo, actually a couple lines got truncated on me :( also watch for line wrapping if copy / paste

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/