diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 8099ad7..ec0798a 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -94,93 +94,103 @@ function (angular, _, kbn) { // Create request for each target var promises = _.map(options.targets, function(target) { - // Don't show undefined and hidden targets - if (target.hide || !target.group || !target.host - || !target.application || !target.item) { - return []; - } + if (!target.ITService) { + // Don't show undefined and hidden targets + if (target.hide || !target.group || !target.host + || !target.application || !target.item) { + return []; + } - // Replace templated variables - var groupname = templateSrv.replace(target.group.name); - var hostname = templateSrv.replace(target.host.name); - var appname = templateSrv.replace(target.application.name); - var itemname = templateSrv.replace(target.item.name); + // Replace templated variables + var groupname = templateSrv.replace(target.group.name); + var hostname = templateSrv.replace(target.host.name); + var appname = templateSrv.replace(target.application.name); + var itemname = templateSrv.replace(target.item.name); - // Extract zabbix groups, hosts and apps from string: - // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] - var groups = zabbixHelperSrv.splitMetrics(groupname); - var hosts = zabbixHelperSrv.splitMetrics(hostname); - var apps = zabbixHelperSrv.splitMetrics(appname); + // Extract zabbix groups, hosts and apps from string: + // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] + var groups = zabbixHelperSrv.splitMetrics(groupname); + var hosts = zabbixHelperSrv.splitMetrics(hostname); + var apps = zabbixHelperSrv.splitMetrics(appname); - // Remove hostnames from item names and then - // extract item names - // "hostname: itemname" --> "itemname" - var delete_hostname_pattern = /(?:\[[\w\.]+]:\s)/g; - var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, '')); + // Remove hostnames from item names and then + // extract item names + // "hostname: itemname" --> "itemname" + var delete_hostname_pattern = /(?:\[[\w\.]+]:\s)/g; + var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, '')); - // Find items by item names and perform queries - var self = this; - return this.zabbixAPI.itemFindQuery(groups, hosts, apps) - .then(function (items) { + // Find items by item names and perform queries + var self = this; + return this.zabbixAPI.itemFindQuery(groups, hosts, apps) + .then(function (items) { - // Filter hosts by regex - if (target.host.visible_name === 'All') { - if (target.hostFilter && _.every(items, _.identity.hosts)) { + // Filter hosts by regex + if (target.host.visible_name === 'All') { + if (target.hostFilter && _.every(items, _.identity.hosts)) { - // Use templated variables in filter - var host_pattern = new RegExp(templateSrv.replace(target.hostFilter)); - items = _.filter(items, function (item) { - return _.some(item.hosts, function (host) { - return host_pattern.test(host.name); + // Use templated variables in filter + var host_pattern = new RegExp(templateSrv.replace(target.hostFilter)); + items = _.filter(items, function (item) { + return _.some(item.hosts, function (host) { + return host_pattern.test(host.name); + }); }); - }); + } } - } - if (itemnames[0] === 'All') { + if (itemnames[0] === 'All') { - // Filter items by regex - if (target.itemFilter) { + // Filter items by regex + if (target.itemFilter) { - // Use templated variables in filter - var item_pattern = new RegExp(templateSrv.replace(target.itemFilter)); + // Use templated variables in filter + var item_pattern = new RegExp(templateSrv.replace(target.itemFilter)); + return _.filter(items, function (item) { + return item_pattern.test(zabbixHelperSrv.expandItemName(item)); + }); + } else { + return items; + } + } else { + + // Filtering items return _.filter(items, function (item) { - return item_pattern.test(zabbixHelperSrv.expandItemName(item)); + return _.contains(itemnames, zabbixHelperSrv.expandItemName(item)); }); - } else { - return items; } - } else { + }).then(function (items) { - // Filtering items - return _.filter(items, function (item) { - return _.contains(itemnames, zabbixHelperSrv.expandItemName(item)); - }); - } - }).then(function (items) { - - // Don't perform query for high number of items - // to prevent Grafana slowdown - if (items.length > self.limitmetrics) { - var message = "Try to increase limitmetrics parameter in datasource config.
" - + "Current limitmetrics value is " + self.limitmetrics; - alertSrv.set("Metrics limit exceeded", message, "warning", 10000); - return []; - } else { - items = _.flatten(items); - - // Use alias only for single metric, otherwise use item names - var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias); - - if ((from < useTrendsFrom) && self.trends) { - return self.zabbixAPI.getTrends(items, from, to) - .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale)); + // Don't perform query for high number of items + // to prevent Grafana slowdown + if (items.length > self.limitmetrics) { + var message = "Try to increase limitmetrics parameter in datasource config.
" + + "Current limitmetrics value is " + self.limitmetrics; + alertSrv.set("Metrics limit exceeded", message, "warning", 10000); + return []; } else { - return self.zabbixAPI.getHistory(items, from, to) - .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); + items = _.flatten(items); + + // Use alias only for single metric, otherwise use item names + var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias); + + if ((from < useTrendsFrom) && self.trends) { + return self.zabbixAPI.getTrends(items, from, to) + .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale)); + } else { + return self.zabbixAPI.getHistory(items, from, to) + .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); + } } - } - }); + }); + } else { + // Don't show undefined and hidden targets + if (target.hide || !target.itservice || !target.slaProperty) { + return []; + } else { + return this.zabbixAPI.getSLA(target.itservice.serviceid, from, to) + .then(_.bind(zabbixHelperSrv.handleSLAResponse, zabbixHelperSrv, target.itservice, target.slaProperty)); + } + } }, this); return $q.all(_.flatten(promises)).then(function (results) { diff --git a/zabbix/helperFunctions.js b/zabbix/helperFunctions.js index 881c69b..4f076b3 100644 --- a/zabbix/helperFunctions.js +++ b/zabbix/helperFunctions.js @@ -109,6 +109,35 @@ function (angular, _) { }); }; + /** + * Convert Zabbix API service.getsla response to Grafana format + * + * @param itservice + * @param slaProperty + * @param slaObject + * @returns {{target: *, datapoints: *[]}} + */ + this.handleSLAResponse = function (itservice, slaProperty, slaObject) { + var targetSLA = slaObject[itservice.serviceid].sla[0]; + if (slaProperty.property === 'status') { + var targetStatus = slaObject[itservice.serviceid].status; + return { + target: itservice.name + ' ' + slaProperty.name, + datapoints: [ + [targetStatus, targetSLA.to * 1000] + ] + }; + } else { + return { + target: itservice.name + ' ' + slaProperty.name, + datapoints: [ + [targetSLA[slaProperty.property], targetSLA.from * 1000], + [targetSLA[slaProperty.property], targetSLA.to * 1000] + ] + }; + } + }; + /** * Expand item parameters, for example: * CPU $2 time ($3) --> CPU system time (avg1) diff --git a/zabbix/partials/query.editor.html b/zabbix/partials/query.editor.html index 30b767b..1ac3ccc 100644 --- a/zabbix/partials/query.editor.html +++ b/zabbix/partials/query.editor.html @@ -16,6 +16,16 @@ - @@ -237,5 +275,23 @@ +
+
IT services
+

+ Select "IT services" in targets menu to activate IT services mode. +

+
+ +
+
IT service property
+ +
\ No newline at end of file diff --git a/zabbix/queryCtrl.js b/zabbix/queryCtrl.js index 0a02a58..d565d1a 100644 --- a/zabbix/queryCtrl.js +++ b/zabbix/queryCtrl.js @@ -1,212 +1,257 @@ define([ - 'angular', - 'lodash', - './helperFunctions' -], -function (angular, _) { - 'use strict'; + 'angular', + 'lodash', + './helperFunctions' + ], + function (angular, _) { + 'use strict'; - var module = angular.module('grafana.controllers'); - var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + var module = angular.module('grafana.controllers'); + var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv, zabbixHelperSrv) { + module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv) { - $scope.init = function() { - $scope.targetLetters = targetLetters; - $scope.metric = { - hostGroupList: [], - hostList: [{name: '*', visible_name: 'All'}], - applicationList: [{name: '*', visible_name: 'All'}], - itemList: [{name: 'All'}] + $scope.init = function () { + $scope.targetLetters = targetLetters; + if ($scope.target.mode === 1) { + $scope.slaPropertyList = [ + {name: "Status", property: "status"}, + {name: "SLA", property: "sla"}, + {name: "OK time", property: "okTime"}, + {name: "Problem time", property: "problemTime"}, + {name: "Down time", property: "downtimeTime"} + ]; + $scope.itserviceList = [{name: "test"}]; + $scope.updateITServiceList(); + } else { + $scope.metric = { + hostGroupList: [], + hostList: [{name: '*', visible_name: 'All'}], + applicationList: [{name: '*', visible_name: 'All'}], + itemList: [{name: 'All'}] + }; + + // Update host group, host, application and item lists + $scope.updateGroupList(); + $scope.updateHostList(); + $scope.updateAppList(); + $scope.updateItemList(); + + setItemAlias(); + } + + $scope.target.errors = validateTarget($scope.target); }; - // Update host group, host, application and item lists - $scope.updateGroupList(); - $scope.updateHostList(); - $scope.updateAppList(); - $scope.updateItemList(); + /** + * Switch query editor to specified mode. + * Modes: + * 0 - items + * 1 - IT services + */ + $scope.switchEditorMode = function (mode) { + $scope.target.mode = mode; + $scope.init(); + }; - setItemAlias(); - - $scope.target.errors = validateTarget($scope.target); - }; - - /** - * Take alias from item name by default - */ - function setItemAlias() { - if (!$scope.target.alias && $scope.target.item) { - $scope.target.alias = $scope.target.item.name; + /** + * Take alias from item name by default + */ + function setItemAlias() { + if (!$scope.target.alias && $scope.target.item) { + $scope.target.alias = $scope.target.item.name; + } } - } - $scope.targetBlur = function() { - setItemAlias(); - $scope.target.errors = validateTarget($scope.target); - if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { - $scope.oldTarget = angular.copy($scope.target); - $scope.get_data(); - } - }; + $scope.targetBlur = function () { + setItemAlias(); + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - /** - * Call when host group selected - */ - $scope.selectHostGroup = function() { - $scope.updateHostList(); - $scope.updateAppList(); - $scope.updateItemList(); + /** + * Call when IT service is selected. + */ + $scope.selectITService = function () { + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - $scope.target.errors = validateTarget($scope.target); - if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { - $scope.oldTarget = angular.copy($scope.target); - $scope.get_data(); - } - }; + /** + * Call when host group selected + */ + $scope.selectHostGroup = function () { + $scope.updateHostList(); + $scope.updateAppList(); + $scope.updateItemList(); - /** - * Call when host selected - */ - $scope.selectHost = function() { - $scope.updateAppList(); - $scope.updateItemList(); + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - $scope.target.errors = validateTarget($scope.target); - if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { - $scope.oldTarget = angular.copy($scope.target); - $scope.get_data(); - } - }; + /** + * Call when host selected + */ + $scope.selectHost = function () { + $scope.updateAppList(); + $scope.updateItemList(); - /** - * Call when application selected - */ - $scope.selectApplication = function() { - $scope.updateItemList(); + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - $scope.target.errors = validateTarget($scope.target); - if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { - $scope.oldTarget = angular.copy($scope.target); - $scope.get_data(); - } - }; + /** + * Call when application selected + */ + $scope.selectApplication = function () { + $scope.updateItemList(); - /** - * Call when item selected - */ - $scope.selectItem = function() { - setItemAlias(); - $scope.target.errors = validateTarget($scope.target); - if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { - $scope.oldTarget = angular.copy($scope.target); - $scope.get_data(); - } - }; + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; + /** + * Call when item selected + */ + $scope.selectItem = function () { + setItemAlias(); + $scope.target.errors = validateTarget($scope.target); + if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { + $scope.oldTarget = angular.copy($scope.target); + $scope.get_data(); + } + }; - $scope.moveMetricQuery = function(fromIndex, toIndex) { - _.move($scope.panel.targets, fromIndex, toIndex); - }; + $scope.duplicate = function () { + var clone = angular.copy($scope.target); + $scope.panel.targets.push(clone); + }; - ////////////////////////////// - // SUGGESTION QUERIES - ////////////////////////////// + $scope.moveMetricQuery = function (fromIndex, toIndex) { + _.move($scope.panel.targets, fromIndex, toIndex); + }; - /** - * Update list of host groups - */ - $scope.updateGroupList = function() { - $scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) { - $scope.metric.groupList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.groupList); - $scope.metric.groupList = $scope.metric.groupList.concat(groups); - }); - }; + ////////////////////////////// + // SUGGESTION QUERIES + ////////////////////////////// - /** - * Update list of hosts - */ - $scope.updateHostList = function() { - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; - if (groups) { - $scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) { - $scope.metric.hostList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.hostList); - $scope.metric.hostList = $scope.metric.hostList.concat(hosts); + /** + * Update list of IT services + */ + $scope.updateITServiceList = function () { + $scope.datasource.zabbixAPI.getITService().then(function (iteservices) { + $scope.itserviceList = []; + $scope.itserviceList = $scope.itserviceList.concat(iteservices); }); - } - }; + }; - /** - * Update list of host applications - */ - $scope.updateAppList = function() { - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; - var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; - if (groups && hosts) { - $scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) { - apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { - return {name: appname}; + /** + * Update list of host groups + */ + $scope.updateGroupList = function () { + $scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) { + $scope.metric.groupList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.groupList); + $scope.metric.groupList = $scope.metric.groupList.concat(groups); + }); + }; + + /** + * Update list of hosts + */ + $scope.updateHostList = function () { + var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; + if (groups) { + $scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) { + $scope.metric.hostList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.hostList); + $scope.metric.hostList = $scope.metric.hostList.concat(hosts); }); - $scope.metric.applicationList = [{name: '*', visible_name: 'All'}]; - addTemplatedVariables($scope.metric.applicationList); - $scope.metric.applicationList = $scope.metric.applicationList.concat(apps); - }); - } - }; + } + }; - /** - * Update list of items - */ - $scope.updateItemList = function() { - var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; - var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; - var apps = $scope.target.application ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined; - if (groups && hosts && apps) { - $scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) { - // Show only unique item names - var uniq_items = _.map(_.uniq(items, function (item) { - return zabbixHelperSrv.expandItemName(item); - }), function (item) { - return {name: zabbixHelperSrv.expandItemName(item)}; + /** + * Update list of host applications + */ + $scope.updateAppList = function () { + var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; + var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; + if (groups && hosts) { + $scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) { + apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { + return {name: appname}; + }); + $scope.metric.applicationList = [{name: '*', visible_name: 'All'}]; + addTemplatedVariables($scope.metric.applicationList); + $scope.metric.applicationList = $scope.metric.applicationList.concat(apps); + }); + } + }; + + /** + * Update list of items + */ + $scope.updateItemList = function () { + var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; + var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; + var apps = $scope.target.application ? + zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined; + if (groups && hosts && apps) { + $scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) { + // Show only unique item names + var uniq_items = _.map(_.uniq(items, function (item) { + return zabbixHelperSrv.expandItemName(item); + }), function (item) { + return {name: zabbixHelperSrv.expandItemName(item)}; + }); + $scope.metric.itemList = [{name: 'All'}]; + addTemplatedVariables($scope.metric.itemList); + $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); + }); + } + }; + + /** + * Add templated variables to list of available metrics + * + * @param {Array} metricList List of metrics which variables add to + */ + function addTemplatedVariables(metricList) { + _.each(templateSrv.variables, function (variable) { + metricList.push({ + name: '$' + variable.name, + templated: true }); - $scope.metric.itemList = [{name: 'All'}]; - addTemplatedVariables($scope.metric.itemList); - $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); }); } - }; - /** - * Add templated variables to list of available metrics - * - * @param {Array} metricList List of metrics which variables add to - */ - function addTemplatedVariables(metricList) { - _.each(templateSrv.variables, function(variable) { - metricList.push({ - name: '$' + variable.name, - templated: true - }); - }); - } + ////////////////////////////// + // VALIDATION + ////////////////////////////// - ////////////////////////////// - // VALIDATION - ////////////////////////////// - - function validateTarget(target) { - var errs = {}; - if (!target) { - errs = 'Not defined'; + function validateTarget(target) { + var errs = {}; + if (!target) { + errs = 'Not defined'; + } + return errs; } - return errs; - } + + }); }); - -}); diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 0399915..c0b8980 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -245,8 +245,8 @@ function (angular, _) { /** * Get the list of hosts * - * @param {array} groupids - * @return {array} array of Zabbix host objects + * @param {string|string[]} groupids + * @return {Object} array of Zabbix host objects */ p.performHostSuggestQuery = function(groupids) { var params = { @@ -269,7 +269,7 @@ function (angular, _) { * * @param {array} hostids * @param {array} groupids - * @return {array} array of Zabbix application objects + * @return {Object} array of Zabbix application objects */ p.performAppSuggestQuery = function(hostids, /* optional */ groupids) { var params = { @@ -289,10 +289,10 @@ function (angular, _) { /** * Items request * - * @param {string or Array} hostids /////////////////////////// - * @param {string or Array} applicationids // Zabbix API parameters // - * @param {string or Array} groupids /////////////////////////// - * @return {string or Array} Array of Zabbix API item objects + * @param {string|string[]} hostids /////////////////////////// + * @param {string|string[]} applicationids // Zabbix API parameters // + * @param {string|string[]} groupids /////////////////////////// + * @return {string|string[]} Array of Zabbix API item objects */ p.performItemSuggestQuery = function(hostids, applicationids, /* optional */ groupids) { var params = { @@ -508,6 +508,25 @@ function (angular, _) { }); }; + p.getITService = function(/* optional */ serviceids) { + var params = { + output: 'extend', + serviceids: serviceids + }; + return this.performZabbixAPIRequest('service.get', params); + }; + + p.getSLA = function(serviceids, from, to) { + var params = { + serviceids: serviceids, + intervals: [{ + from: from, + to: to + }] + }; + return this.performZabbixAPIRequest('service.getsla', params); + }; + return ZabbixAPI; });