利用PhantomJS 自动截图Kibana ,python发送网站运营日报

如题,先来张最终效果运营日报

利用PhantomJS 自动截图Kibana ,python发送网站运营日报

利用PhantomJS 自动截图Kibana ,python发送网站运营日报

下面介绍下实现过程

【前期准备】

kibana配置视图,并做好条件过滤视图,这里就是做介绍,可以参考博文,

视图做好后生成一个短链接,这里我们生成的是

http://10.0.0.110:5601/goto/4d641c075d7cbf2c7d70a82b16436769

1、安装配置PhantomJS

# yum -y install gcc gcc-c++ make flex bison gperf ruby \
  openssl-devel freetype-devel fontconfig-devel libicu-devel sqlite-devel \
  libpng-devel libjpeg-devel bitmap-fonts bitmap-fonts-cjk
# git clone git://github.com/ariya/phantomjs.git
# cd phantomjs
# git checkout 1.9.8
# ./build.sh

2、PhantomJS截图脚本

创建TimeOut2s.js

var page = require('webpage').create();
var address = 'http://10.0.0.110:5601/goto/4d641c075d7cbf2c7d70a82b16436769';
var output = 'TimeOut2s.png';
page.viewportSize = { width: 1600, height: 600 };
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.render(output);
            phantom.exit();
        }, 20000);
    }
});

做完以上工作 ,我们先测试下,是否可以正常截图

#/data/programs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs  /data/scripts/reports/TimeOut2s.js

果然在路径下产生了一个TimeOut2s.png,截图成功

好了,截图过程很简单吧,接下来配置python发送邮件过程,,我直接上python代码,大家都懂的。

#!/usr/bin/env python
#coding: utf-8  
import smtplib  
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText  
from email.mime.image import MIMEImage
import datetime
import subprocess

def getPhantomjs():
    commend='phantomjs ./re_code.js'
    sub_commend = subprocess.Popen(commend, shell=True)
    result = sub_commend.wait()
    return result

def getYesterday():
    today=datetime.date.today()
    oneday=datetime.timedelta(days=1)
    yesterday=today-oneday
    return yesterday

def addimg(src,imgid):
    fp = open(src, 'rb')  
    msgImage = MIMEImage(fp.read())  
    fp.close()  
    msgImage.add_header('Content-ID', imgid)  
    return msgImage

def sendmail():
    yesterday = getYesterday()
    HOST = "smtp.sqbj.com"
    SUBJECT = u"ELK 异常日志分析日报(%s)"%(yesterday)
    TO = "[email protected]"
    FROM = "[email protected]"
    msg = MIMEMultipart('related')
    utext = '''
    <img src="cid:weekly" border="1" width="90%" height="auto">
    '''
    msgtext = MIMEText(utext,"html","utf-8")
    msg.attach(msgtext)
    msg.attach(addimg("/usr/script/elk_daily/recode.png","weekly"))

    #attach = MIMEText(open("/usr/script/elk_daily/recode.png", "rb").read(), "base64", "utf-8")
    #attach["Content-Type"] = "application/octet-stream"
    #msg.attach(attach)

    msg['Subject'] = SUBJECT
    msg['From']=FROM
    msg['To']=TO
    try:
        server = smtplib.SMTP()
        server.connect(HOST)
        server.login("[email protected]","wmj3267642")
        server.sendmail(FROM, TO, msg.as_string())
        server.quit()
        print "邮件发送成功!"
    except Exception, e:
        print "邮件发送失败:"+str(e)

if __name__ == '__main__':
    result = getPhantomjs()
    if result == 0:
        print "抓取kibana统计报表成功!"
        sendmail()
    else:
        print "抓取kibana统计报表失败!"
        exit(1)

相关推荐