#!/bin/bash
Export PATH
Export LANG=ZH_CN. UTF-8
#
#
#top之后输入数字1, you can view the status of each CPU.
#
#
#先配置好mailx邮箱账号密码:
#cat >/etc/mail.rc<< "EOF"
#set Sendcharsets=iso-8859-1,utf-8
#set [email protected]
#set smtp=smtp.163.com
#set [email protected]
#set smtp-auth-password=xxx
#set Smtp-auth=login
#EOF
#文件存放位置
#
#判断是否安装bc
Ping-c 2 www.baidu.com | | Exit 5
[' echo ' 1 > 2 ' |BC '] | | Yum Install Bc-y
Sleep 3
[' Rpm-qa|grep Dos2unix '] | | Yum Install Dos2unix-y
Sleep 3
#
#
#时间
ReadOnly times=$ (Date +%y "/"%m "/"%d ""%k ":"%m ")
#服务器IP
ReadOnly machine=$ (IP add | grep inet|grep-v ' inet6\|127.0.0.1 ' |awk ' {print $} ' | sed ' s/\/.*$//')
ReadOnly names=$ (hostname)
#cpu个数
ReadOnly cpuinfos=$ (Cat/proc/cpuinfo|grep processor|wc-l)
#在线用户数
ReadOnly onlines= ' 2 '
#cpu负载阈值
ReadOnly cpuload= ' 80 '
#判断用户在线数
userd=$ (top-n 1|grep users|awk-f ' [,]+ ' {print $6} ')
Whos=$ (WHO)
If [$userd > $onlines];then
echo "$times $machine current Online users: $whos"
Fi
#
#
function Top_check () {
#服务器运行多久
runup=$ (top-n 1|grep user|awk-f ' [,]+ ' {print $} ')
#用户空间占用CPU百分比
cpuus=$ (top-n 1|grep cpu|awk-f "," ' {print $} ' |awk-f ' "' {print $} ')
#内核空间占用CPU百分比
cpusy=$ (top-n 1|grep cpu|awk-f "," ' {print $} ' |awk-f ' "' {print $} ')
#改变过优先级的进程占用CPU的百分比
cpuni=$ (top-n 1|grep cpu|awk-f "," ' {print $} ' |awk-f ' "' {print $} ')
#空闲CPU百分比
cpuid=$ (top-n 1|grep cpu|awk-f "," ' {print $4} ' |awk-f ' "' {print $} ')
Percentage of CPU #I/O waiting to be consumed
cpuwa=$ (top-n 1|grep cpu|awk-f "," ' {print $} ' |awk-f ' "' {print $} ')
#硬中断占用CPU的百分比
cpuhi=$ (top-n 1|grep cpu|awk-f "," ' {print $6} ' |awk-f ' "' {print $} ')
#软中断占用CPU的百分比
cpusi=$ (top-n 1|grep cpu|awk-f "," ' {print $7} ' |awk-f ' "' {print $} ')
Percentage of time #虚拟 CPU waits for the actual CPU
cpust=$ (top-n 1|grep cpu|awk-f "," ' {print $8} ' |awk-f ' "' {print $} ')
}
function Load_time () {
#一分钟负载
load1=$ (top-n 1|grep user|awk-f ' [,]+ ' {print $11} ')
#五分钟负载
load5=$ (top-n 1|grep user|awk-f ' [,]+ ' {print $} ')
#十五分钟负载
load15=$ (Top-b-n 1|grep user|awk ' {print $NF} ')
}
function Load_mem () {
#内存总容量
memtotal=$ ($ (top-n 1|grep buff/cache|awk-f ' [,]+ ' ' {print $4} ')/1024))
#当前空闲内存
memfree=$ ($ (top-n 1|grep buff/cache|awk-f ' [,]+ ' ' {print $7} ')/1024))
#当前已使用内存
Memused=$ ($ ($ (top-n 1|grep buff/cache|awk-f ' [,]+ ' ' {print $} ')/1024))
#内存buff/cache
membuff=$ ($ (top-n 1|grep buff/cache|awk-f ' [,]+ ' ' {print $13} ')/1024))
#swap总容量
Swaptotal=$ ($ ($ (top-n 1|grep avail|awk-f ' [,]+ ' ' {print $} ')/1024))
#swap当前空闲
swapfree=$ ($ (top-n 1|grep avail|awk-f ' [,]+ ' ' {print $6} ')/1024))
#swap当前已使用
swapused=$ ($ (top-n 1|grep avail|awk-f ' [,]+ ' ' {print $9} ')/1024))
#swap内存avail Mem
swapavail=$ ($ (top-n 1|grep avail|awk-f ' [,]+ ' ' {print $11} ')/1024))
}
function Echo_run () {
echo "How long $times $machine run: $runup minutes"
}
function Echo_cpu () {
echo "$times $machine User space consumption CPU percentage: $cpuus"
echo "$times $machine kernel space consumption CPU percentage: $cpusy"
echo "$times $machine a percentage of CPU-intensive processes that have changed priority: $cpuni"
echo "$times $machine Idle CPU percentage: $cpuid"
echo "$times percentage of CPU $machine I/O Waiting: $cpuwa"
echo "$times $machine percent of hard interrupts occupied by CPU: $cpuhi"
echo "$times $machine the percentage of CPU that the soft interrupt consumes: $cpusi"
echo "$times $machine The 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 used: $swapused M"
echo "$times $machine swap memory avail Mem: $swapavail M"
}
function Echo_load () {
echo "$times $machine 1 min CPU load: $load 1"
echo "$times $machine 5 min CPU Load: $load 5"
echo "$times $machine 15 min CPU Load: $load 15"
}
#调用函数
Top_check
Load_time
Load_mem
#
If [' Echo ' $load 1 > $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
#发送邮件
Dos2unix-k $runlog
Dos2unix-k $loadlog
Dos2unix-k $cpulog
Dos2unix-k $memlog
Mailx-s "Server $names $machine" [email protected] < $runlog
Mailx-s "Server $names $machine" [email protected] < $loadlog
Mailx-s "Server $names $machine" [email protected] < $cpulog
Mailx-s "Server $names $machine" [email protected] < $memlog
Fi
#判断I/o WA is too high
If [' echo ' $cpuwa > ' |BC '-eq 1];then
ReadOnly ioslogs= "/tmp/wa.log"
IOSTAT-CDX 3 > $ioslogs
Dos2unix-k $ioslogs
Mailx-s "Server $names $machine load wa%" [email protected] < $ioslogs
Fi
#判断磁盘空间不足
disks=$ (DF | grep/dev|grep-v "Shm\|boot\|devtmpfs" |awk ' {print $} ' |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" [email protected] < $disklog
Fi
Done
This article is from "A Recruit Apprentice" blog, please be sure to keep this source http://sadoc.blog.51cto.com/12189157/1943283
Linux system CPU memory disk monitoring Send mail script two