How the shell monitors cpu,memory,load average

Source: Internet
Author: User
Tags log memory usage time interval cpu usage

Shell monitor Cpu,memory,load average, log to log, when load pressure, email notify admin.

Principle:

1. Get the value of the Cpu,memory,load average

2. Determine whether the value exceeds the scope of the customization, for example (Cpu>90%,memory<10%,load average>2)

3. The value exceeds the range, send email to notify the administrator. There is a time interval for sending, which is sent only once per hour.

4. Writes a value to log.

5. Set Crontab to run every 30 seconds.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

servermonitor.sh

#!/bin/bash # System monitoring, recording CPU, memory, load average, when more than the specified value email notification Administrator # * * * Config start * * Current directory path root=$ (CD "$ (dirname" $) "; pwd) # Current server name host=$ (hostname) # log file path cpu_log= ' ${ROOT}/LOGS/CPU.L   
      
OG "mem_log=" ${root}/logs/mem.log "load_log=" ${root}/logs/load.log "# notify Email list notice_email= ' admin@admin.com ' # Cpu,memory,load average record last send notification email time cpu_remark= '/tmp/servermonitor_cpu.remark ' mem_remark= '/tmp/servermo Nitor_mem.remark ' load_remark= '/tmp/servermonitor_loadaverage.remark ' # notification email interval time remark_expire=3600 NOW=$ (  
    Date +%s) # * * * * * * * * * * * * * * * * * * * * * * * * Function start # Get CPU usage function getcpu  cpufree=$ (vmstat 1 5 |sed-n ' 3, $p ' |awk ' {x = x + $} end {print X/5} ' |awk-f. ' {print} ') cpuused=$ (100 -$cpufree)) Echo $cpuused Local remark remark=$ (Getremark ${cpu_remark}) # Check CPU usage is more than 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} # Get memory usage function getme M () {mem=$ (free-m | sed-n ' 3,3p ') used=$ (echo $mem | awk-f ' ' {print $} ') free=$ (echo $mem | awk  
      
    -F ' ' {print $} ') total=$ (($used + $free)) limit=$ (($total/10)) echo "${total} ${used} ${free}" Local remark remark=$ (Getremark ${mem_remark}) # Check that the memory footprint exceeds the 90% if [' $remark ' = '] ;& ["$limit"-GT "$free"]; Then echo "Subject: ${host} Memory uses than more 90% $ (date +%y-%m-%d ' '%h:%m:%s ')" |  SendMail ${notice_email} Echo ' $ (date +%s) ' > ' $MEM _remark ' fi} # Get load Average function Getload () {load=$ (uptime | awk-f ' Load average: ' {print $} ') m1=$ (echo $load | awk-f ', ' {print $} ')  
    m5=$ (echo $load | awk-f ', ' {print $} ') m15=$ (echo $load | awk-f ', ' {print $} ') echo "${m1} ${ M5} ${m15} "m1u=$ (echo $m 1 | awk-f '. ') ' {print '} ') local remark remark=$ (Getremark ${load_remark}) # Check to see if the load has pressure if [$r Emark "=" "] && [" $m 1u "-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} # get last email time function G  
      
        Etremark () {Local remark if [-f ' $] && [-S ' $]; then remark=$ (cat $) If [$ ($NOW-$remark))-gt "$REMARK _expire"];  Then rm-f $ 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 * * * * * * * * * * * *  
/home/xxxx/servermonitor.sh

Author: csdn blog proud Snow star Maple

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.