mysql-connector: add config options for selecting data source

and load SQL data source during plugin init
This commit is contained in:
Alexander Zobnin
2017-07-21 17:32:46 +03:00
parent 50d6694aaf
commit 6fa4baed0c
14 changed files with 359 additions and 113 deletions

View File

@@ -183,12 +183,15 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
_export('ZabbixAPIDatasource', 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
@@ -217,35 +220,48 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
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 = _.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 = _.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');
}
}
}, {
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) {
_.forEach(alert.thresholds, function (threshold) {
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
_this3.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
@@ -258,7 +274,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
// Prevent changes of original object
var target = _.cloneDeep(t);
_this.replaceTargetVariables(target, options);
_this3.replaceTargetVariables(target, options);
// Apply Time-related functions (timeShift(), etc)
var timeFunctions = bindFunctionDefs(target.functions, 'Time');
@@ -273,7 +289,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}
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) {
@@ -286,9 +302,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}
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);
}
}
@@ -299,7 +315,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return [];
}
return _this.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return _this3.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
@@ -313,7 +329,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeRange, useTrends, options) {
var _this2 = this;
var _this4 = this;
var _timeRange = _slicedToArray(timeRange, 2),
timeFrom = _timeRange[0],
@@ -326,8 +342,8 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
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 responseHandler.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
@@ -341,14 +357,14 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
});
} else {
// Use history
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
getHistoryPromise = _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return responseHandler.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) {
@@ -427,7 +443,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'queryTextData',
value: function queryTextData(target, timeRange) {
var _this3 = this;
var _this5 = this;
var _timeRange2 = _slicedToArray(timeRange, 2),
timeFrom = _timeRange2[0],
@@ -438,7 +454,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
};
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 responseHandler.handleText(history, items, target);
});
} else {
@@ -449,12 +465,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
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",
@@ -480,14 +496,14 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
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
_.each(utils.splitTemplateQuery(query), function (part) {
part = _this5.replaceTemplateVars(part, {});
part = _this7.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -524,7 +540,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
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);
@@ -542,7 +558,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
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 = _.filter(triggers, function (trigger) {
return utils.buildRegex(triggerName).test(trigger.description);
@@ -559,7 +575,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
});
var objectids = _.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 = _.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
@@ -593,23 +609,23 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'alertQuery',
value: function alertQuery(options) {
var _this7 = this;
var _this9 = this;
var enabled_targets = filterEnabledTargets(options.targets);
var getPanelItems = _.map(enabled_targets, function (t) {
var target = _.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 = _.flatten(results);
var itemids = _.map(items, 'itemid');
return _this7.zabbix.getAlerts(itemids);
return _this9.zabbix.getAlerts(itemids);
}).then(function (triggers) {
triggers = _.filter(triggers, function (trigger) {
return trigger.priority >= _this7.alertingMinSeverity;
return trigger.priority >= _this9.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
@@ -637,12 +653,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this8 = this;
var _this10 = this;
var parts = ['group', 'host', 'application', 'item'];
_.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);
@@ -650,9 +666,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
_.forEach(target.functions, function (func) {
func.params = _.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);
}
});
});