Can used as plugin since Grafana 3.0.
This commit is contained in:
Alexander Zobnin
2015-12-26 23:04:53 +03:00
parent 24639e58cb
commit e95c61608b
15 changed files with 734 additions and 0 deletions

View File

@@ -0,0 +1,398 @@
define([
'angular',
'lodash',
'app/core/utils/datemath',
'./directives',
'./zabbixAPIWrapper',
'./helperFunctions',
'./queryCtrl'
],
function (angular, _, dateMath) {
'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv, alertSrv, ZabbixAPI, zabbixHelperSrv) {
/**
* Datasource initialization. Calls when you refresh page, add
* or modify datasource.
*
* @param {Object} datasource Grafana datasource object.
*/
function ZabbixAPIDatasource(datasource) {
this.name = datasource.name;
this.url = datasource.url;
this.basicAuth = datasource.basicAuth;
this.withCredentials = datasource.withCredentials;
if (datasource.jsonData) {
this.username = datasource.jsonData.username;
this.password = datasource.jsonData.password;
// Use trends instead history since specified time
this.trends = datasource.jsonData.trends;
this.trendsFrom = datasource.jsonData.trendsFrom || '7d';
// Limit metrics per panel for templated request
this.limitmetrics = datasource.jsonData.limitMetrics || 100;
} else {
// DEPRECATED. Loads settings from plugin.json file.
// For backward compatibility only.
this.username = datasource.meta.username;
this.password = datasource.meta.password;
this.trends = datasource.meta.trends;
this.trendsFrom = datasource.meta.trendsFrom || '7d';
this.limitmetrics = datasource.meta.limitmetrics || 100;
}
// Initialize Zabbix API
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
}
/**
* Test connection to Zabbix API
*
* @return {object} Connection status and Zabbix API version
*/
ZabbixAPIDatasource.prototype.testDatasource = function() {
var self = this;
return this.zabbixAPI.getZabbixAPIVersion().then(function (apiVersion) {
return self.zabbixAPI.performZabbixAPILogin().then(function (auth) {
if (auth) {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + apiVersion
};
} else {
return {
status: "error",
title: "Invalid user name or password",
message: "Zabbix API version: " + apiVersion
};
}
});
}, function(error) {
return {
status: "error",
title: "Connection failed",
message: "Could not connect to " + error.config.url
};
});
};
/**
* 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) {
// get from & to in seconds
var from = Math.ceil(dateMath.parse(options.range.from) / 1000);
var to = Math.ceil(dateMath.parse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
// Create request for each target
var promises = _.map(options.targets, function(target) {
if (target.mode !== 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.group || !target.host
|| !target.application || !target.item) {
return [];
}
// Replace templated variables
var groupname = templateSrv.replace(target.group.name, options.scopedVars);
var hostname = templateSrv.replace(target.host.name, options.scopedVars);
var appname = templateSrv.replace(target.application.name, options.scopedVars);
var itemname = templateSrv.replace(target.item.name, options.scopedVars);
// Extract zabbix groups, hosts and apps from string:
// "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
var groups = zabbixHelperSrv.splitMetrics(groupname);
var hosts = zabbixHelperSrv.splitMetrics(hostname);
var apps = zabbixHelperSrv.splitMetrics(appname);
// Remove hostnames from item names and then
// extract item names
// "hostname: itemname" --> "itemname"
var delete_hostname_pattern = /(?:\[[\w\.]+]:\s)/g;
var itemnames = zabbixHelperSrv.splitMetrics(itemname.replace(delete_hostname_pattern, ''));
var self = this;
// Query numeric data
if (!target.mode) {
// Find items by item names and perform queries
return this.zabbixAPI.itemFindQuery(groups, hosts, apps)
.then(function (items) {
// Filter hosts by regex
if (target.host.visible_name === 'All') {
if (target.hostFilter && _.every(items, _.identity.hosts)) {
// Use templated variables in filter
var host_pattern = new RegExp(templateSrv.replace(target.hostFilter, options.scopedVars));
items = _.filter(items, function (item) {
return _.some(item.hosts, function (host) {
return host_pattern.test(host.name);
});
});
}
}
if (itemnames[0] === 'All') {
// Filter items by regex
if (target.itemFilter) {
// Use templated variables in filter
var item_pattern = new RegExp(templateSrv.replace(target.itemFilter, options.scopedVars));
return _.filter(items, function (item) {
return item_pattern.test(zabbixHelperSrv.expandItemName(item));
});
} else {
return items;
}
} else {
// Filtering items
return _.filter(items, function (item) {
return _.contains(itemnames, zabbixHelperSrv.expandItemName(item));
});
}
}).then(function (items) {
// Don't perform query for high number of items
// to prevent Grafana slowdown
if (items.length > self.limitmetrics) {
var message = "Try to increase limitmetrics parameter in datasource config.<br>"
+ "Current limitmetrics value is " + self.limitmetrics;
alertSrv.set("Metrics limit exceeded", message, "warning", 10000);
return [];
} else {
items = _.flatten(items);
// Use alias only for single metric, otherwise use item names
var alias = target.item.name === 'All' || itemnames.length > 1 ?
undefined : templateSrv.replace(target.alias, options.scopedVars);
var history;
if ((from < useTrendsFrom) && self.trends) {
var points = target.downsampleFunction ? target.downsampleFunction.value : "avg";
history = self.zabbixAPI.getTrends(items, from, to)
.then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale, points));
} else {
history = self.zabbixAPI.getHistory(items, from, to)
.then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale));
}
return history.then(function (timeseries) {
var timeseries_data = _.flatten(timeseries);
return _.map(timeseries_data, function (timeseries) {
// Series downsampling
if (timeseries.datapoints.length > options.maxDataPoints) {
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
var downsampleFunc = target.downsampleFunction ? target.downsampleFunction.value : "avg";
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
}
return timeseries;
});
});
}
});
}
// Query text data
else if (target.mode === 2) {
// Find items by item names and perform queries
return this.zabbixAPI.itemFindQuery(groups, hosts, apps, "text")
.then(function (items) {
items = _.filter(items, function (item) {
return _.contains(itemnames, zabbixHelperSrv.expandItemName(item));
});
return self.zabbixAPI.getHistory(items, from, to).then(function(history) {
return {
target: target.item.name,
datapoints: _.map(history, function (p) {
return [p.value, p.clock * 1000];
})
};
});
});
}
}
// IT services mode
else if (target.mode === 1) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
} else {
return this.zabbixAPI.getSLA(target.itservice.serviceid, from, to)
.then(_.bind(zabbixHelperSrv.handleSLAResponse, zabbixHelperSrv, target.itservice, target.slaProperty));
}
}
}, this);
return $q.all(_.flatten(promises)).then(function (results) {
var timeseries_data = _.flatten(results);
return { data: timeseries_data };
});
};
////////////////
// Templating //
////////////////
/**
* Find metrics from templated request.
*
* @param {string} query Query from Templating
* @return {string} Metric name - group, host, app or item or list
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
ZabbixAPIDatasource.prototype.metricFindQuery = function (query) {
// Split query. Query structure:
// group.host.app.item
var parts = [];
_.each(query.split('.'), function (part) {
part = templateSrv.replace(part);
if (part[0] === '{') {
// Convert multiple mettrics to array
// "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
parts.push(zabbixHelperSrv.splitMetrics(part));
} else {
parts.push(part);
}
});
var template = _.object(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
return this.zabbixAPI.itemFindQuery(template.group, template.host, template.app)
.then(function (result) {
return _.map(result, function (item) {
var itemname = zabbixHelperSrv.expandItemName(item);
return {
text: itemname,
expandable: false
};
});
});
}
// Get applications
else if (parts.length === 3) {
return this.zabbixAPI.appFindQuery(template.host, template.group).then(function (result) {
return _.map(result, function (app) {
return {
text: app.name,
expandable: false
};
});
});
}
// Get hosts
else if (parts.length === 2) {
return this.zabbixAPI.hostFindQuery(template.group).then(function (result) {
return _.map(result, function (host) {
return {
text: host.name,
expandable: false
};
});
});
}
// Get groups
else if (parts.length === 1) {
return this.zabbixAPI.getGroupByName(template.group).then(function (result) {
return _.map(result, function (hostgroup) {
return {
text: hostgroup.name,
expandable: false
};
});
});
}
// Return empty object for invalid request
else {
var d = $q.defer();
d.resolve([]);
return d.promise;
}
};
/////////////////
// Annotations //
/////////////////
ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
var from = Math.ceil(dateMath.parse(rangeUnparsed.from) / 1000);
var to = Math.ceil(dateMath.parse(rangeUnparsed.to) / 1000);
var self = this;
var params = {
output: ['triggerid', 'description'],
search: {
'description': annotation.trigger
},
searchWildcardsEnabled: true,
expandDescription: true
};
if (annotation.host) {
params.host = templateSrv.replace(annotation.host);
}
else if (annotation.group) {
params.group = templateSrv.replace(annotation.group);
}
return this.zabbixAPI.performZabbixAPIRequest('trigger.get', params)
.then(function (result) {
if(result) {
var objects = _.indexBy(result, 'triggerid');
var params = {
output: 'extend',
time_from: from,
time_till: to,
objectids: _.keys(objects),
select_acknowledges: 'extend'
};
// Show problem events only
if (!annotation.showOkEvents) {
params.value = 1;
}
return self.zabbixAPI.performZabbixAPIRequest('event.get', params)
.then(function (result) {
var events = [];
_.each(result, function(e) {
var formatted_acknowledges = zabbixHelperSrv.formatAcknowledges(e.acknowledges);
events.push({
annotation: annotation,
time: e.clock * 1000,
title: Number(e.value) ? 'Problem' : 'OK',
text: objects[e.objectid].description + formatted_acknowledges
});
});
return events;
});
} else {
return [];
}
});
};
return ZabbixAPIDatasource;
});
});

View File

@@ -0,0 +1,21 @@
define([
'angular'
],
function (angular) {
'use strict';
var module = angular.module('grafana.directives');
module.directive('metricQueryEditorZabbix', function() {
return {controller: 'ZabbixAPIQueryCtrl', templateUrl: 'app/plugins/datasource/zabbix/partials/query.editor.html'};
});
module.directive('metricQueryOptionsZabbix', function() {
return {templateUrl: 'app/plugins/datasource/zabbix/partials/query.options.html'};
});
module.directive('annotationsQueryEditorZabbix', function() {
return {templateUrl: 'app/plugins/datasource/zabbix/partials/annotations.editor.html'};
});
});

View File

@@ -0,0 +1,311 @@
define([
'angular',
'lodash'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.service('zabbixHelperSrv', function($q) {
var self = this;
/**
* Convert Zabbix API history.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param alias
* @param scale
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleHistoryResponse = function(items, alias, scale, history) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Group items and history by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid];
return {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.expandItemName(item)),
datapoints: _.map(history, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Convert Zabbix API trends.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param alias
* @param scale
* @param {string} points Point value to return: min, max or avg
* @param {Array} trends Array of Zabbix Trends
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleTrendResponse = function (items, alias, scale, points, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_trends = _.groupBy(trends, 'itemid');
var self = this;
return $q.when(_.map(grouped_trends, function (trends, itemid) {
var item = indexed_items[itemid];
return {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.expandItemName(item)),
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value;
if (points === "min") {
value = Number(p.value_min);
}
else if (points === "max") {
value = Number(p.value_max);
}
else {
value = Number(p.value_avg);
}
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Convert Zabbix API service.getsla response to Grafana format
*
* @param itservice
* @param slaProperty
* @param slaObject
* @returns {{target: *, datapoints: *[]}}
*/
this.handleSLAResponse = function (itservice, slaProperty, slaObject) {
var targetSLA = slaObject[itservice.serviceid].sla[0];
if (slaProperty.property === 'status') {
var targetStatus = slaObject[itservice.serviceid].status;
return {
target: itservice.name + ' ' + slaProperty.name,
datapoints: [
[targetStatus, targetSLA.to * 1000]
]
};
} else {
return {
target: itservice.name + ' ' + slaProperty.name,
datapoints: [
[targetSLA[slaProperty.property], targetSLA.from * 1000],
[targetSLA[slaProperty.property], targetSLA.to * 1000]
]
};
}
};
/**
* Expand item parameters, for example:
* CPU $2 time ($3) --> CPU system time (avg1)
*
* @param item: zabbix api item object
* @return {string} expanded item name (string)
*/
this.expandItemName = function(item) {
var name = item.name;
var key = item.key_;
// extract params from key:
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
// replace item parameters
for (var i = key_params.length; i >= 1; i--) {
name = name.replace('$' + i, key_params[i - 1]);
}
return name;
};
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
this.splitMetrics = function(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern);
};
/**
* Convert Date object to local time in format
* YYYY-MM-DD HH:mm:ss
*
* @param {Date} date Date object
* @return {string} formatted local time YYYY-MM-DD HH:mm:ss
*/
this.getShortTime = function(date) {
var MM = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return date.getFullYear() + '-' + MM + '-' + DD + ' ' + HH + ':' + mm + ':' + ss;
};
/**
* Format acknowledges.
*
* @param {array} acknowledges array of Zabbix acknowledge objects
* @return {string} HTML-formatted table
*/
this.formatAcknowledges = function(acknowledges) {
if (acknowledges.length) {
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
+ '<td><b>User</b></td><td><b>Comments</b></td></tr>';
_.each(_.map(acknowledges, function (ack) {
var time = new Date(ack.clock * 1000);
return '<tr><td><i>' + self.getShortTime(time) + '</i></td><td>' + ack.alias
+ ' (' + ack.name + ' ' + ack.surname + ')' + '</td><td>' + ack.message + '</td></tr>';
}), function (ack) {
formatted_acknowledges = formatted_acknowledges.concat(ack);
});
formatted_acknowledges = formatted_acknowledges.concat('</table>');
return formatted_acknowledges;
} else {
return '';
}
};
/**
* Downsample datapoints series
*
* @param {Object[]} datapoints [[<value>, <unixtime>], ...]
* @param {integer} time_to Panel time to
* @param {integer} ms_interval Interval in milliseconds for grouping datapoints
* @param {string} func Value to return: min, max or avg
* @return {Object[]} [[<value>, <unixtime>], ...]
*/
this.downsampleSeries = function(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
var frame = [];
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
}
else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
}
else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
};
/**
* Convert event age from Unix format (milliseconds sins 1970)
* to Zabbix format (like at Last 20 issues panel).
* @param {Date} AgeUnix time in Unix format
* @return {string} Formatted time
*/
this.toZabbixAgeFormat = function(ageUnix) {
var age = new Date(+ageUnix);
var ageZabbix = age.getSeconds() + 's';
if (age.getMinutes()) {
ageZabbix = age.getMinutes() + 'm ' + ageZabbix;
}
if (age.getHours()) {
ageZabbix = age.getHours() + 'h ' + ageZabbix;
}
if (age.getDate() - 1) {
ageZabbix = age.getDate() - 1 + 'd ' + ageZabbix;
}
if (age.getMonth()) {
ageZabbix = age.getMonth() + 'M ' + ageZabbix;
}
if (age.getYear() - 70) {
ageZabbix = age.getYear() -70 + 'y ' + ageZabbix;
}
return ageZabbix;
};
});
});

View File

@@ -0,0 +1,36 @@
<div class="editor-row">
<div class="section">
<h5>Zabbix trigger
</h5>
</div>
</div>
<div class="editor-row">
<div class="editor-option">
<label class="small">Group</label>
<input type="text" class="input-medium" ng-model='currentAnnotation.group'></input>
</div>
<div class="editor-option">
<label class="small">Host</label>
<input type="text" class="input-medium" ng-model='currentAnnotation.host'></input>
</div>
<div class="editor-option">
<label class="small">Trigger
<tip>Trigger name for search. Wildcards are supports. Examples: Lack of free swap space, Lack of*.
</tip>
</label>
<input type="text" style="width: 25em" ng-model='currentAnnotation.trigger' placeholder="Trigger name"></input>
</div>
</div>
<div class="editor-row">
<div class="section">
<h5>Options</h5>
<input type="checkbox" class="cr1" id="currentAnnotation.showOkEvents"
ng-model="currentAnnotation.showOkEvents"
ng-checked="currentAnnotation.showOkEvents">
<label for="currentAnnotation.showOkEvents" class="cr1">Show OK events
<tip>Show events, generated when trigger release to OK state</tip>
</label>
</div>
</div>

View File

@@ -0,0 +1,63 @@
<div ng-include="httpConfigPartialSrc"></div>
<br>
<h5>Zabbix API details</h5>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
User
</li>
<li>
<input type="text" class="tight-form-input input-large"
ng-model='current.jsonData.username'
placeholder="">
</li>
<li class="tight-form-item">
Password
</li>
<li>
<input type="password" class="tight-form-input input-large"
ng-model='current.jsonData.password'
placeholder="">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
Trends
</li>
<li class="tight-form-item">
Enable&nbsp;
<input class="cr1" id="current.jsonData.trends" type="checkbox"
ng-model="current.jsonData.trends"
ng-checked="current.jsonData.trends">
<label for="current.jsonData.trends" class="cr1"></label>
</li>
<li class="tight-form-item" ng-if="current.jsonData.trends">
Use trends from
</li>
<li ng-if="current.jsonData.trends">
<input type="text" class="tight-form-input input-small"
ng-model='current.jsonData.trendsFrom'
placeholder="7d">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
Metrics limit
</li>
<li>
<input type="text" class="tight-form-input input-small"
ng-model='current.jsonData.limitMetrics'
placeholder="100">
</li>
</ul>
<div class="clearfix"></div>
</div>

View File

@@ -0,0 +1,227 @@
<div class="tight-form">
<ul class="tight-form-list pull-right">
<li class="tight-form-item small" ng-show="target.datasource">
<em>{{target.datasource}}</em>
</li>
<li class="tight-form-item">
<div class="dropdown">
<a class="pointer dropdown-toggle"
data-toggle="dropdown"
tabindex="1">
<i class="fa fa-bars"></i>
</a>
<ul class="dropdown-menu pull-right" role="menu">
<!-- Switch editor mode -->
<li role="menuitem" ng-show="target.mode">
<a class="pointer" tabindex="1"
ng-click="switchEditorMode(0)">Numeric metrics</a>
</li>
<li role="menuitem" ng-show="target.mode != 1">
<a class="pointer" tabindex="1"
ng-click="switchEditorMode(1)">IT services</a>
</li>
<li role="menuitem" ng-show="target.mode != 2">
<a class="pointer" tabindex="1"
ng-click="switchEditorMode(2)">Text metrics</a>
</li>
<li class="divider" role="menuitem"></li>
<li role="menuitem"><a tabindex="1" ng-click="duplicate()">Duplicate</a></li>
<li role="menuitem"><a tabindex="1" ng-click="moveMetricQuery($index, $index-1)">Move up</a></li>
<li role="menuitem"><a tabindex="1" ng-click="moveMetricQuery($index, $index+1)">Move down</a></li>
</ul>
</div>
</li>
<li class="tight-form-item last">
<a class="pointer" tabindex="1" ng-click="removeDataQuery(target)">
<i class="fa fa-remove"></i>
</a>
</li>
</ul>
<ul class="tight-form-list">
<li class="tight-form-item" style="min-width: 15px; text-align: center">
{{target.refId}}
</li>
<li>
<a class="tight-form-item"
ng-click="target.hide = !target.hide; get_data();"
role="menuitem">
<i class="fa fa-eye"></i>
</a>
</li>
</ul>
<!-- IT Service editor -->
<ul class="tight-form-list" role="menu" ng-show="target.mode == 1">
<li class="tight-form-item input-small">IT Service</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectITService()"
ng-model="target.itservice"
bs-tooltip="target.itservice.name.length > 25 ? target.itservice.name : ''"
ng-options="itservice.name for itservice in itserviceList track by itservice.name">
<option value="">-- Select IT service --</option>
</select>
</li>
<li class="tight-form-item input-medium">IT service property</li>
<li>
<select class="tight-form-input input-medium"
ng-change="selectITService()"
ng-model="target.slaProperty"
ng-options="slaProperty.name for slaProperty in slaPropertyList track by slaProperty.name">
<option value="">-- Property --</option>
</select>
</li>
</ul>
<ul class="tight-form-list" role="menu" ng-hide="target.mode == 1">
<!-- Alias -->
<li>
<input type="text"
class="tight-form-input input-medium"
ng-model="target.alias"
spellcheck='false'
placeholder="Alias"
ng-blur="targetBlur()">
</li>
<!-- Select Host Group -->
<li class="tight-form-item input-small" style="width: 5em">Group</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectHostGroup()"
ng-model="target.group"
bs-tooltip="target.group.name.length > 25 ? target.group.name : ''"
ng-options="group.visible_name ? group.visible_name : group.name for group in metric.groupList track by group.name">
<option value="">-- Select host group --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="fa fa-warning"></i>
</a>
</li>
<!-- Select Host -->
<li class="tight-form-item input-small" style="width: 3em">Host</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectHost()"
ng-model="target.host"
bs-tooltip="target.host.name.length > 25 ? target.host.name : ''"
ng-options="host.visible_name ? host.visible_name : host.name for host in metric.hostList track by host.name">
<option value="">-- Select host --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
<!-- Host filter -->
<li class="tight-form-item" ng-hide="target.mode == 2">
Filter
<i class="fa fa-question-circle"
bs-tooltip="'Filtering hosts by regex. Select All in items and specify regex for host names.'"></i>
</li>
<li ng-hide="target.mode == 2">
<input type="text"
class="tight-form-input input-large"
ng-model="target.hostFilter"
spellcheck='false'
placeholder="Host filter (regex)"
ng-blur="targetBlur()">
</li>
<!-- Downsampling function -->
<li class="tight-form-item input-medium" ng-hide="target.mode == 2">
Downsampling
</li>
<li ng-hide="target.mode == 2">
<select class="tight-form-input input-small"
ng-change="targetBlur()"
ng-model="target.downsampleFunction"
bs-tooltip="'Downsampling function'"
ng-options="func.name for func in downsampleFunctionList track by func.name">
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form" ng-hide="target.mode == 1">
<ul class="tight-form-list" role="menu">
<li class="tight-form-item" style="min-width: 15px; text-align: center">&nbsp</li>
<li class="tight-form-item">
<i class="fa fa-eye invisible"></i>
</li>
<li class="tight-form-item" style="width: 135px">&nbsp</li>
<!-- Select Application -->
<li class="tight-form-item input-small" style="width: 5em">Application</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectApplication()"
ng-model="target.application"
bs-tooltip="target.application.name.length > 15 ? target.application.name : ''"
ng-options="app.visible_name ? app.visible_name : app.name for app in metric.applicationList track by app.name">
<option value="">-- Select application --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
<!-- Select Item -->
<li class="tight-form-item input-small" style="width: 3em">Item</li>
<li>
<select class="tight-form-input input-large"
ng-change="selectItem()"
ng-model="target.item"
bs-tooltip="target.item.name.length > 25 ? target.item.name : ''"
ng-options="item.name for item in metric.itemList track by item.name">
<option value="">-- Select item --</option>
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
<!-- Item filter -->
<li class="tight-form-item" ng-hide="target.mode == 2">
Filter
<i class="fa fa-question-circle"
bs-tooltip="'Filtering items by regex. Select All in items and specify regex for item names.'"></i>
</li>
<li ng-hide="target.mode == 2">
<input type="text"
class="tight-form-input input-large"
ng-model="target.itemFilter"
spellcheck='false'
placeholder="Item filter (regex)"
ng-blur="targetBlur()">
</li>
<!-- Scale -->
<li class="tight-form-item" ng-hide="target.mode == 2">
Scale
<i class="fa fa-question-circle"
bs-tooltip="'Set a custom multiplier for series values, for example -1 to invert series'"></i>
</li>
<li ng-hide="target.mode == 2">
<input type="text"
class="tight-form-input input-small"
ng-model="target.scale"
spellcheck='false'
placeholder="1"
ng-blur="targetBlur()">
</li>
</ul>
<div class="clearfix"></div>
</div>

View File

@@ -0,0 +1,84 @@
<section class="grafana-metric-options">
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item tight-form-item-icon">
<i class="fa fa-wrench"></i>
</li>
<li class="tight-form-item">
Max data points
</li>
<li>
<input type="text"
class="input-mini tight-form-input"
ng-model="panel.maxDataPoints"
bs-tooltip="'Override max data points, automatically set to graph width in pixels.'"
data-placement="right"
ng-model-onblur ng-change="get_data()"
spellcheck='false'
placeholder="auto">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item tight-form-item-icon">
<i class="fa fa-info-circle"></i>
</li>
<li class="tight-form-item">
<a ng-click="toggleEditorHelp(1)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
Max data points
</a>
</li>
<li class="tight-form-item">
<a ng-click="toggleEditorHelp(2)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
IT services
</a>
</li>
<li class="tight-form-item">
<a ng-click="toggleEditorHelp(3)" bs-tooltip="'click to show helpful info'" data-placement="bottom">
IT service property
</a>
</li>
</ul>
<div class="clearfix"></div>
</div>
</section>
<div class="editor-row">
<div class="pull-left" style="margin-top: 30px;">
<div class="grafana-info-box span8" ng-if="editorHelpIndex === 1">
<h5>Max data points</h5>
<ul>
<li>Grafana-Zabbix plugin uses maxDataPoints parameter to consolidate the real number of values down to this
number
</li>
<li>If there are more real values, then by default they will be consolidated using averages</li>
<li>This could hide real peaks and max values in your series</li>
<li>Point consolidation will effect series legend values (min,max,total,current)</li>
<li>If you override maxDataPoint and set a high value performance can be severely effected</li>
</ul>
</div>
<div class="grafana-info-box span8" ng-if="editorHelpIndex === 2">
<h5>IT services</h5>
<ul>
<li>Select "IT services" in targets menu to activate IT services mode.</li>
</ul>
</div>
<div class="grafana-info-box span8" ng-if="editorHelpIndex === 3">
<h5>IT service property</h5>
<ul>
<li>Zabbix returns the following availability information about IT service</li>
<li>Status - current status of the IT service</li>
<li>SLA - SLA for the given time interval</li>
<li>OK time - time the service was in OK state, in seconds</li>
<li>Problem time - time the service was in problem state, in seconds</li>
<li>Down time - time the service was in scheduled downtime, in seconds</li>
</ul>
</div>
</div>
</div>

View File

@@ -0,0 +1,16 @@
{
"pluginType": "datasource",
"name": "Zabbix",
"type": "zabbix",
"serviceName": "ZabbixAPIDatasource",
"module": "app/plugins/datasource/zabbix/datasource",
"partials": {
"config": "app/plugins/datasource/zabbix/partials/config.html"
},
"metrics": true,
"annotations": true
}

View File

@@ -0,0 +1,273 @@
define([
'angular',
'lodash',
'./helperFunctions'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv) {
$scope.init = function () {
$scope.targetLetters = targetLetters;
if (!$scope.target.mode || $scope.target.mode !== 1) {
$scope.downsampleFunctionList = [
{name: "avg", value: "avg"},
{name: "min", value: "min"},
{name: "max", value: "max"}
];
// Set avg by default
if (!$scope.target.downsampleFunction) {
$scope.target.downsampleFunction = $scope.downsampleFunctionList[0];
}
if (!$scope.metric) {
$scope.metric = {
hostGroupList: [],
hostList: [{name: '*', visible_name: 'All'}],
applicationList: [{name: '*', visible_name: 'All'}],
itemList: [{name: 'All'}]
};
}
// Update host group, host, application and item lists
$scope.updateGroupList();
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
setItemAlias();
}
else if ($scope.target.mode === 1) {
$scope.slaPropertyList = [
{name: "Status", property: "status"},
{name: "SLA", property: "sla"},
{name: "OK time", property: "okTime"},
{name: "Problem time", property: "problemTime"},
{name: "Down time", property: "downtimeTime"}
];
$scope.itserviceList = [{name: "test"}];
$scope.updateITServiceList();
}
$scope.target.errors = validateTarget($scope.target);
};
/**
* Switch query editor to specified mode.
* Modes:
* 0 - items
* 1 - IT services
*/
$scope.switchEditorMode = function (mode) {
$scope.target.mode = mode;
$scope.init();
};
/**
* Take alias from item name by default
*/
function setItemAlias() {
if (!$scope.target.alias && $scope.target.item) {
$scope.target.alias = $scope.target.item.name;
}
}
$scope.targetBlur = function () {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when IT service is selected.
*/
$scope.selectITService = function () {
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host group selected
*/
$scope.selectHostGroup = function () {
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host selected
*/
$scope.selectHost = function () {
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when application selected
*/
$scope.selectApplication = function () {
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when item selected
*/
$scope.selectItem = function () {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
$scope.duplicate = function () {
var clone = angular.copy($scope.target);
$scope.panel.targets.push(clone);
};
$scope.moveMetricQuery = function (fromIndex, toIndex) {
_.move($scope.panel.targets, fromIndex, toIndex);
};
//////////////////////////////
// SUGGESTION QUERIES
//////////////////////////////
/**
* Update list of IT services
*/
$scope.updateITServiceList = function () {
$scope.datasource.zabbixAPI.getITService().then(function (iteservices) {
$scope.itserviceList = [];
$scope.itserviceList = $scope.itserviceList.concat(iteservices);
});
};
/**
* Update list of host groups
*/
$scope.updateGroupList = function () {
$scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
$scope.metric.groupList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.groupList);
$scope.metric.groupList = $scope.metric.groupList.concat(groups);
});
};
/**
* Update list of hosts
*/
$scope.updateHostList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
if (groups) {
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.hostList);
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
});
}
};
/**
* Update list of host applications
*/
$scope.updateAppList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
if (groups && hosts) {
$scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) {
apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname};
});
$scope.metric.applicationList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.applicationList);
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
});
}
};
/**
* Update list of items
*/
$scope.updateItemList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
var apps = $scope.target.application ?
zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
var itemtype = $scope.target.mode === 2 ? "text" : "numeric";
if (groups && hosts && apps) {
$scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps, itemtype).then(function (items) {
// Show only unique item names
var uniq_items = _.map(_.uniq(items, function (item) {
return zabbixHelperSrv.expandItemName(item);
}), function (item) {
return {name: zabbixHelperSrv.expandItemName(item)};
});
$scope.metric.itemList = [{name: 'All'}];
addTemplatedVariables($scope.metric.itemList);
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
});
}
};
/**
* Add templated variables to list of available metrics
*
* @param {Array} metricList List of metrics which variables add to
*/
function addTemplatedVariables(metricList) {
_.each(templateSrv.variables, function (variable) {
metricList.push({
name: '$' + variable.name,
templated: true
});
});
}
//////////////////////////////
// VALIDATION
//////////////////////////////
function validateTarget(target) {
var errs = {};
if (!target) {
errs = 'Not defined';
}
return errs;
}
$scope.init();
});
});

View File

@@ -0,0 +1,584 @@
define([
'angular',
'lodash'
],
function (angular, _) {
'use strict';
var module = angular.module('grafana.services');
module.factory('ZabbixAPI', function($q, backendSrv) {
function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) {
// Initialize API parameters.
this.url = api_url;
this.username = username;
this.password = password;
this.basicAuth = basicAuth;
this.withCredentials = withCredentials;
}
var p = ZabbixAPI.prototype;
//////////////////
// Core methods //
//////////////////
/**
* Request data from Zabbix API
*
* @param {string} method Zabbix API method name
* @param {object} params method params
* @return {object} data.result field or []
*/
p.performZabbixAPIRequest = function(method, params) {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: this.url,
data: {
jsonrpc: '2.0',
method: method,
params: params,
auth: this.auth,
id: 1
}
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers.Authorization = this.basicAuth;
}
var self = this;
return backendSrv.datasourceRequest(options).then(function (response) {
if (!response.data) {
return [];
}
// Handle Zabbix API errors
else if (response.data.error) {
// Handle auth errors
if (response.data.error.data === "Session terminated, re-login, please." ||
response.data.error.data === "Not authorised." ||
response.data.error.data === "Not authorized") {
return self.performZabbixAPILogin().then(function (response) {
self.auth = response;
return self.performZabbixAPIRequest(method, params);
});
}
}
return response.data.result;
});
};
/**
* Get authentication token.
*
* @return {string} auth token
*/
p.performZabbixAPILogin = function() {
var options = {
url : this.url,
method : 'POST',
data: {
jsonrpc: '2.0',
method: 'user.login',
params: {
user: this.username,
password: this.password
},
auth: null,
id: 1
}
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers = options.headers || {};
options.headers.Authorization = this.basicAuth;
}
return backendSrv.datasourceRequest(options).then(function (result) {
if (!result.data) {
return null;
}
return result.data.result;
});
};
/////////////////////////
// API method wrappers //
/////////////////////////
/**
* Request version of the Zabbix API.
*
* @return {string} Zabbix API version
*/
p.getZabbixAPIVersion = function() {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: this.url,
data: {
jsonrpc: '2.0',
method: 'apiinfo.version',
params: [],
id: 1
}
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers = options.headers || {};
options.headers.Authorization = this.basicAuth;
}
return backendSrv.datasourceRequest(options).then(function (result) {
if (!result.data) {
return null;
}
return result.data.result;
});
};
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds
* @param {Number} end Time in seconds
* @return {Array} Array of Zabbix history objects
*/
p.getHistory = function(items, start, end) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('history.get', params);
}, this)).then(function (results) {
return _.flatten(results);
});
};
/**
* Perform trends query from Zabbix API
* Use trends api extension from ZBXNEXT-1193 patch.
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} start Time in seconds
* @param {Number} end Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
p.getTrends = function(items, start, end) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return $q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: start
};
// Relative queries (e.g. last hour) don't include an end time
if (end) {
params.time_till = end;
}
return this.performZabbixAPIRequest('trend.get', params);
}, this)).then(function (results) {
return _.flatten(results);
});
};
/**
* Get the list of host groups
*
* @return {array} array of Zabbix hostgroup objects
*/
p.performHostGroupSuggestQuery = function() {
var params = {
output: ['name'],
sortfield: 'name',
// Return only host groups that contain hosts
real_hosts: true,
// Return only host groups that contain monitored hosts.
monitored_hosts: true
};
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Get the list of hosts
*
* @param {string|string[]} groupids
* @return {Object} array of Zabbix host objects
*/
p.performHostSuggestQuery = function(groupids) {
var params = {
output: ['name', 'host'],
sortfield: 'name',
// Return only hosts that have items with numeric type of information.
with_simple_graph_items: true,
// Return only monitored hosts.
monitored_hosts: true
};
// Return only hosts in given group
if (groupids) {
params.groupids = groupids;
}
return this.performZabbixAPIRequest('host.get', params);
};
/**
* Get the list of applications
*
* @param {array} hostids
* @param {array} groupids
* @return {Object} array of Zabbix application objects
*/
p.performAppSuggestQuery = function(hostids, /* optional */ groupids) {
var params = {
output: ['name'],
sortfield: 'name'
};
if (hostids) {
params.hostids = hostids;
}
else if (groupids) {
params.groupids = groupids;
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Items request
*
* @param {string|string[]} hostids ///////////////////////////
* @param {string|string[]} applicationids // Zabbix API parameters //
* @param {string|string[]} groupids ///////////////////////////
* @return {string|string[]} Array of Zabbix API item objects
*/
p.performItemSuggestQuery = function(hostids, applicationids, groupids, itemtype) {
var params = {
output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name',
//Include web items in the result
webitems: true,
// Return only numeric items
filter: {
value_type: [0, 3]
},
// Return only enabled items
monitored: true,
searchByAny: true
};
if (itemtype === "text") {
params.filter.value_type = [1, 2, 4];
}
// Filter by hosts or by groups
if (hostids) {
params.hostids = hostids;
} else if (groupids) {
params.groupids = groupids;
}
// If application selected return only relative items
if (applicationids) {
params.applicationids = applicationids;
}
// Return host property for multiple hosts
if (!hostids || (_.isArray(hostids) && hostids.length > 1)) {
params.selectHosts = ['name'];
}
return this.performZabbixAPIRequest('item.get', params);
};
/**
* Get groups by names
*
* @param {string or array} group group names
* @return {array} array of Zabbix API hostgroup objects
*/
p.getGroupByName = function (group) {
var params = {
output: ['name']
};
if (group && group[0] !== '*') {
params.filter = {
name: group
};
}
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Search group by name.
*
* @param {string} group group name
* @return {array} groups
*/
p.searchGroup = function (group) {
var params = {
output: ['name'],
search: {
name: group
},
searchWildcardsEnabled: true
};
return this.performZabbixAPIRequest('hostgroup.get', params);
};
/**
* Get hosts by names
*
* @param {string or array} hostnames hosts names
* @return {array} array of Zabbix API host objects
*/
p.getHostByName = function (hostnames) {
var params = {
output: ['host', 'name']
};
if (hostnames && hostnames[0] !== '*') {
params.filter = {
name: hostnames
};
}
return this.performZabbixAPIRequest('host.get', params);
};
/**
* Get applications by names
*
* @param {string or array} application applications names
* @return {array} array of Zabbix API application objects
*/
p.getAppByName = function (application) {
var params = {
output: ['name']
};
if (application && application[0] !== '*') {
params.filter = {
name: application
};
}
return this.performZabbixAPIRequest('application.get', params);
};
/**
* Get items belongs to passed groups, hosts and
* applications
*
* @param {string or array} groups
* @param {string or array} hosts
* @param {string or array} apps
* @return {array} array of Zabbix API item objects
*/
p.itemFindQuery = function(groups, hosts, apps, itemtype) {
var promises = [];
// Get hostids from names
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.getGroupByName(groups));
}
// Get applicationids from names
if (apps && apps[0] !== '*') {
promises.push(this.getAppByName(apps));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
var applicationids;
if (groups) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
if (apps && apps[0] !== '*') {
applicationids = _.map(_.filter(results, function (object) {
return object.applicationid;
}), 'applicationid');
}
return self.performItemSuggestQuery(hostids, applicationids, groupids, itemtype);
});
};
/**
* Find applications belongs to passed groups and hosts
*
* @param {string or array} hosts
* @param {string or array} groups
* @return {array} array of Zabbix API application objects
*/
p.appFindQuery = function(hosts, groups) {
var promises = [];
// Get hostids from names
if (hosts && hosts[0] !== '*') {
promises.push(this.getHostByName(hosts));
}
// Get groupids from names
else if (groups) {
promises.push(this.getGroupByName(groups));
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids;
var hostids;
if (groups) {
groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
}
if (hosts && hosts[0] !== '*') {
hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
}
return self.performAppSuggestQuery(hostids, groupids);
});
};
/**
* Find hosts belongs to passed groups
*
* @param {string or array} groups
* @return {array} array of Zabbix API host objects
*/
p.hostFindQuery = function(groups) {
var self = this;
return this.getGroupByName(groups).then(function (results) {
results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
return self.performHostSuggestQuery(groupids);
});
};
p.getITService = function(/* optional */ serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
};
return this.performZabbixAPIRequest('service.get', params);
};
p.getSLA = function(serviceids, from, to) {
var params = {
serviceids: serviceids,
intervals: [{
from: from,
to: to
}]
};
return this.performZabbixAPIRequest('service.getsla', params);
};
p.getTriggers = function(limit, sortfield, groupids, hostids, applicationids, name) {
var params = {
output: 'extend',
expandDescription: true,
expandData: true,
monitored: true,
//only_true: true,
filter: {
value: 1
},
search : {
description: name
},
searchWildcardsEnabled: false,
groupids: groupids,
hostids: hostids,
applicationids: applicationids,
limit: limit,
sortfield: 'lastchange',
sortorder: 'DESC'
};
if (sortfield) {
params.sortfield = sortfield;
}
return this.performZabbixAPIRequest('trigger.get', params);
};
p.getAcknowledges = function(triggerids, from) {
var params = {
output: 'extend',
objectids: triggerids,
acknowledged: true,
select_acknowledges: 'extend',
sortfield: 'clock',
sortorder: 'DESC',
time_from: from
};
return this.performZabbixAPIRequest('event.get', params)
.then(function (events) {
return _.flatten(_.map(events, 'acknowledges'));
});
};
return ZabbixAPI;
});
});