I need help writing script to monitor the hard disk space
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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:
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).
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:
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;
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
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%
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?
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.
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.
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??
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'.
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??
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.