说明:此脚本安装在CentOS的操作系统上
一、监控脚本如下
vim /root/script/systemmonitor.sh
#!/bin/bash
#监控系统负载与CPU、内存、硬盘、登录用户数,超出警戒值则发邮件告警。
#提取本服务器的IP地址信息
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
# 1、监控系统负载的变化情况,超出时发邮件告警:
#抓取cpu的总核数
cpu_num=`grep -c ‘model name‘ /proc/cpuinfo`
#抓取当前系统15分钟的平均负载值
load_15=`uptime | awk ‘{print $12}‘`
#计算当前系统单个核心15分钟的平均负载值,结果小于1.0时前面个位数补0。
average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc`
#取上面平均负载值的个位整数
average_int=`echo $average_load | cut -f 1 -d "."`
#设置系统单个核心15分钟的平均负载的告警值为0.70(即使用超过70%的时候告警)。
load_warn=0.70
#当单个核心15分钟的平均负载值大于等于1.0(即个位整数大于0) ,直接发邮件告警;如果小于1.0则进行二次比较
if (($average_int > 0)); then
echo "$IP服务器15分钟的系统平均负载为$average_load,超过警戒值1.0,请立即处理!!!" | mail -s "$IP 服务器系统负载严重告警!!!" [email protected]
else
#当前系统15分钟平均负载值与告警值进行比较(当大于告警值0.70时会返回1,小于时会返回0 )
load_now=`expr $average_load \> $load_warn`
#如果系统单个核心15分钟的平均负载值大于告警值0.70(返回值为1),则发邮件给管理员
if (($load_now == 1)); then
echo "$IP服务器15分钟的系统平均负载达到 $average_load,超过警戒值0.70,请及时处理。" | mail -s "$IP 服务器系统负载告警" [email protected]
fi
fi
# 2、监控系统cpu的情况,当使用超过80%的时候发告警邮件:
#取当前空闲cpu百份比值(只取整数部分)
cpu_idle=`top -b -n 1 | grep Cpu | awk ‘{print $5}‘ | cut -f 1 -d "."`
#设置空闲cpu的告警值为20%,如果当前cpu使用超过80%(即剩余小于20%),立即发邮件告警
if (($cpu_idle < 20)); then
echo "$IP服务器cpu剩余$cpu_idle%,使用率已经超过80%,请及时处理。" | mail -s "$IP 服务器CPU告警" [email protected]
fi
# 3、监控系统交换分区swap的情况,当使用超过90%的时候发告警邮件:
#系统分配的交换分区总量
swap_total=`free -m | grep Swap | awk ‘{print $2}‘`
#当前剩余的交换分区free大小
swap_free=`free -m | grep Swap | awk ‘{print $4}‘`
#当前已使用的交换分区used大小
swap_used=`free -m | grep Swap | awk ‘{print $3}‘`
if (($swap_used != 0)); then
#如果交换分区已被使用,则计算当前剩余交换分区free所占总量的百分比,用小数来表示,要在小数点前面补一个整数位0
swap_per=0`echo "scale=2;$swap_free/$swap_total" | bc`
#设置交换分区的告警值为10%(即使用超过90%的时候告警)。
swap_warn=0.10
#当前剩余交换分区百分比与告警值进行比较(当大于告警值(即剩余10%以上)时会返回1,小于(即剩余不足10%)时会返回0 )
swap_now=`expr $swap_per \> $swap_warn`
#如果当前交换分区使用超过90%(即剩余小于10%,上面的返回值等于0),立即发邮件告警
if (($swap_now == 0)); then
echo "$IP服务器swap交换分区只剩下 $swap_free M 未使用,剩余不足10%,使用率已经超过90%,请及时处理。" | mail -s "$IP 服务器内存告警" [email protected]
fi
fi
# 4、监控系统硬盘根分区使用的情况,当使用超过80%的时候发告警邮件:
#取当前根分区(/dev/sda2)已用的百份比值(只取整数部分)
disk_sda2=`df -h | grep /dev/sda2 | awk ‘{print $5}‘ | cut -f 1 -d "%"`
#设置空闲硬盘容量的告警值为80%,如果当前硬盘使用超过80%,立即发邮件告警
if (($disk_sda2 > 80)); then
echo "$IP 服务器 /根分区 使用率已经超过80%,请及时处理。" | mail -s "$IP 服务器硬盘告警" [email protected]
fi
#5、监控系统用户登录的情况,当用户数超过3个的时候发告警邮件:
#取当前用户登录数(只取数值部分)
users=`uptime | awk ‘{print $6}‘`
#设置登录用户数的告警值为3个,如果当前用户数超过3个,立即发邮件告警
if (($users >= 3)); then
echo "$IP 服务器用户数已经达到$users个,请及时处理。" | mail -s "$IP 服务器用户数告警" [email protected]
fi
二、给脚本添加执行权限
chmod +x /root/script/systemmonitor.sh
三、将脚本加入计划任务
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * root /root/script/systemmonitor.sh
四、发邮件到自己注册的163邮箱
(1)首先要安装mailx
yum install -y mailx
(2) 其次配置mail.rc,配置文件如下
cat /etc/mail.rc
# This is the configuration file for Heirloom mailx (formerly
# known under the name "nail".
# See mailx(1) for further options.
# This file is not overwritten when ‘make install‘ is run in
# the mailx build process again.
# Sccsid @(#)nail.rc 2.10 (gritter) 3/4/06
# Do not forward to mbox by default since this is likely to be
# irritating for most users today.
set hold
# Append rather than prepend when writing to mbox automatically.
# This has no effect unless ‘hold‘ is unset again.
set append
# Ask for a message subject.
set ask
# Assume a CRT-like terminal and invoke a pager.
set crt
# Messages may be terminated by a dot.
set dot
# Do not remove empty mail folders in the spool directory.
# This may be relevant for privacy since other users could
# otherwise create them with different permissions.
set keep
# Do not remove empty private mail folders.
set emptybox
# Quote the original message in replies by "> " as usual on the Internet.
set indentprefix="> "
# Automatically quote the text of the message that is responded to.
set quote
# Outgoing messages are sent in ISO-8859-1 if all their characters are
# representable in it, otherwise in UTF-8.
set sendcharsets=iso-8859-1,utf-8
# Display sender‘s real names in header summaries.
set showname
# Display the recipients of messages sent by the user himself in
# header summaries.
set showto
# Automatically check for new messages at each prompt, but avoid polling
# of IMAP servers or maildir folders.
set newmail=nopoll
# If threaded mode is activated, automatically collapse thread.
set autocollapse
# Hide some header fields which are uninteresting for most human readers.
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
# Only include selected header fields when forwarding messages.
fwdretain subject date from to
# For Linux and BSD, this should be set.
set bsdcompat
set [email protected]
set smtp=smtp.163.com
set [email protected]
set smtp-auth-password=wxhjshfjsjdfsjdfjdhjdsfh #这个是163.com的POP开通验证码
set smtp-auth=login
set ssl-verify=ignore
(3)重启邮件服务,如果没有sendmail就yum安装yum install -y sendmail
service sendmail restart ##二次,第一次失败
service sendmail restart ##第二次成功
(4)验证
echo "test" | mail -s "test" [email protected]
(5)查看163.com的邮箱发现有邮件说明成功
这里要先开通163的邮件协议,开通之前要开通验证码这个只出现一次要记好,这个就是mail.rc的smtp-auth-password密码,之前不需要,现在163改变了。