Shell monitors cpu, memory, and load average, records the log, and sends an email to the Administrator when the load is under pressure.
Principle:
1. Obtain the value of cpu, memory, and load average.
2. Determine whether the value exceeds the custom range, for example (CPU> 90%, Memory <10%, load average> 2)
3. If the value range is exceeded, send an email to the Administrator. The message is sent at intervals and is sent only once every hour.
4. Write the value to log.
5. Set crontab to run every 30 seconds.
ServerMonitor. sh
#! /Bin/bash # system monitoring, recording cpu, memory, and load average, when the specified value is exceeded, an email is sent to the Administrator # *** config start *** # current directory path ROOT =$ (cd "$ (dirname" $0 ")"; pwd) # current server name HOST =$ (hostname) # log file path CPU_LOG = "$ {ROOT}/logs/cpu. log "MEM_LOG =" $ {ROOT}/logs/mem. log "LOAD_LOG =" $ {ROOT}/logs/load. log "# notification email list NOTICE_EMAIL = 'admin @ admin.com '# cpu, memory, load average record the last email sending notification time CPU_REMARK = '/tmp/servermonitor_cpu.remark' MEM _ REMARK ='/tmp/servermonitor_mem.remark' LOAD _ REMARK = '/tmp/upload' # send a notification email interval REMARK_EXPIRE = 3600NOW =$ (date + % s) # *** config end *** # *** function start *** # obtain the CPU usage function GetCpu () {cpufree = $ (vmstat 1 5 | sed-n' 3, $ P' | awk '{x = x + $15} END {print x/5}' | awk-F. '{print $1}') cpuused = $ (100-$ cpufree) echo $ cpuused local remark = $ (GetRemark $ {CPU_REMARK }) # Check whether the CPU usage exceeds 90% if ["$ remark" = "] & [" $ cpuused "-gt 90]; then echo" Subject: $ {HOST} CPU uses more than 90% $ (date + % Y-% m-% d' % H: % M: % S) "| sendmail $ {NOTICE_EMAIL} echo" $ (date + % s) ">" $ CPU_REMARK "fi} # obtain memory usage function GetMem () {mem = $ (free-m | sed-n'3, 3p ') used = $ (echo $ mem | awk-f''' {print $3 }') free = $ (echo $ mem | awk-F ''' {print $4} ') total = $ ($ used + $ free )) limit = $ ($ total/10) echo "$ {total }$ {used }$ {free}" local remark =$ (GetRemark $ {MEM_REMARK }) # Check whether the memory usage exceeds 90% if ["$ remark" = "] & [" $ limit "-gt" $ free "]; then echo" Subject: $ {HOST} Memory uses more than 90% $ (date + % Y-% m-% d' % H: % M: % S) "| sendmail $ {NOTICE_EMAIL} echo" $ (date + % s) ">" $ MEM_REMARK "fi} # Get load averagefunction GetLoad () {load = $ (uptime | awk-F 'Load average: ''{print $2} ') m1 = $ (echo $ load | awk-F ', ''{print $1} ') m5 = $ (echo $ load | awk-F','' {print $2 }') m15 = $ (echo $ load | awk-F', ''{print $3 }') echo "$ {m1 }$ {m5 }$ {m15}" m1u =$ (echo $ m1 | awk-F '. ''{print $1} ') local remark = $ (GetRemark $ {LOAD_REMARK }) # Check whether the load is under pressure. if ["$ remark" = "] & [" $ m1u "-gt" 2 "]; then echo" Subject: $ {HOST} Load Average more than 2 $ (date + % Y-% m-% d' % H: % M: % S) "| sendmail $ {NOTICE_EMAIL} echo" $ (date + % s) ">" $ LOAD_REMARK "fi} # obtain the last email sending time function GetRemark () {local remark if [-f "$1"] & [-s "$1"]; then remark = $ (cat $1) if [$ ($ NOW-$ remark)-gt "$ REMARK_EXPIRE"]; then rm-f $1 remark = "" fi else remark = "" fi echo $ remark} # *** function end *** cpuinfo =$ (GetCpu) meminfo = $ (GetMem) loadinfo = $ (GetLoad) echo "cpu :$ {cpuinfo}" >>" $ {CPU_LOG} "echo" mem: $ {meminfo} ">>>" $ {MEM_LOG}" echo "load :$ {loadinfo}" >>>" $ {LOAD_LOG} "exit 0
Crontab
* * * * * /home/xxxx/ServerMonitor.sh* * * * * sleep 30; /home/xxxx/ServerMonitor.sh