The recent Linux server disk usage often to 100%, until the impact of the normal service failure will pay attention to, do not take precautions, today in the online collection of information, plus their own changes, wrote a shell script for real-time monitoring of disk usage and in excess of the threshold after the automatic alarm message sent.
Script Simple description: Use the DF command to view the disk usage information, more than the script set the Threshold (CRITICAL=90) 90%, will automatically use the SendEmail command to send the alarm mail (mail SMTP, email, etc. need to modify their own to real existence).
1, first download the Monitoring script (click to download) or directly copy the following script code to save as a disk_check.sh file
The code is as follows |
Copy Code |
#!/bin/bash partition_list= (' df-h | awk ' Nf>3&&nr>1{sub (/%/, "", $ (NF-1));p rint $NF, $ (NF-1)} ") critical=90 Notification_email () { emailuser= ' user@domain.com ' emailpasswd= ' password ' emailsmtp= ' smtp.domain.com ' sendto= ' user1@domain.com ' 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_list [@]};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 Fi Done If ["$crit _info"!= ""];then echo-e $crit _info | Notification_email Fi |
2, upload disk_check.sh files to the Linux server/opt directory
3. Add executable permissions to disk_check.sh
The code is as follows |
Copy Code |
[Root@localhost/]# chmod 755/opt/disk_check.sh |
4, Download sendemail mail program command Http://pan.baidu.com/s/1mgjxRtu, uploaded to the server/opt directory
5, decompression/opt/sendemail-v1.56.tar.gz
The code is as follows |
Copy Code |
[Root@localhost/]# Tar xvf/opt/sendemail-v1.56.tar.gz |
6, test script/alarm mail is running normally, you can see that there are 2 partitions are normal, show ok!, no more than the set threshold, there is a partition using more than the threshold, and automatically sent the alarm mail, and then to my set of Inbox, you can see that has received this alert message:
The code is as follows |
Copy Code |
[Root@localhost/]# 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 are are manually typing in a message: -The must be received within seconds. -End manual input and a ctrl-d on its own line. June 17:32:44 localhost sendemail[5585]: message input complete. June 17:32:44 localhost sendemail[5585]: Email was sent successfully! |
7. Finally, you can set up a cron scheduling task to have the script run every 1 hours
code is as follows |
copy code |
[Root@webmail /]# crontab-e Add the following line to cron */60 * * * */opt/disk_check.sh |