美文网首页
用python发送表格数据到邮箱

用python发送表格数据到邮箱

作者: 撸大师 | 来源:发表于2016-09-07 21:32 被阅读6840次

前言:运维工作中,我们经常需要将一些数据每天自动的发送到邮箱,比如网站的pv/uv、网站的状态码统计,或是一些其他日常监控数据的统计等,为了更方便的查看,我们采取用表格形式发到邮箱,这样既清晰有直观。

1、数据准备

  • 我写了一个shell统计脚本,用于统计各个网站的状态码数量,刚开始是直接输出结果,后来为了方便python拿数据,于是稍作修改,代码如下:
#!/bin/bash
#
index_name=logstash-$(date -d "-1 day -8 hour" "+%Y.%m.%d") 
#网站换成自己的网站,数据实际是通过elk收集,通过查询生成的json
for server in www.baidu.com www.360.com www.jd.com www.taobao.com
do
  sed -i "14 s#\"query\": \".*\",#\"query\": \"$server\",#" vd-23.json  #2xx和3xx状态码
  sed -i "14 s#\"query\": \".*\",#\"query\": \"$server\",#" vd-45.json  #4xx和5xx状态码
  sed -i "11 s#\"query\": \".*\",#\"query\": \"$server\",#" vd-all.json  #总数
#
  t2_count=`curl -s -XPOST  -H 'content-type: application/json' -d@/data/sh/vd-23.json http://www.elk.com:9200/${index_name}/_search?search_type=count|jq ".hits.total"`
  f4_count=`curl -s -XPOST  -H 'content-type: application/json' -d@/data/sh/vd-45.json http://www.elk.com:9200/${index_name}/_search?search_type=count|jq ".hits.total"`
  all_count=`curl -s -XPOST  -H 'content-type: application/json' -d@/data/sh/vd-all.json http://www.elk.com:9200/${index_name}/_search?search_type=count|jq ".hits.total"`
#
#变量本来是分开打的,后来改成全打在一行,其实可以考虑打成json格式,python更容易取
echo -n "$t2_count,`echo "$t2_count $all_count"|awk '{printf "%.4f%\n",$1/$2*100}'`,$f4_count,`echo "$f4_count $all_count"|awk '{printf "%.4f%\n",$1/$2*100}'`,$all_count,"
done

得到的数据是一堆用 , 连起来的数据,如下

11038332,91.0479%,1632,0.0135%,12123654,8143047,99.9921%,643,0.0079%,8143690,4558407,99.9964%,165,0.0036%,4558572,8336274,99.9991%,79,0.0009%,8336353,

2、分析数据

  • os.popen执行shell
import os
a=os.popen("bash /data/sh/md_sla.sh").read()
print a.strip('\n').split(',')

得到一串list
取值直接用list的方式取

3、取值并发送邮件

#!/usr/bin/env python
#coding:utf8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email import encoders
import sys
import os
from datetime import *
#
def send_mail(to_list, sub):
        me = mail_user 
        msg = MIMEMultipart()
        msg['Subject'] = sub
        msg['From'] = me
        msg['To'] = ",".join(to_list)
#创造数据
        a=os.popen("bash /data/sh/md_sla.sh").read().strip('\n').split(',')

        #构造html
        d = datetime.now()
        dt = d.strftime('%Y-%m-%d %H:%M:%S')
        at = (d - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S')
        timezone  = at + ' ~ ' + dt
#构造html
        html = """\
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>萌店AB环境</title>
<body>
<div id="container">
  <p><strong>萌店sla状态统计</strong></p>
  <p>采集时间: """ + timezone + """</p>
  <div id="content">
   <table width="500" border="2" bordercolor="red" cellspacing="2">
  <tr>
    <td><strong>站点</strong></td>
    <td><strong>总访问量</strong></td>
    <td><strong>正常数</strong></td>
    <td><strong>正常百分比</strong></td>
    <td><strong>异常数</strong></td>
    <td><strong>异常百分比</strong></td>
  </tr>
  <tr>
    <td><a href="http://log.xxx.com/#/dashboard/file/node.json">node</a></td>
    <td>""" + a[4] + """</td>
    <td>""" + a[0] + """</td>
    <td>""" + a[1] + """</td>
    <td>""" + a[2] + """</td>
    <td>""" + a[3] + """</td>
  </tr>
  <tr>
    <td><a href="http://log.xxxlcom/#/dashboard/file/api.json">api</a></td>
    <td>""" + a[9] + """</td>
    <td>""" + a[5] + """</td>
    <td>""" + a[6] + """</td>
    <td>""" + a[7] + """</td>
    <td>""" + a[8] + """</td>
  </tr>
  <tr>
    <td><a href="http://log.xxx.com/#/dashboard/file/mapi.json">mapi</a></td>
    <td>""" + a[14] + """</td>
    <td>""" + a[10] + """</td>
    <td>""" + a[11] + """</td>
    <td>""" + a[12] + """</td>
    <td>""" + a[13] + """</td>
  </tr>
  <tr>
    <td><a href="http://log.xxx.com/#/dashboard/file/yunying-sla.json">yunying</a></td>
    <td>""" + a[19] + """</td>
    <td>""" + a[15] + """</td>
    <td>""" + a[16] + """</td>
    <td>""" + a[17] + """</td>
    <td>""" + a[18] + """</td>
  </tr>
</table>
  </div>
</div>
<p><strong>点击站点名可查看详细表图</strong> </p>
</div>
</body>
</html>
        """
        context = MIMEText(html,_subtype='html',_charset='utf-8')  #解决乱码
        msg.attach(context) 
        try:
                send_smtp = smtplib.SMTP()
                send_smtp.connect(mail_host)
                send_smtp.login(mail_user, mail_pass)
                send_smtp.sendmail(me, to_list, msg.as_string())
                send_smtp.close()
                return True
        except Exception, e:
                print str(e)[1]
                return False
if __name__ == '__main__':
    # 设置服务器名称、用户名、密码以及邮件后缀
    mail_host = 'mail.126.com'
    mail_user = 'aaa@126.com'
    mail_pass = 'e123131da'    
    #mailto_lists = sys.argv[1]
    #mailto_list = mailto_lists.split(',')   #发送多人
    #sub= sys.argv[2]
    mailto_list = ['763836801@qq.com',]
    sub= "状态sla"
    #send_mail(mailto_list, sub)
    if send_mail(mailto_list, sub):
            print "Send mail succed!"
    else:
            print "Send mail failed!"
  • 发送结果如下:
Paste_Image.png

好了,结束,做个计划任务

相关文章

网友评论

      本文标题:用python发送表格数据到邮箱

      本文链接:https://www.haomeiwen.com/subject/hjxsettx.html