Use PhantomJS to automatically take screenshots of Kibana, python to send website operation daily

If the title, first come to Zhang the final effect operation daily

The following describes the implementation process

【Preliminary preparations】

kibana configuration view, and do a good job of filtering the view, here is the introduction, you can refer to the blog post,

After the view is done, a short link is generated, here we generate

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

1. Install and configure 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 screenshot script

Create 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);
    }
});

After doing the above work, let's test whether it can take screenshots normally.

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

Sure enough, a TimeOut2s.png was generated under the path, and the screenshot was successful.

Well, the screenshot process is very simple. Next, configure python to send emails. I will go directly to the python code, everyone understands it.

#!/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 exception log analysis daily (%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 "Mail sent successfully!"
    except Exception, e:
        print "Failed to send mail: "+str(e)

if __name__ == '__main__':
    result = getPhantomjs()
    if result == 0:
        print "Fetching kibana statistics report successfully!"
        sendmail()
    else:
        print "Failed to fetch kibana statistics report!"
        exit(1)

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326192032&siteId=291194637