释放双眼,带上耳机,听听看~!
**1、监控CPU利用率(通过vmstat工具) **
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 1#!/bin/bash
2#====================================================
3# Create Date: 2015-02-01
4# Description: cpu utilization monitor
5#====================================================
6if [ `uname` != "Linux" ];then
7 echo "check os not linux."
8 exit 1
9fi
10which vmstat &>/dev/null #这样的写法.这条命令的意思是将标准输出(返回0)和错误输出(返回非0)全部重定向到/dev/null中,也就是将产生的所有信息丢弃.
11if [ $? -ne 0 ];then
12 echo "vmstat command no found, please install procps package."
13 exit 1
14fi
15
16######################################################################
17
18cpu_us=`vmstat | awk '{print $13}' | sed -n '$p'` #awk 命令 输出第13列的数据 sed -n '$p' 最简输出内容。
19cpu_sy=`vmstat | awk '{print $14}' | sed -n '$p'`
20cpu_id=`vmstat | awk '{print $15}' | sed -n '$p'`
21cpu_wa=`vmstat | awk '{print $16}' | sed -n '$p'` #等待I/0完成
22cpu_sum=$(($cpu_us+$cpu_sy))
23cpu_info()
24{
25echo "CPU_Sum : $cpu_sum% ( CPU_Use:${cpu_us}% , CPU_System:${cpu_sy}% )"
26echo "CPU_Idle : ${cpu_id}%"
27echo "CPU_Wait : ${cpu_wa}"
28}
29#cpu_info;
30if [ $cpu_sum -ge 90 ];then
31 echo "CPU utilization $cpu_sum." | mail -s "CPU Monitor" xxxx@163.com
32
33fi
34
2、监控内存利用率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1#!/bin/bash
2#====================================================
3# Create Date: 2015-02-01
4# Description: memory utilization monitor
5#====================================================
6which bc &>/dev/null #验证bc命令是否存在,若存在返回值是0,若不存在返回值是非零。
7if [ $? -ne 0 ];then
8 echo "bc command no found, Please install bc package."
9 exit 1
10fi
11Date=`date +%F" "%H:%M` #日期输出 %F 日期(年月日) ; " "分隔符调整显示后的格式 ; %H:%M 时间(时:分)。
12IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2` #查询ip地址
13Total=`free -m | grep Mem | awk '{print $2}'` #查询剩余内存还剩多少以 -m (以M为单位输出)
14Use=`free -m | awk '/buffers\// {print $NF}'`
15Free=$(($Total-$Use))
16Total_conv=`echo "scale=2;$Total/1024" | bc | awk '{print $1"G"}'` #通过bc计算,保留小数点后两位(scale)
17if [ $Free -lt 200 ];then
18 Content=`echo -e "Date : $Date \nHost : $IP \nTotal : ${Total_conv} \nUse : ${Use}M \nFree : ${Free}M"`
19 echo "$Content" | mail -s "Memory Monitor" xxxx@163.com
20fi
21
3、监控磁盘利用率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1#!/bin/bash
2#====================================================
3# Create Date: 2015-02-01
4# Description: disk utilization monitor
5#====================================================
6Date=`date +%F" "%H:%M`
7IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2`
8Total=`fdisk -l | grep "Disk /dev/sd[a-z]" |awk '{print $2$3"GB"}' |sed 's/:/=/' |xargs echo -n |sed 's/[ ]/,/g'` #去掉换行符,并以逗号分隔在邮件显示总每个分区大小
9Disk_Use=`df -h |awk '{print $1"="$5}' | sed '1d' | sed 's/%//g'`
10for i in $Disk_Use
11do
12 A=`echo $i |awk -F'=' '{print $2}'`
13 if [ $A -gt 8 ];then
14 echo -e "Date : $Date \nHost : $IP \nTotal : $Total \nProblem : Part Use ${i}%" | mail -s "Disk Mo
15nitor" xxxx@163.com
16 fi
17done
18