Last active
January 7, 2016 15:48
-
-
Save poppgs/cd6250f1f792efa9b393 to your computer and use it in GitHub Desktop.
A dashboard generator for Grafana
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
var host_regex = ".*"; | |
var client_name = ""; | |
// Grab host name from url param | |
if(!_.isUndefined(ARGS.host)) { | |
host_regex = ARGS.host; | |
console.log("Set host regex to "+host_regex); | |
} | |
// Grab full 'human readable' client name from url param | |
if(!_.isUndefined(ARGS.client)) { | |
client_name = ARGS.client; | |
console.log("Set host regex to "+host_regex); | |
} | |
var host_re = new RegExp(host_regex); | |
/* | |
make_panel | |
Return a panel with the hostname | |
populated in strategic places | |
*/ | |
function make_panel(hostname) { | |
return { | |
title: 'Chart', | |
height: '200px', | |
panels: [ | |
{ | |
title: "Total subscriptions "+hostname, | |
type: 'graph', | |
span: 10, | |
fill: 1, | |
linewidth: 2, | |
targets: [ | |
{ | |
hide : false, | |
measurement : "clients", | |
dsType: "influxdb", | |
"groupBy": [ | |
{ | |
"type": "tag", | |
"params": [ | |
"username" | |
] | |
} | |
], | |
query : "SELECT \"tot_subscriptions\" FROM \"clients\" WHERE \"host\" =~ /"+hostname+"$/ AND $timeFilter GROUP BY \"username\"", | |
refId :"A", | |
resultFormat : "time_series", | |
select :[ | |
[ | |
{"params":["tot_subscriptions"], | |
"type":"field" | |
} | |
] | |
], | |
tags :[{"key":"host","operator":"=~","value":"/"+hostname+"$/"}], | |
} | |
], | |
tooltip: { | |
shared: true | |
}, | |
"lines" : true, | |
"points": true | |
} | |
] | |
} | |
} | |
return function(callback) { | |
// Setup some variables | |
var dashboard; | |
// Intialize a skeleton with some deisred defaults and an empty rows array | |
dashboard = { | |
rows : [], | |
"style": "light", | |
"timezone": "browser", | |
"editable": false, | |
"hideControls": true, | |
}; | |
// Set a title | |
dashboard.title = 'Client Subscriptions'; | |
if (client_name) | |
dashboard.title += " "+client_name; | |
// Set default time | |
// time can be overriden in the url using from/to parameters, but this is | |
// handled automatically in grafana core during dashboard initialization | |
dashboard.time = { | |
from: "now-7d", // Last 7 days | |
to: "now" | |
}; | |
var rows = 1; | |
// Construct a base url to target our influxdb instance | |
var base_url = window.location.hostname; | |
// Now query influx for the list of all of our machines | |
// which use the key 'host' | |
var query_url = "http://" + base_url+':8086/query?db=client_connections&q=SHOW TAG VALUES WITH KEY="host"'; | |
// Send the query | |
$.ajax({ | |
method: 'GET', | |
url: query_url | |
}) | |
// When the query returns, it sends back JSON which is | |
// automatically parsed by the ajax code. | |
.done(function(resp) { | |
// The respose should have a results property which is an array of one | |
if (resp.hasOwnProperty('results')) | |
{ | |
// This in turn has a series property which is an array of one | |
var obj = resp.results[0].series[0]; | |
if (obj.hasOwnProperty('values')) { | |
var values = obj.values; | |
for (var i in values) { | |
var hostname = values[i][0].toString(); // Make sure we have a string here. | |
if (hostname.match(host_re)) | |
dashboard.rows.push(make_panel(hostname)); | |
} | |
} | |
} | |
// when dashboard is composed call the callback | |
// function and pass the dashboard | |
callback(dashboard); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment