LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   disk space check (https://www.linuxquestions.org/questions/programming-9/disk-space-check-615849/)

myint45 01-23-2008 06:18 PM

disk space check
 
Hi,

I'm new to scripting so I'm trying to find a script that'll check disk space for let's say something above 65% usage. If that's the case then send me an e-mail.

And possibly go out and clean the dir.

Thanks in advance for any help you guys can give me.


L.M

unSpawn 01-23-2008 06:49 PM

This is just a generic idea.
Code:

#!/bin/sh --
# /etc/cron.hourly/diskusage.cron
# Error out
set -e
# Percentage threshold:
THRESH="75"
# E-mail recipient:
RECIPIENT="root@localhost"
# Tempfile location:
TEMPF="/dev/shm/diskusage.cron.$$"
# Make a report:
( /bin/df -mh 2>/dev/null | grep ^/ | while read partition size used available percentage mountpoint; do
  [ ${percentage//\%/} -ge $THRESH ] && echo "$mountpoint full $percentage"; done | column -t ) > "$TEMPF"
# If it contains stuff send it off:
if [ -s "$TEMPF" ]; then
 cat "$TEMPF" |  mail -s "${HOSTNAME} [WARN] (${THRESH}%)" $RECIPIENT
fi; rm -f "$TEMPF"
exit 0

Using something like Monit would make things more efficient since it's faster more versatile.
BTW I'd be against automagical deletion, unless it's a tempdir.
In that case you don't want a script but run "tmpwatch" instead.

* I've been lazy and used a temporary file for pretty printing instead of an array. Besides that this script doesn't handle exclusions which it should. Aww well.

ghostdog74 01-23-2008 07:07 PM

Code:

df -k | awk 'int($5) > 95 {
  subject = $1 " More than 95% disk usage "
  email = "email@test.com"
  cmd = "mailx -s \"" subject "\" " email
  cmd  | getline #or system(cmd)
  close(cmd)
  }
'


myint45 01-24-2008 02:38 PM

good stuff.
 
Quote:

Originally Posted by ghostdog74 (Post 3033192)
Code:

df -k | awk 'int($5) > 95 {
  subject = $1 " More than 95% disk usage "
  email = "email@test.com"
  cmd = "mailx -s \"" subject "\" " email
  cmd  | getline #or system(cmd)
  close(cmd)
  }
'


df -k | awk 'int($5) > 95 === can I do df -mh and is int($5) going to make 45% just 45?


cmd | getline #or system(cmd) === what is this line doing?

myint45 01-24-2008 02:40 PM

thanks but I can't read /bin/sh
 
Quote:

Originally Posted by unSpawn (Post 3033171)
This is just a generic idea.
Code:

#!/bin/sh --
# /etc/cron.hourly/diskusage.cron
# Error out
set -e
# Percentage threshold:
THRESH="75"
# E-mail recipient:
RECIPIENT="root@localhost"
# Tempfile location:
TEMPF="/dev/shm/diskusage.cron.$$"
# Make a report:
( /bin/df -mh 2>/dev/null | grep ^/ | while read partition size used available percentage mountpoint; do
  [ ${percentage//\%/} -ge $THRESH ] && echo "$mountpoint full $percentage"; done | column -t ) > "$TEMPF"
# If it contains stuff send it off:
if [ -s "$TEMPF" ]; then
 cat "$TEMPF" |  mail -s "${HOSTNAME} [WARN] (${THRESH}%)" $RECIPIENT
fi; rm -f "$TEMPF"
exit 0

Using something like Monit would make things more efficient since it's faster more versatile.
BTW I'd be against automagical deletion, unless it's a tempdir.
In that case you don't want a script but run "tmpwatch" instead.

* I've been lazy and used a temporary file for pretty printing instead of an array. Besides that this script doesn't handle exclusions which it should. Aww well.

grep ^/ | while read partition size used available percentage mountpoint; == what's happening after the while statment?

chrism01 01-24-2008 05:07 PM

He's using the while syntax:

Code:

while sometest
do 
    some stuff
done

in this case
do
check usage vs theshold, if thresh exceeded, output fact
done
pipe results through tab separators, redirect formatted result to tempfile


HTH

ghostdog74 01-24-2008 08:26 PM

Quote:

Originally Posted by myint45 (Post 3034075)
df -k | awk 'int($5) > 95 === can I do df -mh and is int($5) going to make 45% just 45?

yes. Just do a little test on your command line :
Code:

# df -k | awk 'int($5) > 50'
and see what happens.

Quote:

cmd | getline #or system(cmd) === what is this line doing?
it is going to send email ...


All times are GMT -5. The time now is 03:17 AM.