码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-06-29 22:06:48      阅读:255      评论:0      收藏:0      [点我收藏+]

标签: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 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

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

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

标签:linux   监控   负载   磁盘   cpu   

原文地址:http://sadoc.blog.51cto.com/12189157/1943283

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!