Python写一个小小的项目监控

#!/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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值