linux系统CPU内存磁盘监控发送邮件脚本之二

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux系统CPU内存磁盘监控发送邮件脚本之二相关的知识,希望对你有一定的参考价值。

#!/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 [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 $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" [email protected] < $runlog

mailx -s "服务器$names $machine" [email protected] < $loadlog

mailx -s "服务器$names $machine" [email protected] < $cpulog

mailx -s "服务器$names $machine" [email protected] < $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%" [email protected] < $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" [email protected] < $disklog

fi

done

本文出自 “一招拜师” 博客,请务必保留此出处http://sadoc.blog.51cto.com/12189157/1943283

以上是关于linux系统CPU内存磁盘监控发送邮件脚本之二的主要内容,如果未能解决你的问题,请参考以下文章

简单监控liunx中cpu内存磁盘及发送邮件参考

十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件

开放系统监控shell脚本

用 Bash 脚本监控 Linux 上的内存使用情况 | Linux 中国

linux系统cpu,根分区,内存监控脚本

编写监控脚本,监控集群内所有服务存活状态,内存磁盘剩余率检测,异常则发送报警邮件