Shell script to monitor disk usage (local + remote)

Source: Internet
Author: User
Tags disk usage

Any partition used to 90% send a message to the specified recipient, to 95% on the subject of the message Warning (warning), instructions to send mail program email

#!/bin/bash
#Updated: 2008-03-03 PM by:leif ([email protected])
Email=/usr/local/bin/email
/bin/df-h >/tmp/df.txt

Use= ' Df-h | Grep-o [0-9]*% | Grep-o ' [0-9]\+ '

For I in $USE
Do
if (($i > 95))
Then
$EAMIL-S "WARNING low disk space for $i" [e-mail protected] Break
Fi
if (($i > 90))
Then
$EMAIL-S "Low disk space for $i" [EMAIL protected] fi
Done

/bin/rm-f/tmp/df.txt

To achieve the purpose, any one partition uses 90% to send a message to the specified recipient, to 95% on the subject of the message Warning (warning), stating that the email program email is from Http://www.cleancode.org/projects/email Download and install, more flexible.

Put the shell on the crontab as needed to implement a timed check of the disk

The following are additional content:

Shell script for monitoring remote host disk usage, file name: disklog.sh

#!/bin/bash
# File Name: disklog.sh
# Purpose: Monitor disk usage for remote systems
Logfile= "Diskusage.log"
if [[-N $]]
Then
Logfile=$1
If
if [!-e $logfile]
Then
printf "%-8s%-14s%-9s%-8s%-6s%-6s%-6s%s\n" "Date" "IP ADDRESS" "Device" "Capacity" "used" "Free" "Percent" "Status" > $logfile
Fi

ip_list= "127.0.0.1 0.0.0.0"
# provide a list of remote host IP addresses
(
For IP in $IP _list
Do
SSH [email protected] $ip ' df-h ' | grep ^/dev/>/TMP/$$.DF

while read line;
Do
cur_date=$ (Date +%d)
printf "%-8s%-14s" $cur _date $ip
echo $line | awk ' {printf ("%-9s%-8s%-6s%-6s%-8s", $1,$2,$3,$4,$5);} '

pusg=$ (echo $line | egrep-o "[0-9]+%")
pusg=${pusg/\%/};
If [$PUSG-lt 80];
Then
Echo SAFT
Else
Echo ALERT
Fi
done</TMP/$$.DF
Done
) >> $logfile

We can use cron to schedule script execution at regular intervals, such as by adding such entries in crontab to run the script automatically 10 o'clock in the morning every day:


XX * * */home/sh/disklog.sh/home/log/diskusg.log

Execute the CRONTAB-E command, add the above line and save.

You can also do this manually:

$./disklog.sh

Shell script to monitor disk usage (local + remote)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.