LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I need help writing script to monitor the hard disk space (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-help-writing-script-to-monitor-the-hard-disk-space-667104/)

xmdms 09-02-2008 10:56 AM

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

unSpawn 09-02-2008 01:08 PM

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?

misconfiguration 09-02-2008 01:13 PM

Do you have any language preferences? ksh, bash, Perl etc?

CRC123 09-02-2008 01:24 PM

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!

xmdms 09-03-2008 07:25 AM

Quote:

Originally Posted by CRC123 (Post 3267391)
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,

chrism01 09-03-2008 07:33 AM

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.

CRC123 09-03-2008 07:54 AM

Quote:

Originally Posted by xmdms (Post 3268229)
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

xmdms 09-03-2008 08:41 AM

Quote:

Originally Posted by CRC123 (Post 3268248)
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!!!

chrism01 09-03-2008 09:07 AM

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?

CRC123 09-03-2008 09:12 AM

Quote:

Originally Posted by chrism01 (Post 3268302)
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 :).

xmdms 09-03-2008 10:36 AM

Quote:

Originally Posted by CRC123 (Post 3268305)
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!!

xmdms 09-03-2008 02:28 PM

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

CRC123 09-03-2008 02:33 PM

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'.

xmdms 09-03-2008 05:01 PM

Quote:

Originally Posted by CRC123 (Post 3268614)
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!!

vikas027 09-03-2008 05:35 PM

Quote:

Originally Posted by xmdms (Post 3268776)
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.


All times are GMT -5. The time now is 09:53 PM.