Expand user macros in items, closes #212
This commit is contained in:
59
dist/datasource-zabbix/zabbix.js
vendored
59
dist/datasource-zabbix/zabbix.js
vendored
@@ -3,7 +3,7 @@
|
||||
System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './zabbixCachingProxy.service.js'], function (_export, _context) {
|
||||
"use strict";
|
||||
|
||||
var angular, _, utils, _createClass;
|
||||
var angular, _, utils, _createClass, MACRO_PATTERN;
|
||||
|
||||
function _toConsumableArray(arr) {
|
||||
if (Array.isArray(arr)) {
|
||||
@@ -45,6 +45,7 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
|
||||
// Proxy methods
|
||||
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
|
||||
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
|
||||
|
||||
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
|
||||
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
|
||||
@@ -140,6 +141,20 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
if (!options.showDisabledItems) {
|
||||
items = _.filter(items, { 'status': '0' });
|
||||
}
|
||||
|
||||
return items;
|
||||
}).then(this.expandUserMacro.bind(this));
|
||||
}
|
||||
}, {
|
||||
key: 'expandUserMacro',
|
||||
value: function expandUserMacro(items) {
|
||||
var hostids = getHostIds(items);
|
||||
return this.getMacros(hostids).then(function (macros) {
|
||||
_.forEach(items, function (item) {
|
||||
if (containsMacro(item.name)) {
|
||||
item.name = replaceMacro(item, macros);
|
||||
}
|
||||
});
|
||||
return items;
|
||||
});
|
||||
}
|
||||
@@ -244,6 +259,46 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
return filterByName(list, filter);
|
||||
}
|
||||
}
|
||||
|
||||
function getHostIds(items) {
|
||||
var hostIds = _.map(items, function (item) {
|
||||
return _.map(item.hosts, 'hostid');
|
||||
});
|
||||
return _.uniq(_.flatten(hostIds));
|
||||
}
|
||||
|
||||
function containsMacro(itemName) {
|
||||
return MACRO_PATTERN.test(itemName);
|
||||
}
|
||||
|
||||
function replaceMacro(item, macros) {
|
||||
var itemName = item.name;
|
||||
var item_macros = itemName.match(MACRO_PATTERN);
|
||||
_.forEach(item_macros, function (macro) {
|
||||
var host_macros = _.filter(macros, function (m) {
|
||||
if (m.hostid) {
|
||||
return m.hostid === item.hostid;
|
||||
} else {
|
||||
// Add global macros
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
var macro_def = _.find(host_macros, { macro: macro });
|
||||
if (macro_def && macro_def.value) {
|
||||
var macro_value = macro_def.value;
|
||||
var macro_regex = new RegExp(escapeMacro(macro));
|
||||
itemName = itemName.replace(macro_regex, macro_value);
|
||||
}
|
||||
});
|
||||
|
||||
return itemName;
|
||||
}
|
||||
|
||||
function escapeMacro(macro) {
|
||||
macro = macro.replace(/\$/, '\\\$');
|
||||
return macro;
|
||||
}
|
||||
return {
|
||||
setters: [function (_angular) {
|
||||
angular = _angular.default;
|
||||
@@ -271,7 +326,7 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
};
|
||||
}();
|
||||
|
||||
angular.module('grafana.services').factory('Zabbix', ZabbixFactory);
|
||||
angular.module('grafana.services').factory('Zabbix', ZabbixFactory);MACRO_PATTERN = /{\$[A-Z0-9_\.]+}/g;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
2
dist/datasource-zabbix/zabbix.js.map
vendored
2
dist/datasource-zabbix/zabbix.js.map
vendored
File diff suppressed because one or more lines are too long
20
dist/datasource-zabbix/zabbixAPI.service.js
vendored
20
dist/datasource-zabbix/zabbixAPI.service.js
vendored
@@ -177,6 +177,26 @@ System.register(['angular', 'lodash', './utils', './zabbixAPICore.service'], fun
|
||||
return items;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: 'getMacros',
|
||||
value: function getMacros(hostids) {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
hostids: hostids
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
}, {
|
||||
key: 'getGlobalMacros',
|
||||
value: function getGlobalMacros() {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
globalmacro: true
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
}, {
|
||||
key: 'getLastValue',
|
||||
value: function getLastValue(itemid) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -43,7 +43,9 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
applications: {},
|
||||
items: {},
|
||||
history: {},
|
||||
trends: {}
|
||||
trends: {},
|
||||
macros: {},
|
||||
globalMacros: {}
|
||||
};
|
||||
|
||||
this.historyPromises = {};
|
||||
@@ -63,6 +65,12 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
|
||||
this.itemPromises = {};
|
||||
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI), this.itemPromises, getRequestHash);
|
||||
|
||||
this.macroPromises = {};
|
||||
this.getMacrosOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getMacros, this.zabbixAPI), this.macroPromises, getRequestHash);
|
||||
|
||||
this.globalMacroPromises = {};
|
||||
this.getGlobalMacrosOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGlobalMacros, this.zabbixAPI), this.globalMacroPromises, getRequestHash);
|
||||
}
|
||||
|
||||
_createClass(ZabbixCachingProxy, [{
|
||||
@@ -112,6 +120,14 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
var params = [hostids, appids, itemtype];
|
||||
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
|
||||
}
|
||||
}, {
|
||||
key: 'getMacros',
|
||||
value: function getMacros(hostids) {
|
||||
// Merge global macros and host macros
|
||||
var promises = [this.proxyRequest(this.getMacrosOnce, [hostids], this.cache.macros), this.proxyRequest(this.getGlobalMacrosOnce, [], this.cache.globalMacros)];
|
||||
|
||||
return Promise.all(promises).then(_.flatten);
|
||||
}
|
||||
}, {
|
||||
key: 'getHistoryFromCache',
|
||||
value: function getHistoryFromCache(items, time_from, time_till) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
57
dist/test/datasource-zabbix/zabbix.js
vendored
57
dist/test/datasource-zabbix/zabbix.js
vendored
@@ -48,6 +48,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
||||
|
||||
// Proxy methods
|
||||
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
|
||||
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
|
||||
|
||||
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
|
||||
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
|
||||
@@ -153,6 +154,20 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
||||
if (!options.showDisabledItems) {
|
||||
items = _lodash2.default.filter(items, { 'status': '0' });
|
||||
}
|
||||
|
||||
return items;
|
||||
}).then(this.expandUserMacro.bind(this));
|
||||
}
|
||||
}, {
|
||||
key: 'expandUserMacro',
|
||||
value: function expandUserMacro(items) {
|
||||
var hostids = getHostIds(items);
|
||||
return this.getMacros(hostids).then(function (macros) {
|
||||
_lodash2.default.forEach(items, function (item) {
|
||||
if (containsMacro(item.name)) {
|
||||
item.name = replaceMacro(item, macros);
|
||||
}
|
||||
});
|
||||
return items;
|
||||
});
|
||||
}
|
||||
@@ -264,3 +279,45 @@ function filterByQuery(list, filter) {
|
||||
return filterByName(list, filter);
|
||||
}
|
||||
}
|
||||
|
||||
function getHostIds(items) {
|
||||
var hostIds = _lodash2.default.map(items, function (item) {
|
||||
return _lodash2.default.map(item.hosts, 'hostid');
|
||||
});
|
||||
return _lodash2.default.uniq(_lodash2.default.flatten(hostIds));
|
||||
}
|
||||
|
||||
var MACRO_PATTERN = /{\$[A-Z0-9_\.]+}/g;
|
||||
|
||||
function containsMacro(itemName) {
|
||||
return MACRO_PATTERN.test(itemName);
|
||||
}
|
||||
|
||||
function replaceMacro(item, macros) {
|
||||
var itemName = item.name;
|
||||
var item_macros = itemName.match(MACRO_PATTERN);
|
||||
_lodash2.default.forEach(item_macros, function (macro) {
|
||||
var host_macros = _lodash2.default.filter(macros, function (m) {
|
||||
if (m.hostid) {
|
||||
return m.hostid === item.hostid;
|
||||
} else {
|
||||
// Add global macros
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
var macro_def = _lodash2.default.find(host_macros, { macro: macro });
|
||||
if (macro_def && macro_def.value) {
|
||||
var macro_value = macro_def.value;
|
||||
var macro_regex = new RegExp(escapeMacro(macro));
|
||||
itemName = itemName.replace(macro_regex, macro_value);
|
||||
}
|
||||
});
|
||||
|
||||
return itemName;
|
||||
}
|
||||
|
||||
function escapeMacro(macro) {
|
||||
macro = macro.replace(/\$/, '\\\$');
|
||||
return macro;
|
||||
}
|
||||
|
||||
20
dist/test/datasource-zabbix/zabbixAPI.service.js
vendored
20
dist/test/datasource-zabbix/zabbixAPI.service.js
vendored
@@ -226,6 +226,26 @@ function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
|
||||
return items;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: 'getMacros',
|
||||
value: function getMacros(hostids) {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
hostids: hostids
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
}, {
|
||||
key: 'getGlobalMacros',
|
||||
value: function getGlobalMacros() {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
globalmacro: true
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
}, {
|
||||
key: 'getLastValue',
|
||||
value: function getLastValue(itemid) {
|
||||
|
||||
@@ -36,7 +36,9 @@ function ZabbixCachingProxyFactory() {
|
||||
applications: {},
|
||||
items: {},
|
||||
history: {},
|
||||
trends: {}
|
||||
trends: {},
|
||||
macros: {},
|
||||
globalMacros: {}
|
||||
};
|
||||
|
||||
this.historyPromises = {};
|
||||
@@ -56,6 +58,12 @@ function ZabbixCachingProxyFactory() {
|
||||
|
||||
this.itemPromises = {};
|
||||
this.getItemsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getItems, this.zabbixAPI), this.itemPromises, getRequestHash);
|
||||
|
||||
this.macroPromises = {};
|
||||
this.getMacrosOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getMacros, this.zabbixAPI), this.macroPromises, getRequestHash);
|
||||
|
||||
this.globalMacroPromises = {};
|
||||
this.getGlobalMacrosOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getGlobalMacros, this.zabbixAPI), this.globalMacroPromises, getRequestHash);
|
||||
}
|
||||
|
||||
_createClass(ZabbixCachingProxy, [{
|
||||
@@ -111,6 +119,14 @@ function ZabbixCachingProxyFactory() {
|
||||
var params = [hostids, appids, itemtype];
|
||||
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
|
||||
}
|
||||
}, {
|
||||
key: 'getMacros',
|
||||
value: function getMacros(hostids) {
|
||||
// Merge global macros and host macros
|
||||
var promises = [this.proxyRequest(this.getMacrosOnce, [hostids], this.cache.macros), this.proxyRequest(this.getGlobalMacrosOnce, [], this.cache.globalMacros)];
|
||||
|
||||
return Promise.all(promises).then(_lodash2.default.flatten);
|
||||
}
|
||||
}, {
|
||||
key: 'getHistoryFromCache',
|
||||
value: function getHistoryFromCache(items, time_from, time_till) {
|
||||
|
||||
@@ -26,6 +26,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
||||
|
||||
// Proxy methods
|
||||
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
|
||||
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
|
||||
|
||||
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
|
||||
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
|
||||
@@ -108,6 +109,21 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
||||
if (!options.showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
|
||||
return items;
|
||||
})
|
||||
.then(this.expandUserMacro.bind(this));
|
||||
}
|
||||
|
||||
expandUserMacro(items) {
|
||||
let hostids = getHostIds(items);
|
||||
return this.getMacros(hostids)
|
||||
.then(macros => {
|
||||
_.forEach(items, item => {
|
||||
if (containsMacro(item.name)) {
|
||||
item.name = replaceMacro(item, macros);
|
||||
}
|
||||
});
|
||||
return items;
|
||||
});
|
||||
}
|
||||
@@ -215,3 +231,45 @@ function filterByQuery(list, filter) {
|
||||
return filterByName(list, filter);
|
||||
}
|
||||
}
|
||||
|
||||
function getHostIds(items) {
|
||||
let hostIds = _.map(items, item => {
|
||||
return _.map(item.hosts, 'hostid');
|
||||
});
|
||||
return _.uniq(_.flatten(hostIds));
|
||||
}
|
||||
|
||||
let MACRO_PATTERN = /{\$[A-Z0-9_\.]+}/g;
|
||||
|
||||
function containsMacro(itemName) {
|
||||
return MACRO_PATTERN.test(itemName);
|
||||
}
|
||||
|
||||
function replaceMacro(item, macros) {
|
||||
let itemName = item.name;
|
||||
let item_macros = itemName.match(MACRO_PATTERN);
|
||||
_.forEach(item_macros, macro => {
|
||||
let host_macros = _.filter(macros, m => {
|
||||
if (m.hostid) {
|
||||
return m.hostid === item.hostid;
|
||||
} else {
|
||||
// Add global macros
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
let macro_def = _.find(host_macros, {macro: macro});
|
||||
if (macro_def && macro_def.value) {
|
||||
let macro_value = macro_def.value;
|
||||
let macro_regex = new RegExp(escapeMacro(macro));
|
||||
itemName = itemName.replace(macro_regex, macro_value);
|
||||
}
|
||||
});
|
||||
|
||||
return itemName;
|
||||
}
|
||||
|
||||
function escapeMacro(macro) {
|
||||
macro = macro.replace(/\$/, '\\\$');
|
||||
return macro;
|
||||
}
|
||||
|
||||
@@ -198,6 +198,24 @@ function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
|
||||
}
|
||||
}
|
||||
|
||||
getMacros(hostids) {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
hostids: hostids
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
|
||||
getGlobalMacros() {
|
||||
var params = {
|
||||
output: 'extend',
|
||||
globalmacro: true
|
||||
};
|
||||
|
||||
return this.request('usermacro.get', params);
|
||||
}
|
||||
|
||||
getLastValue(itemid) {
|
||||
var params = {
|
||||
output: ['lastvalue'],
|
||||
|
||||
@@ -20,7 +20,9 @@ function ZabbixCachingProxyFactory() {
|
||||
applications: {},
|
||||
items: {},
|
||||
history: {},
|
||||
trends: {}
|
||||
trends: {},
|
||||
macros: {},
|
||||
globalMacros: {}
|
||||
};
|
||||
|
||||
this.historyPromises = {};
|
||||
@@ -45,6 +47,14 @@ function ZabbixCachingProxyFactory() {
|
||||
this.itemPromises = {};
|
||||
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
|
||||
this.itemPromises, getRequestHash);
|
||||
|
||||
this.macroPromises = {};
|
||||
this.getMacrosOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getMacros, this.zabbixAPI),
|
||||
this.macroPromises, getRequestHash);
|
||||
|
||||
this.globalMacroPromises = {};
|
||||
this.getGlobalMacrosOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGlobalMacros, this.zabbixAPI),
|
||||
this.globalMacroPromises, getRequestHash);
|
||||
}
|
||||
|
||||
isExpired(cacheObject) {
|
||||
@@ -93,6 +103,16 @@ function ZabbixCachingProxyFactory() {
|
||||
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
|
||||
}
|
||||
|
||||
getMacros(hostids) {
|
||||
// Merge global macros and host macros
|
||||
let promises = [
|
||||
this.proxyRequest(this.getMacrosOnce, [hostids], this.cache.macros),
|
||||
this.proxyRequest(this.getGlobalMacrosOnce, [], this.cache.globalMacros)
|
||||
];
|
||||
|
||||
return Promise.all(promises).then(_.flatten);
|
||||
}
|
||||
|
||||
getHistoryFromCache(items, time_from, time_till) {
|
||||
var historyStorage = this.cache.history;
|
||||
var full_history;
|
||||
|
||||
Reference in New Issue
Block a user