From 92a08a5c58adeec49b050ebfed357f160311dfc9 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 18 Oct 2018 20:27:23 +0300 Subject: [PATCH] autodetect zabbix version --- .gitignore | 1 + src/datasource-zabbix/config.controller.js | 29 ++++++++++++++++++- src/datasource-zabbix/datasource.js | 18 ++++++++++++ src/datasource-zabbix/partials/config.html | 11 ++++++- src/datasource-zabbix/utils.js | 18 ++++++++++++ .../zabbix_api/zabbixAPIConnector.js | 3 +- 6 files changed, 77 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8a651e6bb..8087f9863 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ awsconfig /public_gen /tmp vendor/phantomjs/phantomjs +yarn-error.log # Built plugin dist/ diff --git a/src/datasource-zabbix/config.controller.js b/src/datasource-zabbix/config.controller.js index 0996865dd..02de9d267 100644 --- a/src/datasource-zabbix/config.controller.js +++ b/src/datasource-zabbix/config.controller.js @@ -2,6 +2,11 @@ import _ from 'lodash'; import { migrateDSConfig } from './migrations'; const SUPPORTED_SQL_DS = ['mysql', 'postgres']; +const zabbixVersions = [ + { name: '2.x', value: 2 }, + { name: '3.x', value: 3 }, + { name: '4.x', value: 4 }, +]; const defaultConfig = { trends: false, @@ -10,7 +15,8 @@ const defaultConfig = { alerting: false, addThresholds: false, alertingMinSeverity: 3, - disableReadOnlyUsersAck: false + disableReadOnlyUsersAck: false, + zabbixVersion: 3, }; export class ZabbixDSConfigController { @@ -22,6 +28,8 @@ export class ZabbixDSConfigController { this.current.jsonData = migrateDSConfig(this.current.jsonData); _.defaults(this.current.jsonData, defaultConfig); this.sqlDataSources = this.getSupportedSQLDataSources(); + this.zabbixVersions = _.cloneDeep(zabbixVersions); + this.autoDetectZabbixVersion(); } getSupportedSQLDataSources() { @@ -30,4 +38,23 @@ export class ZabbixDSConfigController { return _.includes(SUPPORTED_SQL_DS, ds.type); }); } + + autoDetectZabbixVersion() { + if (!this.current.id) { + return; + } + + this.datasourceSrv.loadDatasource(this.current.name) + .then(ds => { + return ds.getVersion(); + }) + .then(version => { + if (version) { + if (!_.find(zabbixVersions, ['value', version])) { + this.zabbixVersions.push({ name: version + '.x', value: version }); + } + this.current.jsonData.zabbixVersion = version; + } + }); + } } diff --git a/src/datasource-zabbix/datasource.js b/src/datasource-zabbix/datasource.js index 3e2c6a158..3f46418a6 100644 --- a/src/datasource-zabbix/datasource.js +++ b/src/datasource-zabbix/datasource.js @@ -9,6 +9,8 @@ import responseHandler from './responseHandler'; import { Zabbix } from './zabbix/zabbix'; import { ZabbixAPIError } from './zabbix/connectors/zabbix_api/zabbixAPICore'; +const DEFAULT_ZABBIX_VERSION = 3; + export class ZabbixDatasource { /** @ngInject */ @@ -47,6 +49,7 @@ export class ZabbixDatasource { // Other options this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck; + this.zabbixVersion = jsonData.zabbixVersion || DEFAULT_ZABBIX_VERSION; // Direct DB Connection options this.enableDirectDBConnection = jsonData.dbConnectionEnable || false; @@ -59,6 +62,7 @@ export class ZabbixDatasource { password: this.password, basicAuth: this.basicAuth, withCredentials: this.withCredentials, + zabbixVersion: this.zabbixVersion, cacheTTL: this.cacheTTL, enableDirectDBConnection: this.enableDirectDBConnection, dbConnectionDatasourceId: this.dbConnectionDatasourceId, @@ -380,6 +384,20 @@ export class ZabbixDatasource { }); } + /** + * Get Zabbix version + */ + getVersion() { + return this.zabbix.getVersion() + .then(version => { + const zabbixVersion = utils.parseVersion(version); + if (!zabbixVersion) { + return null; + } + return zabbixVersion.major; + }); + } + //////////////// // Templating // //////////////// diff --git a/src/datasource-zabbix/partials/config.html b/src/datasource-zabbix/partials/config.html index 985425189..1530d2f22 100644 --- a/src/datasource-zabbix/partials/config.html +++ b/src/datasource-zabbix/partials/config.html @@ -67,12 +67,21 @@

Zabbix API details

Zabbix data source caches metric names in memory. Specify how often data will be updated. - + +
+ Zabbix version +
+ +
+
diff --git a/src/datasource-zabbix/utils.js b/src/datasource-zabbix/utils.js index efc8ad295..53f0bea09 100644 --- a/src/datasource-zabbix/utils.js +++ b/src/datasource-zabbix/utils.js @@ -240,6 +240,24 @@ export function sequence(funcsArray) { }; } +const versionPattern = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z\.]+))?/; + +export function isValidVersion(version) { + return versionPattern.exec(version); +} + +export function parseVersion(version) { + const match = versionPattern.exec(version); + if (!match) { + return null; + } + const major = Number(match[1]); + const minor = Number(match[2] || 0); + const patch = Number(match[3] || 0); + const meta = match[4]; + return { major, minor, patch, meta }; +} + // Fix for backward compatibility with lodash 2.4 if (!_.includes) { _.includes = _.contains; diff --git a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js index 7a1ef93f6..e6abf7815 100644 --- a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js +++ b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js @@ -92,7 +92,8 @@ export class ZabbixAPIConnector { acknowledgeEvent(eventid, message) { var params = { eventids: eventid, - message: message + message: message, + action: 6 }; return this.request('event.acknowledge', params);