python_sendmail_监控进程

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import smtplib
import os
import time
from email.mime.text import MIMEText
mailto_list=['[email protected]','[email protected]','[email protected]']
mail_host="smtp.abc.com"            #使用的邮箱的smtp服务器地址,这里是163的smtp地址
mail_user="abc"                           #用户名
mail_pass="abcd"                             #授权码密码
mail_postfix="abc.com"                     #邮箱的后缀,网易就是163.com

Java_port = os.popen('netstat -tunlp |grep "xxxx" |wc -l')
Counts = int(Java_port.read())
Port_num = xxxx
Down_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
def send_mail(to_list,sub,content):
    me="某 java 进程报告"+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content,_subtype='plain',_charset='utf-8')
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)                #将收件人列表以‘;’分隔
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)                            #连接服务器
        server.login(mail_user,mail_pass)               #登录操作
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception as e:
        print(e)
        return False
  

if Counts >0:
   print "ok"
else:
    print "this port is down\n时间:%s\nplease check it now" %(Down_time)
    if send_mail(mailto_list,"某 java 进程报告","the bjyz-kettle of java 端口号:%s is down\n 时间:%s\n please check it now!!!" %(Port_num,Down_time)):
        print "done"
    else:
        print "failed!"

猜你喜欢

转载自blog.csdn.net/zuopiezia/article/details/85714707