把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