1. 程式人生 > >2018-06-05 Linux學習

2018-06-05 Linux學習

Linux學習

20.23 告警系統郵件引擎(上)

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??

操作過程

[root@linux-01 mon]# cp /usr/lib/zabbix/alertscripts/mail.py mail/mail.py
[root@linux-01 mon]# cat mail/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.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]
##定義QQ郵箱的賬號和密碼,你需要修改成你自己的賬號和密碼(請不要把真實的用戶名和密碼放到網上公開,否則你會死的很慘)
sendqqmail(‘[email protected]‘,‘aaaaa‘,‘[email protected]‘,to,subject,content)
br/>to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##定義QQ郵箱的賬號和密碼,你需要修改成你自己的賬號和密碼(請不要把真實的用戶名和密碼放到網上公開,否則你會死的很慘)
sendqqmail(‘[email protected]‘,‘aaaaa‘,‘[email protected]‘,to,subject,content)
name == "main":
main()

#####腳本使用說明######
#1. 首先定義好腳本中的郵箱賬號和密碼
#2. 腳本執行命令為:python mail.py 目標郵箱 "郵件主題" "郵件內容"

[root@linux-01 mon]# vim mail/mail.sh
寫入上面 mail.sh內容

20.26 運行告警系統

2018-06-05 Linux學習