Alarm system mail engine

20.23-20.25 Alarm system mail engine

Create a script for sending emails - mail.py

[root@localhost mail]# pwd
/usr/local/sbin/mon/mail

[root@localhost 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.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(): br/>to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##Define the account and password of QQ mailbox, you need Change it to your own account and password (please don't put the real username and password on the Internet
, or you will die miserably)
sendqqmail('[email protected]','sdfsd2.','asdgsfsdl@163. com',to,subject,content)
if name == " main ":
main()

#####Script Instructions######
#1. First define the email account and password in the script#2. The script execution command is: python mail.py Create
target mailbox "mail subject" "mail content"
Mail engine configuration file

#This script is used for alarm convergence
#Written by Adai
log=$1
t_s= #Timestamp date +%s
1
t_s2 = #Timestamp date -d "2 hours ago" +%s
2 (time two hours ago) #The
purpose of defining this timestamp is to ensure that the script is executed for the first time When v is greater than one hour
#guarantee the alarm when it is executed for the first time
#Because the script will only be executed when a failure occurs, so the first execution must be alarmed
if [ ! -f /tmp/$log ]
then
echo $ t_s2 > /tmp/$log
fi #Create
a log file that records timestamps
t_s2= tail -1 /tmp/$log|awk '{print $1}'
echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2] #Interval
time between two timestamps
echo $v
# Timer:
if [ $v -gt 3600 ]
then #If
the time interval is greater than 1 hour, start the alarm
system./mail.py $1 $2 $3
echo "0" > /tmp/$log.txt
#$log.txt is Counter file:
else
if [ ! -f /tmp/$log.txt ]
then
echo "0" > /tmp/$log.txt
fi
#View the number in the counter file
nu= cat /tmp/$log.txt
nu2=$[$nu+1]
echo $nu2>/tmp/$log.txt #Reset the
value of $log.txt
if [ $nu2 -gt 10 ]
then #Alarm
convergence: The fault lasts for ten minutes, and the alarm
starts./mail.py $1 "trouble continue 10 min $2" "$3"
echo "0" > /tmp/$log.txt #Restart
counting after the alarm ends
fi
fi
20.26 Run the alarm system

Execute the alarm system, write the alarm task into the scheduled task, and execute it every minute.

[root@localhost mail]# crontab -e

          • cd /usr/local/sbin/mon/bin; bash main.sh

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843854&siteId=291194637