mysql-connector: add config options for selecting data source
and load SQL data source during plugin init
This commit is contained in:
50
dist/test/datasource-zabbix/config.controller.js
vendored
Normal file
50
dist/test/datasource-zabbix/config.controller.js
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.ZabbixDSConfigController = undefined;
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _lodash = require('lodash');
|
||||
|
||||
var _lodash2 = _interopRequireDefault(_lodash);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var SUPPORTED_SQL_DS = ['mysql'];
|
||||
|
||||
var defaultConfig = {
|
||||
dbConnection: {
|
||||
enable: false
|
||||
}
|
||||
};
|
||||
|
||||
var ZabbixDSConfigController = exports.ZabbixDSConfigController = function () {
|
||||
/** @ngInject */
|
||||
function ZabbixDSConfigController($scope, $injector, datasourceSrv) {
|
||||
_classCallCheck(this, ZabbixDSConfigController);
|
||||
|
||||
this.datasourceSrv = datasourceSrv;
|
||||
|
||||
_lodash2.default.defaults(this.current.jsonData, defaultConfig);
|
||||
this.sqlDataSources = this.getSupportedSQLDataSources();
|
||||
}
|
||||
|
||||
_createClass(ZabbixDSConfigController, [{
|
||||
key: 'getSupportedSQLDataSources',
|
||||
value: function getSupportedSQLDataSources() {
|
||||
var datasources = this.datasourceSrv.getAll();
|
||||
return _lodash2.default.filter(datasources, function (ds) {
|
||||
return _lodash2.default.includes(SUPPORTED_SQL_DS, ds.type);
|
||||
});
|
||||
}
|
||||
}]);
|
||||
|
||||
return ZabbixDSConfigController;
|
||||
}();
|
||||
|
||||
ZabbixDSConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
||||
117
dist/test/datasource-zabbix/datasource.js
vendored
117
dist/test/datasource-zabbix/datasource.js
vendored
@@ -56,12 +56,15 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
||||
var ZabbixAPIDatasource = function () {
|
||||
|
||||
/** @ngInject */
|
||||
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, zabbixAlertingSrv, Zabbix) {
|
||||
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, datasourceSrv, zabbixAlertingSrv, Zabbix) {
|
||||
var _this = this;
|
||||
|
||||
_classCallCheck(this, ZabbixAPIDatasource);
|
||||
|
||||
this.templateSrv = templateSrv;
|
||||
this.alertSrv = alertSrv;
|
||||
this.dashboardSrv = dashboardSrv;
|
||||
this.datasourceSrv = datasourceSrv;
|
||||
this.zabbixAlertingSrv = zabbixAlertingSrv;
|
||||
|
||||
// General data source settings
|
||||
@@ -90,35 +93,59 @@ var ZabbixAPIDatasource = function () {
|
||||
|
||||
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
|
||||
|
||||
// Try to use direct DB connection
|
||||
this.enableDirectDBConnection = instanceSettings.jsonData.dbConnection.enable;
|
||||
if (this.enableDirectDBConnection) {
|
||||
this.loadSQLDataSource(instanceSettings.jsonData.dbConnection.datasourceId).then(function () {}).catch(function (error) {
|
||||
_this.enableDirectDBConnection = false;
|
||||
_this.alertSrv.set("SQL Data Source Error", error, 'error');
|
||||
});
|
||||
}
|
||||
|
||||
// Use custom format for template variables
|
||||
this.replaceTemplateVars = _lodash2.default.partial(replaceTemplateVars, this.templateSrv);
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
// Datasource methods //
|
||||
////////////////////////
|
||||
|
||||
/**
|
||||
* Query panel data. Calls for each panel in dashboard.
|
||||
* @param {Object} options Contains time range, targets and other info.
|
||||
* @return {Object} Grafana metrics object with timeseries data for each target.
|
||||
*/
|
||||
|
||||
|
||||
_createClass(ZabbixAPIDatasource, [{
|
||||
key: 'loadSQLDataSource',
|
||||
value: function loadSQLDataSource(datasourceId) {
|
||||
var _this2 = this;
|
||||
|
||||
var ds = _lodash2.default.find(this.datasourceSrv.getAll(), { 'id': datasourceId });
|
||||
if (ds) {
|
||||
return this.datasourceSrv.loadDatasource(ds.name).then(function (ds) {
|
||||
console.log('Data source loaded', ds);
|
||||
_this2.sqlDataSource = ds;
|
||||
});
|
||||
} else {
|
||||
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found');
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
// Datasource methods //
|
||||
////////////////////////
|
||||
|
||||
/**
|
||||
* Query panel data. Calls for each panel in dashboard.
|
||||
* @param {Object} options Contains time range, targets and other info.
|
||||
* @return {Object} Grafana metrics object with timeseries data for each target.
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: 'query',
|
||||
value: function query(options) {
|
||||
var _this = this;
|
||||
var _this3 = this;
|
||||
|
||||
// Get alerts for current panel
|
||||
if (this.alertingEnabled) {
|
||||
this.alertQuery(options).then(function (alert) {
|
||||
_this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
|
||||
_this3.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
|
||||
|
||||
_this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
|
||||
if (_this.addThresholds) {
|
||||
_this3.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
|
||||
if (_this3.addThresholds) {
|
||||
_lodash2.default.forEach(alert.thresholds, function (threshold) {
|
||||
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
|
||||
_this3.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -131,7 +158,7 @@ var ZabbixAPIDatasource = function () {
|
||||
|
||||
// Prevent changes of original object
|
||||
var target = _lodash2.default.cloneDeep(t);
|
||||
_this.replaceTargetVariables(target, options);
|
||||
_this3.replaceTargetVariables(target, options);
|
||||
|
||||
// Apply Time-related functions (timeShift(), etc)
|
||||
var timeFunctions = bindFunctionDefs(target.functions, 'Time');
|
||||
@@ -146,7 +173,7 @@ var ZabbixAPIDatasource = function () {
|
||||
}
|
||||
var timeRange = [timeFrom, timeTo];
|
||||
|
||||
var useTrends = _this.isUseTrends(timeRange);
|
||||
var useTrends = _this3.isUseTrends(timeRange);
|
||||
|
||||
// Metrics or Text query mode
|
||||
if (target.mode !== c.MODE_ITSERVICE) {
|
||||
@@ -159,9 +186,9 @@ var ZabbixAPIDatasource = function () {
|
||||
}
|
||||
|
||||
if (!target.mode || target.mode === c.MODE_METRICS) {
|
||||
return _this.queryNumericData(target, timeRange, useTrends, options);
|
||||
return _this3.queryNumericData(target, timeRange, useTrends, options);
|
||||
} else if (target.mode === c.MODE_TEXT) {
|
||||
return _this.queryTextData(target, timeRange);
|
||||
return _this3.queryTextData(target, timeRange);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +199,7 @@ var ZabbixAPIDatasource = function () {
|
||||
return [];
|
||||
}
|
||||
|
||||
return _this.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
|
||||
return _this3.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
|
||||
return _responseHandler2.default.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
||||
});
|
||||
}
|
||||
@@ -186,7 +213,7 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'queryNumericData',
|
||||
value: function queryNumericData(target, timeRange, useTrends, options) {
|
||||
var _this2 = this;
|
||||
var _this4 = this;
|
||||
|
||||
var _timeRange = _slicedToArray(timeRange, 2),
|
||||
timeFrom = _timeRange[0],
|
||||
@@ -199,8 +226,8 @@ var ZabbixAPIDatasource = function () {
|
||||
var getHistoryPromise = void 0;
|
||||
|
||||
if (useTrends) {
|
||||
var valueType = _this2.getTrendValueType(target);
|
||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
||||
var valueType = _this4.getTrendValueType(target);
|
||||
getHistoryPromise = _this4.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
||||
return _responseHandler2.default.handleTrends(history, items, valueType);
|
||||
}).then(function (timeseries) {
|
||||
// Sort trend data, issue #202
|
||||
@@ -214,14 +241,14 @@ var ZabbixAPIDatasource = function () {
|
||||
});
|
||||
} else {
|
||||
// Use history
|
||||
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
|
||||
getHistoryPromise = _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
|
||||
return _responseHandler2.default.handleHistory(history, items);
|
||||
});
|
||||
}
|
||||
|
||||
return getHistoryPromise;
|
||||
}).then(function (timeseries) {
|
||||
return _this2.applyDataProcessingFunctions(timeseries, target);
|
||||
return _this4.applyDataProcessingFunctions(timeseries, target);
|
||||
}).then(function (timeseries) {
|
||||
return downsampleSeries(timeseries, options);
|
||||
}).catch(function (error) {
|
||||
@@ -300,7 +327,7 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'queryTextData',
|
||||
value: function queryTextData(target, timeRange) {
|
||||
var _this3 = this;
|
||||
var _this5 = this;
|
||||
|
||||
var _timeRange2 = _slicedToArray(timeRange, 2),
|
||||
timeFrom = _timeRange2[0],
|
||||
@@ -311,7 +338,7 @@ var ZabbixAPIDatasource = function () {
|
||||
};
|
||||
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
|
||||
if (items.length) {
|
||||
return _this3.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
|
||||
return _this5.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
|
||||
return _responseHandler2.default.handleText(history, items, target);
|
||||
});
|
||||
} else {
|
||||
@@ -328,12 +355,12 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'testDatasource',
|
||||
value: function testDatasource() {
|
||||
var _this4 = this;
|
||||
var _this6 = this;
|
||||
|
||||
var zabbixVersion = void 0;
|
||||
return this.zabbix.getVersion().then(function (version) {
|
||||
zabbixVersion = version;
|
||||
return _this4.zabbix.login();
|
||||
return _this6.zabbix.login();
|
||||
}).then(function () {
|
||||
return {
|
||||
status: "success",
|
||||
@@ -372,14 +399,14 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'metricFindQuery',
|
||||
value: function metricFindQuery(query) {
|
||||
var _this5 = this;
|
||||
var _this7 = this;
|
||||
|
||||
var result = void 0;
|
||||
var parts = [];
|
||||
|
||||
// Split query. Query structure: group.host.app.item
|
||||
_lodash2.default.each(utils.splitTemplateQuery(query), function (part) {
|
||||
part = _this5.replaceTemplateVars(part, {});
|
||||
part = _this7.replaceTemplateVars(part, {});
|
||||
|
||||
// Replace wildcard to regex
|
||||
if (part === '*') {
|
||||
@@ -421,7 +448,7 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'annotationQuery',
|
||||
value: function annotationQuery(options) {
|
||||
var _this6 = this;
|
||||
var _this8 = this;
|
||||
|
||||
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
|
||||
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
|
||||
@@ -439,7 +466,7 @@ var ZabbixAPIDatasource = function () {
|
||||
return getTriggers.then(function (triggers) {
|
||||
|
||||
// Filter triggers by description
|
||||
var triggerName = _this6.replaceTemplateVars(annotation.trigger, {});
|
||||
var triggerName = _this8.replaceTemplateVars(annotation.trigger, {});
|
||||
if (utils.isRegex(triggerName)) {
|
||||
triggers = _lodash2.default.filter(triggers, function (trigger) {
|
||||
return utils.buildRegex(triggerName).test(trigger.description);
|
||||
@@ -456,7 +483,7 @@ var ZabbixAPIDatasource = function () {
|
||||
});
|
||||
|
||||
var objectids = _lodash2.default.map(triggers, 'triggerid');
|
||||
return _this6.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
|
||||
return _this8.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
|
||||
var indexedTriggers = _lodash2.default.keyBy(triggers, 'triggerid');
|
||||
|
||||
// Hide acknowledged events if option enabled
|
||||
@@ -497,23 +524,23 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'alertQuery',
|
||||
value: function alertQuery(options) {
|
||||
var _this7 = this;
|
||||
var _this9 = this;
|
||||
|
||||
var enabled_targets = filterEnabledTargets(options.targets);
|
||||
var getPanelItems = _lodash2.default.map(enabled_targets, function (t) {
|
||||
var target = _lodash2.default.cloneDeep(t);
|
||||
_this7.replaceTargetVariables(target, options);
|
||||
return _this7.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
|
||||
_this9.replaceTargetVariables(target, options);
|
||||
return _this9.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
|
||||
});
|
||||
|
||||
return Promise.all(getPanelItems).then(function (results) {
|
||||
var items = _lodash2.default.flatten(results);
|
||||
var itemids = _lodash2.default.map(items, 'itemid');
|
||||
|
||||
return _this7.zabbix.getAlerts(itemids);
|
||||
return _this9.zabbix.getAlerts(itemids);
|
||||
}).then(function (triggers) {
|
||||
triggers = _lodash2.default.filter(triggers, function (trigger) {
|
||||
return trigger.priority >= _this7.alertingMinSeverity;
|
||||
return trigger.priority >= _this9.alertingMinSeverity;
|
||||
});
|
||||
|
||||
if (!triggers || triggers.length === 0) {
|
||||
@@ -544,12 +571,12 @@ var ZabbixAPIDatasource = function () {
|
||||
}, {
|
||||
key: 'replaceTargetVariables',
|
||||
value: function replaceTargetVariables(target, options) {
|
||||
var _this8 = this;
|
||||
var _this10 = this;
|
||||
|
||||
var parts = ['group', 'host', 'application', 'item'];
|
||||
_lodash2.default.forEach(parts, function (p) {
|
||||
if (target[p] && target[p].filter) {
|
||||
target[p].filter = _this8.replaceTemplateVars(target[p].filter, options.scopedVars);
|
||||
target[p].filter = _this10.replaceTemplateVars(target[p].filter, options.scopedVars);
|
||||
}
|
||||
});
|
||||
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
|
||||
@@ -557,9 +584,9 @@ var ZabbixAPIDatasource = function () {
|
||||
_lodash2.default.forEach(target.functions, function (func) {
|
||||
func.params = _lodash2.default.map(func.params, function (param) {
|
||||
if (typeof param === 'number') {
|
||||
return +_this8.templateSrv.replace(param.toString(), options.scopedVars);
|
||||
return +_this10.templateSrv.replace(param.toString(), options.scopedVars);
|
||||
} else {
|
||||
return _this8.templateSrv.replace(param, options.scopedVars);
|
||||
return _this10.templateSrv.replace(param, options.scopedVars);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
10
dist/test/datasource-zabbix/module.js
vendored
10
dist/test/datasource-zabbix/module.js
vendored
@@ -9,14 +9,10 @@ var _datasource = require('./datasource');
|
||||
|
||||
var _query = require('./query.controller');
|
||||
|
||||
var _config = require('./config.controller');
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var ZabbixConfigController = function ZabbixConfigController() {
|
||||
_classCallCheck(this, ZabbixConfigController);
|
||||
};
|
||||
|
||||
ZabbixConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
||||
|
||||
var ZabbixQueryOptionsController = function ZabbixQueryOptionsController() {
|
||||
_classCallCheck(this, ZabbixQueryOptionsController);
|
||||
};
|
||||
@@ -30,7 +26,7 @@ var ZabbixAnnotationsQueryController = function ZabbixAnnotationsQueryController
|
||||
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
||||
|
||||
exports.Datasource = _datasource.ZabbixAPIDatasource;
|
||||
exports.ConfigCtrl = ZabbixConfigController;
|
||||
exports.ConfigCtrl = _config.ZabbixDSConfigController;
|
||||
exports.QueryCtrl = _query.ZabbixQueryController;
|
||||
exports.QueryOptionsCtrl = ZabbixQueryOptionsController;
|
||||
exports.AnnotationsQueryCtrl = ZabbixAnnotationsQueryController;
|
||||
|
||||
Reference in New Issue
Block a user