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,103 +33,72 @@ 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) { // Perform request and then handle result
// Extract zabbix api item objects from targets return this.performTimeSeriesQuery(target.item, from, to).then(_.partial(
var target_items = _.map(displayedTargets, 'item'); this.handleZabbixAPIResponse, target));
} else { }, this);
// No valid targets, return the empty dataset
var d = $q.defer();
d.resolve({ data: [] });
return d.promise;
}
from = Math.ceil(from/1000); return $q.all(promises).then(function(results) {
to = Math.ceil(to/1000); return { data: _.flatten(results) };
return this.performTimeSeriesQuery(target_items, from, to).then(function (response) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Index returned datapoints by item/metric id
var indexed_result = _.groupBy(response, 'itemid');
// 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
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 $q.when({data: series});
}); });
}; };
/////////////////////////////////////////////////////////////////////// // Request data from Zabbix API
/// Query methods ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(target, response) {
/////////////////////////////////////////////////////////////////////// /**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
var series = {
target: target.alias,
datapoints: _.map(response, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
return [value, p.clock * 1000];
})
};
return $q.when(series);
};
// 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 = {
output: 'extend',
history: items.value_type,
itemids: items.itemid,
sortfield: 'clock',
sortorder: 'ASC',
limit: this.limitmetrics,
time_from: start,
};
// Group items by value type for separate requests // Relative queries (e.g. last hour) don't include an end time
var items_by_value_type = _.groupBy(items, 'value_type'); if (end) {
params.time_till = end;
}
var self = this; return this.performZabbixAPIRequest('history.get', params);
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',
history: history_type,
itemids: item_ids,
sortfield: 'clock',
sortorder: 'ASC',
limit: self.limitmetrics,
time_from: start,
},
auth: self.auth,
id: 1
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
data.params.time_till = end;
}
apiRequests.push(self.performZabbixAPIRequest(data));
});
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', output: ['name'],
method: 'hostgroup.get', real_hosts: true, //Return only host groups that contain hosts
params: { sortfield: 'name'
output: ['name'],
real_hosts: true, //Return only host groups that contain hosts
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', output: ['name'],
method: 'host.get', sortfield: 'name'
params: {
output: ['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', output: ['name'],
method: 'application.get', sortfield: 'name',
params: { hostids: hostid
output: ['name'],
sortfield: 'name',
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', output: ['name', 'key_', 'value_type', 'delay'],
method: 'item.get', sortfield: 'name',
params: { hostids: hostid,
output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name', //Include web items in the result
hostids: hostid, webitems: true,
webitems: true, //Include web items in the result // 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>