#!/bin/bash
#触发阈值发邮件还没有写.
export PATH
export LANG=zh_CN.UTF-8
#先配置好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
readonly times=$(date +%Y"/"%m"/"%d" "%k":"%M)
readonly machine="192.168.1.10"
function top_check(){
runup=$(top -n 1|grep user|awk -F ‘[, ]+‘ ‘{print $5}‘)
echo "$times $machine 运行多久: $runup 分钟"
userd=$(top -n 1|grep user|awk -F ‘[, ]+‘ ‘{print $7}‘)
echo "$times $machine 当前在线用户数: $userd"
load1=$(top -n 1|grep user|awk -F ‘[, ]+‘ ‘{print $11}‘)
echo "$times $machine 1分钟CPU负载: $load1"
load5=$(top -n 1|grep user|awk -F ‘[, ]+‘ ‘{print $12}‘)
echo "$times $machine 5分钟CPU负载: $load5"
load15=$(top -b -n 1|grep user|awk ‘{print $NF}‘)
echo "$times $machine 15分钟CPU负载: $load15"
cpuus=$(top -n 1|grep Cpu|awk -F "," ‘{print $1}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 用户空间占用CPU百分比: $cpuus"
cpusy=$(top -n 1|grep Cpu|awk -F "," ‘{print $2}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 内核空间占用CPU百分比: $cpusy"
cpuni=$(top -n 1|grep Cpu|awk -F "," ‘{print $3}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 改变过优先级的进程占用CPU的百分比: $cpuni"
cpuid=$(top -n 1|grep Cpu|awk -F "," ‘{print $4}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 空闲CPU百分比: $cpuid"
cpuwa=$(top -n 1|grep Cpu|awk -F "," ‘{print $5}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine I/O等待占用CPU的百分比: $cpuwa"
cpuhi=$(top -n 1|grep Cpu|awk -F "," ‘{print $6}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 硬中断占用CPU的百分比: $cpuhi"
cpusi=$(top -n 1|grep Cpu|awk -F "," ‘{print $7}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 软中断占用CPU的百分比: $cpusi"
cpust=$(top -n 1|grep Cpu|awk -F "," ‘{print $8}‘|awk -F " " ‘{print $2}‘)
echo "$times $machine 虚拟 CPU 等待实际 CPU 的时间的百分比: $cpust"
#内存部分M为单位
memtotal=$(($(top -n 1|grep buff/cache|awk -F ‘[, ]+‘ ‘{print $4}‘) / 1024))
echo "$times $machine 内存总共: $memtotal M"
memfree=$(($(top -n 1|grep buff/cache|awk -F ‘[, ]+‘ ‘{print $7}‘) / 1024))
echo "$times $machine 空闲内存: $memfree M"
memused=$(($(top -n 1|grep buff/cache|awk -F ‘[, ]+‘ ‘{print $10}‘) / 1024))
echo "$times $machine 已使用内存: $memused M"
membuff=$(($(top -n 1|grep buff/cache|awk -F ‘[, ]+‘ ‘{print $13}‘) / 1024))
echo "$times $machine 内存buff/cache: $membuff M"
#swap部分M
swaptotal=$(($(top -n 1|grep avail|awk -F ‘[, ]+‘ ‘{print $3}‘) / 1024))
echo "$times $machine swap总共: $swaptotal M"
swapfree=$(($(top -n 1|grep avail|awk -F ‘[, ]+‘ ‘{print $6}‘) / 1024))
echo "$times $machine swap空闲: $swaptotal M"
swapused=$(($(top -n 1|grep avail|awk -F ‘[, ]+‘ ‘{print $9}‘) / 1024))
echo "$times $machine swap已使用: $swapused M"
swapavail=$(($(top -n 1|grep avail|awk -F ‘[, ]+‘ ‘{print $11}‘) / 1024))
echo "$times $machine swap内存avail Mem: $swapavail M"
}
files="/tmp/top.logs"
top_check >$files
dos2unix -k $files
mailx -s "$machine" [email protected] < $files