Monitor disk usage under Linux and send alert messages automatically after thresholds are exceeded

Source: Internet
Author: User
Tags disk usage

The recent Linux server disk usage often to 100%, until the impact of normal service failure to pay attention, do not take precautions, today, the online collection of information, plus their own changes, wrote a shell script for real-time monitoring of disk usage and automatically send alarm messages after exceeding the threshold.

Script Simple description: Use the DF command to view disk usage information, more than the script set threshold (critical=90) 90%, will automatically use the SendEmail command to send alarm messages (mail SMTP, email, etc. need to be modified to the real existence).

1, first download the Monitoring script (click to download) or directly copy the following script code to save as disk_check.sh file

#!/bin/bashpartition_list= (' df-h | awk ' Nf>3&&nr>1{sub (/%/, "", $ (NF-1));p rint $NF, $ (NF-1)} ") Critical =90notification_email () {    emailuser= ' [email protected] '    emailpasswd= ' password '    emailsmtp= ' Smtp.domain.com '    sendto= ' [email protected] '    title= ' Disk Space Alarm '    /opt/sendemail-v1.56/sendemail-f $emailuser-T $sendto-S $EMAILSMTP-u $title-xu $emailuser-xp $emailpasswd}crit_info= "" for (i=0;i<${#partition_lis t[@]};i+=2) do    if ["${partition_list[((i+1)]}"-lt "$critical"];then        echo "ok! ${partition_list[i]} used ${partition_list[((i+1))]}% "    else                    If [" ${partition_list[((i+1))]} "-GT" $critical "];then                crit_info= $crit _info" Warning!!! ${partition_list[i]} used ${partition_list[((i+1))]}%\n "            fi     Fidoneif ["$crit _info"! = ""];then    echo-e $crit _info | Notification_emailfi

2. Upload the disk_check.sh file to the/opt directory of the Linux server

3. Add executable permissions to disk_check.sh

[Email protected]/]# chmod 755/opt/disk_check.sh

4, Download SendEmail mail program command (click Download), upload to the server/opt directory

5, decompression/opt/sendemail-v1.56.tar.gz

[Email protected]/]# tar xvf/opt/sendemail-v1.56.tar.gz

6, test script/alarm mail is running normally, you can see that there are 2 partitions is normal, display ok!, there is no more than the set threshold, there is a partition usage exceeded the threshold, and automatically sent the alarm mail, and then go to the Inbox I set up the mailbox, you can see has received this alert mail:

[Email protected]/]# sh/opt/disk_check.sh ok! /dev/shm used 1%ok! /boot used 12%reading message body from STDIN because the '-m ' option is not used. If you is manually typing in a message:  -First line must is received within.  -End manual input with a ctrl-d in its own line. Jul 17:32:44 localhost sendemail[5585]: Message input complete. Jul 17:32:44 localhost sendemail[5585]: Email was sent successfully!


7. Finally, you can set up a cron scheduled task to run it every 1 hours

[Email protected]/]# CRONTAB-E

Add the following line to Cron

*/60 * * * */opt/disk_check.sh


Http://www.heminjie.com/system/linux/1923.html

Monitor disk usage under Linux and send alert messages automatically after thresholds are exceeded

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.