Linux load monitoring script and linux load script

Source: Internet
Author: User

Linux load monitoring script and linux load script

#! /Bin/bash

Date = 'echo $ (date + % Y \-% m \-% d \ % H: % M: % S )'
HostName = 'hostname'
IP = 'ifconfig eth0 | grep "inet addr" | cut-f 2-d ":" | cut-f 1-d ""'
ServerInfo = 'echo-e "-------- $ Date -------- \ n server: $ HostName IP: $ IP "'

# Delete monitoring logs 5 days ago
LOGPWD =/var/log/monitor
LOGDATE = 'echo $ (date-d-5day + % Y % m % d )'
LOGGREP = 'ls $ LOGPWD | grep-c "$ LOGDATE "'
If [$ LOGGREP-gt 0]
Then
Rm-fr $ LOGPWD/* $ LOGDATE. log
Fi

Echo-e "$ ServerInfo \ nuptime \ n' uptime' \ nfree-m \ n' free-m'"> $ LOGPWD/monitor. log
# Monitor cpu load
PhysicalCpu = 'grep' physical id'/proc/cpuinfo | sort-u | wc-l'
CoreCpu = 'grep' core id'/proc/cpuinfo | sort-u | wc-l'
Count_cores = 'echo "$ PhysicalCpu * $ CoreCpu" | bc'
Count_uptime = 'uptime | wc-W'
AverageLoad = 'uptime | awk' {print $ '$ count_uptime '}''
AverageInt = 'echo $ AverageLoad | cut-f 1-d "."'
If [$ AverageInt-gt $ count_cores]
Then
Echo-e "$ ServerInfo \ n15 minutes of load is $ AverageLoad, number of cores $ count_cores, exceeds the number of cpu cores, please handle! ">>$ LOGPWD/cpu_status _ $ (date + % Y % m % d). log
The echo-e "15-minute load is $ AverageLoad. The number of cores $ count_cores exceeds the number of cpu cores. Please handle it! ">>> $ LOGPWD/monitor. log
Else
Echo-e "$ ServerInfo \ n15 minutes of load is $ AverageLoad, number of cores $ count_cores, normal load. ">>$ LOGPWD/cpu_status _ $ (date + % Y % m % d). log
The echo-e "15-minute load is $ AverageLoad, the number of cores $ count_cores, and the load is normal. ">>> $ LOGPWD/monitor. log
Fi
# Monitor memory usage
MemTotal = 'free-m | grep Mem | awk '{print $2 }''
MemFree = 'free-m | grep Mem | awk '{print $4 }''
MemRate = 'echo "100-$ MemFree * 100/$ MemTotal" | bc'
If [$ MemRate-gt 80]
Then
Echo-e "$ ServerInfo \ n memory usage $ MemRate %, greater than 80%, please handle it. ">>$ LOGPWD/mem_status _ $ (date + % Y % m % d). log
Echo-e "memory usage $ MemRate %, greater than 80%, please handle. ">>> $ LOGPWD/monitor. log
Else
Echo-e "$ ServerInfo \ n memory usage $ MemRate %, less than or equal to 80%, the memory load is normal. ">>$ LOGPWD/mem_status _ $ (date + % Y % m % d). log
Echo-e "memory usage $ MemRate %, less than or equal to 80%, the memory load is normal. ">>> $ LOGPWD/monitor. log
Fi

If [$ AverageInt-gt $ count_cores] | [$ MemRate-gt 80]
Then
Cat $ LOGPWD/monitor. log | mail-s "$ HostName server load monitoring alarm" xxx@xxx.xxx
Fi

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.