Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141221
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: Python/Ruby

2017-03-01 09:42:56


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # author: Dily
  4. #  Email: 312797697@qq.com
  5. """功能: 模拟用户监控直播平台服务存活"""

  6. import logging
  7. import re
  8. import time
  9. import subprocess
  10. import smtplib
  11. import os
  12. import string
  13. from email.mime.text import MIMEText

  14. def mylog_init():
  15.     """初始化日志格式"""
  16.     logging.basicConfig(level=logging.DEBUG,
  17.     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s:%(name)s:%(message)s ',
  18.     datefmt='%a, %d %b %Y %H:%M:%S',
  19.     filename='/work/dily/live/rtmp.log',
  20.     filemode='w+')

  21. def send_mail(sub,content):
  22.   """发送报警邮件"""
  23.   # 收件人列表
  24.   mailto_list=['312797697@qq.com']
  25.   # 使用的邮箱的smtp服务器地址
  26.   mail_host="mail.xxx.com"
  27.   # 用户名
  28.   mail_user="xxxxxx"
  29.   # 密码
  30.   mail_pass="xxxxxx"
  31.   # 邮箱主域名
  32.   mail_postfix="xxx.com"

  33.   me="Rtmp_Check_Mail"+"<"+mail_user+"@"+mail_postfix+">"
  34.   msg = MIMEText(content,_subtype='plain')
  35.   msg['Subject'] = sub
  36.   msg['From'] = me
  37.   # 将收件人列表以';'分隔
  38.   msg['To'] = ";".join(mailto_list)
  39.   try:
  40.     server = smtplib.SMTP()
  41.     # 连接邮件服务器
  42.     server.connect(mail_host,"25")
  43.     # 登录邮件服务器
  44.     server.login("xxxxxx",mail_pass)
  45.     server.sendmail(me, mailto_list, msg.as_string())
  46.     server.close()
  47.     return True
  48.   except Exception, e:
  49.     print str(e)
  50.     logging.error(str(e))
  51.     return False

  52. def rtmp_logtime():
  53.   """计算logtime时间"""
  54.   mytime = list(time.strftime('%Y%m%d%H%M',time.localtime(time.time())))
  55.   if mytime[-1] == '0' or mytime[-1] == '1' or mytime[-1] == '2' or mytime[-1] == '3' or mytime[-1] == '4':
  56.     mytime[-1] = '0'
  57.     logtime = ''.join(mytime)
  58.     return logtime
  59.   else:
  60.     mytime[-1] = '5'
  61.     logtime = ''.join(mytime)
  62.     return logtime
  63.  
  64. def rtmp_domain():
  65.   """截取rtmp实时URL"""
  66.   result = 'sudo pssh -x "-p12321" -t 60 -H IP -q -P "cd /cache/logs/acc_log/ && ls -t |grep s.xxx.com_access |head -1 | xargs cat | grep PLAY | tail -1"'
  67.   logging.info(result)
  68.   rtmp_res = subprocess.Popen(result,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
  69.   result_domain = rtmp_res.stdout.readlines()
  70.   logging.info(result_domain)
  71.   C = r'([a-zA-z]+://[^\"\s]+)'
  72.   find = re.compile(C)
  73.   m = find.search(str(result_domain))
  74.   try:
  75.     return m.group(1)
  76.   except AttributeError,e:
  77.     print '没有最新日志可收集'
  78.     logging.error('没有最新日志可收集')
  79.  
  80. def rtmp_ck():
  81.   """检查边缘节点服务是否正常"""
  82.   result = '/usr/local/bin/rtmp_check %s' % (str(rtmp_domain()))
  83.   logging.info('rtmp check cmd is : %s',result)
  84.   rtmp_res = subprocess.Popen(result,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
  85.   error = []
  86.   pattern = re.compile(
  87.     r"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))")
  88.   for line in rtmp_res.stdout.readlines():
  89.     logging.info(' %s',line.strip())
  90.     if 'ERROR' in line:
  91.       error.append(line.strip())
  92.   logging.debug(error)
  93.   if error:
  94.     if send_mail('Fastweb Rtmp Check FAILURE','\n'.join(error)):
  95.       logging.info('mail send success!')
  96.     else:
  97.       logging.error('mail send faild!')
  98.   else:
  99.     logging.info('rtmp check all success!')

  100. def main():
  101.   """主函数"""
  102.   mylog_init()
  103.   rtmp_ck()

  104. if __name__ == '__main__':
  105.   main()

阅读(2104) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~