LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2008, 11:56 AM   #1
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Rep: Reputation: 15
Question I need help writing script to monitor the hard disk space


-Hello,

I was wondering if some one here kind a enough to help me whip up this Linux script to monitor the local hard disk space for shortage. I would like the script to text me on my pager if the disk space reached the threshold.

Please help a newbie.

Thank you in advance.

xmdms
 
Old 09-02-2008, 02:08 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Do you have any (pseudo) script lines you could show? That way it's easier to help and correct. If not then you could familiarise yourself with 'du' output and how to access a pager (e-mail) and then whip up some (pseudo) script lines?
 
Old 09-02-2008, 02:13 PM   #3
misconfiguration
Member
 
Registered: Apr 2007
Location: Indianapolis, Indiana
Distribution: RHEL, Fedora, AIX, HP-UX, FreeBSD, Slackware
Posts: 62

Rep: Reputation: 19
Do you have any language preferences? ksh, bash, Perl etc?
 
Old 09-02-2008, 02:24 PM   #4
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
I think there are already applications that can do this for you. So you may want to search around for them. However, if you really want to do it yourself, it is pretty straightforward:

I'm not going to write the script for you but I have some ideas where you can start.

First, 'df' command will give you listing like this:
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             41286796   8640904  30548608  23% /
udev                   2030404       152   2030252   1% /dev
/dev/sda2             35633880  10203604  23620156  31% /home
/dev/dm-0            240278024    222556 227850004   1% /export
/dev/sdd2             72785240  16575448  52512436  24% /vmware
You would be interested in the 'Use%' column. Since you know what your local hard drives are listed as, grep the 'df' output for the drives you care about. From there, parse out the 'Use%' column and see if it has reached your specified threshold. If it has, send a text message to your phone. In order to send text message, the easiest way would be to use 'mail' command to sned info to an e-mail address that forwards its mail as text messages to your phone. Your wireless provider might support this or there are 3rd party sites that will do this for you.

To get it to 'monitor', set up a cron job to run the script however often you need to (every 5-15min would probably be fine, it's up to you).

Good Luck!

Last edited by CRC123; 09-02-2008 at 02:25 PM.
 
Old 09-03-2008, 08:25 AM   #5
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by CRC123 View Post
I think there are already applications that can do this for you. So you may want to search around for them. However, if you really want to do it yourself, it is pretty straightforward:

I'm not going to write the script for you but I have some ideas where you can start.

First, 'df' command will give you listing like this:
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             41286796   8640904  30548608  23% /
udev                   2030404       152   2030252   1% /dev
/dev/sda2             35633880  10203604  23620156  31% /home
/dev/dm-0            240278024    222556 227850004   1% /export
/dev/sdd2             72785240  16575448  52512436  24% /vmware
You would be interested in the 'Use%' column. Since you know what your local hard drives are listed as, grep the 'df' output for the drives you care about. From there, parse out the 'Use%' column and see if it has reached your specified threshold. If it has, send a text message to your phone. In order to send text message, the easiest way would be to use 'mail' command to sned info to an e-mail address that forwards its mail as text messages to your phone. Your wireless provider might support this or there are 3rd party sites that will do this for you.

To get it to 'monitor', set up a cron job to run the script however often you need to (every 5-15min would probably be fine, it's up to you).

Good Luck!

I would prefer bash script. Would you guys have a sample script that I can examing and modify from there?? So far, this is what I have;

#!/bin/bash
RECIPENTS="your_name@dot.com"
df -h


I need serious help.

Thanks,
 
Old 09-03-2008, 08:33 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
crc123's description is good. You just need to start putting it together, using these:

http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://www.adminschoice.com/docs/cro...Crontab%20file

Have a crack at it and come back if you get stuck.

Note that only you can decide which SMS/Pager system to use, and you have to get the relevant sw and read the docs.

Last edited by chrism01; 09-03-2008 at 08:34 AM.
 
Old 09-03-2008, 08:54 AM   #7
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by xmdms View Post
I would prefer bash script. Would you guys have a sample script that I can examing and modify from there?? So far, this is what I have;

#!/bin/bash
RECIPENTS="your_name@dot.com"
df -h


I need serious help.

Thanks,
All the recommendations on this thread will work in bash. I strongly suggest reading the links that chrism01 posted. It's a good start. I don't think anyone will have any 'example' scripts that do what you're looking for. The commands you need are:

df: to get disk usage info
grep: to find lines that contain words/phrases you pass in
awk or sed: to parse the lines to find specific info on them (percentage)
mail: to mail to your phone/pager
cron: to run automatically

if you don't feel like reading through those pages, you use the 'man' command to read the manual pages of any of these programs. Use 'man man' if you don't know how to use man. lol
 
Old 09-03-2008, 09:41 AM   #8
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by CRC123 View Post
All the recommendations on this thread will work in bash. I strongly suggest reading the links that chrism01 posted. It's a good start. I don't think anyone will have any 'example' scripts that do what you're looking for. The commands you need are:

df: to get disk usage info
grep: to find lines that contain words/phrases you pass in
awk or sed: to parse the lines to find specific info on them (percentage)
mail: to mail to your phone/pager
cron: to run automatically

if you don't feel like reading through those pages, you use the 'man' command to read the manual pages of any of these programs. Use 'man man' if you don't know how to use man. lol
Using it and finding great things!!

Here's what I have and it's working;


RECIPENTS="xmdms@yahoo.com"
typeset -i error="92"
if [ -e \tmp\space.out ]; then
rm \tmp\space.out
fi
for disk in `mount | egrep '^/dev' | egrep -iv 'cdrom|proc|sys|pts' |awk '{print $3}'`
do
typeset -i diskUsage=`df -h $disk|cut -c40-42|grep -i [^a-z]`
if [ "$diskUsage" -ge "$error" ]; then
echo "Disk usage $disk exceeded the threshold of $diskUsage%" >> \tmp\space.txt
fi
done
if [ -e \tmp\space.out ]; then
message=`cat \tmp\space.out`
fi
if [ ${#message} -gt 0 ]; then
cat \tmp\space.out | mail -s "Disk Usage Report for: $HOSTNAME" $RECIPENTS
fi

I do get the e-mail stating the threshold been exceeded. However, I get one single text line displayed for multiple devices in question. So, how do I add a tab or break line in my email? Example below:

Currently:
Disk usage /u01 exceeded the threshold of 96% Disk usage /u02 exceeded the threshold of 92%

I would like to be:
Disk usage /u01 exceeded the threshold of 96% Disk usage
/u02 exceeded the threshold of 92%

Thanks much!!!
 
Old 09-03-2008, 10:07 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I'd use the -s switch to check the file size: http://www.tldp.org/LDP/abs/html/fto.html
The newline thing is odd. A similar check on my box locally works ok. Maybe its a yahoo thing?
 
Old 09-03-2008, 10:12 AM   #10
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by chrism01 View Post
A similar check on my box locally works ok. Maybe its a yahoo thing?
Same here; I mail from my linux box to gmail account and it picks up the newlines fine. For now, maybe put a seperating character(s) like '-' '+' or something like that.

Also, great job reading and LEARNING .
 
Old 09-03-2008, 11:36 AM   #11
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by CRC123 View Post
Same here; I mail from my linux box to gmail account and it picks up the newlines fine. For now, maybe put a seperating character(s) like '-' '+' or something like that.

Also, great job reading and LEARNING .
I will look into another way to feed some line breaks - I will post my results once I find a way.

Many thanks!!
 
Old 09-03-2008, 03:28 PM   #12
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Original Poster
Rep: Reputation: 15
Okay, let's make this more interesting or I might say more technical challenge....Well, maybe to me (-:

Ok, after I have the script all worked out. Now, I would like to run this script from one (1) main server to monitor my three (3) other Linux servers. How or where would I go about remote monitoring between Linux servers??

Again, thanks for all your help and wisdom.

Jorge
 
Old 09-03-2008, 03:33 PM   #13
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Use 'ssh' to run the commands on remote computers. However, to get it to work in scripts, you will have to set it up to do secure passwordless logins. Google 'ssh without password'.
 
Old 09-03-2008, 06:01 PM   #14
xmdms
Member
 
Registered: Oct 2003
Posts: 134

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by CRC123 View Post
Use 'ssh' to run the commands on remote computers. However, to get it to work in scripts, you will have to set it up to do secure passwordless logins. Google 'ssh without password'.
Okay, I will google 'ssh without password' - In the meantime, can you give me a hint on how the syntax should look like??

~Many thanks!!
 
Old 09-03-2008, 06:35 PM   #15
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Smile

Quote:
Originally Posted by xmdms View Post
Okay, I will google 'ssh without password' - In the meantime, can you give me a hint on how the syntax should look like??

~Many thanks!!
For using ssh without password follow these steps:

1) ssh-keygen -t dsa (or rsa)
2) press enter until prompt comes again.
3) two files will be in $HOME/.ssh directory.
id_rsa
id_rsa.pub
4) now simply copy the content of this file, to the $HOME/.ssh/authorized_keys file of the other server.
 
  


Reply


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
Problem with freeing disk space on a two hard disk Fedora Core 4 box favadalikhan Linux - Server 1 04-29-2008 10:04 PM
[SOLVED] Shell script to monitor or watch the disk space.... small help needed vikas027 Programming 5 10-21-2007 11:04 AM
LXer: Perl script to monitor disk space and send an email alert LXer Syndicated Linux News 1 02-23-2007 02:12 PM
Monitor disk space without gkrellm BroX Slackware 3 02-24-2006 04:21 PM
Disk space wastage 73 GB Hard disk rajgopalhg Linux - Hardware 2 10-18-2002 04:41 PM


All times are GMT -5. The time now is 02:08 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration