Shell monitoring Cpu,memory,load Average

Source: Internet
Author: User

The shell monitors cpu,memory,load average, logs to log, and notifies the administrator when the load is under pressure.

Implementation principle:

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

2. Determine if the value exceeds the custom range, for example (Cpu>90%,memory<10%,load average>2)

3. If the value exceeds the range, send an email to notify the administrator. It is sent at a time interval and is sent only once per hour.

4. Write the value to log.

5. Set the crontab to run every 30 seconds.




#!/bin/bash


# system monitoring, recording CPU, memory, load average, email notification Manager when exceeding the specified value


# * * * Config start * * *


# Current directory path

root=$ (CD "$ (dirname") "; 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 e-mail list

Notice_email= ' [EMAIL protected] '


# Cpu,memory,load average record last send notification email time

cpu_remark= '/tmp/servermonitor_cpu.remark '

mem_remark= '/tmp/servermonitor_mem.remark '

load_remark= '/tmp/servermonitor_loadaverage.remark '


# Send notification e-mail interval time

remark_expire=3600

now=$ (Date +%s)


# * * * Config END * * *



# * * * 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 if 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

}


# Get Memory usage

function Getmem () {

mem=$ (free-m | sed-n ' 3,3p ')

used=$ (echo $mem | awk-f ' {print $} ')

free=$ (echo $mem | awk-f "{print $4} ')

total=$ (($used + $free))

limit=$ (($total/10))

echo "${total} ${used} ${free}"


Local remark

remark=$ (Getremark ${mem_remark})


# Check if memory consumption 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 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 the load is under 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

}


# Gets the last time the email was sent

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

}



# * * * 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


This article is from the "Andylinux" blog, make sure to keep this source http://andywen.blog.51cto.com/10283904/1792538

Shell monitoring Cpu,memory,load Average

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.