mysql-connector: initial ZabbixDBConnector class

This commit is contained in:
Alexander Zobnin
2017-07-21 18:45:16 +03:00
parent 6fa4baed0c
commit 6d89f36bb2
12 changed files with 416 additions and 167 deletions

View File

@@ -184,8 +184,6 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, datasourceSrv, zabbixAlertingSrv, Zabbix) {
var _this = this;
_classCallCheck(this, ZabbixAPIDatasource);
this.templateSrv = templateSrv;
@@ -194,6 +192,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
this.datasourceSrv = datasourceSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// Use custom format for template variables
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
// General data source settings
this.name = instanceSettings.name;
this.url = instanceSettings.url;
@@ -218,50 +219,48 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || c.SEV_WARNING;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Try to use direct DB connection
// Direct DB Connection options
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');
});
this.sqlDatasourceId = instanceSettings.jsonData.dbConnection.datasourceId;
var zabbixOptions = {
username: this.username,
password: this.password,
basicAuth: this.basicAuth,
withCredentials: this.withCredentials,
cacheTTL: this.cacheTTL,
enableDirectDBConnection: this.enableDirectDBConnection,
sqlDatasourceId: this.sqlDatasourceId
};
this.zabbix = new Zabbix(this.url, zabbixOptions);
}
// 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 _this3 = this;
var _this = this;
// Get alerts for current panel
if (this.alertingEnabled) {
this.alertQuery(options).then(function (alert) {
_this3.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this3.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this3.addThresholds) {
_this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this.addThresholds) {
_.forEach(alert.thresholds, function (threshold) {
_this3.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
@@ -274,7 +273,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
// Prevent changes of original object
var target = _.cloneDeep(t);
_this3.replaceTargetVariables(target, options);
_this.replaceTargetVariables(target, options);
// Apply Time-related functions (timeShift(), etc)
var timeFunctions = bindFunctionDefs(target.functions, 'Time');
@@ -289,7 +288,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}
var timeRange = [timeFrom, timeTo];
var useTrends = _this3.isUseTrends(timeRange);
var useTrends = _this.isUseTrends(timeRange);
// Metrics or Text query mode
if (target.mode !== c.MODE_ITSERVICE) {
@@ -302,9 +301,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}
if (!target.mode || target.mode === c.MODE_METRICS) {
return _this3.queryNumericData(target, timeRange, useTrends, options);
return _this.queryNumericData(target, timeRange, useTrends, options);
} else if (target.mode === c.MODE_TEXT) {
return _this3.queryTextData(target, timeRange);
return _this.queryTextData(target, timeRange);
}
}
@@ -315,7 +314,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return [];
}
return _this3.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return _this.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
@@ -329,7 +328,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeRange, useTrends, options) {
var _this4 = this;
var _this2 = this;
var _timeRange = _slicedToArray(timeRange, 2),
timeFrom = _timeRange[0],
@@ -342,8 +341,8 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
var getHistoryPromise = void 0;
if (useTrends) {
var valueType = _this4.getTrendValueType(target);
getHistoryPromise = _this4.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return responseHandler.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
@@ -357,14 +356,14 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
});
} else {
// Use history
getHistoryPromise = _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return responseHandler.handleHistory(history, items);
});
}
return getHistoryPromise;
}).then(function (timeseries) {
return _this4.applyDataProcessingFunctions(timeseries, target);
return _this2.applyDataProcessingFunctions(timeseries, target);
}).then(function (timeseries) {
return downsampleSeries(timeseries, options);
}).catch(function (error) {
@@ -443,7 +442,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'queryTextData',
value: function queryTextData(target, timeRange) {
var _this5 = this;
var _this3 = this;
var _timeRange2 = _slicedToArray(timeRange, 2),
timeFrom = _timeRange2[0],
@@ -454,7 +453,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
};
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
if (items.length) {
return _this5.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _this3.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return responseHandler.handleText(history, items, target);
});
} else {
@@ -465,12 +464,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'testDatasource',
value: function testDatasource() {
var _this6 = this;
var _this4 = this;
var zabbixVersion = void 0;
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this6.zabbix.login();
return _this4.zabbix.login();
}).then(function () {
return {
status: "success",
@@ -496,14 +495,14 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this7 = this;
var _this5 = this;
var result = void 0;
var parts = [];
// Split query. Query structure: group.host.app.item
_.each(utils.splitTemplateQuery(query), function (part) {
part = _this7.replaceTemplateVars(part, {});
part = _this5.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -540,7 +539,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var _this8 = this;
var _this6 = this;
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
@@ -558,7 +557,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return getTriggers.then(function (triggers) {
// Filter triggers by description
var triggerName = _this8.replaceTemplateVars(annotation.trigger, {});
var triggerName = _this6.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(triggerName)) {
triggers = _.filter(triggers, function (trigger) {
return utils.buildRegex(triggerName).test(trigger.description);
@@ -575,7 +574,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
});
var objectids = _.map(triggers, 'triggerid');
return _this8.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
return _this6.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
var indexedTriggers = _.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
@@ -609,23 +608,23 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'alertQuery',
value: function alertQuery(options) {
var _this9 = this;
var _this7 = this;
var enabled_targets = filterEnabledTargets(options.targets);
var getPanelItems = _.map(enabled_targets, function (t) {
var target = _.cloneDeep(t);
_this9.replaceTargetVariables(target, options);
return _this9.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
_this7.replaceTargetVariables(target, options);
return _this7.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
});
return Promise.all(getPanelItems).then(function (results) {
var items = _.flatten(results);
var itemids = _.map(items, 'itemid');
return _this9.zabbix.getAlerts(itemids);
return _this7.zabbix.getAlerts(itemids);
}).then(function (triggers) {
triggers = _.filter(triggers, function (trigger) {
return trigger.priority >= _this9.alertingMinSeverity;
return trigger.priority >= _this7.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
@@ -653,12 +652,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this10 = this;
var _this8 = this;
var parts = ['group', 'host', 'application', 'item'];
_.forEach(parts, function (p) {
if (target[p] && target[p].filter) {
target[p].filter = _this10.replaceTemplateVars(target[p].filter, options.scopedVars);
target[p].filter = _this8.replaceTemplateVars(target[p].filter, options.scopedVars);
}
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
@@ -666,9 +665,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 +_this10.templateSrv.replace(param.toString(), options.scopedVars);
return +_this8.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return _this10.templateSrv.replace(param, options.scopedVars);
return _this8.templateSrv.replace(param, options.scopedVars);
}
});
});

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
'use strict';
System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './zabbixCachingProxy.service.js'], function (_export, _context) {
System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './zabbixCachingProxy.service.js', './zabbixDBConnector'], function (_export, _context) {
"use strict";
var angular, _, utils, _createClass;
@@ -27,11 +27,20 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
// Each Zabbix data source instance should initialize its own API instance.
/** @ngInject */
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector) {
var Zabbix = function () {
function Zabbix(url, username, password, basicAuth, withCredentials, cacheTTL) {
function Zabbix(url, options) {
_classCallCheck(this, Zabbix);
var username = options.username,
password = options.password,
basicAuth = options.basicAuth,
withCredentials = options.withCredentials,
cacheTTL = options.cacheTTL,
enableDirectDBConnection = options.enableDirectDBConnection,
sqlDatasourceId = options.sqlDatasourceId;
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
@@ -55,6 +64,10 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
}
_createClass(Zabbix, [{
@@ -274,7 +287,7 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
_ = _lodash.default;
}, function (_utils) {
utils = _utils;
}, function (_zabbixAPIServiceJs) {}, function (_zabbixCachingProxyServiceJs) {}],
}, function (_zabbixAPIServiceJs) {}, function (_zabbixCachingProxyServiceJs) {}, function (_zabbixDBConnector) {}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,100 @@
'use strict';
System.register(['angular', 'lodash'], function (_export, _context) {
"use strict";
var angular, _, _createClass;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
/** @ngInject */
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var ZabbixDBConnector = function () {
function ZabbixDBConnector(sqlDataSourceId) {
_classCallCheck(this, ZabbixDBConnector);
this.sqlDataSourceId = sqlDataSourceId;
// Try to load DS with given id to check it's exist
this.loadSQLDataSource(sqlDataSourceId);
}
_createClass(ZabbixDBConnector, [{
key: 'loadSQLDataSource',
value: function loadSQLDataSource(datasourceId) {
var ds = _.find(datasourceSrv.getAll(), { 'id': datasourceId });
if (ds) {
return datasourceSrv.loadDatasource(ds.name).then(function (ds) {
console.log('SQL data source loaded', ds);
});
} else {
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found');
}
}
}, {
key: 'invokeSQLQuery',
value: function invokeSQLQuery(query) {
var queryDef = {
refId: 'A',
format: 'table',
datasourceId: this.sqlDataSourceId,
rawSql: query
};
return backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [queryDef]
}
}).then(function (response) {
var results = response.data.results;
if (results['A']) {
return _.head(results['A'].tables);
} else {
return null;
}
});
}
}]);
return ZabbixDBConnector;
}();
return ZabbixDBConnector;
}
return {
setters: [function (_angular) {
angular = _angular.default;
}, function (_lodash) {
_ = _lodash.default;
}],
execute: function () {
_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;
};
}();
angular.module('grafana.services').factory('ZabbixDBConnector', ZabbixDBConnectorFactory);
}
};
});
//# sourceMappingURL=zabbixDBConnector.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../src/datasource-zabbix/zabbixDBConnector.js"],"names":["ZabbixDBConnectorFactory","datasourceSrv","backendSrv","ZabbixDBConnector","sqlDataSourceId","loadSQLDataSource","datasourceId","ds","_","find","getAll","loadDatasource","name","then","console","log","Promise","reject","query","queryDef","refId","format","rawSql","datasourceRequest","url","method","data","queries","results","response","head","tables","angular","module","factory"],"mappings":";;;;;;;;;;;;;AAGA;AACA,WAASA,wBAAT,CAAkCC,aAAlC,EAAiDC,UAAjD,EAA6D;AAAA,QAErDC,iBAFqD;AAIzD,iCAAYC,eAAZ,EAA6B;AAAA;;AAC3B,aAAKA,eAAL,GAAuBA,eAAvB;;AAEA;AACA,aAAKC,iBAAL,CAAuBD,eAAvB;AACD;;AATwD;AAAA;AAAA,0CAWvCE,YAXuC,EAWzB;AAC9B,cAAIC,KAAKC,EAAEC,IAAF,CAAOR,cAAcS,MAAd,EAAP,EAA+B,EAAC,MAAMJ,YAAP,EAA/B,CAAT;AACA,cAAIC,EAAJ,EAAQ;AACN,mBAAON,cAAcU,cAAd,CAA6BJ,GAAGK,IAAhC,EACNC,IADM,CACD,cAAM;AACVC,sBAAQC,GAAR,CAAY,wBAAZ,EAAsCR,EAAtC;AACD,aAHM,CAAP;AAID,WALD,MAKO;AACL,mBAAOS,QAAQC,MAAR,8BAA0CX,YAA1C,gBAAP;AACD;AACF;AArBwD;AAAA;AAAA,uCAuB1CY,KAvB0C,EAuBnC;AACpB,cAAIC,WAAW;AACbC,mBAAO,GADM;AAEbC,oBAAQ,OAFK;AAGbf,0BAAc,KAAKF,eAHN;AAIbkB,oBAAQJ;AAJK,WAAf;;AAOA,iBAAOhB,WAAWqB,iBAAX,CAA6B;AAClCC,iBAAK,iBAD6B;AAElCC,oBAAQ,MAF0B;AAGlCC,kBAAM;AACJC,uBAAS,CAACR,QAAD;AADL;AAH4B,WAA7B,EAONN,IAPM,CAOD,oBAAY;AAChB,gBAAIe,UAAUC,SAASH,IAAT,CAAcE,OAA5B;AACA,gBAAIA,QAAQ,GAAR,CAAJ,EAAkB;AAChB,qBAAOpB,EAAEsB,IAAF,CAAOF,QAAQ,GAAR,EAAaG,MAApB,CAAP;AACD,aAFD,MAEO;AACL,qBAAO,IAAP;AACD;AACF,WAdM,CAAP;AAeD;AA9CwD;;AAAA;AAAA;;AAiD3D,WAAO5B,iBAAP;AACD;;;;AAtDM6B,a;;AACAxB,O;;;;;;;;;;;;;;;;;;;;;AAuDPwB,cACGC,MADH,CACU,kBADV,EAEGC,OAFH,CAEW,mBAFX,EAEgClC,wBAFhC","file":"zabbixDBConnector.js","sourcesContent":["import angular from 'angular';\nimport _ from 'lodash';\n\n/** @ngInject */\nfunction ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {\n\n class ZabbixDBConnector {\n\n constructor(sqlDataSourceId) {\n this.sqlDataSourceId = sqlDataSourceId;\n\n // Try to load DS with given id to check it's exist\n this.loadSQLDataSource(sqlDataSourceId);\n }\n\n loadSQLDataSource(datasourceId) {\n let ds = _.find(datasourceSrv.getAll(), {'id': datasourceId});\n if (ds) {\n return datasourceSrv.loadDatasource(ds.name)\n .then(ds => {\n console.log('SQL data source loaded', ds);\n });\n } else {\n return Promise.reject(`SQL Data Source with ID ${datasourceId} not found`);\n }\n }\n\n invokeSQLQuery(query) {\n let queryDef = {\n refId: 'A',\n format: 'table',\n datasourceId: this.sqlDataSourceId,\n rawSql: query\n };\n\n return backendSrv.datasourceRequest({\n url: '/api/tsdb/query',\n method: 'POST',\n data: {\n queries: [queryDef],\n }\n })\n .then(response => {\n let results = response.data.results;\n if (results['A']) {\n return _.head(results['A'].tables);\n } else {\n return null;\n }\n });\n }\n }\n\n return ZabbixDBConnector;\n}\n\nangular\n .module('grafana.services')\n .factory('ZabbixDBConnector', ZabbixDBConnectorFactory);\n"]}

View File

@@ -57,8 +57,6 @@ var ZabbixAPIDatasource = function () {
/** @ngInject */
function ZabbixAPIDatasource(instanceSettings, templateSrv, alertSrv, dashboardSrv, datasourceSrv, zabbixAlertingSrv, Zabbix) {
var _this = this;
_classCallCheck(this, ZabbixAPIDatasource);
this.templateSrv = templateSrv;
@@ -67,6 +65,9 @@ var ZabbixAPIDatasource = function () {
this.datasourceSrv = datasourceSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// Use custom format for template variables
this.replaceTemplateVars = _lodash2.default.partial(replaceTemplateVars, this.templateSrv);
// General data source settings
this.name = instanceSettings.name;
this.url = instanceSettings.url;
@@ -91,35 +92,21 @@ var ZabbixAPIDatasource = function () {
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || c.SEV_WARNING;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Try to use direct DB connection
// Direct DB Connection options
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');
});
}
this.sqlDatasourceId = instanceSettings.jsonData.dbConnection.datasourceId;
// Use custom format for template variables
this.replaceTemplateVars = _lodash2.default.partial(replaceTemplateVars, this.templateSrv);
}
var zabbixOptions = {
username: this.username,
password: this.password,
basicAuth: this.basicAuth,
withCredentials: this.withCredentials,
cacheTTL: this.cacheTTL,
enableDirectDBConnection: this.enableDirectDBConnection,
sqlDatasourceId: this.sqlDatasourceId
};
_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');
}
this.zabbix = new Zabbix(this.url, zabbixOptions);
}
////////////////////////
@@ -132,20 +119,21 @@ var ZabbixAPIDatasource = function () {
* @return {Object} Grafana metrics object with timeseries data for each target.
*/
}, {
_createClass(ZabbixAPIDatasource, [{
key: 'query',
value: function query(options) {
var _this3 = this;
var _this = this;
// Get alerts for current panel
if (this.alertingEnabled) {
this.alertQuery(options).then(function (alert) {
_this3.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this.zabbixAlertingSrv.setPanelAlertState(options.panelId, alert.state);
_this3.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this3.addThresholds) {
_this.zabbixAlertingSrv.removeZabbixThreshold(options.panelId);
if (_this.addThresholds) {
_lodash2.default.forEach(alert.thresholds, function (threshold) {
_this3.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
_this.zabbixAlertingSrv.setPanelThreshold(options.panelId, threshold);
});
}
});
@@ -158,7 +146,7 @@ var ZabbixAPIDatasource = function () {
// Prevent changes of original object
var target = _lodash2.default.cloneDeep(t);
_this3.replaceTargetVariables(target, options);
_this.replaceTargetVariables(target, options);
// Apply Time-related functions (timeShift(), etc)
var timeFunctions = bindFunctionDefs(target.functions, 'Time');
@@ -173,7 +161,7 @@ var ZabbixAPIDatasource = function () {
}
var timeRange = [timeFrom, timeTo];
var useTrends = _this3.isUseTrends(timeRange);
var useTrends = _this.isUseTrends(timeRange);
// Metrics or Text query mode
if (target.mode !== c.MODE_ITSERVICE) {
@@ -186,9 +174,9 @@ var ZabbixAPIDatasource = function () {
}
if (!target.mode || target.mode === c.MODE_METRICS) {
return _this3.queryNumericData(target, timeRange, useTrends, options);
return _this.queryNumericData(target, timeRange, useTrends, options);
} else if (target.mode === c.MODE_TEXT) {
return _this3.queryTextData(target, timeRange);
return _this.queryTextData(target, timeRange);
}
}
@@ -199,7 +187,7 @@ var ZabbixAPIDatasource = function () {
return [];
}
return _this3.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return _this.zabbix.getSLA(target.itservice.serviceid, timeRange).then(function (slaObject) {
return _responseHandler2.default.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
@@ -213,7 +201,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'queryNumericData',
value: function queryNumericData(target, timeRange, useTrends, options) {
var _this4 = this;
var _this2 = this;
var _timeRange = _slicedToArray(timeRange, 2),
timeFrom = _timeRange[0],
@@ -226,8 +214,8 @@ var ZabbixAPIDatasource = function () {
var getHistoryPromise = void 0;
if (useTrends) {
var valueType = _this4.getTrendValueType(target);
getHistoryPromise = _this4.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
@@ -241,14 +229,14 @@ var ZabbixAPIDatasource = function () {
});
} else {
// Use history
getHistoryPromise = _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleHistory(history, items);
});
}
return getHistoryPromise;
}).then(function (timeseries) {
return _this4.applyDataProcessingFunctions(timeseries, target);
return _this2.applyDataProcessingFunctions(timeseries, target);
}).then(function (timeseries) {
return downsampleSeries(timeseries, options);
}).catch(function (error) {
@@ -327,7 +315,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'queryTextData',
value: function queryTextData(target, timeRange) {
var _this5 = this;
var _this3 = this;
var _timeRange2 = _slicedToArray(timeRange, 2),
timeFrom = _timeRange2[0],
@@ -338,7 +326,7 @@ var ZabbixAPIDatasource = function () {
};
return this.zabbix.getItemsFromTarget(target, options).then(function (items) {
if (items.length) {
return _this5.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _this3.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleText(history, items, target);
});
} else {
@@ -355,12 +343,12 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'testDatasource',
value: function testDatasource() {
var _this6 = this;
var _this4 = this;
var zabbixVersion = void 0;
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this6.zabbix.login();
return _this4.zabbix.login();
}).then(function () {
return {
status: "success",
@@ -399,14 +387,14 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this7 = this;
var _this5 = this;
var result = void 0;
var parts = [];
// Split query. Query structure: group.host.app.item
_lodash2.default.each(utils.splitTemplateQuery(query), function (part) {
part = _this7.replaceTemplateVars(part, {});
part = _this5.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -448,7 +436,7 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var _this8 = this;
var _this6 = this;
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
@@ -466,7 +454,7 @@ var ZabbixAPIDatasource = function () {
return getTriggers.then(function (triggers) {
// Filter triggers by description
var triggerName = _this8.replaceTemplateVars(annotation.trigger, {});
var triggerName = _this6.replaceTemplateVars(annotation.trigger, {});
if (utils.isRegex(triggerName)) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return utils.buildRegex(triggerName).test(trigger.description);
@@ -483,7 +471,7 @@ var ZabbixAPIDatasource = function () {
});
var objectids = _lodash2.default.map(triggers, 'triggerid');
return _this8.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
return _this6.zabbix.getEvents(objectids, timeFrom, timeTo, showOkEvents).then(function (events) {
var indexedTriggers = _lodash2.default.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
@@ -524,23 +512,23 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'alertQuery',
value: function alertQuery(options) {
var _this9 = this;
var _this7 = this;
var enabled_targets = filterEnabledTargets(options.targets);
var getPanelItems = _lodash2.default.map(enabled_targets, function (t) {
var target = _lodash2.default.cloneDeep(t);
_this9.replaceTargetVariables(target, options);
return _this9.zabbix.getItemsFromTarget(target, { itemtype: 'num' });
_this7.replaceTargetVariables(target, options);
return _this7.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 _this9.zabbix.getAlerts(itemids);
return _this7.zabbix.getAlerts(itemids);
}).then(function (triggers) {
triggers = _lodash2.default.filter(triggers, function (trigger) {
return trigger.priority >= _this9.alertingMinSeverity;
return trigger.priority >= _this7.alertingMinSeverity;
});
if (!triggers || triggers.length === 0) {
@@ -571,12 +559,12 @@ var ZabbixAPIDatasource = function () {
}, {
key: 'replaceTargetVariables',
value: function replaceTargetVariables(target, options) {
var _this10 = this;
var _this8 = this;
var parts = ['group', 'host', 'application', 'item'];
_lodash2.default.forEach(parts, function (p) {
if (target[p] && target[p].filter) {
target[p].filter = _this10.replaceTemplateVars(target[p].filter, options.scopedVars);
target[p].filter = _this8.replaceTemplateVars(target[p].filter, options.scopedVars);
}
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
@@ -584,9 +572,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 +_this10.templateSrv.replace(param.toString(), options.scopedVars);
return +_this8.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return _this10.templateSrv.replace(param, options.scopedVars);
return _this8.templateSrv.replace(param, options.scopedVars);
}
});
});

View File

@@ -18,6 +18,8 @@ require('./zabbixAPI.service.js');
require('./zabbixCachingProxy.service.js');
require('./zabbixDBConnector');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -30,12 +32,21 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
// Each Zabbix data source instance should initialize its own API instance.
/** @ngInject */
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector) {
var Zabbix = function () {
function Zabbix(url, username, password, basicAuth, withCredentials, cacheTTL) {
function Zabbix(url, options) {
_classCallCheck(this, Zabbix);
var username = options.username,
password = options.password,
basicAuth = options.basicAuth,
withCredentials = options.withCredentials,
cacheTTL = options.cacheTTL,
enableDirectDBConnection = options.enableDirectDBConnection,
sqlDatasourceId = options.sqlDatasourceId;
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
@@ -58,6 +69,10 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
}
_createClass(Zabbix, [{

View File

@@ -0,0 +1,74 @@
'use strict';
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 _angular = require('angular');
var _angular2 = _interopRequireDefault(_angular);
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"); } }
/** @ngInject */
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var ZabbixDBConnector = function () {
function ZabbixDBConnector(sqlDataSourceId) {
_classCallCheck(this, ZabbixDBConnector);
this.sqlDataSourceId = sqlDataSourceId;
// Try to load DS with given id to check it's exist
this.loadSQLDataSource(sqlDataSourceId);
}
_createClass(ZabbixDBConnector, [{
key: 'loadSQLDataSource',
value: function loadSQLDataSource(datasourceId) {
var ds = _lodash2.default.find(datasourceSrv.getAll(), { 'id': datasourceId });
if (ds) {
return datasourceSrv.loadDatasource(ds.name).then(function (ds) {
console.log('SQL data source loaded', ds);
});
} else {
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found');
}
}
}, {
key: 'invokeSQLQuery',
value: function invokeSQLQuery(query) {
var queryDef = {
refId: 'A',
format: 'table',
datasourceId: this.sqlDataSourceId,
rawSql: query
};
return backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [queryDef]
}
}).then(function (response) {
var results = response.data.results;
if (results['A']) {
return _lodash2.default.head(results['A'].tables);
} else {
return null;
}
});
}
}]);
return ZabbixDBConnector;
}();
return ZabbixDBConnector;
}
_angular2.default.module('grafana.services').factory('ZabbixDBConnector', ZabbixDBConnectorFactory);

View File

@@ -20,6 +20,9 @@ class ZabbixAPIDatasource {
this.datasourceSrv = datasourceSrv;
this.zabbixAlertingSrv = zabbixAlertingSrv;
// Use custom format for template variables
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
// General data source settings
this.name = instanceSettings.name;
this.url = instanceSettings.url;
@@ -44,33 +47,21 @@ class ZabbixAPIDatasource {
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || c.SEV_WARNING;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Try to use direct DB connection
// Direct DB Connection options
this.enableDirectDBConnection = instanceSettings.jsonData.dbConnection.enable;
if (this.enableDirectDBConnection) {
this.loadSQLDataSource(instanceSettings.jsonData.dbConnection.datasourceId)
.then(() => {})
.catch(error => {
this.enableDirectDBConnection = false;
this.alertSrv.set("SQL Data Source Error", error, 'error');
});
}
this.sqlDatasourceId = instanceSettings.jsonData.dbConnection.datasourceId;
// Use custom format for template variables
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
}
let zabbixOptions = {
username: this.username,
password: this.password,
basicAuth: this.basicAuth,
withCredentials: this.withCredentials,
cacheTTL: this.cacheTTL,
enableDirectDBConnection: this.enableDirectDBConnection,
sqlDatasourceId: this.sqlDatasourceId
};
loadSQLDataSource(datasourceId) {
let ds = _.find(this.datasourceSrv.getAll(), {'id': datasourceId});
if (ds) {
return this.datasourceSrv.loadDatasource(ds.name).then(ds => {
console.log('Data source loaded', ds);
this.sqlDataSource = ds;
});
} else {
return Promise.reject(`SQL Data Source with ID ${datasourceId} not found`);
}
this.zabbix = new Zabbix(this.url, zabbixOptions);
}
////////////////////////

View File

@@ -3,15 +3,20 @@ import _ from 'lodash';
import * as utils from './utils';
import './zabbixAPI.service.js';
import './zabbixCachingProxy.service.js';
import './zabbixDBConnector';
// Use factory() instead service() for multiple data sources support.
// Each Zabbix data source instance should initialize its own API instance.
/** @ngInject */
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector) {
class Zabbix {
constructor(url, username, password, basicAuth, withCredentials, cacheTTL) {
constructor(url, options) {
let {
username, password, basicAuth, withCredentials, cacheTTL,
enableDirectDBConnection, sqlDatasourceId
} = options;
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
@@ -36,6 +41,10 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
}
getItemsFromTarget(target, options) {

View File

@@ -0,0 +1,59 @@
import angular from 'angular';
import _ from 'lodash';
/** @ngInject */
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
class ZabbixDBConnector {
constructor(sqlDataSourceId) {
this.sqlDataSourceId = sqlDataSourceId;
// Try to load DS with given id to check it's exist
this.loadSQLDataSource(sqlDataSourceId);
}
loadSQLDataSource(datasourceId) {
let ds = _.find(datasourceSrv.getAll(), {'id': datasourceId});
if (ds) {
return datasourceSrv.loadDatasource(ds.name)
.then(ds => {
console.log('SQL data source loaded', ds);
});
} else {
return Promise.reject(`SQL Data Source with ID ${datasourceId} not found`);
}
}
invokeSQLQuery(query) {
let queryDef = {
refId: 'A',
format: 'table',
datasourceId: this.sqlDataSourceId,
rawSql: query
};
return backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
queries: [queryDef],
}
})
.then(response => {
let results = response.data.results;
if (results['A']) {
return _.head(results['A'].tables);
} else {
return null;
}
});
}
}
return ZabbixDBConnector;
}
angular
.module('grafana.services')
.factory('ZabbixDBConnector', ZabbixDBConnectorFactory);