采用nodejs+phantom对grafana整个页面截图

2 篇文章 0 订阅
1 篇文章 0 订阅

把grafana整个页面定时保存为图片既方便发送邮件日报又方便存档。以下方式在grafana-6.6.1,phantom 6.3.0, nodejs 8.16.2上亲测可用。

1. 修改配置文件允许匿名访问(/etc/grafana/grafana.ini,注意需要去掉行开头的";"才能生效。尽量不要改默认文件conf/defaults.ini)

#################################### Anonymous Auth ######################
[auth.anonymous]
# enable anonymous access
enabled = true

# specify organization name that should be used for unauthenticated users
org_name = Main Org.

# specify role for unauthenticated users
org_role = Viewer

2. nodejs调用phantom抓取页面,待渲染完毕再保存为图片

npm install phantom
var phantom = require('phantom');

phantom.create().then(function(ph) {
    ph.createPage().then(function(page) {
        page.open("http://127.0.0.1:3000/d/13-JkEQWz/example?orgId=1&from=now-24h&to=now-3m").then(function(status) {
            page.property('viewportSize', { width: 1920, height: 3580 });
           setTimeout(function(){
            page.render('./images/report.png').then(function() {
                console.log('Page rendered');
                ph.exit();
            });
           },30000);
        });
    });
});

环境要求:

phantomjs安装

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/share/
ln -sf /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/phantomjs
# phantomjs --version
2.1.1

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值