From 81f10ea6a9b5432365e942a1dfbb801160cc9d86 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 29 May 2015 23:35:14 +0300 Subject: [PATCH] [hostname]: itemname alias for multiple hosts items. --- zabbix/datasource.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 9dc4370..33a8f4b 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -61,14 +61,21 @@ function (angular, _, kbn) { var hosts = hostname.match(host_pattern); // Extract item names + var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g; var itemname_pattern = /([^{},]+)/g; - var itemnames = itemname.match(itemname_pattern); + // Remove hostnames from item name + // [hostname]: itemname --> itemname + var itemnames = itemname.replace(delete_hostname_pattern, '').match(itemname_pattern); + //var aliases = itemname.match(itemname_pattern); if (itemnames && (itemnames.length < this.limitmetrics)) { // Find items by item names and perform queries var self = this; return $q.all(_.map(hosts, function (hostname) { - return this.findZabbixItem(hostname, itemnames); + if (hosts.length > 1) { + var selectHosts = true; + } + return this.findZabbixItem(hostname, itemnames, selectHosts); }, this)).then(function (items) { items = _.flatten(items); return self.performTimeSeriesQuery(items, from, to) @@ -140,8 +147,9 @@ function (angular, _, kbn) { var grouped_history = _.groupBy(response, 'itemid'); return $q.when(_.map(grouped_history, function (history, itemid) { + var item = indexed_items[itemid]; var series = { - target: expandItemName(indexed_items[itemid]), + target: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + expandItemName(item), datapoints: _.map(history, function (p) { // Value must be a number for properly work var value = Number(p.value); @@ -337,10 +345,13 @@ function (angular, _, kbn) { }; - ZabbixAPIDatasource.prototype.findZabbixItem = function (host, itemnames) { + ZabbixAPIDatasource.prototype.findZabbixItem = function (host, itemnames, /* optional */ selectHosts) { var params = { output: ['name', 'key_', 'value_type'], host: host + }; + if (selectHosts) { + params.selectHosts = ['name']; } return this.performZabbixAPIRequest('item.get', params).then(function (items) { return _.filter(items, function (item) { @@ -435,7 +446,7 @@ function (angular, _, kbn) { return _.map(result, function (item) { var itemname = expandItemName(item) return { - text: (item.hosts ? item.hosts[0].name + ': ' : '') + itemname, + text: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + itemname, expandable: false }; });