Linux server hardware running status and fault message alert monitoring script sharing _linux Shell

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

Monitor hardware Health

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.

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 "$ (Dirn Ame "$") "; 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" # notify Email list notice_email= ' admin@admin.com ' # cpu,memory,load average record last send notification email time Cpu_remar k= '/tmp/servermonitor_cpu.remark ' mem_remark= '/tmp/servermonitor_mem.remark ' load_remark= '/tmp/servermonitor_ Loadaverage.remark ' # Send notification email interval remark_expire=3600 now=$ (date +%s) # * * * * * * * * * * * * * * * * * * * * * * * *  # 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 for 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 Getmem () {mem=$ (f ree-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_re MARK}) # 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 if load has pressure if ["$remark" = ""] && ["$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 Getremark () { Local remark if [F "$] && [-S" $]; Then remark=$ (cat $) if [$ (($NOW-$remark))-gt "$REMARK _expire"]; Then rm-f $ remark= "" fi Else Remark= "" Fi Echo $remark} # * * * * * * * * *  * cpuinfo=$ (GETCPU) meminfo=$ (GETMEM) loadinfo=$ (getload) echo "CPU: ${cpuinfo}" >> "${cpu_log}" echo "MEM: 
 ${meminfo} ">>" ${mem_log} "echo" Load: ${loadinfo} ">>" ${load_log} "Exit 0


Monitor Web site for exceptions
Shell monitor site for unusual scripts, such as abnormal automatic email notification to administrators.
Process:
1. Check that the Http_code returned by the Web site is equal to 200, if not 200 as an exception.
2. Check the Web site's access time, exceeding Maxloadtime (10 seconds) as an exception.
3. After sending the notification email, the/tmp/monitor_load.remark record is sent in one hour without repetition, such as emptying the/tmp/monitor_load.remark after an hour.

#!/bin/bash sites= ("http://web01.example.com" "http://web02.example.com") # to monitor the site notice_email= ' me@example.com '  # Admin Email maxloadtime=10 # access Timeout set remarkfile= '/tmp/monitor_load.remark ' # Whether a notification email has been sent during the recording, such as sent Issend=0 # No more than an hour to send e-mail expire=3600 # send email every time Number of seconds now=$ (date +%s) if [-F "$REMARKFILE"] && [-S "$REMARKFILE"]; Then remark=$ (cat $REMARKFILE) # Delete expired email delivery time record file if [$ ($NOW-$REMARK))-gt "$EXPIRE"]; Then rm-f ${remarkfile} remark= "" Fi Else Remark= "" fi # Loops to judge each site for site in ${sites[*]}; Do printf "start to load ${site}\n" site_load_time=$ (Curl-o/dev/null-s-W "Time_connect:%{time_connect}\ntime_ Starttransfer:%{time_starttransfer}\ntime_total:%{time_total} "" ${site} "site_access=$ (Curl-o/dev/null-s-W%{htt P_code} "${site}") time_total=${site_load_time##*:} printf "$ (date ' +%y-%m-%d%H:%m:%s ') \ n "printf" Site load time\n${site_load_time}\n "printf" site access:${site_access}\n\n "# Not send If ["$REMARK" = ""]; Then # check access if ["$time _total" = "0.000"] | | ["$site _access"!= "200"]; Then echo "Subject: ${site} can access $ (date +%y-%m-%d '%h:%m:%s)" | SendMail ${notice_email} issend=1 Else # Check load time if [' ${time_total%%.*} '-ge ${maxloadt IME}]; Then echo "Subject: ${site} load Time Total:${time_total} $ (Date +%y-%m-%d ' '%h:%m:%s ')" | SendMail ${notice_email} issend=1 fi fi done # send email after record sending time if ["$ISSEND" = "1"];  Then echo "$ (date +%s)" > $REMARKFILE fi exit 0

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.