diff --git a/.gitignore b/.gitignore index 1028d12..93706d6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ .idea/ *.bat + +# Grafana linter config +.jshintrc +.jscs.json +.jsfmtrc \ No newline at end of file diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 89df0ec..0a0bddb 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -1,3 +1,4 @@ +'use strict'; define([ 'angular', 'lodash', @@ -6,7 +7,7 @@ define([ './queryCtrl' ], function (angular, _, kbn) { - 'use strict'; + //'use strict'; var module = angular.module('grafana.services'); @@ -37,7 +38,6 @@ function (angular, _, kbn) { zabbix.init(this.url, this.username, this.password); } - /** * Calls for each panel in dashboard. * @@ -87,7 +87,7 @@ function (angular, _, kbn) { .then(function (items) { // Filter hosts by regex - if (target.host.visible_name == 'All') { + if (target.host.visible_name === 'All') { if (target.hostFilter && _.every(items, _.identity.hosts)) { var host_pattern = new RegExp(target.hostFilter); items = _.filter(items, function (item) { @@ -98,7 +98,7 @@ function (angular, _, kbn) { } } - if (itemnames == 'All') { + if (itemnames === 'All') { // Filter items by regex if (target.itemFilter) { @@ -154,7 +154,6 @@ function (angular, _, kbn) { }); }; - ZabbixAPIDatasource.prototype.handleTrendResponse = function(items, alias, scale, trends) { // Group items and trends by itemid @@ -183,7 +182,6 @@ function (angular, _, kbn) { }); }; - /** * Convert Zabbix API data to Grafana format * @@ -237,7 +235,6 @@ function (angular, _, kbn) { }); }; - /** * For templated query. * Find metrics from templated request. @@ -260,13 +257,13 @@ function (angular, _, kbn) { parts.push(part); } }); - var template = _.object(['group', 'host', 'app', 'item'], parts) + var template = _.object(['group', 'host', 'app', 'item'], parts); // Get items if (parts.length === 4) { return zabbix.itemFindQuery(template.group, template.host, template.app).then(function (result) { return _.map(result, function (item) { - var itemname = zabbix.expandItemName(item) + var itemname = zabbix.expandItemName(item); return { text: itemname, expandable: false @@ -315,12 +312,10 @@ function (angular, _, kbn) { } }; - ///////////////// // Annotations // ///////////////// - ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) { var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000); var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000); @@ -356,7 +351,7 @@ function (angular, _, kbn) { .then(function (result) { var events = []; _.each(result, function(e) { - var formatted_acknowledges = formatAcknowledges(e.acknowledges);; + var formatted_acknowledges = formatAcknowledges(e.acknowledges); events.push({ annotation: annotation, time: e.clock * 1000, @@ -365,18 +360,17 @@ function (angular, _, kbn) { }); }); return events; - }); + }); } else { return []; } - }); + }); }; return ZabbixAPIDatasource; }); }); - /** * Convert multiple mettrics to array * "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN] @@ -387,10 +381,9 @@ function (angular, _, kbn) { function splitMetrics(metrics) { var remove_brackets_pattern = /^{|}$/g; var metric_split_pattern = /,(?!\s)/g; - return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern) + return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern); } - /** * Convert Date object to local time in format * YYYY-MM-DD HH:mm:ss @@ -407,7 +400,6 @@ function getShortTime(date) { return date.getFullYear() + '-' + MM + '-' + DD + ' ' + HH + ':' + mm + ':' + ss; } - /** * Format acknowledges. * @@ -416,21 +408,22 @@ function getShortTime(date) { */ function formatAcknowledges(acknowledges) { if (acknowledges.length) { - var formatted_acknowledges = '

Acknowledges:
'; + var formatted_acknowledges = '

Acknowledges:
TimeUserComments
' + + ''; _.each(_.map(acknowledges, function (ack) { var time = new Date(ack.clock * 1000); - return ''; + return ''; }), function (ack) { - formatted_acknowledges = formatted_acknowledges.concat(ack) + formatted_acknowledges = formatted_acknowledges.concat(ack); }); - formatted_acknowledges = formatted_acknowledges.concat('
TimeUserComments
' + getShortTime(time) + '' + ack.alias + ' (' + ack.name+ ' ' + ack.surname + ')' + '' + ack.message + '
' + getShortTime(time) + '' + ack.alias + + ' (' + ack.name + ' ' + ack.surname + ')' + '' + ack.message + '
') + formatted_acknowledges = formatted_acknowledges.concat(''); return formatted_acknowledges; } else { return ''; } } - /** * Downsample datapoints series * @@ -440,7 +433,7 @@ function formatAcknowledges(acknowledges) { * @return {array} [[, ], ...] */ function downsampleSeries(datapoints, time_to, ms_interval) { - var downsampledSeries = new Array(); + var downsampledSeries = []; var timeWindow = { from: time_to * 1000 - ms_interval, to: time_to * 1000 diff --git a/zabbix/queryCtrl.js b/zabbix/queryCtrl.js index 4f847c9..ceafa28 100644 --- a/zabbix/queryCtrl.js +++ b/zabbix/queryCtrl.js @@ -31,7 +31,6 @@ function (angular, _) { $scope.target.errors = validateTarget($scope.target); }; - /** * Take alias from item name by default */ @@ -39,8 +38,7 @@ function (angular, _) { if (!$scope.target.alias && $scope.target.item) { $scope.target.alias = $scope.target.item.name; } - }; - + } $scope.targetBlur = function() { setItemAlias(); @@ -51,12 +49,11 @@ function (angular, _) { } }; - /** * Call when host group selected */ $scope.selectHostGroup = function() { - $scope.updateHostList() + $scope.updateHostList(); $scope.updateAppList(); $scope.updateItemList(); @@ -67,7 +64,6 @@ function (angular, _) { } }; - /** * Call when host selected */ @@ -82,7 +78,6 @@ function (angular, _) { } }; - /** * Call when application selected */ @@ -96,7 +91,6 @@ function (angular, _) { } }; - /** * Call when item selected */ @@ -109,13 +103,11 @@ function (angular, _) { } }; - $scope.duplicate = function() { var clone = angular.copy($scope.target); $scope.panel.targets.push(clone); }; - $scope.moveMetricQuery = function(fromIndex, toIndex) { _.move($scope.panel.targets, fromIndex, toIndex); }; @@ -124,7 +116,6 @@ function (angular, _) { // SUGGESTION QUERIES ////////////////////////////// - /** * Update list of host groups */ @@ -137,7 +128,6 @@ function (angular, _) { }); }; - /** * Update list of hosts */ @@ -151,7 +141,6 @@ function (angular, _) { }); }; - /** * Update list of host applications */ @@ -162,19 +151,18 @@ function (angular, _) { var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; zabbix.appFindQuery(hosts, groups).then(function (apps) { - var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { + apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { return {name: appname}; }); $scope.metric.applicationList = $scope.metric.applicationList.concat(apps); }); }; - /** * Update list of items */ $scope.updateItemList = function() { - $scope.metric.itemList = [{name: 'All'}];; + $scope.metric.itemList = [{name: 'All'}]; addTemplatedVariables($scope.metric.itemList); var groups = $scope.target.group ? splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; @@ -185,13 +173,12 @@ function (angular, _) { var uniq_items = _.map(_.uniq(items, function (item) { return zabbix.expandItemName(item); }), function (item) { - return {name: zabbix.expandItemName(item)} + return {name: zabbix.expandItemName(item)}; }); $scope.metric.itemList = $scope.metric.itemList.concat(uniq_items); }); }; - /** * Add templated variables to list of available metrics * @@ -202,10 +189,9 @@ function (angular, _) { metricList.push({ name: '$' + variable.name, templated: true - }) + }); }); - }; - + } ////////////////////////////// // VALIDATION @@ -213,7 +199,9 @@ function (angular, _) { function validateTarget(target) { var errs = {}; - + if (!target) { + errs = 'Not defined'; + } return errs; } @@ -221,7 +209,6 @@ function (angular, _) { }); - /** * Convert multiple mettrics to array * "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN] @@ -230,7 +217,8 @@ function (angular, _) { * @return {Array} [metric1, metcic2,..., metricN] */ function splitMetrics(metrics) { + 'use strict'; var remove_brackets_pattern = /^{|}$/g; var metric_split_pattern = /,(?!\s)/g; - return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern) + return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern); } diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index eae7ea5..3ac7356 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -16,14 +16,12 @@ function (angular, _) { this.url = api_url; this.username = username; this.password = password; - } - + }; ////////////////// // Core methods // ////////////////// - /** * Request data from Zabbix API * @@ -56,9 +54,9 @@ function (angular, _) { else if (response.data.error) { // Handle auth errors - if (response.data.error.data == "Session terminated, re-login, please." || - response.data.error.data == "Not authorised." || - response.data.error.data == "Not authorized") { + if (response.data.error.data === "Session terminated, re-login, please." || + response.data.error.data === "Not authorised." || + response.data.error.data === "Not authorized") { return self.performZabbixAPILogin().then(function (response) { self.auth = response; return self.performZabbixAPIRequest(method, params); @@ -69,7 +67,6 @@ function (angular, _) { }); }; - /** * Get authentication token. * @@ -99,13 +96,10 @@ function (angular, _) { }); }; - - ///////////////////////// // API method wrappers // ///////////////////////// - /** * Perform history query from Zabbix API * @@ -141,7 +135,6 @@ function (angular, _) { }); }; - /** * Perform trends query from Zabbix API * Use trends api extension from ZBXNEXT-1193 patch. @@ -178,7 +171,6 @@ function (angular, _) { }); }; - /** * Get the list of host groups * @@ -197,7 +189,6 @@ function (angular, _) { return this.performZabbixAPIRequest('hostgroup.get', params); }; - /** * Get the list of hosts * @@ -220,7 +211,6 @@ function (angular, _) { return this.performZabbixAPIRequest('host.get', params); }; - /** * Get the list of applications * @@ -243,7 +233,6 @@ function (angular, _) { return this.performZabbixAPIRequest('application.get', params); }; - /** * Items request * @@ -287,7 +276,6 @@ function (angular, _) { return this.performZabbixAPIRequest('item.get', params); }; - /** * Get groups by names * @@ -298,7 +286,7 @@ function (angular, _) { var params = { output: ['name'] }; - if (group != '*') { + if (group !== '*') { params.filter = { name: group }; @@ -306,7 +294,6 @@ function (angular, _) { return this.performZabbixAPIRequest('hostgroup.get', params); }; - /** * Search group by name. * @@ -324,7 +311,6 @@ function (angular, _) { return this.performZabbixAPIRequest('hostgroup.get', params); }; - /** * Get hosts by names * @@ -335,7 +321,7 @@ function (angular, _) { var params = { output: ['host', 'name'] }; - if (hostnames != '*') { + if (hostnames !== '*') { params.filter = { name: hostnames }; @@ -343,7 +329,6 @@ function (angular, _) { return this.performZabbixAPIRequest('host.get', params); }; - /** * Get applications by names * @@ -353,16 +338,15 @@ function (angular, _) { this.getAppByName = function (application) { var params = { output: ['name'] - } - if (application != '*') { + }; + if (application !== '*') { params.filter = { name: application }; - }; + } return this.performZabbixAPIRequest('application.get', params); }; - /** * Get items belongs to passed groups, hosts and * applications @@ -376,7 +360,7 @@ function (angular, _) { var promises = []; // Get hostids from names - if (hosts && hosts != '*') { + if (hosts && hosts !== '*') { promises.push(this.getHostByName(hosts)); } // Get groupids from names @@ -391,18 +375,21 @@ function (angular, _) { var self = this; return $q.all(promises).then(function (results) { results = _.flatten(results); + var groupids; + var hostids; + var applicationids; if (groups) { - var groupids = _.map(_.filter(results, function (object) { + groupids = _.map(_.filter(results, function (object) { return object.groupid; }), 'groupid'); } - if (hosts && hosts != '*') { - var hostids = _.map(_.filter(results, function (object) { + if (hosts && hosts !== '*') { + hostids = _.map(_.filter(results, function (object) { return object.hostid; }), 'hostid'); } if (apps) { - var applicationids = _.map(_.filter(results, function (object) { + applicationids = _.map(_.filter(results, function (object) { return object.applicationid; }), 'applicationid'); } @@ -411,7 +398,6 @@ function (angular, _) { }); }; - /** * Find applications belongs to passed groups and hosts * @@ -423,7 +409,7 @@ function (angular, _) { var promises = []; // Get hostids from names - if (hosts && hosts != '*') { + if (hosts && hosts !== '*') { promises.push(this.getHostByName(hosts)); } // Get groupids from names @@ -434,13 +420,15 @@ function (angular, _) { var self = this; return $q.all(promises).then(function (results) { results = _.flatten(results); + var groupids; + var hostids; if (groups) { - var groupids = _.map(_.filter(results, function (object) { + groupids = _.map(_.filter(results, function (object) { return object.groupid; }), 'groupid'); } - if (hosts && hosts != '*') { - var hostids = _.map(_.filter(results, function (object) { + if (hosts && hosts !== '*') { + hostids = _.map(_.filter(results, function (object) { return object.hostid; }), 'hostid'); } @@ -449,7 +437,6 @@ function (angular, _) { }); }; - /** * Find hosts belongs to passed groups * @@ -468,7 +455,6 @@ function (angular, _) { }); }; - /** * Expand item parameters, for example: * CPU $2 time ($3) --> CPU system time (avg1) @@ -487,9 +473,9 @@ function (angular, _) { // replace item parameters for (var i = key_params.length; i >= 1; i--) { name = name.replace('$' + i, key_params[i - 1]); - }; + } return name; - } + }; }); });