Expand user macros in items, closes #212
This commit is contained in:
@@ -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