#!/bin/bash
#获取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
#获取内存情况
Mem_total=
free -m | awk -F ‘[ :]+‘ ‘NR==2{print $2}‘
Mem_used=
free -m | awk -F ‘[ :]+‘ ‘NR==3{print $3}‘
#统计内存使用率
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 protected] </tmp/jiankong.log
}
function Check () {
if [["$cpuUsage" > 80]] | | [[$diskUsage > 80] | | [["$mem _used_persent" >]];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
Use the shell to monitor CPU, disk, memory usage, reach alert thresholds and email notifications