backend: proxy Zabbix API requests through the backend
This commit is contained in:
@@ -17,6 +17,7 @@ export class ZabbixDatasource {
|
||||
/** @ngInject */
|
||||
constructor(instanceSettings, templateSrv, backendSrv, datasourceSrv, zabbixAlertingSrv) {
|
||||
this.templateSrv = templateSrv;
|
||||
this.backendSrv = backendSrv;
|
||||
this.zabbixAlertingSrv = zabbixAlertingSrv;
|
||||
|
||||
this.enableDebugLog = config.buildInfo.env === 'development';
|
||||
@@ -25,6 +26,7 @@ export class ZabbixDatasource {
|
||||
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
|
||||
|
||||
// General data source settings
|
||||
this.datasourceId = instanceSettings.id;
|
||||
this.name = instanceSettings.name;
|
||||
this.url = instanceSettings.url;
|
||||
this.basicAuth = instanceSettings.basicAuth;
|
||||
@@ -74,7 +76,7 @@ export class ZabbixDatasource {
|
||||
dbConnectionRetentionPolicy: this.dbConnectionRetentionPolicy,
|
||||
};
|
||||
|
||||
this.zabbix = new Zabbix(zabbixOptions, datasourceSrv, backendSrv);
|
||||
this.zabbix = new Zabbix(zabbixOptions, datasourceSrv, backendSrv, this.datasourceId);
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
@@ -87,6 +89,9 @@ export class ZabbixDatasource {
|
||||
* @return {Object} Grafana metrics object with timeseries data for each target.
|
||||
*/
|
||||
query(options) {
|
||||
// console.log('invoking doTsdbRequest()');
|
||||
// this.doTsdbRequest(options);
|
||||
|
||||
// Get alerts for current panel
|
||||
if (this.alertingEnabled) {
|
||||
this.alertQuery(options).then(alert => {
|
||||
@@ -166,6 +171,27 @@ export class ZabbixDatasource {
|
||||
});
|
||||
}
|
||||
|
||||
doTsdbRequest(options) {
|
||||
const tsdbRequestData = {
|
||||
queries: options.targets.map(target => {
|
||||
target.datasourceId = this.datasourceId;
|
||||
target.queryType = 'zabbixAPI';
|
||||
return target;
|
||||
}),
|
||||
};
|
||||
|
||||
if (options.range) {
|
||||
tsdbRequestData.from = options.range.from.valueOf().toString();
|
||||
tsdbRequestData.to = options.range.to.valueOf().toString();
|
||||
}
|
||||
|
||||
return this.backendSrv.datasourceRequest({
|
||||
url: '/api/tsdb/query',
|
||||
method: 'POST',
|
||||
data: tsdbRequestData
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Query target data for Metrics mode
|
||||
*/
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ZBX_ACK_ACTION_NONE, ZBX_ACK_ACTION_ACK, ZBX_ACK_ACTION_ADD_MESSAGE, MI
|
||||
* Wraps API calls and provides high-level methods.
|
||||
*/
|
||||
export class ZabbixAPIConnector {
|
||||
constructor(api_url, username, password, version, basicAuth, withCredentials, backendSrv) {
|
||||
constructor(api_url, username, password, version, basicAuth, withCredentials, backendSrv, datasourceId) {
|
||||
this.url = api_url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
@@ -22,6 +22,9 @@ export class ZabbixAPIConnector {
|
||||
withCredentials: withCredentials
|
||||
};
|
||||
|
||||
this.datasourceId = datasourceId;
|
||||
this.backendSrv = backendSrv;
|
||||
|
||||
this.loginPromise = null;
|
||||
this.loginErrorCount = 0;
|
||||
this.maxLoginAttempts = 3;
|
||||
@@ -37,6 +40,8 @@ export class ZabbixAPIConnector {
|
||||
//////////////////////////
|
||||
|
||||
request(method, params) {
|
||||
this.tsdbRequest(method, params);
|
||||
|
||||
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
|
||||
.catch(error => {
|
||||
if (isNotAuthorized(error.data)) {
|
||||
@@ -55,6 +60,25 @@ export class ZabbixAPIConnector {
|
||||
});
|
||||
}
|
||||
|
||||
tsdbRequest(method, params) {
|
||||
const tsdbRequestData = {
|
||||
queries: [{
|
||||
datasourceId: this.datasourceId,
|
||||
queryType: 'zabbixAPI',
|
||||
target: {
|
||||
method,
|
||||
params,
|
||||
},
|
||||
}],
|
||||
};
|
||||
|
||||
return this.backendSrv.datasourceRequest({
|
||||
url: '/api/tsdb/query',
|
||||
method: 'POST',
|
||||
data: tsdbRequestData
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* When API unauthenticated or auth token expired each request produce login()
|
||||
* call. But auth token is common to all requests. This function wraps login() method
|
||||
|
||||
@@ -25,7 +25,7 @@ const REQUESTS_TO_BIND = [
|
||||
];
|
||||
|
||||
export class Zabbix {
|
||||
constructor(options, datasourceSrv, backendSrv) {
|
||||
constructor(options, datasourceSrv, backendSrv, datasourceId) {
|
||||
let {
|
||||
url,
|
||||
username,
|
||||
@@ -49,7 +49,7 @@ export class Zabbix {
|
||||
};
|
||||
this.cachingProxy = new CachingProxy(cacheOptions);
|
||||
|
||||
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, zabbixVersion, basicAuth, withCredentials, backendSrv);
|
||||
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, zabbixVersion, basicAuth, withCredentials, backendSrv, datasourceId);
|
||||
|
||||
this.proxyfyRequests();
|
||||
this.cacheRequests();
|
||||
|
||||
Reference in New Issue
Block a user