Use the shell to monitor CPU, disk, memory usage, reach alert thresholds and email notifications
And with the task plan, you can get the alarm information in time
#!/bin/bash############################################## #Author: liuzhengwei - [email Protected] #QQ: 1135960569#last modified: 2017-04-19 21:50#filename: jiankong.sh#description : ############################################## #获取cpu使用率cpuUsage = ' Top -n 1 | awk -F ' [ %]+ ' ' nr==3 {print $2} ' #获取磁盘使用率data_name = '/dev/vda1 ' diskusage= ' df - h | grep $data _name | awk -f ' [ %]+ ' ' {print $5} ' logFile=/ tmp/jiankong.log# get memory situation mem_total= ' free -m | awk -f ' [ :]+ ' ' NR==2{print $2} ' mem_used= ' free -m | awk -f ' [ :]+ ' ' nr==3{print $3} ' # Statistical memory Utilization mem_used_persent= ' awk ' begin{printf "%.0f\n", (' $mem _used '/' $mem _total ') *100} ' #获取报警时间now_time = ' date ' +%f %t ' Function send_mail () { mail -s "Monitoring Alarm" [email&Nbsp;protected] < /tmp/jiankong.log}function check () { if [[ "$cpuUsage" > 80 ]] | | [[ "$diskUsage" > 80 ]] | | [[ "$mem _used_persent" > 80 ]];then echo "Alarm Time: ${now_time}" > $logFile echo " CPU Usage:${cpuusage}% --> disk usage:${diskusage}% --> memory usage: ${mem_used_persent}% " >> $logFile send_mail fi}function main () { check}main
This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1917501
Monitor CPU, disk, and memory usage with the shell