#!/usr/bin/python
# -*- encoding:utf-8 -*-
# 日志信息落库,监控ERROR数量,ERROR达到3个以上发出邮件告警
# db_conn() 连接数据库
# get_time() 返回当日时间
# get_data() 获取数据库数据数据
# send_mail() 发送邮件
# task() 判断ERROR数量
import MySQLdb
import time
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def db_conn():
host = "*.*.*.*"
port = 5099
db = "****"
userid = "***"
passwd = "****"
conn = MySQLdb.connect(host=host,db=db,user=userid,passwd=passwd,port=port)
return conn
def get_time():
cur_date = time.strftime("%Y-%m-%d",time.localtime(time.time()))
return cur_date
def get_data():
cur_date = get_time()
sql = "select * from record_handle where handle_code='ERROR' and created_date = %s"
db = db_conn()
cursor = db.cursor()
data = cursor.execute(sql,cur_date)
#data = cursor.fetchall()
return data
def send_mail():
sender_mail = "*********"
sender_passwd = "*********"
to = "*********"
msg_root = MIMEMultipart('mixed')
msg_root["From"] = "*********"
msg_root["To"] = to
subject = "Log have logs of ERROR!!"
msg_root["Subject"] = Header(subject,'utf-8')
text_info = "Log Error "
text_sub=MIMEText(text_info,'plain','utf-8')
msg_root.attach(text_sub)
try :
sftp_obj = smtplib.SMTP_SSL('smtp.exmail.qq.com',465)
sftp_obj.login(sender_mail,sender_passwd)
sftp_obj.sendmail(sender_mail,to,msg_root.as_string())
sftp_obj.quit()
print("email sucess send")
except Exception as e:
print("fail")
print(e)
def task():
datas = get_data()
datas = int(datas)
if datas > 3:
send_mail()
else:
pass
if __name__ == "__main__":
task()
Python写一个小小的项目监控
最新推荐文章于 2024-02-01 10:03:30 发布