Skip to content

Instantly share code, notes, and snippets.

@kolobaev
Created June 25, 2018 09:56
Show Gist options
  • Save kolobaev/19646d0c16fa87c20bf579e1382ac479 to your computer and use it in GitHub Desktop.
Save kolobaev/19646d0c16fa87c20bf579e1382ac479 to your computer and use it in GitHub Desktop.
/* global _ */
/*
* Complex scripted dashboard
* This script generates a dashboard object that Grafana can load. It also takes a number of user
* supplied URL parameters (in the ARGS variable)
*
* Return a dashboard object, or a function
*
* For async scripts, return a function, this function must take a single callback function as argument,
* call this callback function with the dashboard object (look at scripted_async.js for an example)
*/
'use strict';
// accessible variables in this scope
var window, document, ARGS, $, jQuery, moment, kbn;
// Setup some variables
var dashboard;
// All url parameters are available via the ARGS object
var ARGS;
// Initialize a skeleton with nothing but a rows array and service object
dashboard = {
rows : [],
};
// Set a title
dashboard.title = 'Scripted dash';
// Set default time
// time can be overridden in the url using from/to parameters, but this is
// handled automatically in grafana core during dashboard initialization
dashboard.time = {
from: "now-1h",
to: "now"
};
var rows = 1;
var seriesName = 'seriesName';
var panelName = 'panelName';
var currentMetric = '';
if(!_.isUndefined(ARGS.rows)) {
rows = parseInt(ARGS.rows, 10);
}
if(!_.isUndefined(ARGS.seriesName)) {
seriesName = ARGS.seriesName;
}
if(!_.isUndefined(ARGS.panelName)) {
panelName = ARGS.panelName;
}
if(!_.isUndefined(ARGS.currentMetric)) {
currentMetric = ARGS.currentMetric;
}
for (var i = 0; i < rows; i++) {
dashboard.rows.push({
title: 'Chart',
height: '300px',
panels: [
{
title: panelName,
type: 'graph',
span: 12,
fill: 1,
linewidth: 2,
nullPointMode: 'connected',
targets: [
{
'target': "grep(" + seriesName + ", '" + currentMetric + "')"
}
],
seriesOverrides: [
{
alias: '/random/',
yaxis: 2,
fill: 0,
linewidth: 5
}
],
tooltip: {
shared: true
}
}
]
});
}
return dashboard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment