Merge branch 'multidatasource-pr' of github.com:dneth/grafana-zabbix into dneth-multidatasource-pr

This commit is contained in:
Alexander Zobnin
2019-12-13 13:52:09 +03:00
22 changed files with 6852 additions and 860 deletions

View File

@@ -9,13 +9,29 @@ import dataProcessor from './dataProcessor';
import responseHandler from './responseHandler';
import { Zabbix } from './zabbix/zabbix';
import { ZabbixAPIError } from './zabbix/connectors/zabbix_api/zabbixAPICore';
import {
DataSourceApi,
DataSourceInstanceSettings,
} from '@grafana/ui';
import { BackendSrv, DataSourceSrv } from '@grafana/runtime';
import { ZabbixAlertingService } from './zabbixAlerting.service';
import { ZabbixConnectionTestQuery, ZabbixConnectionInfo, TemplateSrv, TSDBResponse } from './types';
const DEFAULT_ZABBIX_VERSION = 3;
export class ZabbixDatasource {
export class ZabbixDatasource extends DataSourceApi {
/** @ngInject */
/**
* @ngInject
* @param {DataSourceInstanceSettings} instanceSettings
* @param {TemplateSrv} templateSrv
* @param {BackendSrv} backendSrv
* @param {DataSourceSrv} datasourceSrv
* @param {ZabbixAlertingService} zabbixAlertingSrv
*/
constructor(instanceSettings, templateSrv, backendSrv, datasourceSrv, zabbixAlertingSrv) {
super(instanceSettings);
this.type = 'zabbix';
this.templateSrv = templateSrv;
this.backendSrv = backendSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
@@ -185,11 +201,26 @@ export class ZabbixDatasource {
tsdbRequestData.to = options.range.to.valueOf().toString();
}
return this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: tsdbRequestData
});
return this.backendSrv.post('/api/tsdb/query', tsdbRequestData);
}
/**
* @returns {Promise<TSDBResponse>}
*/
doTSDBConnectionTest() {
/**
* @type {{ queries: ZabbixConnectionTestQuery[] }}
*/
const tsdbRequestData = {
queries: [
{
datasourceId: this.datasourceId,
queryType: 'connectionTest'
}
]
};
return this.backendSrv.post('/api/tsdb/query', tsdbRequestData);
}
/**
@@ -385,10 +416,13 @@ export class ZabbixDatasource {
/**
* Test connection to Zabbix API and external history DB.
*/
testDatasource() {
return this.zabbix.testDataSource()
.then(result => {
const { zabbixVersion, dbConnectorStatus } = result;
async testDatasource() {
try {
const result = await this.doTSDBConnectionTest();
/**
* @type {ZabbixConnectionInfo}
*/
const { zabbixVersion, dbConnectorStatus } = result.results["zabbixAPI"].meta;
let message = `Zabbix API version: ${zabbixVersion}`;
if (dbConnectorStatus) {
message += `, DB connector type: ${dbConnectorStatus.dsType}`;
@@ -398,27 +432,30 @@ export class ZabbixDatasource {
title: "Success",
message: message
};
})
.catch(error => {
}
catch (error) {
if (error instanceof ZabbixAPIError) {
return {
status: "error",
title: error.message,
message: error.message
};
} else if (error.data && error.data.message) {
}
else if (error.data && error.data.message) {
return {
status: "error",
title: "Connection failed",
message: "Connection failed: " + error.data.message
title: "Zabbix Client Error",
message: error.data.message
};
} else if (typeof(error) === 'string') {
}
else if (typeof (error) === 'string') {
return {
status: "error",
title: "Connection failed",
message: "Connection failed: " + error
title: "Unknown Error",
message: error
};
} else {
}
else {
console.log(error);
return {
status: "error",
@@ -426,7 +463,7 @@ export class ZabbixDatasource {
message: "Could not connect to given url"
};
}
});
}
}
/**

View File

@@ -1,4 +1,5 @@
import _ from 'lodash';
import { ZabbixMetricsQuery } from './types';
/**
* Query format migration.
@@ -19,7 +20,7 @@ export function isGrafana2target(target) {
}
}
export function migrateFrom2To3version(target) {
export function migrateFrom2To3version(target: ZabbixMetricsQuery) {
target.group.filter = target.group.name === "*" ? "/.*/" : target.group.name;
target.host.filter = target.host.name === "*" ? convertToRegex(target.hostFilter) : target.host.name;
target.application.filter = target.application.name === "*" ? "" : target.application.name;

View File

@@ -1,4 +1,4 @@
import { loadPluginCss } from 'grafana/app/plugins/sdk';
import { loadPluginCss } from '@grafana/runtime';
import { ZabbixDatasource } from './datasource';
import { ZabbixQueryController } from './query.controller';
import { ZabbixDSConfigController } from './config.controller';

View File

@@ -0,0 +1,85 @@
import { DataQuery } from '@grafana/ui';
export interface ZabbixConnectionInfo {
zabbixVersion: string;
dbConnectorStatus: {
dsType: string;
dsName: string;
};
}
export interface ZabbixConnectionTestQuery {
datasourceId: number;
queryType: string;
}
export interface ZabbixMetricsQuery extends DataQuery {
triggers: { minSeverity: string; acknowledged: boolean; count: number; };
queryType: string;
datasourceId: number;
functions: { name: string; params: any; def: { name: string; params: any; } }[];
options: any;
textFilter: string;
mode: number;
itemids: number[];
useCaptureGroups: boolean;
group: { filter: string; name: string; };
host: { filter: string; name: string; };
hostFilter: string;
application: { filter: string; name: string; };
item: { filter: string; name: string; };
itemFilter: string;
}
// export { TemplateSrv } from 'grafana/app/features/templating/template_srv';
// export { DashboardSrv } from 'grafana/app/features/dashboard/dashboard_srv';
// The paths of these files have moved around in Grafana and they don't resolve properly
// either. Safer not to bother trying to import them just for type hinting.
export interface TemplateSrv {
variables: {
name: string;
};
highlightVariablesAsHtml(str: any): any;
replace(target: any, scopedVars?: any, format?: any): any;
}
export interface DashboardSrv {
dash: any
}
// Grafana types from backend code
type RowValues = object[];
type TimePoint = [number?, number?];
type TimeSeriesPoints = TimePoint[];
type TimeSeriesSlice = TimeSeries[];
interface TimeSeries {
name: string;
points: TimeSeriesPoints;
tags: { [key: string]: string };
}
interface TableColumn {
text: string;
}
interface Table {
columns: TableColumn[];
rows: RowValues[];
}
interface QueryResult {
error: string;
refId: string;
meta: any;
series: TimeSeriesSlice[];
tables: Table[];
}
export interface TSDBResponse {
results: { [key: string]: QueryResult };
message: string;
}

View File

@@ -110,37 +110,37 @@ export class Zabbix {
}
```
*/
testDataSource() {
let zabbixVersion;
let dbConnectorStatus;
return this.getVersion()
.then(version => {
zabbixVersion = version;
return this.login();
})
.then(() => {
if (this.enableDirectDBConnection) {
return this.dbConnector.testDataSource();
} else {
return Promise.resolve();
}
})
.catch(error => {
if (error instanceof ZabbixNotImplemented) {
return Promise.resolve();
}
return Promise.reject(error);
})
.then(testResult => {
if (testResult) {
dbConnectorStatus = {
dsType: this.dbConnector.datasourceTypeName,
dsName: this.dbConnector.datasourceName
};
}
return { zabbixVersion, dbConnectorStatus };
});
}
// testDataSource() {
// let zabbixVersion;
// let dbConnectorStatus;
// return this.getVersion()
// .then(version => {
// zabbixVersion = version;
// return this.login();
// })
// .then(() => {
// if (this.enableDirectDBConnection) {
// return this.dbConnector.testDataSource();
// } else {
// return Promise.resolve();
// }
// })
// .catch(error => {
// if (error instanceof ZabbixNotImplemented) {
// return Promise.resolve();
// }
// return Promise.reject(error);
// })
// .then(testResult => {
// if (testResult) {
// dbConnectorStatus = {
// dsType: this.dbConnector.datasourceTypeName,
// dsName: this.dbConnector.datasourceName
// };
// }
// return { zabbixVersion, dbConnectorStatus };
// });
// }
getItemsFromTarget(target, options) {
let parts = ['group', 'host', 'application', 'item'];

View File

@@ -2,9 +2,11 @@ import _ from 'lodash';
import $ from 'jquery';
import angular from 'angular';
class ZabbixAlertingService {
export class ZabbixAlertingService {
/** @ngInject */
/**
* @ngInject
*/
constructor(dashboardSrv) {
this.dashboardSrv = dashboardSrv;
}

View File

@@ -18,6 +18,14 @@ jest.mock('angular', () => {
};
}, {virtual: true});
jest.mock('grafana/app/features/templating/template_srv', () => {
return {};
}, {virtual: true});
jest.mock('grafana/app/features/dashboard/dashboard_srv', () => {
return {};
}, {virtual: true});
jest.mock('grafana/app/core/core_module', () => {
return {
directive: function() {},
@@ -28,7 +36,6 @@ let mockPanelCtrl = PanelCtrl;
jest.mock('grafana/app/plugins/sdk', () => {
return {
QueryCtrl: null,
loadPluginCss: () => {},
PanelCtrl: mockPanelCtrl
};
}, {virtual: true});
@@ -74,9 +81,9 @@ jest.mock('grafana/app/core/config', () => {
jest.mock('jquery', () => 'module not found', {virtual: true});
jest.mock('@grafana/ui', () => {
return {};
}, {virtual: true});
jest.mock('@grafana/ui');
jest.mock('@grafana/runtime');
// Required for loading angularjs
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');