Skip to content

Commit

Permalink
Merge branch 'fix-event-ack'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Oct 21, 2018
2 parents 61380be + c73078d commit 8bcd9bc
Show file tree
Hide file tree
Showing 12 changed files with 1,515 additions and 926 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
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = function(grunt) {

const sass = require('node-sass');
require('load-grunt-tasks')(grunt);

grunt.loadNpmTasks('grunt-execute');
Expand Down Expand Up @@ -114,6 +115,7 @@ module.exports = function(grunt) {

sass: {
options: {
implementation: sass,
sourceMap: true
},
dist: {
Expand Down
33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,35 @@
"url": "https://github.com/alexanderzobnin/grafana-zabbix/issues"
},
"devDependencies": {
"babel-jest": "^21.2.0",
"babel": "^6.23.0",
"babel-jest": "^23.6.0",
"babel-plugin-transform-es2015-for-of": "^6.6.0",
"babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
"babel-preset-es2015": "^6.5.0",
"babel": "~6.5.1",
"benchmark": "^2.1.4",
"codecov": "^3.0.0",
"grunt-babel": "~6.0.0",
"codecov": "^3.1.0",
"grunt": "^1.0.3",
"grunt-babel": "^7.0.0",
"grunt-benchmark": "^1.0.0",
"grunt-cli": "~1.2.0",
"grunt-contrib-clean": "~0.6.0",
"grunt-contrib-copy": "~0.8.2",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-uglify": "~0.11.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-execute": "~0.2.2",
"grunt-jscs": "^2.8.0",
"grunt-sass": "^1.1.0",
"grunt-systemjs-builder": "^0.2.5",
"grunt": "~0.4.5",
"grunt-cli": "^1.3.1",
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jshint": "^2.0.0",
"grunt-contrib-uglify": "^4.0.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-execute": "^0.2.2",
"grunt-jscs": "^3.0.1",
"grunt-sass": "^3.0.2",
"grunt-systemjs-builder": "^1.0.0",
"jest": "^23.5.0",
"jsdom": "~11.3.0",
"jshint-stylish": "^2.1.0",
"load-grunt-tasks": "~3.2.0",
"lodash": "~4.17.5",
"moment": "~2.21.0",
"node-sass": "^4.9.4",
"systemjs": "^0.20.19",
"tether-drop": "^1.4.2"
},
"dependencies": {
},
"homepage": "http://grafana-zabbix.org"
}
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;
}
});
}
}
11 changes: 8 additions & 3 deletions src/datasource-zabbix/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Data point
export const DATAPOINT_VALUE = 0;
export const DATAPOINT_TS = 1;

// Editor modes
export const MODE_METRICS = 0;
export const MODE_ITSERVICE = 1;
Expand All @@ -17,9 +21,10 @@ export const SHOW_ALL_TRIGGERS = [0, 1];
export const SHOW_ALL_EVENTS = [0, 1];
export const SHOW_OK_EVENTS = 1;

// Data point
export const DATAPOINT_VALUE = 0;
export const DATAPOINT_TS = 1;
// Acknowledge
export const ZBX_ACK_ACTION_NONE = 0;
export const ZBX_ACK_ACTION_ACK = 2;
export const ZBX_ACK_ACTION_ADD_MESSAGE = 4;

export const TRIGGER_SEVERITY = [
{val: 0, text: 'Not classified'},
Expand Down
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
@@ -1,18 +1,20 @@
import _ from 'lodash';
import * as utils from '../../../utils';
import { ZabbixAPICore } from './zabbixAPICore';
import { ZBX_ACK_ACTION_NONE, ZBX_ACK_ACTION_ACK, ZBX_ACK_ACTION_ADD_MESSAGE } from '../../../constants';

/**
* Zabbix API Wrapper.
* Creates Zabbix API instance with given parameters (url, credentials and other).
* Wraps API calls and provides high-level methods.
*/
export class ZabbixAPIConnector {
constructor(api_url, username, password, basicAuth, withCredentials, backendSrv) {
constructor(api_url, username, password, version, basicAuth, withCredentials, backendSrv) {
this.url = api_url;
this.username = username;
this.password = password;
this.auth = "";
this.auth = '';
this.version = version;

this.requestOptions = {
basicAuth: basicAuth,
Expand Down Expand Up @@ -47,9 +49,7 @@ export class ZabbixAPIConnector {
.then(() => this.request(method, params));
}
} else {
// Handle API errors
let message = error.data ? error.data : error.statusText;
return Promise.reject(message);
return Promise.reject(error);
}
});
}
Expand Down Expand Up @@ -92,9 +92,11 @@ export class ZabbixAPIConnector {
////////////////////////////////

acknowledgeEvent(eventid, message) {
var params = {
const action = this.version >= 4 ? ZBX_ACK_ACTION_ACK + ZBX_ACK_ACTION_ADD_MESSAGE : ZBX_ACK_ACTION_NONE;
const params = {
eventids: eventid,
message: message
message: message,
action: action
};

return this.request('event.acknowledge', params);
Expand Down
5 changes: 3 additions & 2 deletions src/datasource-zabbix/zabbix/zabbix.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const REQUESTS_TO_CACHE = [

const REQUESTS_TO_BIND = [
'getHistory', 'getTrend', 'getMacros', 'getItemsByIDs', 'getEvents', 'getAlerts', 'getHostAlerts',
'getAcknowledges', 'getITService', 'getVersion', 'login'
'getAcknowledges', 'getITService', 'getVersion', 'login', 'acknowledgeEvent'
];

export class Zabbix {
Expand All @@ -28,6 +28,7 @@ export class Zabbix {
password,
basicAuth,
withCredentials,
zabbixVersion,
cacheTTL,
enableDirectDBConnection,
dbConnectionDatasourceId,
Expand All @@ -43,7 +44,7 @@ export class Zabbix {
};
this.cachingProxy = new CachingProxy(cacheOptions);

this.zabbixAPI = new ZabbixAPIConnector(url, username, password, basicAuth, withCredentials, backendSrv);
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, zabbixVersion, basicAuth, withCredentials, backendSrv);

if (enableDirectDBConnection) {
let dbConnectorOptions = {
Expand Down
36 changes: 20 additions & 16 deletions src/panel-triggers/triggers_panel_ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ export class TriggerPanelCtrl extends PanelCtrl {
this.events.on('refresh', this.onRefresh.bind(this));
}

setPanelError(err, defaultError) {
const defaultErrorMessage = defaultError || "Request Error";
this.inspector = { error: err };
this.error = err.message || defaultErrorMessage;
if (err.data) {
if (err.data.message) {
this.error = err.data.message;
}
if (err.data.error) {
this.error = err.data.error;
}
}

this.events.emit('data-error', err);
console.log('Panel data error:', err);
}

initDatasources() {
let promises = _.map(this.panel.datasources, (ds) => {
// Load datasource
Expand Down Expand Up @@ -161,18 +178,7 @@ export class TriggerPanelCtrl extends PanelCtrl {
return;
}

this.error = err.message || "Request Error";
if (err.data) {
if (err.data.message) {
this.error = err.data.message;
}
if (err.data.error) {
this.error = err.data.error;
}
}

this.events.emit('data-error', err);
console.log('Panel data error:', err);
this.setPanelError(err);
});
}

Expand Down Expand Up @@ -459,16 +465,14 @@ export class TriggerPanelCtrl extends PanelCtrl {
return Promise.reject({message: 'You have no permissions to acknowledge events.'});
}
if (eventid) {
return datasource.zabbix.zabbixAPI.acknowledgeEvent(eventid, ack_message);
return datasource.zabbix.acknowledgeEvent(eventid, ack_message);
} else {
return Promise.reject({message: 'Trigger has no events. Nothing to acknowledge.'});
}
})
.then(this.onRefresh.bind(this))
.catch((err) => {
this.error = err.message || "Acknowledge Error";
this.events.emit('data-error', err);
console.log('Panel data error:', err);
this.setPanelError(err);
});
}

Expand Down
Loading

0 comments on commit 8bcd9bc

Please sign in to comment.