Refactoring: code formatting and add functions descriptions and some comments.
This commit is contained in:
@@ -27,13 +27,22 @@ function (angular, _, kbn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls for each panel in dashboard.
|
||||||
|
* @param {Object} options Query options. Contains time range, targets
|
||||||
|
* and other info.
|
||||||
|
* @return {Object} Grafana metrics object with timeseries data
|
||||||
|
* for each target.
|
||||||
|
*/
|
||||||
ZabbixAPIDatasource.prototype.query = function(options) {
|
ZabbixAPIDatasource.prototype.query = function(options) {
|
||||||
|
|
||||||
// get from & to in seconds
|
// get from & to in seconds
|
||||||
var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
|
var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
|
||||||
var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
|
var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
|
||||||
|
|
||||||
// Create request for each target
|
// Create request for each target
|
||||||
var promises = _.map(options.targets, function(target) {
|
var promises = _.map(options.targets, function(target) {
|
||||||
|
|
||||||
// Remove undefined and hidden targets
|
// Remove undefined and hidden targets
|
||||||
if (target.hide || !target.item) {
|
if (target.hide || !target.item) {
|
||||||
return [];
|
return [];
|
||||||
@@ -51,7 +60,9 @@ function (angular, _, kbn) {
|
|||||||
return this.performTimeSeriesQuery(item, from, to).then(_.partial(
|
return this.performTimeSeriesQuery(item, from, to).then(_.partial(
|
||||||
this.handleHistoryResponse, alias));
|
this.handleHistoryResponse, alias));
|
||||||
} else {
|
} else {
|
||||||
// Handle templated target
|
/**
|
||||||
|
* Handle templated target
|
||||||
|
*/
|
||||||
|
|
||||||
var itemname = templateSrv.replace(target.item.name);
|
var itemname = templateSrv.replace(target.item.name);
|
||||||
var hostname = templateSrv.replace(target.host.name);
|
var hostname = templateSrv.replace(target.host.name);
|
||||||
@@ -60,25 +71,31 @@ function (angular, _, kbn) {
|
|||||||
var host_pattern = /([\w\.\s]+)/g;
|
var host_pattern = /([\w\.\s]+)/g;
|
||||||
var hosts = hostname.match(host_pattern);
|
var hosts = hostname.match(host_pattern);
|
||||||
|
|
||||||
|
// Remove hostnames from item names and then
|
||||||
// Extract item names
|
// Extract item names
|
||||||
|
// [hostname]: itemname --> itemname
|
||||||
var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
|
var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
|
||||||
var itemname_pattern = /([^{},]+)/g;
|
var itemname_pattern = /([^{},]+)/g;
|
||||||
// Remove hostnames from item name
|
var itemnames = itemname.replace(delete_hostname_pattern, '')
|
||||||
// [hostname]: itemname --> itemname
|
.match(itemname_pattern);
|
||||||
var itemnames = itemname.replace(delete_hostname_pattern, '').match(itemname_pattern);
|
|
||||||
//var aliases = itemname.match(itemname_pattern);
|
//var aliases = itemname.match(itemname_pattern);
|
||||||
|
|
||||||
|
// Don't perform query for high number of items
|
||||||
|
// to prevent Grafana slowdown
|
||||||
if (itemnames && (itemnames.length < this.limitmetrics)) {
|
if (itemnames && (itemnames.length < this.limitmetrics)) {
|
||||||
// Find items by item names and perform queries
|
|
||||||
var self = this;
|
// Select the host that the item belongs for multiple hosts request
|
||||||
if (hosts.length > 1) {
|
if (hosts.length > 1) {
|
||||||
// Select the host that the item belongs for multiple hosts request
|
|
||||||
var selectHosts = true;
|
var selectHosts = true;
|
||||||
}
|
}
|
||||||
return this.findZabbixItem(hosts, itemnames, selectHosts).then(function (items) {
|
|
||||||
items = _.flatten(items);
|
// Find items by item names and perform queries
|
||||||
return self.performTimeSeriesQuery(items, from, to)
|
var self = this;
|
||||||
.then(_.partial(self.handleHistoryResponse, items));
|
return this.findZabbixItem(hosts, itemnames, selectHosts)
|
||||||
|
.then(function (items) {
|
||||||
|
items = _.flatten(items);
|
||||||
|
return self.performTimeSeriesQuery(items, from, to)
|
||||||
|
.then(_.partial(self.handleHistoryResponse, items));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return [];
|
return [];
|
||||||
@@ -93,9 +110,11 @@ function (angular, _, kbn) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform time series query to Zabbix API
|
* Perform time series query from Zabbix API
|
||||||
*
|
* @param {Array} items Array of Zabbix item objects
|
||||||
* @param items: array of zabbix api item objects
|
* @param {Number} start Time in seconds
|
||||||
|
* @param {Number} end Time in seconds
|
||||||
|
* @return {Array} Array of Zabbix history objects
|
||||||
*/
|
*/
|
||||||
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
|
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
|
||||||
// Group items by value type
|
// Group items by value type
|
||||||
@@ -125,8 +144,17 @@ function (angular, _, kbn) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Convert Zabbix API data to Grafana format
|
/**
|
||||||
ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, response) {
|
* Convert Zabbix API data to Grafana format
|
||||||
|
* @param {Array} items Array of Zabbix Items
|
||||||
|
* @param {Array} history Array of Zabbix History
|
||||||
|
* @return {Array} Array of timeseries in Grafana format
|
||||||
|
* {
|
||||||
|
* target: "Metric name",
|
||||||
|
* datapoints: [[<value>, <unixtime>], ...]
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, history) {
|
||||||
/**
|
/**
|
||||||
* Response should be in the format:
|
* Response should be in the format:
|
||||||
* data: [
|
* data: [
|
||||||
@@ -143,7 +171,7 @@ function (angular, _, kbn) {
|
|||||||
|
|
||||||
// Group items and history by itemid
|
// Group items and history by itemid
|
||||||
var indexed_items = _.indexBy(items, 'itemid');
|
var indexed_items = _.indexBy(items, 'itemid');
|
||||||
var grouped_history = _.groupBy(response, 'itemid');
|
var grouped_history = _.groupBy(history, 'itemid');
|
||||||
|
|
||||||
return $q.when(_.map(grouped_history, function (history, itemid) {
|
return $q.when(_.map(grouped_history, function (history, itemid) {
|
||||||
var item = indexed_items[itemid];
|
var item = indexed_items[itemid];
|
||||||
@@ -359,11 +387,12 @@ function (angular, _, kbn) {
|
|||||||
if (selectHosts) {
|
if (selectHosts) {
|
||||||
params.selectHosts = ['name'];
|
params.selectHosts = ['name'];
|
||||||
}
|
}
|
||||||
return self.performZabbixAPIRequest('item.get', params).then(function (items) {
|
return self.performZabbixAPIRequest('item.get', params)
|
||||||
return _.filter(items, function (item) {
|
.then(function (items) {
|
||||||
return _.contains(itemnames, expandItemName(item));
|
return _.filter(items, function (item) {
|
||||||
|
return _.contains(itemnames, expandItemName(item));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -542,34 +571,36 @@ function (angular, _, kbn) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.performZabbixAPIRequest('trigger.get', params).then(function (result) {
|
return this.performZabbixAPIRequest('trigger.get', params)
|
||||||
if(result) {
|
.then(function (result) {
|
||||||
var obs = {};
|
if(result) {
|
||||||
obs = _.indexBy(result, 'triggerid');
|
var obs = {};
|
||||||
|
obs = _.indexBy(result, 'triggerid');
|
||||||
|
|
||||||
var params = {
|
var params = {
|
||||||
output: 'extend',
|
output: 'extend',
|
||||||
sortorder: 'DESC',
|
sortorder: 'DESC',
|
||||||
time_from: from,
|
time_from: from,
|
||||||
time_till: to,
|
time_till: to,
|
||||||
objectids: _.keys(obs)
|
objectids: _.keys(obs)
|
||||||
};
|
};
|
||||||
|
|
||||||
return self.performZabbixAPIRequest('event.get', params).then(function (result) {
|
return self.performZabbixAPIRequest('event.get', params)
|
||||||
var events = [];
|
.then(function (result) {
|
||||||
_.each(result, function(e) {
|
var events = [];
|
||||||
events.push({
|
_.each(result, function(e) {
|
||||||
annotation: annotation,
|
events.push({
|
||||||
time: e.clock * 1000,
|
annotation: annotation,
|
||||||
title: obs[e.objectid].description,
|
time: e.clock * 1000,
|
||||||
text: e.eventid,
|
title: obs[e.objectid].description,
|
||||||
});
|
text: e.eventid,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return events;
|
||||||
});
|
});
|
||||||
return events;
|
} else {
|
||||||
});
|
return [];
|
||||||
} else {
|
}
|
||||||
return [];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user