Expand user macros in items, closes #212

This commit is contained in:
Alexander Zobnin
2017-02-11 20:15:57 +03:00
parent 94c24e9be4
commit 48018744bd
12 changed files with 288 additions and 8 deletions

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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) {