shell script monitoring cpu / memory usage

The script detects the usage of cpu and memory. It only needs to adjust the email address of memorySetting, cpu setting and userEmail to send email alarm
If you don't have a friend who has configured the email parameters, you can fly to the code area
1.vim /etc/mail.rc
2. Find the following
set from=yangxingyi@duoduofenqi.com #From what
set smtp=smtp.exmail.qq.com ා fill in according to your email server, this is TX's corporate email
set smtp-auth-user=yangxingyi@duoduofenqi.com #Mailbox user name
Set SMTP auth password = your password. Please note that it is the email password. Some email service providers have different login password and email password
set smtp-auth=login
If you think it's useful, just move away, don't contact me!!!

#/bin/sh
#auth yangxingyi 2017-12-12 17:50
#email openweixin666@126.com
#this script  check cpu used rate and memory used rate
userEmail="269754243@qq.com openweixin666@126.com"
webIp="www101.200.***.***"
memorySetting="80"
cpuSetting="80"
#check memory used rate 
totalMemory=$(free -m|awk  '{print $2}'|sed -n '2p')
usedMemory=$(free -m|awk  '{print $3}'|sed -n '3p')
freeMemory=$(free -m|awk  '{print $4}'|sed -n '3p')
usedPerMemory=$(awk 'BEGIN{printf "%.0f",('$usedMemory'/'$totalMemory')*100}')
freePerMemory=$(awk 'BEGIN{printf "%.0f",('$freeMemory'/'$totalMemory')*100}')
if [ $usedPerMemory -ge $memorySetting  ]
   then
     minfo="totalMemory:$totalMemory MB,used:$usedMemory MB,free:$freeMemory MB,usedPercent:$usedPerMemory%,freePrecent:$freePerMemory%"
     echo "$(date) $minfo used memory was gt $memorySetting% !" >> /sh/log_hard_disk_check
     echo " $minfo {$webIp}!" | mail -s "{$webIp} used memory was high!"  $userEmail
fi

#check cpu used rate
cpuUsed=$(top -n 1 | awk -F '[ %]+' 'NR==3 {print $2}'|awk -F '.' '{print $1}')
if [ $cpuUsed -gt $cpuSetting ]
   then
      echo  "$(date) cpu used $cpuUsed% "
      echo  "$(date) cpu used $cpuUsed%"|mail -s "$webIp cpu used $cpuUsed%" $userEmail
fi

Tags: vim

Posted on Tue, 02 Jun 2020 11:32:43 -0400 by utpal