IT-services: able to use regex for selecting of multiple services, issue #415

This commit is contained in:
Alexander Zobnin
2017-07-24 19:02:09 +03:00
parent 1d4cad85c0
commit 70f34db385
18 changed files with 298 additions and 119 deletions

View File

@@ -181,14 +181,7 @@ var ZabbixAPIDatasource = function () {
// IT services mode
else if (target.mode === c.MODE_ITSERVICE) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
}
return _this.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return _responseHandler2.default.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
return _this.queryITServiceData(target, timeRange, options);
}
});
@@ -345,6 +338,45 @@ var ZabbixAPIDatasource = function () {
}
});
}
}, {
key: 'queryITServiceData',
value: function queryITServiceData(target, timeRange, options) {
var _this4 = this;
// Don't show undefined and hidden targets
if (target.hide || !target.itservice && !target.itServiceFilter || !target.slaProperty) {
return [];
}
var itServiceIds = [];
var itServices = [];
var itServiceFilter = void 0;
var isOldVersion = target.itservice && !target.itServiceFilter;
if (isOldVersion) {
// Backward compatibility
itServiceFilter = '/.*/';
} else {
itServiceFilter = this.replaceTemplateVars(target.itServiceFilter, options.scopedVars);
}
return this.zabbix.getITServices(itServiceFilter).then(function (itservices) {
itServices = itservices;
if (isOldVersion) {
itServices = _lodash2.default.filter(itServices, { 'serviceid': target.itservice.serviceid });
}
itServiceIds = _lodash2.default.map(itServices, 'serviceid');
return itServiceIds;
}).then(function (serviceids) {
return _this4.zabbix.getSLA(serviceids, timeRange);
}).then(function (slaResponse) {
return _lodash2.default.map(itServiceIds, function (serviceid) {
var itservice = _lodash2.default.find(itServices, { 'serviceid': serviceid });
return _responseHandler2.default.handleSLAResponse(itservice, target.slaProperty, slaResponse);
});
});
}
/**
* Test connection to Zabbix API
@@ -354,12 +386,12 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'testDatasource',
value: function testDatasource() {
var _this4 = this;
var _this5 = this;
var zabbixVersion = void 0;
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this4.zabbix.login();
return _this5.zabbix.login();
}).then(function () {
return {
status: "success",
@@ -398,14 +430,14 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this5 = this;
var _this6 = this;
var result = void 0;
var parts = [];
// Split query. Query structure: group.host.app.item
_lodash2.default.each(utils.splitTemplateQuery(query), function (part) {
part = _this5.replaceTemplateVars(part, {});
part = _this6.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -447,7 +479,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var _this6 = this;
var _this7 = this;
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
@@ -465,7 +497,7 @@ var ZabbixAPIDatasource = function () {
return getTriggers.then(function (triggers) {
// Filter triggers by description
var triggerName = _this6.replaceTemplateVars(annotation.trigger, {});
var triggerName = _this7.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(triggerName)) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return utils.buildRegex(triggerName).test(trigger.description);
@@ -482,7 +514,7 @@ var ZabbixAPIDatasource = function () {
});
var objectids = _lodash2.default.map(triggers, 'triggerid');
return _this6.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
return _this7.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
var indexedTriggers = _lodash2.default.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
@@ -523,23 +555,23 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'alertQuery',
value: function alertQuery(options) {
var _this7 = this;
var _this8 = this;
var enabled_targets = filterEnabledTargets(options.targets);
var getPanelItems = _lodash2.default.map(enabled_targets, function (t) {
var target = _lodash2.default.cloneDeep(t);
_this7.replaceTargetVariables(target, options);
return _this7.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
_this8.replaceTargetVariables(target, options);
return _this8.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
});
return Promise.all(getPanelItems).then(function (results) {
var items = _lodash2.default.flatten(results);
var itemids = _lodash2.default.map(items, 'itemid');
return _this7.zabbix.getAlerts(itemids);
return _this8.zabbix.getAlerts(itemids);
}).then(function (triggers) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return trigger.priority >= _this7.alertingMinSeverity;
return trigger.priority >= _this8.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
@@ -570,12 +602,12 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this8 = this;
var _this9 = this;
var parts = ['group', 'host', 'application', 'item'];
_lodash2.default.forEach(parts, function (p) {
if (target[p] && target[p].filter) {
target[p].filter = _this8.replaceTemplateVars(target[p].filter, options.scopedVars);
target[p].filter = _this9.replaceTemplateVars(target[p].filter, options.scopedVars);
}
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
@@ -583,9 +615,9 @@ var ZabbixAPIDatasource = function () {
_lodash2.default.forEach(target.functions, function (func) {
func.params = _lodash2.default.map(func.params, function (param) {
if (typeof param === 'number') {
return +_this8.templateSrv.replace(param.toString(), options.scopedVars);
return +_this9.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return _this8.templateSrv.replace(param, options.scopedVars);
return _this9.templateSrv.replace(param, options.scopedVars);
}
});
});

View File

@@ -70,11 +70,14 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
2: { value: 'text', text: 'Text', mode: c.MODE_TEXT }
};
_this.slaPropertyList = [{ name: "Status", property: "status" }, { name: "SLA", property: "sla" }, { name: "OK time", property: "okTime" }, { name: "Problem time", property: "problemTime" }, { name: "Down time", property: "downtimeTime" }];
// Map functions for bs-typeahead
_this.getGroupNames = _lodash2.default.bind(_this.getMetricNames, _this, 'groupList');
_this.getHostNames = _lodash2.default.bind(_this.getMetricNames, _this, 'hostList', true);
_this.getApplicationNames = _lodash2.default.bind(_this.getMetricNames, _this, 'appList');
_this.getItemNames = _lodash2.default.bind(_this.getMetricNames, _this, 'itemList');
_this.getITServices = _lodash2.default.bind(_this.getMetricNames, _this, 'itServiceList');
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', function () {
@@ -124,9 +127,8 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
this.initFilters();
} else if (target.mode === c.MODE_ITSERVICE) {
this.slaPropertyList = [{ name: "Status", property: "status" }, { name: "SLA", property: "sla" }, { name: "OK time", property: "okTime" }, { name: "Problem time", property: "problemTime" }, { name: "Down time", property: "downtimeTime" }];
this.itserviceList = [{ name: "test" }];
this.updateITServiceList();
_lodash2.default.defaults(target, { slaProperty: { name: "SLA", property: "sla" } });
this.suggestITServices();
}
};
@@ -212,6 +214,16 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
return items;
});
}
}, {
key: 'suggestITServices',
value: function suggestITServices() {
var _this6 = this;
return this.zabbix.getITService().then(function (itservices) {
_this6.metric.itServiceList = itservices;
return itservices;
});
}
}, {
key: 'isRegex',
value: function isRegex(str) {
@@ -246,11 +258,11 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
}, {
key: 'isContainsVariables',
value: function isContainsVariables() {
var _this6 = this;
var _this7 = this;
return _lodash2.default.some(['group', 'host', 'application'], function (field) {
if (_this6.target[field] && _this6.target[field].filter) {
return utils.isTemplateVariable(_this6.target[field].filter, _this6.templateSrv.variables);
if (_this7.target[field] && _this7.target[field].filter) {
return utils.isTemplateVariable(_this7.target[field].filter, _this7.templateSrv.variables);
} else {
return false;
}
@@ -365,11 +377,11 @@ var ZabbixQueryController = exports.ZabbixQueryController = function (_QueryCtrl
}, {
key: 'updateITServiceList',
value: function updateITServiceList() {
var _this7 = this;
var _this8 = this;
this.zabbix.getITService().then(function (iteservices) {
_this7.itserviceList = [];
_this7.itserviceList = _this7.itserviceList.concat(iteservices);
_this8.itserviceList = [];
_this8.itserviceList = _this8.itserviceList.concat(iteservices);
});
}

View File

@@ -201,6 +201,13 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
return filterByQuery(items, itemFilter);
});
}
}, {
key: 'getITServices',
value: function getITServices(itServiceFilter) {
return this.cachingProxy.getITServices().then(function (itServices) {
return findByFilter(itServices, itServiceFilter);
});
}
/**
* Build query - convert target filters to array of Zabbix items

View File

@@ -39,7 +39,8 @@ function ZabbixCachingProxyFactory() {
history: {},
trends: {},
macros: {},
globalMacros: {}
globalMacros: {},
itServices: {}
};
this.historyPromises = {};
@@ -65,6 +66,9 @@ function ZabbixCachingProxyFactory() {
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getItems, this.zabbixAPI), this.itemPromises, getRequestHash);
this.itServicesPromises = {};
this.getITServicesOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getITService, this.zabbixAPI), this.itServicesPromises, getRequestHash);
this.macroPromises = {};
this.getMacrosOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getMacros, this.zabbixAPI), this.macroPromises, getRequestHash);
@@ -125,6 +129,11 @@ function ZabbixCachingProxyFactory() {
var params = [hostids, appids, itemtype];
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
}
}, {
key: 'getITServices',
value: function getITServices() {
return this.proxyRequest(this.getITServicesOnce, [], this.cache.itServices);
}
}, {
key: 'getMacros',
value: function getMacros(hostids) {