Chore: convert Zabbix connector to TS

This commit is contained in:
Alexander Zobnin
2020-05-15 14:38:29 +03:00
parent c4f8b5bf2b
commit 11214d0d17
4 changed files with 64 additions and 46 deletions

View File

@@ -78,7 +78,7 @@ export function containsMacro(itemName) {
return MACRO_PATTERN.test(itemName); return MACRO_PATTERN.test(itemName);
} }
export function replaceMacro(item, macros, isTriggerItem) { export function replaceMacro(item, macros, isTriggerItem?) {
let itemName = isTriggerItem ? item.url : item.name; let itemName = isTriggerItem ? item.url : item.name;
const item_macros = itemName.match(MACRO_PATTERN); const item_macros = itemName.match(MACRO_PATTERN);
_.forEach(item_macros, macro => { _.forEach(item_macros, macro => {

View File

@@ -350,7 +350,7 @@ export class ZabbixAPIConnector {
return self.request('trend.get', params); return self.request('trend.get', params);
} }
getITService(serviceids) { getITService(serviceids?) {
const params = { const params = {
output: 'extend', output: 'extend',
serviceids: serviceids serviceids: serviceids

View File

@@ -0,0 +1,5 @@
export interface ZabbixConnector {
getMacros(hostids: any[]): any;
getVersion(): string;
login(): any;
}

View File

@@ -7,6 +7,7 @@ import { DBConnector } from './connectors/dbConnector';
import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector'; import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector';
import { SQLConnector } from './connectors/sql/sqlConnector'; import { SQLConnector } from './connectors/sql/sqlConnector';
import { InfluxDBConnector } from './connectors/influxdb/influxdbConnector'; import { InfluxDBConnector } from './connectors/influxdb/influxdbConnector';
import { ZabbixConnector } from './types';
const REQUESTS_TO_PROXYFY = [ const REQUESTS_TO_PROXYFY = [
'getHistory', 'getTrend', 'getGroups', 'getHosts', 'getApps', 'getItems', 'getMacros', 'getItemsByIDs', 'getHistory', 'getTrend', 'getGroups', 'getHosts', 'getApps', 'getItems', 'getMacros', 'getItemsByIDs',
@@ -24,9 +25,20 @@ const REQUESTS_TO_BIND = [
'getExtendedEventData' 'getExtendedEventData'
]; ];
export class Zabbix { export class Zabbix implements ZabbixConnector {
enableDirectDBConnection: boolean;
cachingProxy: CachingProxy;
zabbixAPI: ZabbixAPIConnector;
getHistoryDB: any;
dbConnector: any;
getTrendsDB: any;
getMacros: any;
getVersion: any;
login: any;
constructor(options) { constructor(options) {
let { const {
url, url,
username, username,
password, password,
@@ -42,7 +54,7 @@ export class Zabbix {
this.enableDirectDBConnection = enableDirectDBConnection; this.enableDirectDBConnection = enableDirectDBConnection;
// Initialize caching proxy for requests // Initialize caching proxy for requests
let cacheOptions = { const cacheOptions = {
enabled: true, enabled: true,
ttl: cacheTTL ttl: cacheTTL
}; };
@@ -55,7 +67,7 @@ export class Zabbix {
this.bindRequests(); this.bindRequests();
if (enableDirectDBConnection) { if (enableDirectDBConnection) {
const connectorOptions = { dbConnectionRetentionPolicy }; const connectorOptions: any = { dbConnectionRetentionPolicy };
this.initDBConnector(dbConnectionDatasourceId, dbConnectionDatasourceName, connectorOptions) this.initDBConnector(dbConnectionDatasourceId, dbConnectionDatasourceName, connectorOptions)
.then(() => { .then(() => {
this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector); this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector);
@@ -67,7 +79,7 @@ export class Zabbix {
initDBConnector(datasourceId, datasourceName, options) { initDBConnector(datasourceId, datasourceName, options) {
return DBConnector.loadDatasource(datasourceId, datasourceName) return DBConnector.loadDatasource(datasourceId, datasourceName)
.then(ds => { .then(ds => {
let connectorOptions = { datasourceId, datasourceName }; const connectorOptions: any = { datasourceId, datasourceName };
if (ds.type === 'influxdb') { if (ds.type === 'influxdb') {
connectorOptions.retentionPolicy = options.dbConnectionRetentionPolicy; connectorOptions.retentionPolicy = options.dbConnectionRetentionPolicy;
this.dbConnector = new InfluxDBConnector(connectorOptions); this.dbConnector = new InfluxDBConnector(connectorOptions);
@@ -79,19 +91,19 @@ export class Zabbix {
} }
proxyfyRequests() { proxyfyRequests() {
for (let request of REQUESTS_TO_PROXYFY) { for (const request of REQUESTS_TO_PROXYFY) {
this.zabbixAPI[request] = this.cachingProxy.proxyfy(this.zabbixAPI[request], request, this.zabbixAPI); this.zabbixAPI[request] = this.cachingProxy.proxyfy(this.zabbixAPI[request], request, this.zabbixAPI);
} }
} }
cacheRequests() { cacheRequests() {
for (let request of REQUESTS_TO_CACHE) { for (const request of REQUESTS_TO_CACHE) {
this.zabbixAPI[request] = this.cachingProxy.cacheRequest(this.zabbixAPI[request], request, this.zabbixAPI); this.zabbixAPI[request] = this.cachingProxy.cacheRequest(this.zabbixAPI[request], request, this.zabbixAPI);
} }
} }
bindRequests() { bindRequests() {
for (let request of REQUESTS_TO_BIND) { for (const request of REQUESTS_TO_BIND) {
this[request] = this.zabbixAPI[request].bind(this.zabbixAPI); this[request] = this.zabbixAPI[request].bind(this.zabbixAPI);
} }
} }
@@ -100,14 +112,14 @@ export class Zabbix {
* Perform test query for Zabbix API and external history DB. * Perform test query for Zabbix API and external history DB.
* @return {object} test result object: * @return {object} test result object:
* ``` * ```
{ * {
zabbixVersion, * zabbixVersion,
dbConnectorStatus: { * dbConnectorStatus: {
dsType, * dsType,
dsName * dsName
} * }
} * }
``` * ```
*/ */
testDataSource() { testDataSource() {
let zabbixVersion; let zabbixVersion;
@@ -142,18 +154,19 @@ export class Zabbix {
} }
getItemsFromTarget(target, options) { getItemsFromTarget(target, options) {
let parts = ['group', 'host', 'application', 'item']; const parts = ['group', 'host', 'application', 'item'];
let filters = _.map(parts, p => target[p].filter); const filters = _.map(parts, p => target[p].filter);
return this.getItems(...filters, options); return this.getItems(...filters, options);
} }
getHostsFromTarget(target) { getHostsFromTarget(target) {
let parts = ['group', 'host', 'application']; const parts = ['group', 'host', 'application'];
let filters = _.map(parts, p => target[p].filter); const filters = _.map(parts, p => target[p].filter);
return Promise.all([ return Promise.all([
this.getHosts(...filters), this.getHosts(...filters),
this.getApps(...filters), this.getApps(...filters),
]).then((results) => { ]).then((results) => {
// tslint:disable-next-line: prefer-const
let [hosts, apps] = results; let [hosts, apps] = results;
if (apps.appFilterEmpty) { if (apps.appFilterEmpty) {
apps = []; apps = [];
@@ -177,12 +190,12 @@ export class Zabbix {
getAllHosts(groupFilter) { getAllHosts(groupFilter) {
return this.getGroups(groupFilter) return this.getGroups(groupFilter)
.then(groups => { .then(groups => {
let groupids = _.map(groups, 'groupid'); const groupids = _.map(groups, 'groupid');
return this.zabbixAPI.getHosts(groupids); return this.zabbixAPI.getHosts(groupids);
}); });
} }
getHosts(groupFilter, hostFilter) { getHosts(groupFilter?, hostFilter?) {
return this.getAllHosts(groupFilter) return this.getAllHosts(groupFilter)
.then(hosts => findByFilter(hosts, hostFilter)); .then(hosts => findByFilter(hosts, hostFilter));
} }
@@ -193,15 +206,15 @@ export class Zabbix {
getAllApps(groupFilter, hostFilter) { getAllApps(groupFilter, hostFilter) {
return this.getHosts(groupFilter, hostFilter) return this.getHosts(groupFilter, hostFilter)
.then(hosts => { .then(hosts => {
let hostids = _.map(hosts, 'hostid'); const hostids = _.map(hosts, 'hostid');
return this.zabbixAPI.getApps(hostids); return this.zabbixAPI.getApps(hostids);
}); });
} }
getApps(groupFilter, hostFilter, appFilter) { getApps(groupFilter?, hostFilter?, appFilter?) {
return this.getHosts(groupFilter, hostFilter) return this.getHosts(groupFilter, hostFilter)
.then(hosts => { .then(hosts => {
let hostids = _.map(hosts, 'hostid'); const hostids = _.map(hosts, 'hostid');
if (appFilter) { if (appFilter) {
return this.zabbixAPI.getApps(hostids) return this.zabbixAPI.getApps(hostids)
.then(apps => filterByQuery(apps, appFilter)); .then(apps => filterByQuery(apps, appFilter));
@@ -214,13 +227,13 @@ export class Zabbix {
}); });
} }
getAllItems(groupFilter, hostFilter, appFilter, options = {}) { getAllItems(groupFilter, hostFilter, appFilter, options: any = {}) {
return this.getApps(groupFilter, hostFilter, appFilter) return this.getApps(groupFilter, hostFilter, appFilter)
.then(apps => { .then(apps => {
if (apps.appFilterEmpty) { if (apps.appFilterEmpty) {
return this.zabbixAPI.getItems(apps.hostids, undefined, options.itemtype); return this.zabbixAPI.getItems(apps.hostids, undefined, options.itemtype);
} else { } else {
let appids = _.map(apps, 'applicationid'); const appids = _.map(apps, 'applicationid');
return this.zabbixAPI.getItems(undefined, appids, options.itemtype); return this.zabbixAPI.getItems(undefined, appids, options.itemtype);
} }
}) })
@@ -235,7 +248,7 @@ export class Zabbix {
} }
expandUserMacro(items, isTriggerItem) { expandUserMacro(items, isTriggerItem) {
let hostids = getHostIds(items); const hostids = getHostIds(items);
return this.getMacros(hostids) return this.getMacros(hostids)
.then(macros => { .then(macros => {
_.forEach(items, item => { _.forEach(items, item => {
@@ -251,7 +264,7 @@ export class Zabbix {
}); });
} }
getItems(groupFilter, hostFilter, appFilter, itemFilter, options = {}) { getItems(groupFilter?, hostFilter?, appFilter?, itemFilter?, options = {}) {
return this.getAllItems(groupFilter, hostFilter, appFilter, options) return this.getAllItems(groupFilter, hostFilter, appFilter, options)
.then(items => filterByQuery(items, itemFilter)); .then(items => filterByQuery(items, itemFilter));
} }
@@ -265,7 +278,7 @@ export class Zabbix {
* Build query - convert target filters to array of Zabbix items * Build query - convert target filters to array of Zabbix items
*/ */
getTriggers(groupFilter, hostFilter, appFilter, options, proxyFilter) { getTriggers(groupFilter, hostFilter, appFilter, options, proxyFilter) {
let promises = [ const promises = [
this.getGroups(groupFilter), this.getGroups(groupFilter),
this.getHosts(groupFilter, hostFilter), this.getHosts(groupFilter, hostFilter),
this.getApps(groupFilter, hostFilter, appFilter) this.getApps(groupFilter, hostFilter, appFilter)
@@ -273,8 +286,8 @@ export class Zabbix {
return Promise.all(promises) return Promise.all(promises)
.then(results => { .then(results => {
let [filteredGroups, filteredHosts, filteredApps] = results; const [filteredGroups, filteredHosts, filteredApps] = results;
let query = {}; const query: any = {};
if (appFilter) { if (appFilter) {
query.applicationids = _.flatten(_.map(filteredApps, 'applicationid')); query.applicationids = _.flatten(_.map(filteredApps, 'applicationid'));
@@ -322,7 +335,7 @@ export class Zabbix {
} }
getHistoryTS(items, timeRange, options) { getHistoryTS(items, timeRange, options) {
let [timeFrom, timeTo] = timeRange; const [timeFrom, timeTo] = timeRange;
if (this.enableDirectDBConnection) { if (this.enableDirectDBConnection) {
return this.getHistoryDB(items, timeFrom, timeTo, options) return this.getHistoryDB(items, timeFrom, timeTo, options)
.then(history => this.dbConnector.handleGrafanaTSResponse(history, items)); .then(history => this.dbConnector.handleGrafanaTSResponse(history, items));
@@ -333,12 +346,12 @@ export class Zabbix {
} }
getTrends(items, timeRange, options) { getTrends(items, timeRange, options) {
let [timeFrom, timeTo] = timeRange; const [timeFrom, timeTo] = timeRange;
if (this.enableDirectDBConnection) { if (this.enableDirectDBConnection) {
return this.getTrendsDB(items, timeFrom, timeTo, options) return this.getTrendsDB(items, timeFrom, timeTo, options)
.then(history => this.dbConnector.handleGrafanaTSResponse(history, items)); .then(history => this.dbConnector.handleGrafanaTSResponse(history, items));
} else { } else {
let valueType = options.consolidateBy || options.valueType; const valueType = options.consolidateBy || options.valueType;
return this.zabbixAPI.getTrend(items, timeFrom, timeTo) return this.zabbixAPI.getTrend(items, timeFrom, timeTo)
.then(history => responseHandler.handleTrends(history, items, valueType)) .then(history => responseHandler.handleTrends(history, items, valueType))
.then(responseHandler.sortTimeseries); // Sort trend data, issue #202 .then(responseHandler.sortTimeseries); // Sort trend data, issue #202
@@ -346,7 +359,7 @@ export class Zabbix {
} }
getHistoryText(items, timeRange, target) { getHistoryText(items, timeRange, target) {
let [timeFrom, timeTo] = timeRange; const [timeFrom, timeTo] = timeRange;
if (items.length) { if (items.length) {
return this.zabbixAPI.getHistory(items, timeFrom, timeTo) return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
.then(history => { .then(history => {
@@ -366,11 +379,11 @@ export class Zabbix {
if (options.isOldVersion) { if (options.isOldVersion) {
itServices = _.filter(itServices, {'serviceid': target.itservice.serviceid}); itServices = _.filter(itServices, {'serviceid': target.itservice.serviceid});
} }
let itServiceIds = _.map(itServices, 'serviceid'); const itServiceIds = _.map(itServices, 'serviceid');
return this.zabbixAPI.getSLA(itServiceIds, timeRange, options) return this.zabbixAPI.getSLA(itServiceIds, timeRange, options)
.then(slaResponse => { .then(slaResponse => {
return _.map(itServiceIds, serviceid => { return _.map(itServiceIds, serviceid => {
let itservice = _.find(itServices, {'serviceid': serviceid}); const itservice = _.find(itServices, {'serviceid': serviceid});
return responseHandler.handleSLAResponse(itservice, target.slaProperty, slaResponse); return responseHandler.handleSLAResponse(itservice, target.slaProperty, slaResponse);
}); });
}); });
@@ -386,7 +399,7 @@ export class Zabbix {
* @return array with finded element or empty array * @return array with finded element or empty array
*/ */
function findByName(list, name) { function findByName(list, name) {
var finded = _.find(list, {'name': name}); const finded = _.find(list, {'name': name});
if (finded) { if (finded) {
return [finded]; return [finded];
} else { } else {
@@ -403,7 +416,7 @@ function findByName(list, name) {
* @return {[type]} array with finded element or empty array * @return {[type]} array with finded element or empty array
*/ */
function filterByName(list, name) { function filterByName(list, name) {
var finded = _.filter(list, {'name': name}); const finded = _.filter(list, {'name': name});
if (finded) { if (finded) {
return finded; return finded;
} else { } else {
@@ -412,8 +425,8 @@ function filterByName(list, name) {
} }
function filterByRegex(list, regex) { function filterByRegex(list, regex) {
var filterPattern = utils.buildRegex(regex); const filterPattern = utils.buildRegex(regex);
return _.filter(list, function (zbx_obj) { return _.filter(list, (zbx_obj) => {
return filterPattern.test(zbx_obj.name); return filterPattern.test(zbx_obj.name);
}); });
} }
@@ -435,7 +448,7 @@ function filterByQuery(list, filter) {
} }
function getHostIds(items) { function getHostIds(items) {
let hostIds = _.map(items, item => { const hostIds = _.map(items, item => {
return _.map(item.hosts, 'hostid'); return _.map(item.hosts, 'hostid');
}); });
return _.uniq(_.flatten(hostIds)); return _.uniq(_.flatten(hostIds));