Skip to content

Commit

Permalink
autodetect zabbix version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Oct 18, 2018
1 parent a3f5090 commit 92a08a5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ awsconfig
/public_gen
/tmp
vendor/phantomjs/phantomjs
yarn-error.log

# Built plugin
dist/
Expand Down
29 changes: 28 additions & 1 deletion src/datasource-zabbix/config.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -10,7 +15,8 @@ const defaultConfig = {
alerting: false,
addThresholds: false,
alertingMinSeverity: 3,
disableReadOnlyUsersAck: false
disableReadOnlyUsersAck: false,
zabbixVersion: 3,
};

export class ZabbixDSConfigController {
Expand All @@ -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() {
Expand All @@ -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;
}
});
}
}
18 changes: 18 additions & 0 deletions src/datasource-zabbix/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 //
////////////////
Expand Down
11 changes: 10 additions & 1 deletion src/datasource-zabbix/partials/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ <h3 class="page-heading">Zabbix API details</h3>
Zabbix data source caches metric names in memory. Specify how often data will be updated.
</info-popover>
</span>
<input class="gf-form-input max-width-5"
<input class="gf-form-input max-width-7"
type="text"
ng-model='ctrl.current.jsonData.cacheTTL'
placeholder="1h">
</input>
</div>

<div class="gf-form max-width-20">
<span class="gf-form-label width-12">Zabbix version</span>
<div class="gf-form-select-wrapper max-width-7">
<select class="gf-form-input" ng-model="ctrl.current.jsonData.zabbixVersion"
ng-options="s.value as s.name for s in ctrl.zabbixVersions">
</select>
</div>
</div>
</div>

<div class="gf-form-group">
Expand Down
18 changes: 18 additions & 0 deletions src/datasource-zabbix/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 92a08a5

Please sign in to comment.