Shell script for monitoring disk usage (local + Remote)

Source: Internet
Author: User

Monitordisk. sh: Copy codeThe Code is as follows :#! /Bin/bash
# Updated: PM By: leif (liangliwen@163.com)
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" liangliwen@163.com break
Fi
If ($ I> 90 ))
Then
$ EMAIL-s "Low disk space for $ I" liangliwen@163.com fi
Done

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

To achieve this, any partition uses 90% to send an EMAIL to the specified recipient, and then sends a warning (warning) on the subject of the EMAIL to 95%, indicating that the EMAIL sending program EMAIL is used, is to download the installation from the http://www.cleancode.org/projects/email, more flexible.

Place the shell on crontab as needed to regularly check disk Conditions

The following is a supplement:

Shell script used to monitor disk usage of the remote host. File Name: disklog. shCopy codeThe Code is as follows :#! /Bin/bash
# File name: disklog. sh
# Purpose: monitor the disk usage of a remote system
Logfile = "diskusage. log"
If [[-n $1]
Then
Logfile = $1
If
If [! -E $ logfile]
Then
Printf "%-8 s %-14 s %-9 s %-8 s %-6 s %-6 s %-6 s % 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 slynux @ $ ip 'df-H' | grep ^/dev/>/tmp/$. df

While read line;
Do
Cur_date = $ (date + % D)
Printf "%-8 s %-14 s" $ cur_date $ ip
Echo $ line | awk '{printf ("%-9 s %-8 s %-6 s %-6 s %-8 s", $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 a fixed interval. For example, we can add such entries to the crontab to automatically run the script at ten o'clock A.M. every day:

00 10 ***/home/sh/disklog. sh/home/log/diskusg. log

Run the crontab-e command to add and save the preceding line.

You can also manually execute:

$./Disklog. sh

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.