Merge branch 'develop'

This commit is contained in:
Alexander Zobnin
2015-05-18 12:39:50 +03:00
2 changed files with 95 additions and 193 deletions

View File

@@ -12,8 +12,8 @@ function (angular, _, kbn) {
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) { module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) {
function ZabbixAPIDatasource(datasource) { function ZabbixAPIDatasource(datasource) {
this.name = datasource.name;
this.type = 'zabbix'; this.type = 'zabbix';
this.name = datasource.name;
this.url = datasource.url; this.url = datasource.url;
@@ -33,31 +33,30 @@ function (angular, _, kbn) {
ZabbixAPIDatasource.prototype.query = function(options) { ZabbixAPIDatasource.prototype.query = function(options) {
// get from & to in seconds // get from & to in seconds
var from = kbn.parseDate(options.range.from).getTime(); var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
var to = kbn.parseDate(options.range.to).getTime(); var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
// Need for find target alias // Create request for each target
var targets = options.targets; var promises = _.map(options.targets, function(target) {
// Remove undefined and hidden targets // Remove undefined and hidden targets
var displayedTargets = _.filter(targets, function (target) { if (target.hide || !target.item) {
return (!target.hide && target.item); return [];
});
if (displayedTargets.length) {
// Extract zabbix api item objects from targets
var target_items = _.map(displayedTargets, 'item');
} else {
// No valid targets, return the empty dataset
var d = $q.defer();
d.resolve({ data: [] });
return d.promise;
} }
from = Math.ceil(from/1000); // Perform request and then handle result
to = Math.ceil(to/1000); return this.performTimeSeriesQuery(target.item, from, to).then(_.partial(
this.handleZabbixAPIResponse, target));
}, this);
return this.performTimeSeriesQuery(target_items, from, to).then(function (response) { return $q.all(promises).then(function(results) {
return { data: _.flatten(results) };
});
};
// Request data from Zabbix API
ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(target, response) {
/** /**
* Response should be in the format: * Response should be in the format:
* data: [ * data: [
@@ -72,64 +71,34 @@ function (angular, _, kbn) {
* ] * ]
*/ */
// Index returned datapoints by item/metric id var series = {
var indexed_result = _.groupBy(response, 'itemid'); target: target.alias,
datapoints: _.map(response, function (p) {
// TODO: realize correct timeseries reduce
/*
// Reduce timeseries to the same size for stacking and tooltip work properly
var min_length = _.min(_.map(indexed_result, function (history) {
return history.length;
}));
_.each(indexed_result, function (item) {
item.splice(0, item.length - min_length);
});*/
// Sort result as the same as targets for display
// stacked timeseries in proper order
var sorted_history = _.sortBy(indexed_result, function (value, key, list) {
return _.indexOf(_.map(target_items, 'itemid'), key);
});
var series = _.map(sorted_history,
// Foreach itemid index: iterate over the data points and
// normalize to Grafana response format.
function (history, index) {
return {
// Lookup itemid:alias map
//target: targets[itemid].alias,
target: targets[index].alias,
datapoints: _.map(history, function (p) {
// Value must be a number for properly work // Value must be a number for properly work
var value = Number(p.value); var value = Number(p.value);
// TODO: Correct time for proper stacking
//var clock = Math.round(Number(p.clock) / 60) * 60;
return [value, p.clock * 1000]; return [value, p.clock * 1000];
}) })
}; };
})
return $q.when({data: series}); return $q.when(series);
});
}; };
///////////////////////////////////////////////////////////////////////
/// Query methods
///////////////////////////////////////////////////////////////////////
// Request data from Zabbix API // Request data from Zabbix API
ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(request_data) { ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) {
var options = { var options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
url: this.url, url: this.url,
data: request_data data: {
jsonrpc: '2.0',
method: method,
params: params,
auth: this.auth,
id: 1
}
}; };
var performedQuery; var performedQuery;
@@ -162,68 +131,22 @@ function (angular, _, kbn) {
* @param items: array of zabbix api item objects * @param items: array of zabbix api item objects
*/ */
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) { ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
var params = {
// Group items by value type for separate requests
var items_by_value_type = _.groupBy(items, 'value_type');
var self = this;
var apiRequests = [];
// Prepare requests for each value type
_.each(items_by_value_type, function (value, key, list) {
var item_ids = _.map(value, 'itemid');
var history_type = key;
var data = {
jsonrpc: '2.0',
method: 'history.get',
params: {
output: 'extend', output: 'extend',
history: history_type, history: items.value_type,
itemids: item_ids, itemids: items.itemid,
sortfield: 'clock', sortfield: 'clock',
sortorder: 'ASC', sortorder: 'ASC',
limit: self.limitmetrics, limit: this.limitmetrics,
time_from: start, time_from: start,
},
auth: self.auth,
id: 1
}; };
// Relative queries (e.g. last hour) don't include an end time // Relative queries (e.g. last hour) don't include an end time
if (end) { if (end) {
data.params.time_till = end; params.time_till = end;
} }
apiRequests.push(self.performZabbixAPIRequest(data)); return this.performZabbixAPIRequest('history.get', params);
});
return this.handleMultipleRequest(apiRequests);
};
// Handle multiple request
ZabbixAPIDatasource.prototype.handleMultipleRequest = function(apiRequests) {
var history = [];
var performedQuery = null;
// Build chain of api requests and put all history data into single array
_.each(apiRequests, function (apiRequest) {
if(!performedQuery) {
performedQuery = apiRequest.then(function (response) {
history = history.concat(response);
return history;
});
} else {
performedQuery = performedQuery.then(function () {
return apiRequest.then(function (response) {
history = history.concat(response);
return history;
});
});
}
});
return performedQuery;
}; };
@@ -255,83 +178,62 @@ function (angular, _, kbn) {
// Get the list of host groups // Get the list of host groups
ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() { ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() {
var data = { var params = {
jsonrpc: '2.0',
method: 'hostgroup.get',
params: {
output: ['name'], output: ['name'],
real_hosts: true, //Return only host groups that contain hosts real_hosts: true, //Return only host groups that contain hosts
sortfield: 'name' sortfield: 'name'
},
auth: this.auth,
id: 1
}; };
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('hostgroup.get', params);
}; };
// Get the list of hosts // Get the list of hosts
ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) { ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) {
var data = { var params = {
jsonrpc: '2.0',
method: 'host.get',
params: {
output: ['name'], output: ['name'],
sortfield: 'name' sortfield: 'name'
},
auth: this.auth,
id: 1
}; };
// Return only hosts in given group
if (groupid) { if (groupid) {
data.params.groupids = groupid; params.groupids = groupid;
} }
return this.performZabbixAPIRequest('host.get', params);
return this.performZabbixAPIRequest(data);
}; };
// Get the list of applications // Get the list of applications
ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostid) { ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostid) {
var data = { var params = {
jsonrpc: '2.0',
method: 'application.get',
params: {
output: ['name'], output: ['name'],
sortfield: 'name', sortfield: 'name',
hostids: hostid hostids: hostid
},
auth: this.auth,
id: 1
}; };
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('application.get', params);
}; };
// Get the list of host items // Get the list of host items
ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostid, applicationid) { ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostid, applicationid) {
var data = { var params = {
jsonrpc: '2.0',
method: 'item.get',
params: {
output: ['name', 'key_', 'value_type', 'delay'], output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name', sortfield: 'name',
hostids: hostid, hostids: hostid,
webitems: true, //Include web items in the result
//Include web items in the result
webitems: true,
// Return only numeric items
filter: { filter: {
value_type: [0,3] value_type: [0,3]
} }
},
auth: this.auth,
id: 1
}; };
// If application selected return only relative items // If application selected return only relative items
if (applicationid) { if (applicationid) {
data.params.applicationids = applicationid; params.applicationids = applicationid;
} }
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('item.get', params);
}; };

View File

@@ -10,13 +10,13 @@
User User
</li> </li>
<li> <li>
<input type="text" class="tight-form-input input-large" ng-model='current.zabbixUser' placeholder=""></input> <input type="text" class="tight-form-input input-large" ng-model='current.user' placeholder=""></input>
</li> </li>
<li class="tight-form-item"> <li class="tight-form-item">
Password Password
</li> </li>
<li> <li>
<input type="password" class="tight-form-input input-large" ng-model='current.zabbixPassword' placeholder=""></input> <input type="password" class="tight-form-input input-large" ng-model='current.password' placeholder=""></input>
</li> </li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>