博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现celery中出现拥挤队列时,及时发邮件通知
阅读量:5915 次
发布时间:2019-06-19

本文共 2834 字,大约阅读时间需要 9 分钟。

里面有几个常用的功能,以后值得借鉴。

如获取脚本目录,IP,获取shell返回值,发送邮件等..

上午写完,中午测试,下午上线~~

 

#!/usr/bin/env python# -*- coding:utf-8 -*-import smtplibfrom email.mime.text import MIMETextimport socketimport fcntlimport structimport sysimport os######################################## 实现celery中出现拥挤队列时,及时发邮件通知 ##             aguncn@163.com          ######################################### 从系统命令中获取返回值def run_cmd(cmd):      try:          import subprocess      except ImportError:          _, result_f, error_f = os.popen3(cmd)      else:          process = subprocess.Popen(cmd, shell = True,          stdout = subprocess.PIPE, stderr = subprocess.PIPE)          result_f, error_f = process.stdout, process.stderr        errors = error_f.read()      if errors:        pass      result_str = result_f.read().strip()      if result_f:        result_f.close()      if error_f:        error_f.close()        return result_str# 格式化命令,获取脚本绝对脚本,crontab也可执行def format_cmd(cmd):    filename = sys.argv[0]    dirname = os.path.dirname(filename)    abspath = os.path.abspath(dirname)    return "python %s/manage_prd.py celery inspect %s -d celeryd@deploy" % (abspath, cmd)# 获取指定网卡的IPdef get_ip(ifname):      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)      return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])  #发送邮件class PM(object):    def __init__(self, msg_str=None):        self.msg_str = msg_str    def send_email(self, email_list):        mail_host = "x.x.x.x"        sender_email = "a@b.c"        subject = "celery alert"        msg = MIMEText(self.msg_str.encode('utf8'), _subtype='plain', _charset='utf8')        msg['From'] = sender_email        msg['Subject'] = u'%s' % subject        msg['To'] = ",".join(email_list)        try:            s = smtplib.SMTP(mail_host, 25)            s.sendmail(sender_email, receivers_list, msg.as_string())            s.close()        except Exception as e:            print 'Exception: ', e            if __name__ == '__main__':    active_cmd_str = format_cmd("active")    revoked_cmd_str = format_cmd("revoked")    reserved_cmd_str = format_cmd("reserved")    reserved_return_str = run_cmd(reserved_cmd_str)    if "empty" not in reserved_return_str:        receivers_list = ["a@b.c", "a@b.c"]        ip = get_ip('eth0')        email_str = "http://%s:8000/" %(ip)        email_str = email_str + "\n" + "active: " + "\n"        email_str = email_str + run_cmd(active_cmd_str) + "\n"        email_str = email_str + "reserved: " + "\n"        email_str = email_str + run_cmd(reserved_cmd_str) + "\n"        email_str = email_str + "revoked: " + "\n"        email_str = email_str + run_cmd(revoked_cmd_str) + "\n"        email_inst = PM(email_str)        email_inst.send_email(receivers_list)        print "send a alert email."    else:        print "all ok, no email send."

 

转载地址:http://togpx.baihongyu.com/

你可能感兴趣的文章
C++ Error : initial value of reference to non-const must be an lvalue
查看>>
lua笔记
查看>>
线程函数
查看>>
Jmeter+maven+Jenkins构建云性能测试平台(mark 推荐)
查看>>
REMOTE HOST IDENTIFICATION HAS CHANGED
查看>>
信号处理——曲线拟合与分布拟合
查看>>
Unity的Asset Store商店下载文件路径
查看>>
hadoop2.4.1 伪分布
查看>>
LDA与QDA
查看>>
使用Gson将对象类转成Json对象时出现\u003d的问题
查看>>
开发遇到的奇葩问题
查看>>
【WPF】自定义形状的按钮Button
查看>>
InlineModelAdmin
查看>>
R语言可视化学习笔记之添加p-value和显著性标记--转载
查看>>
NGUI可展开列表的实现
查看>>
现代软件工程 期中/期末总结博客作业
查看>>
DateTime.ToString()的用法
查看>>
SQL重复记录查询(转载)
查看>>
nginx模块_使用gdb调试nginx源码
查看>>
Xamarin.Android之定位
查看>>