Use the shell to monitor CPU, disk, memory usage, reach alert thresholds and email notifications

Source: Internet
Author: User
Tags memory usage cpu usage disk usage diskusage

#!/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

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.