LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-23-2008, 06:18 PM   #1
myint45
LQ Newbie
 
Registered: Aug 2006
Posts: 10

Rep: Reputation: 0
Cool 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
 
Old 01-23-2008, 06:49 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,014
Blog Entries: 54

Rep: Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763
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.

Last edited by unSpawn; 01-23-2008 at 06:53 PM.
 
Old 01-23-2008, 07:07 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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)
  }
'
 
Old 01-24-2008, 02:38 PM   #4
myint45
LQ Newbie
 
Registered: Aug 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Smile good stuff.

Quote:
Originally Posted by ghostdog74 View Post
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?
 
Old 01-24-2008, 02:40 PM   #5
myint45
LQ Newbie
 
Registered: Aug 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Post thanks but I can't read /bin/sh

Quote:
Originally Posted by unSpawn View Post
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?
 
Old 01-24-2008, 05:07 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
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
 
Old 01-24-2008, 08:26 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by myint45 View Post
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 ...
 
  


Reply

Tags
linux, scripting, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check disk space used in a directory cli_man Linux - General 5 03-13-2009 03:28 PM
How do you check disk space? verlaine Linux - Software 4 11-12-2006 01:57 PM
How do you check disk space? tim1348 Linux - Newbie 10 10-31-2006 09:39 AM
How to check free disk space TigerLinux Linux - Newbie 9 10-04-2005 05:04 PM
Yum - check for disk space maimonoded Linux - Software 0 03-31-2005 09:23 AM


All times are GMT -5. The time now is 04:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration