From 218f8b8f352a886cb95a9040cb0eebfaf0de9c30 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sat, 25 Jul 2015 18:17:38 +0300 Subject: [PATCH] iss #19 - Get IT service data and handle response. --- zabbix/datasource.js | 150 ++++++++++++++++++++------------------ zabbix/helperFunctions.js | 18 +++++ 2 files changed, 98 insertions(+), 70 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 8099ad7..ec0798a 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -94,93 +94,103 @@ function (angular, _, kbn) { // Create request for each target var promises = _.map(options.targets, function(target) { - // Don't show undefined and hidden targets - if (target.hide || !target.group || !target.host - || !target.application || !target.item) { - return []; - } + if (!target.ITService) { + // Don't show undefined and hidden targets + if (target.hide || !target.group || !target.host + || !target.application || !target.item) { + return []; + } - // Replace templated variables - var groupname = templateSrv.replace(target.group.name); - var hostname = templateSrv.replace(target.host.name); - var appname = templateSrv.replace(target.application.name); - var itemname = templateSrv.replace(target.item.name); + // Replace templated variables + var groupname = templateSrv.replace(target.group.name); + 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 = zabbixHelperSrv.splitMetrics(groupname); - var hosts = zabbixHelperSrv.splitMetrics(hostname); - var apps = zabbixHelperSrv.splitMetrics(appname); + // Extract zabbix groups, hosts and apps from string: + // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] + var groups = zabbixHelperSrv.splitMetrics(groupname); + var hosts = zabbixHelperSrv.splitMetrics(hostname); + var apps = zabbixHelperSrv.splitMetrics(appname); - // Remove hostnames from item names and then - // extract item names - // "hostname: itemname" --> "itemname" - var delete_hostname_pattern = /(?:\[[\w\.]+]:\s)/g; - var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, '')); + // Remove hostnames from item names and then + // extract item names + // "hostname: itemname" --> "itemname" + var delete_hostname_pattern = /(?:\[[\w\.]+]:\s)/g; + var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, '')); - // Find items by item names and perform queries - var self = this; - return this.zabbixAPI.itemFindQuery(groups, hosts, apps) - .then(function (items) { + // Find items by item names and perform queries + var self = this; + return this.zabbixAPI.itemFindQuery(groups, hosts, apps) + .then(function (items) { - // Filter hosts by regex - if (target.host.visible_name === 'All') { - if (target.hostFilter && _.every(items, _.identity.hosts)) { + // Filter hosts by regex + if (target.host.visible_name === 'All') { + if (target.hostFilter && _.every(items, _.identity.hosts)) { - // Use templated variables in filter - var host_pattern = new RegExp(templateSrv.replace(target.hostFilter)); - items = _.filter(items, function (item) { - return _.some(item.hosts, function (host) { - return host_pattern.test(host.name); + // Use templated variables in filter + var host_pattern = new RegExp(templateSrv.replace(target.hostFilter)); + items = _.filter(items, function (item) { + return _.some(item.hosts, function (host) { + return host_pattern.test(host.name); + }); }); - }); + } } - } - if (itemnames[0] === 'All') { + if (itemnames[0] === 'All') { - // Filter items by regex - if (target.itemFilter) { + // Filter items by regex + if (target.itemFilter) { - // Use templated variables in filter - var item_pattern = new RegExp(templateSrv.replace(target.itemFilter)); + // Use templated variables in filter + var item_pattern = new RegExp(templateSrv.replace(target.itemFilter)); + return _.filter(items, function (item) { + return item_pattern.test(zabbixHelperSrv.expandItemName(item)); + }); + } else { + return items; + } + } else { + + // Filtering items return _.filter(items, function (item) { - return item_pattern.test(zabbixHelperSrv.expandItemName(item)); + return _.contains(itemnames, zabbixHelperSrv.expandItemName(item)); }); - } else { - return items; } - } else { + }).then(function (items) { - // Filtering items - return _.filter(items, function (item) { - return _.contains(itemnames, zabbixHelperSrv.expandItemName(item)); - }); - } - }).then(function (items) { - - // Don't perform query for high number of items - // to prevent Grafana slowdown - if (items.length > self.limitmetrics) { - var message = "Try to increase limitmetrics parameter in datasource config.
" - + "Current limitmetrics value is " + self.limitmetrics; - alertSrv.set("Metrics limit exceeded", message, "warning", 10000); - return []; - } else { - items = _.flatten(items); - - // Use alias only for single metric, otherwise use item names - var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias); - - if ((from < useTrendsFrom) && self.trends) { - return self.zabbixAPI.getTrends(items, from, to) - .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale)); + // Don't perform query for high number of items + // to prevent Grafana slowdown + if (items.length > self.limitmetrics) { + var message = "Try to increase limitmetrics parameter in datasource config.
" + + "Current limitmetrics value is " + self.limitmetrics; + alertSrv.set("Metrics limit exceeded", message, "warning", 10000); + return []; } else { - return self.zabbixAPI.getHistory(items, from, to) - .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); + items = _.flatten(items); + + // Use alias only for single metric, otherwise use item names + var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias); + + if ((from < useTrendsFrom) && self.trends) { + return self.zabbixAPI.getTrends(items, from, to) + .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale)); + } else { + return self.zabbixAPI.getHistory(items, from, to) + .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); + } } - } - }); + }); + } else { + // Don't show undefined and hidden targets + if (target.hide || !target.itservice || !target.slaProperty) { + return []; + } else { + return this.zabbixAPI.getSLA(target.itservice.serviceid, from, to) + .then(_.bind(zabbixHelperSrv.handleSLAResponse, zabbixHelperSrv, target.itservice, target.slaProperty)); + } + } }, this); return $q.all(_.flatten(promises)).then(function (results) { diff --git a/zabbix/helperFunctions.js b/zabbix/helperFunctions.js index 881c69b..a07a1e2 100644 --- a/zabbix/helperFunctions.js +++ b/zabbix/helperFunctions.js @@ -109,6 +109,24 @@ function (angular, _) { }); }; + /** + * Convert Zabbix API service.getsla response to Grafana format + * + * @param itservice + * @param slaProperty + * @param slaObject + * @returns {{target: *, datapoints: *[]}} + */ + this.handleSLAResponse = function (itservice, slaProperty, slaObject) { + var targetSLA = slaObject[itservice.serviceid].sla[0]; + return { + target: itservice.name, + datapoints: [ + [targetSLA[slaProperty.property], targetSLA.to * 1000] + ] + }; + }; + /** * Expand item parameters, for example: * CPU $2 time ($3) --> CPU system time (avg1)