[root@garytao-01 mon]# cd mail/
[root@garytao-01 mail]# vim mail.py
增加如下内容:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
gserver = ‘smtp.qq.com‘
gport = 25
try:
msg = MIMEText(unicode(content).encode(‘utf-8‘))
msg[‘from‘] = mailfrom
msg[‘to‘] = mailto
msg[‘Reply-To‘] = mailfrom
msg[‘Subject‘] = subject
smtp = smtplib.SMTP(gserver, gport)
smtp.set_debuglevel(0)
smtp.ehlo()
smtp.login(username,password)
smtp.sendmail(mailfrom, mailto, msg.as_string())
smtp.close()
except Exception,err:
print "Send mail failed. Error: %s" % err
def main():
to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)
sendqqmail(‘1234567@qq.com‘,‘aaaaaaaaaa‘,‘1234567@qq.com‘,to,subject,content)
if __name__ == "__main__":
main()
#####脚本使用说明######
#1. 首先定义好脚本中的邮箱账号和密码
#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
##在mail目录下创建脚本mail.sh ,当系统服务异常的时候就会调用mail.sh
[root@garytao-01 mail]# vim mail.sh
增加如下内容:
log=$1
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
echo $t_s2 > /tmp/$log
fi
t_s2=`tail -1 /tmp/$log|awk ‘{print $1}‘`
echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2]
echo $v
if [ $v -gt 3600 ]
then
./mail.py $1 $2 $3
echo "0" > /tmp/$log.txt
else
if [ ! -f /tmp/$log.txt ]
then
echo "0" > /tmp/$log.txt
fi
nu=`cat /tmp/$log.txt`
nu2=$[$nu+1]
echo $nu2>/tmp/$log.txt
if [ $nu2 -gt 10 ]
then
./mail.py $1 "trouble continue 10 min $2" "$3"
echo "0" > /tmp/$log.txt
fi
fi??
[root@garytao-01 mail]# pwd
/usr/local/sbin/mon/mail
[root@garytao-01 mail]# crontab -e
[root@garytao-01 mail]# cd /usr/local/sbin/mon/bin/
[root@garytao-01 bin]# sh -x main.sh
+ export send=1
+ send=1
++ /sbin/ifconfig
++ grep -A1 ‘ens33: ‘
++ awk ‘/inet/ {print $2}‘
+ export addr=172.16.111.100
+ addr=172.16.111.100
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ echo /usr/local/sbin/mon/bin
++ awk -F/ ‘{print $NF}‘
+ last_dir=bin
+ ‘[‘ bin == bin ‘]‘
+ conf_file=../conf/mon.conf
+ exec
[root@garytao-01 bin]# cat ../log/err.log //查看日志
++ date ‘+%F %T‘
+ echo ‘2018-02-07 17:01:39 load average‘
+ /bin/bash ../shares/load.sh
+ grep -q to_mon_502=1 ../conf/mon.conf
++ grep logfile= ../conf/mon.conf
++ awk -F = ‘{print $2}‘
++ sed ‘s/ //g‘
+ export log=/data/log/xxx.xxx.com/access.log
+ log=/data/log/xxx.xxx.com/access.log
+ $‘/bin/bash\302\240\302\240../shares/502.sh‘
main.sh:行23: /bin/bash??../shares/502.sh: 没有那个文件或目录
[root@garytao-01 bin]# vi ../conf/mon.conf //不需要写入502
[root@garytao-01 bin]# vi main.sh //暂时注释掉日志
[root@garytao-01 bin]# sh -x main.sh //重新执行脚本
+ export send=1
+ send=1
++ grep -A1 ‘ens33: ‘
++ awk ‘/inet/ {print $2}‘
++ /sbin/ifconfig
+ export addr=172.16.111.100
+ addr=172.16.111.100
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ echo /usr/local/sbin/mon/bin
++ awk -F/ ‘{print $NF}‘
+ last_dir=bin
+ ‘[‘ bin == bin ‘]‘
+ conf_file=../conf/mon.conf
++ date ‘+%F %T‘
+ echo ‘2018-02-26 19:02:10 load average‘
2018-02-26 19:02:10 load average
+ /bin/bash ../shares/load.sh
19:02:10 load is 0
+ grep -q to_mon_502=1 ../conf/mon.conf
[root@garytao-01 bin]# bash ../shares/load.sh
19:03:04 load is 0
原文地址:http://blog.51cto.com/taoxie/2073229