Templates support and flexible metric queries.

This commit is contained in:
Alexander Zobnin
2015-06-09 18:54:54 +03:00
5 changed files with 636 additions and 257 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
*.sublime-workspace *.sublime-workspace
*.sublime-project *.sublime-project
.idea/

View File

@@ -2,7 +2,7 @@ define([
'angular', 'angular',
'lodash', 'lodash',
'kbn', 'kbn',
'./queryCtrl', './queryCtrl'
], ],
function (angular, _, kbn) { function (angular, _, kbn) {
'use strict'; 'use strict';
@@ -11,6 +11,12 @@ function (angular, _, kbn) {
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) { module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) {
/**
* Datasource initialization. Calls when you refresh page, add
* or modify datasource.
*
* @param {Object} datasource Grafana datasource object.
*/
function ZabbixAPIDatasource(datasource) { function ZabbixAPIDatasource(datasource) {
this.name = datasource.name; this.name = datasource.name;
this.url = datasource.url; this.url = datasource.url;
@@ -19,12 +25,22 @@ function (angular, _, kbn) {
this.username = datasource.meta.username; this.username = datasource.meta.username;
this.password = datasource.meta.password; this.password = datasource.meta.password;
// For testing // Limit metrics per panel for templated request
this.ds = datasource; this.limitmetrics = datasource.meta.limitmetrics || 50;
} }
/**
* Calls for each panel in dashboard.
*
* @param {Object} options Query options. Contains time range, targets
* and other info.
*
* @return {Object} Grafana metrics object with timeseries data
* for each target.
*/
ZabbixAPIDatasource.prototype.query = function(options) { ZabbixAPIDatasource.prototype.query = function(options) {
// get from & to in seconds // get from & to in seconds
var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000); var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000); var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
@@ -32,24 +48,113 @@ function (angular, _, kbn) {
// Create request for each target // Create request for each target
var promises = _.map(options.targets, function(target) { var promises = _.map(options.targets, function(target) {
// Remove undefined and hidden targets // Don't show undefined and hidden targets
if (target.hide || !target.item) { if (target.hide || !target.group || !target.host
|| !target.application || !target.item) {
return []; return [];
} }
// Perform request and then handle result // Replace templated variables
return this.performTimeSeriesQuery(target.item, from, to).then(_.partial( var groupname = templateSrv.replace(target.group.name);
this.handleZabbixAPIResponse, target)); var hostname = templateSrv.replace(target.host.name);
var appname = templateSrv.replace(target.application.name);
var itemname = templateSrv.replace(target.item.name);
// Extract zabbix groups, hosts and apps from string:
// "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
var groups = splitMetrics(groupname);
var hosts = splitMetrics(hostname);
var apps = splitMetrics(appname);
// Remove hostnames from item names and then
// extract item names
// "hostname: itemname" --> "itemname"
var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, ''));
// Find items by item names and perform queries
var self = this;
return this.itemFindQuery(groups, hosts, apps)
.then(function (items) {
if (itemnames == 'All') {
return items;
} else {
// Filtering items
return _.filter(items, function (item) {
return _.contains(itemnames, expandItemName(item));
});
}
}).then(function (items) {
// Don't perform query for high number of items
// to prevent Grafana slowdown
if (items.length > self.limitmetrics) {
return [];
} else {
items = _.flatten(items);
return self.performTimeSeriesQuery(items, from, to)
.then(_.partial(self.handleHistoryResponse, items));
}
});
}, this); }, this);
return $q.all(promises).then(function(results) { return $q.all(_.flatten(promises)).then(function (results) {
return { data: _.flatten(results) }; return { data: _.flatten(results) };
}); });
}; };
// Request data from Zabbix API /**
ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(target, response) { * Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds
* @param {Number} end Time in seconds
*
* @return {Array} Array of Zabbix history objects
*/
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('history.get', params);
}, this)).then(function (results) {
return _.flatten(results);
});
};
/**
* Convert Zabbix API data to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, history) {
/** /**
* Response should be in the format: * Response should be in the format:
* data: [ * data: [
@@ -64,20 +169,34 @@ function (angular, _, kbn) {
* ] * ]
*/ */
// Group items and history by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid');
return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid];
var series = { var series = {
target: target.alias, target: (item.hosts ? item.hosts[0].name+': ' : '') + expandItemName(item),
datapoints: _.map(response, function (p) { datapoints: _.map(history, function (p) {
// Value must be a number for properly work // Value must be a number for properly work
var value = Number(p.value); var value = Number(p.value);
return [value, p.clock * 1000]; return [value, p.clock * 1000];
}) })
}; };
return series;
return $q.when(series); }));
}; };
// Request data from Zabbix API /**
* Request data from Zabbix API
*
* @param {string} method Zabbix API method name
* @param {object} params method params
*
* @return {object} data.result field or []
*/
ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) { ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) {
var options = { var options = {
method: 'POST', method: 'POST',
@@ -94,54 +213,28 @@ function (angular, _, kbn) {
} }
}; };
var performedQuery;
// Check authorization first
if (!this.auth) {
var self = this; var self = this;
performedQuery = this.performZabbixAPILogin().then(function (response) { return backendSrv.datasourceRequest(options).then(function (response) {
self.auth = response;
options.data.auth = response;
return backendSrv.datasourceRequest(options);
});
} else {
performedQuery = backendSrv.datasourceRequest(options);
}
// Handle response
return performedQuery.then(function (response) {
if (!response.data) { if (!response.data) {
return []; return [];
} }
// Handle Zabbix API errors
else if (response.data.error) {
// Handle auth errors
if (response.data.error.data == "Session terminated, re-login, please." ||
response.data.error.data == 'Not authorised.') {
return self.performZabbixAPILogin().then(function (response) {
self.auth = response;
return self.performZabbixAPIRequest(method, params);
});
}
}
return response.data.result; return response.data.result;
}); });
}; };
/**
* Perform time series query to Zabbix API
*
* @param items: array of zabbix api item objects
*/
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
var params = {
output: 'extend',
history: items.value_type,
itemids: items.itemid,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('history.get', params);
};
// Get authentication token // Get authentication token
ZabbixAPIDatasource.prototype.performZabbixAPILogin = function() { ZabbixAPIDatasource.prototype.performZabbixAPILogin = function() {
var options = { var options = {
@@ -172,8 +265,11 @@ function (angular, _, kbn) {
ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() { ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() {
var params = { var params = {
output: ['name'], output: ['name'],
real_hosts: true, //Return only host groups that contain hosts sortfield: 'name',
sortfield: 'name' // Return only host groups that contain hosts
real_hosts: true,
// Return only host groups that contain monitored hosts.
monitored_hosts: true
}; };
return this.performZabbixAPIRequest('hostgroup.get', params); return this.performZabbixAPIRequest('hostgroup.get', params);
@@ -181,89 +277,193 @@ function (angular, _, kbn) {
// Get the list of hosts // Get the list of hosts
ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) { ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupids) {
var params = { var params = {
output: ['name'], output: ['name', 'host'],
sortfield: 'name' sortfield: 'name',
// Return only hosts that have items with numeric type of information.
with_simple_graph_items: true,
// Return only monitored hosts.
monitored_hosts: true
}; };
// Return only hosts in given group // Return only hosts in given group
if (groupid) { if (groupids) {
params.groupids = groupid; params.groupids = groupids;
} }
return this.performZabbixAPIRequest('host.get', params); return this.performZabbixAPIRequest('host.get', params);
}; };
// Get the list of applications // Get the list of applications
ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostid) { ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name', sortfield: 'name'
hostids: hostid
}; };
if (hostids) {
params.hostids = hostids;
}
else if (groupids) {
params.groupids = groupids;
}
return this.performZabbixAPIRequest('application.get', params); return this.performZabbixAPIRequest('application.get', params);
}; };
// Get the list of host items /**
ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostid, applicationid) { * Items request
*
* @param {string or Array} hostids ///////////////////////////
* @param {string or Array} applicationids // Zabbix API parameters //
* @param {string or Array} groupids ///////////////////////////
*
* @return {string or Array} Array of Zabbix API item objects
*/
ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostids, applicationids, /* optional */ groupids) {
var params = { var params = {
output: ['name', 'key_', 'value_type', 'delay'], output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name', sortfield: 'name',
hostids: hostid,
//Include web items in the result //Include web items in the result
webitems: true, webitems: true,
// Return only numeric items // Return only numeric items
filter: { filter: {
value_type: [0,3] value_type: [0,3]
} },
// Return only enabled items
monitored: true,
searchByAny: true
}; };
// Filter by hosts or by groups
if (hostids) {
params.hostids = hostids;
} else if (groupids) {
params.groupids = groupids;
}
// If application selected return only relative items // If application selected return only relative items
if (applicationid) { if (applicationids) {
params.applicationids = applicationid; params.applicationids = applicationids;
}
// Return host property for multiple hosts
if (!hostids || (_.isArray(hostids) && hostids.length > 1)) {
params.selectHosts = ['name'];
} }
return this.performZabbixAPIRequest('item.get', params); return this.performZabbixAPIRequest('item.get', params);
}; };
// For templated query /**
ZabbixAPIDatasource.prototype.metricFindQuery = function (query) { * Find groups by names
var interpolated; *
try { * @param {string or array} group group names
interpolated = templateSrv.replace(query); * @return {array} array of Zabbix API hostgroup objects
} */
catch (err) { ZabbixAPIDatasource.prototype.findZabbixGroup = function (group) {
return $q.reject(err);
}
var parts = interpolated.split('.');
var template = {
'group': parts[0],
'host': parts[1],
'item': parts[2]
};
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name',
// Case insensitive search
search: { search: {
name : template.group name: group
},
searchByAny: true,
searchWildcardsEnabled: true
} }
return this.performZabbixAPIRequest('hostgroup.get', params);
}; };
var self = this;
return this.performZabbixAPIRequest('hostgroup.get', params) /**
.then(function (result) { * Find hosts by names
var groupid = null; *
if (result.length && template.group) { * @param {string or array} hostnames hosts names
groupid = result[0].groupid; * @return {array} array of Zabbix API host objects
*/
ZabbixAPIDatasource.prototype.findZabbixHost = function (hostnames) {
var params = {
output: ['host', 'name'],
search: {
host: hostnames,
name: hostnames
},
searchByAny: true,
searchWildcardsEnabled: true
} }
return self.performHostSuggestQuery(groupid) return this.performZabbixAPIRequest('host.get', params);
.then(function (result) { };
/**
* Find applications by names
*
* @param {string or array} application applications names
* @return {array} array of Zabbix API application objects
*/
ZabbixAPIDatasource.prototype.findZabbixApp = function (application) {
var params = {
output: ['name'],
search: {
name: application
},
searchByAny: true,
searchWildcardsEnabled: true,
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* For templated query.
* Find metrics from templated request.
*
* @param {string} query Query from Templating
* @return {string} Metric name - group, host, app or item or list
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
ZabbixAPIDatasource.prototype.metricFindQuery = function (query) {
// Split query. Query structure:
// group.host.app.item
var parts = [];
_.each(query.split('.'), function (part) {
part = templateSrv.replace(part);
if (part[0] === '{') {
// Convert multiple mettrics to array
// "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
parts.push(splitMetrics(part));
} else {
parts.push(part);
}
});
var template = _.object(['group', 'host', 'app', 'item'], parts)
// Get items
if (parts.length === 4) {
return this.itemFindQuery(template.group, template.host, template.app).then(function (result) {
return _.map(result, function (item) {
var itemname = expandItemName(item)
return {
text: itemname,
expandable: false
};
});
});
}
// Get applications
else if (parts.length === 3) {
return this.appFindQuery(template.host, template.group).then(function (result) {
return _.map(result, function (app) {
return {
text: app.name,
expandable: false
};
});
});
}
// Get hosts
else if (parts.length === 2) {
return this.hostFindQuery(template.group).then(function (result) {
return _.map(result, function (host) { return _.map(result, function (host) {
return { return {
text: host.name, text: host.name,
@@ -271,10 +471,140 @@ function (angular, _, kbn) {
}; };
}); });
}); });
}
// Get groups
else if (parts.length === 1) {
return this.performHostGroupSuggestQuery().then(function (result) {
return _.map(result, function (hostgroup) {
return {
text: hostgroup.name,
expandable: false
};
});
});
}
// Return empty object for invalid request
else {
var d = $q.defer();
d.resolve([]);
return d.promise;
}
};
/**
* Find items belongs to passed groups, hosts and
* applications
*
* @param {string or array} groups
* @param {string or array} hosts
* @param {string or array} apps
*
* @return {array} array of Zabbix API item objects
*/
ZabbixAPIDatasource.prototype.itemFindQuery = function(groups, hosts, apps) {
var promises = [];
// Get hostids from names
if (hosts && hosts != '*') {
promises.push(this.findZabbixHost(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.findZabbixGroup(groups));
}
// Get applicationids from names
if (apps) {
promises.push(this.findZabbixApp(apps));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
if (groups) {
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts != '*') {
var hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
if (apps) {
var applicationids = _.map(_.filter(results, function (object) {
return object.applicationid;
}), 'applicationid');
}
return self.performItemSuggestQuery(hostids, applicationids, groupids);
}); });
}; };
/**
* Find applications belongs to passed groups and hosts
*
* @param {string or array} hosts
* @param {string or array} groups
*
* @return {array} array of Zabbix API application objects
*/
ZabbixAPIDatasource.prototype.appFindQuery = function(hosts, groups) {
var promises = [];
// Get hostids from names
if (hosts && hosts != '*') {
promises.push(this.findZabbixHost(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.findZabbixGroup(groups));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
if (groups) {
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts != '*') {
var hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
return self.performAppSuggestQuery(hostids, groupids);
});
};
/**
* Find hosts belongs to passed groups
*
* @param {string or array} groups
* @return {array} array of Zabbix API host objects
*/
ZabbixAPIDatasource.prototype.hostFindQuery = function(groups) {
var self = this;
return this.findZabbixGroup(groups).then(function (results) {
results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
return self.performHostSuggestQuery(groupids);
});
};
/////////////////
// Annotations //
/////////////////
ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) { ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000); var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000);
var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000); var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000);
@@ -287,7 +617,8 @@ function (angular, _, kbn) {
}, },
}; };
return this.performZabbixAPIRequest('trigger.get', params).then(function (result) { return this.performZabbixAPIRequest('trigger.get', params)
.then(function (result) {
if(result) { if(result) {
var obs = {}; var obs = {};
obs = _.indexBy(result, 'triggerid'); obs = _.indexBy(result, 'triggerid');
@@ -300,7 +631,8 @@ function (angular, _, kbn) {
objectids: _.keys(obs) objectids: _.keys(obs)
}; };
return self.performZabbixAPIRequest('event.get', params).then(function (result) { return self.performZabbixAPIRequest('event.get', params)
.then(function (result) {
var events = []; var events = [];
_.each(result, function(e) { _.each(result, function(e) {
events.push({ events.push({
@@ -321,3 +653,40 @@ function (angular, _, kbn) {
return ZabbixAPIDatasource; return ZabbixAPIDatasource;
}); });
}); });
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
function splitMetrics(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern)
}
/**
* Expand item parameters, for example:
* CPU $2 time ($3) --> CPU system time (avg1)
*
* @param item: zabbix api item object
* @return: expanded item name (string)
*/
function expandItemName(item) {
var name = item.name;
var key = item.key_;
// extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
// replace item parameters
for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]);
};
return name;
};

View File

@@ -46,7 +46,7 @@
<!-- Alias --> <!-- Alias -->
<li> <li>
<input type="text" <input type="text"
class="tight-form-input input-large" class="tight-form-input input-medium"
ng-model="target.alias" ng-model="target.alias"
spellcheck='false' spellcheck='false'
placeholder="alias" placeholder="alias"
@@ -54,13 +54,13 @@
</li> </li>
<!-- Select Host Group --> <!-- Select Host Group -->
<li> <li>
<select style="width: 10em" <select style="width: 12em"
class="tight-form-input input-small" class="tight-form-input input-small"
ng-change="selectHostGroup()" ng-change="selectHostGroup()"
ng-model="target.hostGroup" ng-model="target.group"
bs-tooltip="target.hostGroup.name.length > 25 ? target.hostGroup.name : ''" bs-tooltip="target.group.name.length > 25 ? target.group.name : ''"
ng-options="hostgroup.name for hostgroup in metric.hostGroupList" > ng-options="group.visible_name ? group.visible_name : group.name for group in metric.groupList track by group.name" >
<option value="">All</option> <option value="">-- Select host group --</option>
</select> </select>
<a bs-tooltip="target.errors.metric" <a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)" style="color: rgb(229, 189, 28)"
@@ -75,8 +75,8 @@
ng-change="selectHost()" ng-change="selectHost()"
ng-model="target.host" ng-model="target.host"
bs-tooltip="target.host.name.length > 25 ? target.host.name : ''" bs-tooltip="target.host.name.length > 25 ? target.host.name : ''"
ng-options="host.name for host in metric.hostList" > ng-options="host.visible_name ? host.visible_name : host.name for host in metric.hostList track by host.name" >
<option value="">-- select host --</option> <option value="">-- Select host --</option>
</select> </select>
<a bs-tooltip="target.errors.metric" <a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)" style="color: rgb(229, 189, 28)"
@@ -86,13 +86,13 @@
</li> </li>
<!-- Select Application --> <!-- Select Application -->
<li> <li>
<select style="width: 12em" <select style="width: 15em"
class="tight-form-input input-medium" class="tight-form-input input-medium"
ng-change="selectApplication()" ng-change="selectApplication()"
ng-model="target.application" ng-model="target.application"
bs-tooltip="target.application.name.length > 15 ? target.application.name : ''" bs-tooltip="target.application.name.length > 15 ? target.application.name : ''"
ng-options="app.name for app in metric.applicationList" > ng-options="app.visible_name ? app.visible_name : app.name for app in metric.applicationList track by app.name" >
<option value="">All</option> <option value="">-- Select application --</option>
</select> </select>
<a bs-tooltip="target.errors.metric" <a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)" style="color: rgb(229, 189, 28)"
@@ -106,9 +106,9 @@
class="tight-form-input input-medium" class="tight-form-input input-medium"
ng-change="selectItem()" ng-change="selectItem()"
ng-model="target.item" ng-model="target.item"
bs-tooltip="target.expandedName.length > 30 ? target.expandedName : ''" bs-tooltip="target.item.name.length > 25 ? target.item.name : ''"
ng-options="item.expandedName for item in metric.itemList" > ng-options="item.name for item in metric.itemList track by item.name" >
<option value="">--select item--</option> <option value="">-- Select item --</option>
</select> </select>
<a bs-tooltip="target.errors.metric" <a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)" style="color: rgb(229, 189, 28)"

View File

@@ -16,6 +16,8 @@
"username": "guest", "username": "guest",
"password": "", "password": "",
"limitmetrics": 50,
"metrics": true, "metrics": true,
"annotations": true "annotations": true
} }

View File

@@ -8,7 +8,7 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function($scope) { module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv) {
$scope.init = function() { $scope.init = function() {
$scope.targetLetters = targetLetters; $scope.targetLetters = targetLetters;
@@ -20,33 +20,27 @@ function (angular, _) {
}; };
// Update host group, host, application and item lists // Update host group, host, application and item lists
$scope.updateHostGroupList(); $scope.updateGroupList();
if ($scope.target.hostGroup) {
$scope.updateHostList($scope.target.hostGroup.groupid);
} else {
$scope.updateHostList(); $scope.updateHostList();
} $scope.updateAppList();
if ($scope.target.host) { $scope.updateItemList();
$scope.updateAppList($scope.target.host.hostid);
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
}
setItemAlias(); setItemAlias();
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
}; };
// Take alias from item name by default
/**
* Take alias from item name by default
*/
function setItemAlias() { function setItemAlias() {
if (!$scope.target.alias && $scope.target.item) { if (!$scope.target.alias && $scope.target.item) {
$scope.target.alias = $scope.target.item.expandedName; $scope.target.alias = expandItemName($scope.target.item);
} }
}; };
$scope.targetBlur = function() { $scope.targetBlur = function() {
setItemAlias(); setItemAlias();
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
@@ -56,15 +50,14 @@ function (angular, _) {
} }
}; };
// Call when host group selected
/**
* Call when host group selected
*/
$scope.selectHostGroup = function() { $scope.selectHostGroup = function() {
$scope.updateHostList()
// Update host list $scope.updateAppList();
if ($scope.target.hostGroup) { $scope.updateItemList();
$scope.updateHostList($scope.target.hostGroup.groupid);
} else {
$scope.updateHostList('');
}
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
@@ -73,19 +66,13 @@ function (angular, _) {
} }
}; };
// Call when host selected
/**
* Call when host selected
*/
$scope.selectHost = function() { $scope.selectHost = function() {
if ($scope.target.host) { $scope.updateAppList();
// Update item list $scope.updateItemList();
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
// Update application list
$scope.updateAppList($scope.target.host.hostid);
}
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
@@ -95,15 +82,11 @@ function (angular, _) {
}; };
// Call when application selected /**
* Call when application selected
*/
$scope.selectApplication = function() { $scope.selectApplication = function() {
$scope.updateItemList();
// Update item list
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
@@ -113,7 +96,9 @@ function (angular, _) {
}; };
// Call when item selected /**
* Call when item selected
*/
$scope.selectItem = function() { $scope.selectItem = function() {
setItemAlias(); setItemAlias();
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
@@ -142,15 +127,12 @@ function (angular, _) {
/** /**
* Update list of host groups * Update list of host groups
*/ */
$scope.updateHostGroupList = function() { $scope.updateGroupList = function() {
$scope.datasource.performHostGroupSuggestQuery().then(function (series) { $scope.metric.groupList = [{name: '*', visible_name: 'All'}];
$scope.metric.hostGroupList = series; addTemplatedVariables($scope.metric.groupList);
if ($scope.target.hostGroup) {
$scope.target.hostGroup = $scope.metric.hostGroupList.filter(function (item, index, array) { $scope.datasource.performHostGroupSuggestQuery().then(function (groups) {
// Find selected host in metric.hostList $scope.metric.groupList = $scope.metric.groupList.concat(groups);
return (item.groupid == $scope.target.hostGroup.groupid);
}).pop();
}
}); });
}; };
@@ -158,16 +140,13 @@ function (angular, _) {
/** /**
* Update list of hosts * Update list of hosts
*/ */
$scope.updateHostList = function(groupid) { $scope.updateHostList = function() {
$scope.datasource.performHostSuggestQuery(groupid).then(function (series) { $scope.metric.hostList = [{name: '*', visible_name: 'All'}];
$scope.metric.hostList = series; addTemplatedVariables($scope.metric.hostList);
if ($scope.target.host) { var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
$scope.target.host = $scope.metric.hostList.filter(function (item, index, array) { $scope.datasource.hostFindQuery(groups).then(function (hosts) {
// Find selected host in metric.hostList $scope.metric.hostList = $scope.metric.hostList.concat(hosts);
return (item.hostid == $scope.target.host.hostid);
}).pop();
}
}); });
}; };
@@ -175,15 +154,18 @@ function (angular, _) {
/** /**
* Update list of host applications * Update list of host applications
*/ */
$scope.updateAppList = function(hostid) { $scope.updateAppList = function() {
$scope.datasource.performAppSuggestQuery(hostid).then(function (series) { $scope.metric.applicationList = [{name: '*', visible_name: 'All'}];
$scope.metric.applicationList = series; addTemplatedVariables($scope.metric.applicationList);
if ($scope.target.application) {
$scope.target.application = $scope.metric.applicationList.filter(function (item, index, array) { var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
// Find selected application in metric.hostList var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
return (item.applicationid == $scope.target.application.applicationid); $scope.datasource.appFindQuery(hosts, groups).then(function (apps) {
}).pop(); // TODO: work with app names, not objects
} var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname};
});
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
}); });
}; };
@@ -191,29 +173,37 @@ function (angular, _) {
/** /**
* Update list of items * Update list of items
*/ */
$scope.updateItemList = function(hostid, applicationid) { $scope.updateItemList = function() {
$scope.metric.itemList = [{name: 'All'}];;
addTemplatedVariables($scope.metric.itemList);
// Update only if host selected var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
if (hostid) { var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
$scope.datasource.performItemSuggestQuery(hostid, applicationid).then(function (series) { var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
$scope.metric.itemList = series; $scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) {
// Show only unique item names
var uniq_items = _.map(_.uniq(items, function (item) {
return expandItemName(item);
}), function (item) {
return {name: expandItemName(item)}
});
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
});
};
// Expand item parameters
$scope.metric.itemList.forEach(function (item, index, array) { /**
if (item && item.key_ && item.name) { * Add templated variables to list of available metrics
item.expandedName = expandItemName(item); *
} * @param {Array} metricList List of metrics which variables add to
*/
function addTemplatedVariables(metricList) {
_.each(templateSrv.variables, function(variable) {
metricList.push({
name: '$' + variable.name,
templated: true
})
}); });
if ($scope.target.item) {
$scope.target.item = $scope.metric.itemList.filter(function (item, index, array) {
// Find selected item in metric.hostList
return (item.itemid == $scope.target.item.itemid);
}).pop();
}
});
} else {
$scope.metric.itemList = [];
}
}; };
@@ -221,13 +211,14 @@ function (angular, _) {
* Expand item parameters, for example: * Expand item parameters, for example:
* CPU $2 time ($3) --> CPU system time (avg1) * CPU $2 time ($3) --> CPU system time (avg1)
* *
* @param item: zabbix api item object * @param {Object} item Zabbix item object
* @return: expanded item name (string) * @return {string} expanded item name
*/ */
function expandItemName(item) { function expandItemName(item) {
var name = item.name; var name = item.name;
var key = item.key_; var key = item.key_;
if (key) {
// extract params from key: // extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"] // "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(','); var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
@@ -236,6 +227,7 @@ function (angular, _) {
for (var i = key_params.length; i >= 1; i--) { for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]); name = name.replace('$' + i, key_params[i - 1]);
}; };
}
return name; return name;
}; };
@@ -253,3 +245,17 @@ function (angular, _) {
}); });
}); });
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
function splitMetrics(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern)
}