The second script of sending mail for CPU memory disk monitoring in linux system
#! / bin/bash
Export PATH
Export LANG=zh_CN.UTF-8
#
#
Enter the number 1 after # top to check the status of each CPU.
#
#
# configure the mailx email account password first:
# cat > / etc/mail.rc $onlines]; then
Echo "$times $machine current online users: $whos"
Fi
#
#
Function top_check () {
# how long does the server run?
Runup=$ (top-n 1 | grep user | awk-F'[,] +'{print $5}')
# percentage of CPU occupied by user space
Cpuus=$ (top-n 1 | grep Cpu | awk-F ","'{print $1}'| awk-F ""'{print $2}')
# percentage of CPU occupied by kernel space
Cpusy=$ (top-n 1 | grep Cpu | awk-F ","'{print $2}'| awk-F ""'{print $2}')
# percentage of CPU occupied by processes that have changed priority
Cpuni=$ (top-n 1 | grep Cpu | awk-F ","'{print $3}'| awk-F ""'{print $2}')
# percentage of idle CPU
Cpuid=$ (top-n 1 | grep Cpu | awk-F ","'{print $4}'| awk-F ""'{print $2}')
# percentage of CPU occupied by IPUBO waiting
Cpuwa=$ (top-n 1 | grep Cpu | awk-F ","'{print $5}'| awk-F ""'{print $2}')
# percentage of CPU occupied by hard interrupts
Cpuhi=$ (top-n 1 | grep Cpu | awk-F ","'{print $6}'| awk-F ""'{print $2}')
# percentage of CPU occupied by soft interrupt
Cpusi=$ (top-n 1 | grep Cpu | awk-F ","'{print $7}'| awk-F ""'{print $2}')
# percentage of time that the virtual CPU waits for the actual CPU
Cpust=$ (top-n 1 | grep Cpu | awk-F ","'{print $8}'| awk-F ""'{print $2}')
}
Function load_time () {
# one minute load
Load1=$ (top-n 1 | grep user | awk-F'[,] +'{print $11}')
# five-minute load
Load5=$ (top-n 1 | grep user | awk-F'[,] +'{print $12}')
# 15 minute load
Load15=$ (top-b-n 1 | grep user | awk'{print $NF}')
}
Function load_mem () {
# Total memory capacity
Memtotal=$ (($(top-n 1 | grep buff/cache | awk-F'[,] +'{print $4}') / 1024)
# current free memory
Memfree=$ (($(top-n 1 | grep buff/cache | awk-F'[,] +'{print $7}') / 1024)
# memory currently used
Memused=$ (($(top-n 1 | grep buff/cache | awk-F'[,] +'{print $10}') / 1024)
# memory buff/cache
Membuff=$ (($(top-n 1 | grep buff/cache | awk-F'[,] +'{print $13}') / 1024)
# Total capacity of swap
Swaptotal=$ (($(top-n 1 | grep avail | awk-F'[,] +'{print $3}') / 1024)
# swap is currently idle
Swapfree=$ (($(top-n 1 | grep avail | awk-F'[,] +'{print $6}') / 1024))
# swap is currently in use
Swapused=$ (($(top-n 1 | grep avail | awk-F'[,] +'{print $9}') / 1024))
# swap memory avail Mem
Swapavail=$ (($(top-n 1 | grep avail | awk-F'[,] +'{print $11}') / 1024)
}
Function echo_run () {
Echo "$times $machine how long to run: $runup minutes"
}
Function echo_cpu () {
Echo "$times $machine percentage of CPU occupied by user space: $cpuus"
Echo "$times $machine kernel space occupies CPU percentage: $cpusy"
Echo "$times $machine percentage of CPU occupied by processes that have changed priority: $cpuni"
Echo "$times $machine percentage of idle CPU: $cpuid"
Echo "$times $machine Ibank O percentage of CPU waiting: $cpuwa"
Echo "$times $machine percentage of CPU occupied by hard interrupts: $cpuhi"
Percentage of CPU occupied by echo "$times $machine soft interrupts: $cpusi"
Echo "$times $machine percentage of time the virtual CPU waits for the actual CPU: $cpust"
}
Function echo_mem () {
Echo "$times $machine Total memory capacity: $memtotal M"
Echo "$times $machine Free memory: $memfree M"
Echo "$times $machine used memory: $memused M"
Echo "$times $machine memory buff/cache: $membuff M"
Echo "$times $machine swap Total: $swaptotal M"
Echo "$times $machine swap Idle: $swaptotal M"
Echo "$times $machine swap has been used: $swapused M"
Echo "$times $machine swap memory avail Mem: $swapavail M"
}
Function echo_load () {
Echo "$times $machine 1 minute CPU load: $load1"
Echo "$times $machine 5-minute CPU load: $load5"
Echo "$times $machine 15 minutes CPU load: $load15"
}
# call function
Top_check
Load_time
Load_mem
#
If [`echo "$load1 > $cpuinfos" | bc`-eq 1]; then
Readonly runlog= "/ tmp/runs.log"
Echo_run > $runlog
Readonly loadlog= "/ tmp/load.log"
Echo_load > $loadlog
Readonly cpulog= "/ tmp/cpus.log"
Echo_cpu > $cpulog
Readonly memlog= "/ tmp/mems.log"
Echo_mem > $memlog
# send email
Dos2unix-k $runlog
Dos2unix-k $loadlog
Dos2unix-k $cpulog
Dos2unix-k $memlog
Mailx-s "server $names $machine" lumia98@vip.qq.com
< $runlog mailx -s "服务器$names $machine" lumia98@vip.qq.com < $loadlog mailx -s "服务器$names $machine" lumia98@vip.qq.com < $cpulog mailx -s "服务器$names $machine" lumia98@vip.qq.com < $memlog fi #判断I/O wa是否过高 if [ `echo "$cpuwa >60 "| bc`-eq 1]; then
Readonly ioslogs= "/ tmp/wa.log"
Iostat-cdx 3 > $ioslogs
Dos2unix-k $ioslogs
Mailx-s "load wa% of server $names $machine" lumia98@vip.qq.com
< $ioslogs fi #判断磁盘空间不足 disks=$(df | grep /dev|grep -v "shm\|boot\|devtmpfs"|awk '{print $5}'|sed 's/\%.*$//') for x in $disks do if [ $x -gt 70 ]; then disklog="/tmp/disk.log" df -h >$disklog
Dos2unix-k $disklog
Mailx-s "Server $names $machine" lumia98@vip.qq.com < $disklog
Fi
Done