Refactor: move zabbix requests to single place (zabbix.js module).
This commit is contained in:
@@ -6,15 +6,15 @@ import * as migrations from './migrations';
|
|||||||
import * as metricFunctions from './metricFunctions';
|
import * as metricFunctions from './metricFunctions';
|
||||||
import DataProcessor from './DataProcessor';
|
import DataProcessor from './DataProcessor';
|
||||||
import responseHandler from './responseHandler';
|
import responseHandler from './responseHandler';
|
||||||
import './zabbixAPI.service.js';
|
import './zabbix.js';
|
||||||
import './zabbixCache.service.js';
|
|
||||||
import './queryBuilder.js';
|
|
||||||
import {ZabbixAPIError} from './zabbixAPICore.service.js';
|
import {ZabbixAPIError} from './zabbixAPICore.service.js';
|
||||||
|
|
||||||
class ZabbixAPIDatasource {
|
class ZabbixAPIDatasource {
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(instanceSettings, templateSrv, alertSrv, zabbixAPIService, ZabbixCachingProxy, QueryBuilder) {
|
constructor(instanceSettings, templateSrv, alertSrv, Zabbix) {
|
||||||
|
this.templateSrv = templateSrv;
|
||||||
|
this.alertSrv = alertSrv;
|
||||||
|
|
||||||
// General data source settings
|
// General data source settings
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@@ -34,19 +34,7 @@ class ZabbixAPIDatasource {
|
|||||||
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
|
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
|
||||||
this.cacheTTL = utils.parseInterval(ttl);
|
this.cacheTTL = utils.parseInterval(ttl);
|
||||||
|
|
||||||
// Initialize Zabbix API
|
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
|
||||||
var ZabbixAPI = zabbixAPIService;
|
|
||||||
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
|
|
||||||
|
|
||||||
// Initialize cache service
|
|
||||||
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI, this.cacheTTL);
|
|
||||||
|
|
||||||
// Initialize query builder
|
|
||||||
this.queryBuilder = new QueryBuilder(this.zabbixCache);
|
|
||||||
|
|
||||||
// Dependencies
|
|
||||||
this.templateSrv = templateSrv;
|
|
||||||
this.alertSrv = alertSrv;
|
|
||||||
|
|
||||||
// Use custom format for template variables
|
// Use custom format for template variables
|
||||||
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
|
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
|
||||||
@@ -119,7 +107,7 @@ class ZabbixAPIDatasource {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.zabbixAPI
|
return this.zabbix
|
||||||
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
|
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
|
||||||
.then(slaObject => {
|
.then(slaObject => {
|
||||||
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
||||||
@@ -148,7 +136,7 @@ class ZabbixAPIDatasource {
|
|||||||
let options = {
|
let options = {
|
||||||
itemtype: 'num'
|
itemtype: 'num'
|
||||||
};
|
};
|
||||||
return this.queryBuilder.build(target, options)
|
return this.zabbix.getItemsFromTarget(target, options)
|
||||||
.then(items => {
|
.then(items => {
|
||||||
// Add hostname for items from multiple hosts
|
// Add hostname for items from multiple hosts
|
||||||
var addHostName = utils.isRegex(target.host.filter);
|
var addHostName = utils.isRegex(target.host.filter);
|
||||||
@@ -164,7 +152,7 @@ class ZabbixAPIDatasource {
|
|||||||
});
|
});
|
||||||
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
|
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
|
||||||
|
|
||||||
getHistory = this.zabbixAPI
|
getHistory = this.zabbix
|
||||||
.getTrend(items, timeFrom, timeTo)
|
.getTrend(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return responseHandler.handleTrends(history, items, addHostName, valueType);
|
return responseHandler.handleTrends(history, items, addHostName, valueType);
|
||||||
@@ -173,7 +161,7 @@ class ZabbixAPIDatasource {
|
|||||||
|
|
||||||
// Use history
|
// Use history
|
||||||
else {
|
else {
|
||||||
getHistory = this.zabbixCache
|
getHistory = this.zabbix
|
||||||
.getHistory(items, timeFrom, timeTo)
|
.getHistory(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return responseHandler.handleHistory(history, items, addHostName);
|
return responseHandler.handleHistory(history, items, addHostName);
|
||||||
@@ -227,10 +215,10 @@ class ZabbixAPIDatasource {
|
|||||||
let options = {
|
let options = {
|
||||||
itemtype: 'text'
|
itemtype: 'text'
|
||||||
};
|
};
|
||||||
return this.queryBuilder.build(target, options)
|
return this.zabbix.getItemsFromTarget(target, options)
|
||||||
.then(items => {
|
.then(items => {
|
||||||
if (items.length) {
|
if (items.length) {
|
||||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
return this.zabbix.getHistory(items, timeFrom, timeTo)
|
||||||
.then(history => {
|
.then(history => {
|
||||||
return responseHandler.convertHistory(history, items, false, (point) => {
|
return responseHandler.convertHistory(history, items, false, (point) => {
|
||||||
let value = point.value;
|
let value = point.value;
|
||||||
@@ -255,10 +243,10 @@ class ZabbixAPIDatasource {
|
|||||||
*/
|
*/
|
||||||
testDatasource() {
|
testDatasource() {
|
||||||
let zabbixVersion;
|
let zabbixVersion;
|
||||||
return this.zabbixAPI.getVersion()
|
return this.zabbix.getVersion()
|
||||||
.then(version => {
|
.then(version => {
|
||||||
zabbixVersion = version;
|
zabbixVersion = version;
|
||||||
return this.zabbixAPI.login();
|
return this.zabbix.login();
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return {
|
return {
|
||||||
@@ -317,16 +305,16 @@ class ZabbixAPIDatasource {
|
|||||||
if (template.app === '/.*/') {
|
if (template.app === '/.*/') {
|
||||||
template.app = '';
|
template.app = '';
|
||||||
}
|
}
|
||||||
result = this.queryBuilder.getItems(template.group, template.host, template.app, template.item);
|
result = this.zabbix.getItems(template.group, template.host, template.app, template.item);
|
||||||
} else if (parts.length === 3) {
|
} else if (parts.length === 3) {
|
||||||
// Get applications
|
// Get applications
|
||||||
result = this.queryBuilder.getApps(template.group, template.host, template.app);
|
result = this.zabbix.getApps(template.group, template.host, template.app);
|
||||||
} else if (parts.length === 2) {
|
} else if (parts.length === 2) {
|
||||||
// Get hosts
|
// Get hosts
|
||||||
result = this.queryBuilder.getHosts(template.group, template.host);
|
result = this.zabbix.getHosts(template.group, template.host);
|
||||||
} else if (parts.length === 1) {
|
} else if (parts.length === 1) {
|
||||||
// Get groups
|
// Get groups
|
||||||
result = this.zabbixCache.getGroups(template.group);
|
result = this.zabbix.getGroups(template.group);
|
||||||
} else {
|
} else {
|
||||||
result = Promise.resolve([]);
|
result = Promise.resolve([]);
|
||||||
}
|
}
|
||||||
@@ -349,15 +337,13 @@ class ZabbixAPIDatasource {
|
|||||||
// Show all triggers
|
// Show all triggers
|
||||||
var showTriggers = [0, 1];
|
var showTriggers = [0, 1];
|
||||||
|
|
||||||
var buildQuery = this.queryBuilder
|
var getTriggers = this.zabbix
|
||||||
.buildTriggerQuery(this.replaceTemplateVars(annotation.group, {}),
|
.getTriggers(this.replaceTemplateVars(annotation.group, {}),
|
||||||
this.replaceTemplateVars(annotation.host, {}),
|
this.replaceTemplateVars(annotation.host, {}),
|
||||||
this.replaceTemplateVars(annotation.application, {}));
|
this.replaceTemplateVars(annotation.application, {}),
|
||||||
|
showTriggers);
|
||||||
|
|
||||||
return buildQuery.then(query => {
|
return getTriggers.then(triggers => {
|
||||||
return this.zabbixAPI
|
|
||||||
.getTriggers(query.groupids, query.hostids, query.applicationids, showTriggers)
|
|
||||||
.then(triggers => {
|
|
||||||
|
|
||||||
// Filter triggers by description
|
// Filter triggers by description
|
||||||
if (utils.isRegex(annotation.trigger)) {
|
if (utils.isRegex(annotation.trigger)) {
|
||||||
@@ -376,7 +362,7 @@ class ZabbixAPIDatasource {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var objectids = _.map(triggers, 'triggerid');
|
var objectids = _.map(triggers, 'triggerid');
|
||||||
return this.zabbixAPI
|
return this.zabbix
|
||||||
.getEvents(objectids, timeFrom, timeTo, showOkEvents)
|
.getEvents(objectids, timeFrom, timeTo, showOkEvents)
|
||||||
.then(events => {
|
.then(events => {
|
||||||
var indexedTriggers = _.keyBy(triggers, 'triggerid');
|
var indexedTriggers = _.keyBy(triggers, 'triggerid');
|
||||||
@@ -408,7 +394,6 @@ class ZabbixAPIDatasource {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +1,40 @@
|
|||||||
import angular from 'angular';
|
import angular from 'angular';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import * as utils from './utils';
|
import * as utils from './utils';
|
||||||
|
import './zabbixAPI.service.js';
|
||||||
|
import './zabbixCache.service.js';
|
||||||
|
|
||||||
function QueryBuilderFactory() {
|
// Use factory() instead service() for multiple data sources support.
|
||||||
|
// Each Zabbix data source instance should initialize its own API instance.
|
||||||
|
|
||||||
class QueryBuilder {
|
/** @ngInject */
|
||||||
constructor(zabbixCacheInstance) {
|
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
||||||
this.cache = zabbixCacheInstance;
|
|
||||||
|
class Zabbix {
|
||||||
|
constructor(url, username, password, basicAuth, withCredentials, cacheTTL) {
|
||||||
|
|
||||||
|
// Initialize Zabbix API
|
||||||
|
var ZabbixAPI = zabbixAPIService;
|
||||||
|
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
|
||||||
|
|
||||||
|
// Initialize cache
|
||||||
|
this.cache = new ZabbixCachingProxy(this.zabbixAPI, cacheTTL);
|
||||||
|
|
||||||
|
// Proxy methods
|
||||||
|
this.getHistory = this.cache.getHistory.bind(this.cache);
|
||||||
|
|
||||||
|
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
|
||||||
|
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
|
||||||
|
this.getAcknowledges = this.zabbixAPI.getAcknowledges.bind(this.zabbixAPI);
|
||||||
|
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
|
||||||
|
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
|
||||||
|
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeCache() {
|
getItemsFromTarget(target, options) {
|
||||||
if (this.cache._initialized) {
|
|
||||||
return Promise.resolve();
|
|
||||||
} else {
|
|
||||||
return this.cache.refresh();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build query - convert target filters to array of Zabbix items
|
|
||||||
*/
|
|
||||||
build(target, options) {
|
|
||||||
function getFiltersFromTarget(target) {
|
|
||||||
let parts = ['group', 'host', 'application', 'item'];
|
let parts = ['group', 'host', 'application', 'item'];
|
||||||
return _.map(parts, p => target[p].filter);
|
let filters = _.map(parts, p => target[p].filter);
|
||||||
}
|
return this.getItems(...filters, options);
|
||||||
|
|
||||||
return this.initializeCache()
|
|
||||||
.then(() => {
|
|
||||||
return this.getItems(...getFiltersFromTarget(target), options);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build trigger query in asynchronous manner
|
|
||||||
*/
|
|
||||||
buildTriggerQuery(groupFilter, hostFilter, appFilter) {
|
|
||||||
return this.initializeCache()
|
|
||||||
.then(() => {
|
|
||||||
return this.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllGroups() {
|
getAllGroups() {
|
||||||
@@ -120,7 +115,7 @@ function QueryBuilderFactory() {
|
|||||||
/**
|
/**
|
||||||
* Build query - convert target filters to array of Zabbix items
|
* Build query - convert target filters to array of Zabbix items
|
||||||
*/
|
*/
|
||||||
buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter) {
|
getTriggers(groupFilter, hostFilter, appFilter, showTriggers) {
|
||||||
let promises = [
|
let promises = [
|
||||||
this.getGroups(groupFilter),
|
this.getGroups(groupFilter),
|
||||||
this.getHosts(groupFilter, hostFilter),
|
this.getHosts(groupFilter, hostFilter),
|
||||||
@@ -145,16 +140,21 @@ function QueryBuilderFactory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
|
}).then(query => {
|
||||||
|
return this.zabbixAPI
|
||||||
|
.getTriggers(query.groupids, query.hostids, query.applicationids, showTriggers);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return QueryBuilder;
|
return Zabbix;
|
||||||
}
|
}
|
||||||
|
|
||||||
angular
|
angular
|
||||||
.module('grafana.services')
|
.module('grafana.services')
|
||||||
.factory('QueryBuilder', QueryBuilderFactory);
|
.factory('Zabbix', ZabbixFactory);
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find group, host, app or item by given name.
|
* Find group, host, app or item by given name.
|
||||||
@@ -106,9 +106,9 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// Load datasource
|
// Load datasource
|
||||||
return this.datasourceSrv.get(this.panel.datasource).then(datasource => {
|
return this.datasourceSrv.get(this.panel.datasource)
|
||||||
var zabbix = datasource.zabbixAPI;
|
.then(datasource => {
|
||||||
var queryBuilder = datasource.queryBuilder;
|
var zabbix = datasource.zabbix;
|
||||||
var showEvents = self.panel.showEvents.value;
|
var showEvents = self.panel.showEvents.value;
|
||||||
var triggerFilter = self.panel.triggers;
|
var triggerFilter = self.panel.triggers;
|
||||||
|
|
||||||
@@ -117,13 +117,8 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
|
|||||||
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
|
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
|
||||||
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
|
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
|
||||||
|
|
||||||
var buildQuery = queryBuilder.buildTriggerQuery(groupFilter, hostFilter, appFilter);
|
var getTriggers = zabbix.getTriggers(groupFilter, hostFilter, appFilter, showEvents);
|
||||||
return buildQuery.then(query => {
|
return getTriggers.then(triggers => {
|
||||||
return zabbix.getTriggers(query.groupids,
|
|
||||||
query.hostids,
|
|
||||||
query.applicationids,
|
|
||||||
showEvents)
|
|
||||||
.then(triggers => {
|
|
||||||
return _.map(triggers, trigger => {
|
return _.map(triggers, trigger => {
|
||||||
let triggerObj = trigger;
|
let triggerObj = trigger;
|
||||||
|
|
||||||
@@ -230,7 +225,6 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switchComment(trigger) {
|
switchComment(trigger) {
|
||||||
|
|||||||
Reference in New Issue
Block a user