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