您好,登錄后才能下訂單哦!
#!/bin/bash
export PATH
export LANG=zh_CN.UTF-8
#
#
#top之后輸入數字1,可以查看每顆CPU的情況.
#
#
#先配置好mailx郵箱賬號密碼:
#cat>/etc/mail.rc<<"EOF"
#set sendcharsets=iso-8859-1,utf-8
#set from=xxx@163.com
#set smtp=smtp.163.com
#set smtp-auth-user=xx@163.com
#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 $2 }' | 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 當前在線用戶數: $whos"
fi
#
#
function top_check(){
#服務器運行多久
runup=$(top -n 1|grep user|awk -F '[, ]+' '{print $5}')
#用戶空間占用CPU百分比
cpuus=$(top -n 1|grep Cpu|awk -F "," '{print $1}'|awk -F " " '{print $2}')
#內核空間占用CPU百分比
cpusy=$(top -n 1|grep Cpu|awk -F "," '{print $2}'|awk -F " " '{print $2}')
#改變過優先級的進程占用CPU的百分比
cpuni=$(top -n 1|grep Cpu|awk -F "," '{print $3}'|awk -F " " '{print $2}')
#空閑CPU百分比
cpuid=$(top -n 1|grep Cpu|awk -F "," '{print $4}'|awk -F " " '{print $2}')
#I/O等待占用CPU的百分比
cpuwa=$(top -n 1|grep Cpu|awk -F "," '{print $5}'|awk -F " " '{print $2}')
#硬中斷占用CPU的百分比
cpuhi=$(top -n 1|grep Cpu|awk -F "," '{print $6}'|awk -F " " '{print $2}')
#軟中斷占用CPU的百分比
cpusi=$(top -n 1|grep Cpu|awk -F "," '{print $7}'|awk -F " " '{print $2}')
#虛擬 CPU 等待實際 CPU 的時間的百分比
cpust=$(top -n 1|grep Cpu|awk -F "," '{print $8}'|awk -F " " '{print $2}')
}
function load_time(){
#一分鐘負載
load1=$(top -n 1|grep user|awk -F '[, ]+' '{print $11}')
#五分鐘負載
load5=$(top -n 1|grep user|awk -F '[, ]+' '{print $12}')
#十五分鐘負載
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 $10}') / 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 $3}') / 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 "$times $machine 運行多久: $runup 分鐘 "
}
function echo_cpu(){
echo "$times $machine 用戶空間占用CPU百分比: $cpuus "
echo "$times $machine 內核空間占用CPU百分比: $cpusy "
echo "$times $machine 改變過優先級的進程占用CPU的百分比: $cpuni "
echo "$times $machine 空閑CPU百分比: $cpuid "
echo "$times $machine I/O等待占用CPU的百分比: $cpuwa"
echo "$times $machine 硬中斷占用CPU的百分比: $cpuhi "
echo "$times $machine 軟中斷占用CPU的百分比: $cpusi "
echo "$times $machine 虛擬 CPU 等待實際 CPU 的時間的百分比: $cpust "
}
function echo_mem(){
echo "$times $machine 內存總容量: $memtotal M "
echo "$times $machine 空閑內存: $memfree M "
echo "$times $machine 已使用內存: $memused M "
echo "$times $machine 內存buff/cache: $membuff M "
echo "$times $machine swap總共: $swaptotal M "
echo "$times $machine swap空閑: $swaptotal M "
echo "$times $machine swap已使用: $swapused M "
echo "$times $machine swap內存avail Mem: $swapavail M "
}
function echo_load(){
echo "$times $machine 1分鐘CPU負載: $load1 "
echo "$times $machine 5分鐘CPU負載: $load5 "
echo "$times $machine 15分鐘CPU負載: $load15 "
}
#調用函數
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
#發送郵件
dos2unix -k $runlog
dos2unix -k $loadlog
dos2unix -k $cpulog
dos2unix -k $memlog
mailx -s "服務器$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 "服務器$names $machine的負載wa%" 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 "服務器$names $machine" lumia98@vip.qq.com < $disklog
fi
done
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。