20.23 20.4 20.4 告警系統郵件引擎(上中下)python
shell項目-告警系統mail.shlinux
1. 建立mail.py腳本 :git
[root@hao-01 ~]# vim /usr/local/bin/mon/mail/mail.pyshell
#####腳本使用說明######vim
#1. 首先定義好腳本中的郵箱帳號和密碼bash
#2. 腳本執行命令爲:python mail.py 目標郵箱 "郵件主題" "郵件內容"ide
內容參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.pyspa
添加內容 :.net
#!/usr/bin/env pythondebug
#-*- coding: UTF-8 -*-
import os,sys
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.163.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]
##定義郵箱的帳號和密碼,你須要修改爲你本身的帳號和密碼(請不要把真實的用戶名和密碼放到網上公開)
sendqqmail('zhuneianxiang@163.com','mima','zhuneianxiang@163.com',to,subject,content)
if __name__ == "__main__":
main()
2. 設定mail.py腳本權限爲755 :
[root@hao-01 ~]# chmod 755 /usr/local/bin/mon/mail/mail.py
3. 建立mail.sh腳本 :
[root@hao-01 ~]# vim /usr/local/bin/mon/mail/mail.sh
mail.sh內容 //其中mail.py內容到這裏下載https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py
添加內容 :
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
20.26 運行告警系統
1. 暫時關閉掉502:
[root@hao-01 ~]# vim /usr/local/bin/mon/conf/mon.conf
2. 暫時註釋掉main.sh腳本中日誌行:
[root@hao-01 ~]# vim /usr/local/bin/mon/bin/main.sh
3. 執行main.sh
[root@hao-01 ~]# sh -x /usr/local/bin/mon/bin/main.sh
4. 執行load.sh
[root@hao-01 ~]# bash /usr/local/bin/mon/shares/load.sh