Merge branch 'master' into docs

This commit is contained in:
Alexander Zobnin
2016-11-16 23:15:35 +03:00
34 changed files with 2414 additions and 1558 deletions

15
.github/CONTRIBUTING.md vendored Normal file
View File

@@ -0,0 +1,15 @@
## How to contribute to Grafana-Zabbix
#### **Did you find a bug?**
* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/alexanderzobnin/grafana-zabbix/issues).
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/alexanderzobnin/grafana-zabbix/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible.
#### **Did you write a patch that fixes a bug?**
* Open a new GitHub pull request with the patch.
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
Thanks! :heart: :heart: :heart:

17
.github/ISSUE_TEMPLATE.md vendored Normal file
View File

@@ -0,0 +1,17 @@
#### I'm submitting a ...
- [ ] Bug report
- [ ] Feature request
- [ ] Question / Support request
#### For bug report please include this information:
- What Grafana version are you using?
- What Zabbix version are you using?
- What zabbix plugin version are you using?
- What OS are you running grafana on?
- What did you do?
- What was the expected result?
- What happened instead?
**IMPORTANT** If it relates to metric data visualization would be great to get:
- An image or text representation of your metric query
- The raw query and response for the network request (check this in chrome dev tools network tab, here you can see metric requests and other request, please include the request body and request response)

View File

@@ -34,6 +34,13 @@
"define": true,
"require": true,
"Chromath": false,
"setImmediate": true
"setImmediate": true,
"expect": true,
"it": true,
"describe": true,
"sinon": true,
"module": true,
"beforeEach": true,
"inject": true
}
}

View File

@@ -1 +0,0 @@
# Contributing to Grafana-Zabbix

View File

@@ -33,18 +33,20 @@ module.exports = function(grunt) {
watch: {
rebuild_all: {
files: ['src/**/*', 'plugin.json'],
tasks: ['default'],
tasks: ['watchTask'],
options: {spawn: false}
},
},
babel: {
options: {
sourceMap: true,
presets: ["es2015"],
plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"],
presets: ["es2015"]
},
dist: {
options: {
sourceMap: true,
plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"]
},
files: [{
cwd: 'src',
expand: true,
@@ -57,6 +59,34 @@ module.exports = function(grunt) {
dest: 'dist/'
}]
},
distTestNoSystemJs: {
files: [{
cwd: 'src',
expand: true,
src: ['**/*.js'],
dest: 'dist/test'
}]
},
distTestsSpecsNoSystemJs: {
files: [{
expand: true,
cwd: 'specs',
src: ['**/*.js'],
dest: 'dist/test/specs'
}]
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: [
'dist/test/datasource-zabbix/specs/test-main.js',
'dist/test/datasource-zabbix/specs/*_specs.js'
]
}
},
sass: {
@@ -68,6 +98,29 @@ module.exports = function(grunt) {
'dist/panel-triggers/css/panel_triggers.css' : 'src/panel-triggers/sass/panel_triggers.scss',
}
}
},
jshint: {
source: {
files: {
src: ['src/**/*.js'],
}
},
options: {
jshintrc: true,
reporter: require('jshint-stylish'),
ignores: [
'node_modules/*',
'dist/*',
]
}
},
jscs: {
src: ['src/**/*.js'],
options: {
config: ".jscs.json",
},
}
});
@@ -76,7 +129,20 @@ module.exports = function(grunt) {
'clean',
'copy:src_to_dist',
'copy:pluginDef',
'jshint',
'jscs',
'sass',
'babel',
'sass'
'mochaTest'
]);
grunt.registerTask('watchTask', [
'clean',
'copy:src_to_dist',
'copy:pluginDef',
'jshint',
'jscs',
'sass',
'babel:dist'
]);
};

201
LICENSE Normal file
View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2015-2016 Alexander Zobnin <alexanderzobnin@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -1,13 +0,0 @@
Copyright 2015-2016 Alexander Zobnin <alexanderzobnin@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -27,6 +27,7 @@ Or see more installation options in [docs](http://docs.grafana-zabbix.org/instal
- [About](http://docs.grafana-zabbix.org)
- [Installation](http://docs.grafana-zabbix.org/installation)
- [Getting Started](http://docs.grafana-zabbix.org/guides/gettingstarted)
- [Templating](http://docs.grafana-zabbix.org/guides/templating)
## Dockerized Grafana with Zabbix datasource

View File

@@ -22,7 +22,7 @@ a number of ways to get help:
- [Gitter room](https://gitter.im/alexanderzobnin/grafana-zabbix)
- [Twitter](https://twitter.com/AlexanderZobnin)
Or you can just send me [email](mailto:alexanderzobnin@gmail.com).
Or you can send me [email](mailto:alexanderzobnin@gmail.com).
## Support Project
I develop this project in my free time, but if you really find it helpful and promising, you can
@@ -34,5 +34,5 @@ Triggers panel was sponsored by [Core IT Project](http://coreit.fr/)).
By utilizing this software, you agree to the terms of the included license. Grafana-Zabbix plugin is
licensed under the Apache 2.0 agreement. See
[LICENSE](https://github.com/alexanderzobnin/grafana-zabbix/blob/master/LICENSE.md) for the full
[LICENSE](https://github.com/alexanderzobnin/grafana-zabbix/blob/master/LICENSE) for the full
license terms.

View File

@@ -30,6 +30,13 @@ scale(100)
scale(0.01)
```
### delta
```
delta()
```
Convert absolute values to delta, for example, bits to bits/sec.
Aggregate
---------
@@ -71,6 +78,38 @@ max(interval)
```
**Deprecated**, use `aggregateBy(interval, max)` instead.
Filter
---------
### top
```
top(N, value)
```
Returns top N series, sorted by _value_, which can be one of: _avg_, _min_, _max_, _median_.
Examples:
```
top(10, avg)
top(5, max)
```
### bottom
```
bottom(N, value)
```
Returns bottom N series, sorted by _value_, which can be one of: _avg_, _min_, _max_, _median_.
Examples:
```
bottom(5, avg)
```
## Trends
### trendValue

View File

@@ -23,16 +23,28 @@
"grunt-contrib-copy": "~0.8.2",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-uglify": "~0.11.0",
"grunt-mocha-test": "~0.12.7",
"grunt-systemjs-builder": "^0.2.5",
"load-grunt-tasks": "~3.2.0",
"grunt-execute": "~0.2.2",
"grunt-contrib-clean": "~0.6.0"
"grunt-contrib-clean": "~0.6.0",
"prunk": "~1.2.1",
"jsdom": "~3.1.2",
"q": "~1.4.1",
"chai": "~3.5.0",
"sinon-chai": "~2.8.0",
"moment": "~2.14.1"
},
"dependencies": {
"babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
"babel-plugin-transform-es2015-for-of": "^6.5.0",
"babel-plugin-transform-es2015-for-of": "^6.6.0",
"babel-preset-es2015": "^6.5.0",
"lodash": "~4.0.0"
"grunt-contrib-jshint": "^1.0.0",
"grunt-jscs": "^2.8.0",
"jshint-stylish": "^2.1.0",
"lodash": "~4.0.0",
"mocha": "^2.4.5",
"sinon": "~1.16.1"
},
"homepage": "http://grafana-zabbix.org"
}

View File

@@ -1,252 +0,0 @@
import _ from 'lodash';
import * as utils from './utils';
export default class DataProcessor {
/**
* Downsample datapoints series
*/
static downsampleSeries(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();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
static groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, function(point) {
// Calculate time for group of points
return Math.floor(point[1] / ms_interval) * ms_interval;
});
// frame: { '<unixtime>': [[<value>, <unixtime>], ...] }
// return [{ '<unixtime>': <value> }, { '<unixtime>': <value> }, ...]
var grouped = _.mapValues(frames, function(frame) {
var points = _.map(frame, function(point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function(value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
static sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function(point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function(series) {
var timestamps = _.map(series, function(point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function(timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
static AVERAGE(values) {
var sum = 0;
_.each(values, function(value) {
sum += value;
});
return sum / values.length;
}
static MIN(values) {
return _.min(values);
}
static MAX(values) {
return _.max(values);
}
static MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
static setAlias(alias, timeseries) {
timeseries.target = alias;
return timeseries;
}
static scale(factor, datapoints) {
return _.map(datapoints, point => {
return [
point[0] * factor,
point[1]
];
});
}
static groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = DataProcessor.aggregationFunctions[groupFunc];
return DataProcessor.groupBy(interval, groupByCallback, datapoints);
}
static aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy()
var flattenedPoints = _.flatten(datapoints, true);
var groupByCallback = DataProcessor.aggregationFunctions[aggregateFunc];
return DataProcessor.groupBy(interval, groupByCallback, flattenedPoints);
}
static aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
return DataProcessor.groupBy(interval, groupByCallback, flattenedPoints);
}
static get aggregationFunctions() {
return {
avg: this.AVERAGE,
min: this.MIN,
max: this.MAX,
median: this.MEDIAN
};
}
static get metricFunctions() {
return {
groupBy: this.groupByWrapper,
scale: this.scale,
aggregateBy: this.aggregateByWrapper,
average: _.partial(this.aggregateWrapper, this.AVERAGE),
min: _.partial(this.aggregateWrapper, this.MIN),
max: _.partial(this.aggregateWrapper, this.MAX),
median: _.partial(this.aggregateWrapper, this.MEDIAN),
sumSeries: this.sumSeries,
setAlias: this.setAlias,
};
}
}
function sortByTime(series) {
return _.sortBy(series, function(point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return (left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]));
}
}
function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}

View File

@@ -0,0 +1,291 @@
import _ from 'lodash';
import * as utils from './utils';
/**
* Downsample datapoints series
*/
function downsampleSeries(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();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, function(point) {
// Calculate time for group of points
return Math.floor(point[1] / ms_interval) * ms_interval;
});
// frame: { '<unixtime>': [[<value>, <unixtime>], ...] }
// return [{ '<unixtime>': <value> }, { '<unixtime>': <value> }, ...]
var grouped = _.mapValues(frames, function(frame) {
var points = _.map(frame, function(point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function(value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function(point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function(series) {
var timestamps = _.map(series, function(point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function(timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
function limit(order, n, orderByFunc, timeseries) {
let orderByCallback = aggregationFunctions[orderByFunc];
let sortByIteratee = (ts) => {
let values = _.map(ts.datapoints, (point) => {
return point[0];
});
return orderByCallback(values);
};
let sortedTimeseries = _.sortBy(timeseries, sortByIteratee);
if (order === 'bottom') {
return sortedTimeseries.slice(0, n);
} else {
return sortedTimeseries.slice(-n);
}
}
function AVERAGE(values) {
var sum = 0;
_.each(values, function(value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _.min(values);
}
function MAX(values) {
return _.max(values);
}
function MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
function setAlias(alias, timeseries) {
timeseries.target = alias;
return timeseries;
}
function scale(factor, datapoints) {
return _.map(datapoints, point => {
return [
point[0] * factor,
point[1]
];
});
}
function delta(datapoints) {
let newSeries = [];
let deltaValue;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints);
}
function aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy()
var flattenedPoints = _.flatten(datapoints, true);
var groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(interval, groupByCallback, flattenedPoints);
}
function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
return groupBy(interval, groupByCallback, flattenedPoints);
}
function sortByTime(series) {
return _.sortBy(series, function(point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return (left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]));
}
}
function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
let metricFunctions = {
groupBy: groupByWrapper,
scale: scale,
delta: delta,
aggregateBy: aggregateByWrapper,
average: _.partial(aggregateWrapper, AVERAGE),
min: _.partial(aggregateWrapper, MIN),
max: _.partial(aggregateWrapper, MAX),
median: _.partial(aggregateWrapper, MEDIAN),
sumSeries: sumSeries,
top: _.partial(limit, 'top'),
bottom: _.partial(limit, 'bottom'),
setAlias: setAlias
};
let aggregationFunctions = {
avg: AVERAGE,
min: MIN,
max: MAX,
median: MEDIAN
};
export default {
downsampleSeries: downsampleSeries,
groupBy: groupBy,
AVERAGE: AVERAGE,
MIN: MIN,
MAX: MAX,
MEDIAN: MEDIAN,
get aggregationFunctions() {
return aggregationFunctions;
},
get metricFunctions() {
return metricFunctions;
}
};

View File

@@ -4,15 +4,17 @@ import * as dateMath from 'app/core/utils/datemath';
import * as utils from './utils';
import * as migrations from './migrations';
import * as metricFunctions from './metricFunctions';
import DataProcessor from './DataProcessor';
import './zabbixAPI.service.js';
import './zabbixCache.service.js';
import './queryProcessor.service.js';
import dataProcessor from './dataProcessor';
import responseHandler from './responseHandler';
import './zabbix.js';
import {ZabbixAPIError} from './zabbixAPICore.service.js';
export class ZabbixAPIDatasource {
class ZabbixAPIDatasource {
/** @ngInject */
constructor(instanceSettings, $q, templateSrv, alertSrv, zabbixAPIService, ZabbixCachingProxy, QueryProcessor) {
constructor(instanceSettings, templateSrv, alertSrv, Zabbix) {
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
// General data source settings
this.name = instanceSettings.name;
@@ -32,25 +34,10 @@ export class ZabbixAPIDatasource {
var ttl = instanceSettings.jsonData.cacheTTL || '1h';
this.cacheTTL = utils.parseInterval(ttl);
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
// Initialize cache service
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI, this.cacheTTL);
// Initialize query builder
this.queryProcessor = new QueryProcessor(this.zabbixCache);
// Dependencies
this.q = $q;
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
// Use custom format for template variables
this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
console.log(this.zabbixCache);
}
////////////////////////
@@ -63,19 +50,20 @@ export class ZabbixAPIDatasource {
* @return {Object} Grafana metrics object with timeseries data for each target.
*/
query(options) {
var self = this;
// get from & to in seconds
var timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
var useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
var useTrends = (timeFrom < useTrendsFrom) && this.trends;
var useTrends = (timeFrom <= useTrendsFrom) && this.trends;
// Create request for each target
var promises = _.map(options.targets, target => {
// Prevent changes of original object
target = _.cloneDeep(target);
this.replaceTargetVariables(target, options);
// Metrics or Text query mode
if (target.mode !== 1) {
// Migrate old targets
target = migrations.migrate(target);
@@ -84,22 +72,10 @@ export class ZabbixAPIDatasource {
return [];
}
// Replace templated variables
var groupFilter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
var hostFilter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
var appFilter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
var itemFilter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
// Query numeric data
if (!target.mode || target.mode === 0) {
return self.queryNumericData(target, groupFilter, hostFilter, appFilter, itemFilter,
timeFrom, timeTo, useTrends, options, self);
}
// Query text data
else if (target.mode === 2) {
return self.queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter,
timeFrom, timeTo, options, self);
return this.queryNumericData(target, timeFrom, timeTo, useTrends);
} else if (target.mode === 2) {
return this.queryTextData(target, timeFrom, timeTo);
}
}
@@ -110,148 +86,122 @@ export class ZabbixAPIDatasource {
return [];
}
return this.zabbixAPI
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
return this.zabbix.getSLA(target.itservice.serviceid, timeFrom, timeTo)
.then(slaObject => {
return self.queryProcessor
.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
});
}
}, this);
});
// Data for panel (all targets)
return this.q.all(_.flatten(promises))
return Promise.all(_.flatten(promises))
.then(_.flatten)
.then(timeseries_data => {
// Series downsampling
var data = _.map(timeseries_data, timeseries => {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DataProcessor
.groupBy(options.interval, DataProcessor.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
return downsampleSeries(timeseries_data, options);
})
.then(data => {
return { data: data };
});
}
queryNumericData(target, groupFilter, hostFilter, appFilter, itemFilter, timeFrom, timeTo, useTrends, options, self) {
// Build query in asynchronous manner
return self.queryProcessor
.build(groupFilter, hostFilter, appFilter, itemFilter, 'num')
queryNumericData(target, timeFrom, timeTo, useTrends) {
let options = {
itemtype: 'num'
};
return this.zabbix.getItemsFromTarget(target, options)
.then(items => {
// Add hostname for items from multiple hosts
var addHostName = utils.isRegex(target.host.filter);
var getHistory;
let getHistoryPromise;
// Use trends
if (useTrends) {
let valueType = this.getTrendValueType(target);
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
.then(history => {
return responseHandler.handleTrends(history, items, valueType);
});
} else {
// Use history
getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo)
.then(history => {
return responseHandler.handleHistory(history, items);
});
}
return getHistoryPromise.then(timeseries_data => {
return this.applyDataProcessingFunctions(timeseries_data, target);
});
});
}
getTrendValueType(target) {
// Find trendValue() function and get specified trend value
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _.find(target.functions, func => {
return _.contains(trendFunctions, func.def.name);
});
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg";
getHistory = self.zabbixAPI
.getTrend(items, timeFrom, timeTo)
.then(history => {
return self.queryProcessor.handleTrends(history, items, addHostName, valueType);
return _.includes(trendFunctions, func.def.name);
});
return trendValueFunc ? trendValueFunc.params[0] : "avg";
}
// Use history
else {
getHistory = self.zabbixCache
.getHistory(items, timeFrom, timeTo)
.then(history => {
return self.queryProcessor.handleHistory(history, items, addHostName);
});
}
return getHistory.then(timeseries_data => {
applyDataProcessingFunctions(timeseries_data, target) {
let transformFunctions = bindFunctionDefs(target.functions, 'Transform');
let aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
let filterFunctions = bindFunctionDefs(target.functions, 'Filter');
let aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
// Apply transformation functions
timeseries_data = _.map(timeseries_data, timeseries => {
// Filter only transformation functions
var transformFunctions = bindFunctionDefs(target.functions, 'Transform', DataProcessor);
// Timeseries processing
var dp = timeseries.datapoints;
for (var i = 0; i < transformFunctions.length; i++) {
dp = transformFunctions[i](dp);
}
timeseries.datapoints = dp;
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
return timeseries;
});
// Apply aggregations
var aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate', DataProcessor);
var dp = _.map(timeseries_data, 'datapoints');
if (aggregationFunctions.length) {
for (var i = 0; i < aggregationFunctions.length; i++) {
dp = aggregationFunctions[i](dp);
// Apply filter functions
if (filterFunctions.length) {
timeseries_data = sequence(filterFunctions)(timeseries_data);
}
var lastAgg = _.findLast(target.functions, func => {
return _.contains(
_.map(metricFunctions.getCategories()['Aggregate'], 'name'), func.def.name);
// Apply aggregations
if (aggregationFunctions.length) {
let dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
let lastAgg = _.findLast(target.functions, func => {
return _.includes(aggFuncNames, func.def.name);
});
timeseries_data = [
{
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}
];
}];
}
// Apply alias functions
var aliasFunctions = bindFunctionDefs(target.functions, 'Alias', DataProcessor);
for (var j = 0; j < aliasFunctions.length; j++) {
_.each(timeseries_data, aliasFunctions[j]);
}
_.each(timeseries_data, sequence(aliasFunctions));
return timeseries_data;
});
});
}
queryTextData(target, groupFilter, hostFilter, appFilter, itemFilter, timeFrom, timeTo, options, self) {
return self.queryProcessor
.build(groupFilter, hostFilter, appFilter, itemFilter, 'text')
queryTextData(target, timeFrom, timeTo) {
let options = {
itemtype: 'text'
};
return this.zabbix.getItemsFromTarget(target, options)
.then(items => {
if (items.length) {
var textItemsPromises = _.map(items, item => {
return self.zabbixAPI.getLastValue(item.itemid);
});
return self.q.all(textItemsPromises)
.then(result => {
return _.map(result, (lastvalue, index) => {
var extractedValue;
return this.zabbix.getHistory(items, timeFrom, timeTo)
.then(history => {
return responseHandler.convertHistory(history, items, false, (point) => {
let value = point.value;
// Regex-based extractor
if (target.textFilter) {
var text_extract_pattern = new RegExp(self.replaceTemplateVars(target.textFilter, options.scopedVars));
extractedValue = text_extract_pattern.exec(lastvalue);
if (extractedValue) {
if (target.useCaptureGroups) {
extractedValue = extractedValue[1];
} else {
extractedValue = extractedValue[0];
value = extractText(point.value, target.textFilter, target.useCaptureGroups);
}
}
} else {
extractedValue = lastvalue;
}
return {
target: items[index].name,
datapoints: [[extractedValue, timeTo * 1000]]
};
return [value, point.clock * 1000];
});
});
} else {
return self.q.when([]);
return Promise.resolve([]);
}
});
}
@@ -261,38 +211,33 @@ export class ZabbixAPIDatasource {
* @return {object} Connection status and Zabbix API version
*/
testDatasource() {
var self = this;
return this.zabbixAPI.getVersion()
let zabbixVersion;
return this.zabbix.getVersion()
.then(version => {
return self.zabbixAPI.login()
.then(auth => {
if (auth) {
zabbixVersion = version;
return this.zabbix.login();
})
.then(() => {
return {
status: "success",
title: "Success",
message: "Zabbix API version: " + version
message: "Zabbix API version: " + zabbixVersion
};
} else {
return {
status: "error",
title: "Invalid user name or password",
message: "Zabbix API version: " + version
};
}
}, error => {
})
.catch(error => {
if (error instanceof ZabbixAPIError) {
return {
status: "error",
title: error.message,
message: error.data
};
});
}, error => {
console.log(error);
} else {
return {
status: "error",
title: "Connection failed",
message: "Could not connect to given url"
};
}
});
}
@@ -308,12 +253,12 @@ export class ZabbixAPIDatasource {
* of metrics in "{metric1,metcic2,...,metricN}" format.
*/
metricFindQuery(query) {
// Split query. Query structure:
// group.host.app.item
var self = this;
var parts = [];
_.each(query.split('.'), function (part) {
part = self.replaceTemplateVars(part, {});
let result;
let parts = [];
// Split query. Query structure: group.host.app.item
_.each(query.split('.'), part => {
part = this.replaceTemplateVars(part, {});
// Replace wildcard to regex
if (part === '*') {
@@ -321,7 +266,7 @@ export class ZabbixAPIDatasource {
}
parts.push(part);
});
var template = _.object(['group', 'host', 'app', 'item'], parts);
let template = _.zipObject(['group', 'host', 'app', 'item'], parts);
// Get items
if (parts.length === 4) {
@@ -329,41 +274,24 @@ export class ZabbixAPIDatasource {
if (template.app === '/.*/') {
template.app = '';
}
return this.queryProcessor
.getItems(template.group, template.host, template.app)
.then(items => {
return _.map(items, formatMetric);
});
}
result = this.zabbix.getItems(template.group, template.host, template.app, template.item);
} else if (parts.length === 3) {
// Get applications
else if (parts.length === 3) {
return this.queryProcessor
.getApps(template.group, template.host)
.then(apps => {
return _.map(apps, formatMetric);
});
}
result = this.zabbix.getApps(template.group, template.host, template.app);
} else if (parts.length === 2) {
// Get hosts
else if (parts.length === 2) {
return this.queryProcessor
.getHosts(template.group)
.then(hosts => {
return _.map(hosts, formatMetric);
});
}
result = this.zabbix.getHosts(template.group, template.host);
} else if (parts.length === 1) {
// Get groups
else if (parts.length === 1) {
return this.zabbixCache
.getGroups(template.group)
.then(groups => {
return _.map(groups, formatMetric);
result = this.zabbix.getGroups(template.group);
} else {
result = Promise.resolve([]);
}
return result.then(metrics => {
return metrics.map(formatMetric);
});
}
// Return empty object for invalid request
else {
return this.q.when([]);
}
}
/////////////////
// Annotations //
@@ -378,16 +306,13 @@ export class ZabbixAPIDatasource {
// Show all triggers
var showTriggers = [0, 1];
var buildQuery = this.queryProcessor
.buildTriggerQuery(this.replaceTemplateVars(annotation.group, {}),
var getTriggers = this.zabbix
.getTriggers(this.replaceTemplateVars(annotation.group, {}),
this.replaceTemplateVars(annotation.host, {}),
this.replaceTemplateVars(annotation.application, {}));
var self = this;
return buildQuery.then(query => {
return self.zabbixAPI
.getTriggers(query.groupids, query.hostids, query.applicationids,
showTriggers, timeFrom, timeTo)
.then(triggers => {
this.replaceTemplateVars(annotation.application, {}),
showTriggers);
return getTriggers.then(triggers => {
// Filter triggers by description
if (utils.isRegex(annotation.trigger)) {
@@ -406,10 +331,10 @@ export class ZabbixAPIDatasource {
});
var objectids = _.map(triggers, 'triggerid');
return self.zabbixAPI
return this.zabbix
.getEvents(objectids, timeFrom, timeTo, showOkEvents)
.then(events => {
var indexedTriggers = _.indexBy(triggers, 'triggerid');
var indexedTriggers = _.keyBy(triggers, 'triggerid');
// Hide acknowledged events if option enabled
if (annotation.hideAcknowledged) {
@@ -419,44 +344,71 @@ export class ZabbixAPIDatasource {
}
return _.map(events, event => {
var title ='';
let tags;
if (annotation.showHostname) {
title += event.hosts[0].name + ': ';
tags = _.map(event.hosts, 'name');
}
// Show event type (OK or Problem)
title += Number(event.value) ? 'Problem' : 'OK';
let title = Number(event.value) ? 'Problem' : 'OK';
var formatted_acknowledges = utils.formatAcknowledges(event.acknowledges);
let formatted_acknowledges = utils.formatAcknowledges(event.acknowledges);
return {
annotation: annotation,
time: event.clock * 1000,
title: title,
tags: tags,
text: indexedTriggers[event.objectid].description + formatted_acknowledges
};
});
});
});
}
// Replace template variables
replaceTargetVariables(target, options) {
let parts = ['group', 'host', 'application', 'item'];
parts.forEach(p => {
target[p].filter = this.replaceTemplateVars(target[p].filter, options.scopedVars);
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
_.forEach(target.functions, func => {
func.params = func.params.map(param => {
if (typeof param === 'number') {
return +this.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return this.templateSrv.replace(param, options.scopedVars);
}
});
});
}
}
function bindFunctionDefs(functionDefs, category, DataProcessor) {
'use strict';
function bindFunctionDefs(functionDefs, category) {
var aggregationFunctions = _.map(metricFunctions.getCategories()[category], 'name');
var aggFuncDefs = _.filter(functionDefs, function(func) {
return _.contains(aggregationFunctions, func.def.name);
return _.includes(aggregationFunctions, func.def.name);
});
return _.map(aggFuncDefs, function(func) {
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
return funcInstance.bindFunction(DataProcessor.metricFunctions);
return funcInstance.bindFunction(dataProcessor.metricFunctions);
});
}
function downsampleSeries(timeseries_data, options) {
return _.map(timeseries_data, timeseries => {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = dataProcessor
.groupBy(options.interval, dataProcessor.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
}
function formatMetric(metricObj) {
'use strict';
return {
text: metricObj.name,
expandable: false
@@ -473,7 +425,7 @@ function formatMetric(metricObj) {
* template variables, for example
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
*/
function zabbixTemplateFormat(value, variable) {
function zabbixTemplateFormat(value) {
if (typeof value === 'string') {
return utils.escapeRegex(value);
}
@@ -482,7 +434,8 @@ function zabbixTemplateFormat(value, variable) {
return '(' + escapedValues.join('|') + ')';
}
/** If template variables are used in request, replace it using regex format
/**
* If template variables are used in request, replace it using regex format
* and wrap with '/' for proper multi-value work. Example:
* $variable selected as a, b, c
* We use filter $variable
@@ -491,8 +444,38 @@ function zabbixTemplateFormat(value, variable) {
*/
function replaceTemplateVars(templateSrv, target, scopedVars) {
var replacedTarget = templateSrv.replace(target, scopedVars, zabbixTemplateFormat);
if (target !== replacedTarget && !utils.regexPattern.test(replacedTarget)) {
if (target !== replacedTarget && !utils.isRegex(replacedTarget)) {
replacedTarget = '/^' + replacedTarget + '$/';
}
return replacedTarget;
}
function extractText(str, pattern, useCaptureGroups) {
let extractPattern = new RegExp(pattern);
let extractedValue = extractPattern.exec(str);
if (extractedValue) {
if (useCaptureGroups) {
extractedValue = extractedValue[1];
} else {
extractedValue = extractedValue[0];
}
}
return extractedValue;
}
// Apply function one by one:
// sequence([a(), b(), c()]) = c(b(a()));
function sequence(funcsArray) {
return function(result) {
for (var i = 0; i < funcsArray.length; i++) {
result = funcsArray[i].call(this, result);
}
return result;
};
}
export {ZabbixAPIDatasource, zabbixTemplateFormat};
// Fix for backward compatibility with lodash 2.4
if (!_.includes) {_.includes = _.contains;}
if (!_.keyBy) {_.keyBy = _.indexBy;}

View File

@@ -5,6 +5,7 @@ var index = [];
var categories = {
Transform: [],
Aggregate: [],
Filter: [],
Trends: [],
Alias: []
};
@@ -39,6 +40,13 @@ addFuncDef({
defaultParams: [100],
});
addFuncDef({
name: 'delta',
category: 'Transform',
params: [],
defaultParams: [],
});
addFuncDef({
name: 'sumSeries',
category: 'Aggregate',
@@ -92,6 +100,26 @@ addFuncDef({
defaultParams: ['1m', 'avg'],
});
addFuncDef({
name: 'top',
category: 'Filter',
params: [
{ name: 'number', type: 'int' },
{ name: 'value', type: 'string', options: ['avg', 'min', 'max', 'median'] }
],
defaultParams: [5, 'avg'],
});
addFuncDef({
name: 'bottom',
category: 'Filter',
params: [
{ name: 'number', type: 'int' },
{ name: 'value', type: 'string', options: ['avg', 'min', 'max', 'median'] }
],
defaultParams: [5, 'avg'],
});
addFuncDef({
name: 'trendValue',
category: 'Trends',

View File

@@ -1,4 +1,5 @@
import {QueryCtrl} from 'app/plugins/sdk';
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
import * as metricFunctions from './metricFunctions';
@@ -12,14 +13,9 @@ import './css/query-editor.css!';
export class ZabbixQueryController extends QueryCtrl {
// ZabbixQueryCtrl constructor
constructor($scope, $injector, $rootScope, $sce, $q, templateSrv) {
// Call superclass constructor
constructor($scope, $injector, $rootScope, $sce, templateSrv) {
super($scope, $injector);
this.zabbix = this.datasource.zabbixAPI;
this.cache = this.datasource.zabbixCache;
this.$q = $q;
this.zabbix = this.datasource.zabbix;
// Use custom format for template variables
this.replaceTemplateVars = this.datasource.replaceTemplateVars;
@@ -105,85 +101,57 @@ export class ZabbixQueryController extends QueryCtrl {
}
initFilters() {
var self = this;
var itemtype = self.editorModes[self.target.mode].value;
return this.$q.when(this.suggestGroups())
.then(() => {return self.suggestHosts();})
.then(() => {return self.suggestApps();})
.then(() => {return self.suggestItems(itemtype);});
let itemtype = this.editorModes[this.target.mode].value;
return Promise.all([
this.suggestGroups(),
this.suggestHosts(),
this.suggestApps(),
this.suggestItems(itemtype)
]);
}
suggestGroups() {
var self = this;
return this.cache.getGroups().then(groups => {
self.metric.groupList = groups;
return this.zabbix.getAllGroups()
.then(groups => {
this.metric.groupList = groups;
return groups;
});
}
suggestHosts() {
var self = this;
var groupFilter = this.replaceTemplateVars(this.target.group.filter);
return this.datasource.queryProcessor
.filterGroups(self.metric.groupList, groupFilter)
.then(groups => {
var groupids = _.map(groups, 'groupid');
return self.zabbix
.getHosts(groupids)
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
return this.zabbix.getAllHosts(groupFilter)
.then(hosts => {
self.metric.hostList = hosts;
this.metric.hostList = hosts;
return hosts;
});
});
}
suggestApps() {
var self = this;
var hostFilter = this.replaceTemplateVars(this.target.host.filter);
return this.datasource.queryProcessor
.filterHosts(self.metric.hostList, hostFilter)
.then(hosts => {
var hostids = _.map(hosts, 'hostid');
return self.zabbix
.getApps(hostids)
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
let hostFilter = this.replaceTemplateVars(this.target.host.filter);
return this.zabbix.getAllApps(groupFilter, hostFilter)
.then(apps => {
return self.metric.appList = apps;
});
this.metric.appList = apps;
return apps;
});
}
suggestItems(itemtype='num') {
var self = this;
var appFilter = this.replaceTemplateVars(this.target.application.filter);
if (appFilter) {
// Filter by applications
return this.datasource.queryProcessor
.filterApps(self.metric.appList, appFilter)
.then(apps => {
var appids = _.map(apps, 'applicationid');
return self.zabbix
.getItems(undefined, appids, itemtype)
suggestItems(itemtype = 'num') {
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
let hostFilter = this.replaceTemplateVars(this.target.host.filter);
let appFilter = this.replaceTemplateVars(this.target.application.filter);
let options = {
itemtype: itemtype,
showDisabledItems: this.target.options.showDisabledItems
};
return this.zabbix
.getAllItems(groupFilter, hostFilter, appFilter, options)
.then(items => {
if (!self.target.options.showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
self.metric.itemList = items;
this.metric.itemList = items;
return items;
});
});
} else {
// Return all items belonged to selected hosts
var hostids = _.map(self.metric.hostList, 'hostid');
return self.zabbix
.getItems(hostids, undefined, itemtype)
.then(items => {
if (!self.target.options.showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
self.metric.itemList = items;
return items;
});
}
}
isRegex(str) {
@@ -330,7 +298,6 @@ export class ZabbixQueryController extends QueryCtrl {
this.panelCtrl.refresh();
}
}
}
// Set templateUrl as static property

View File

@@ -1,372 +0,0 @@
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
/** @ngInject */
angular.module('grafana.services').factory('QueryProcessor', function($q) {
class QueryProcessor {
constructor(zabbixCacheInstance) {
this.cache = zabbixCacheInstance;
this.$q = $q;
}
/**
* Build query in asynchronous manner
*/
build(groupFilter, hostFilter, appFilter, itemFilter, itemtype) {
var self = this;
if (this.cache._initialized) {
return this.$q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype));
} else {
return this.cache.refresh().then(function() {
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype);
});
}
}
/**
* Build trigger query in asynchronous manner
*/
buildTriggerQuery(groupFilter, hostFilter, appFilter) {
var self = this;
if (this.cache._initialized) {
return this.$q.when(self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter));
} else {
return this.cache.refresh().then(function() {
return self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter);
});
}
}
filterGroups(groups, groupFilter) {
return this.$q.when(
findByFilter(groups, groupFilter)
);
}
/**
* Get list of host belonging to given groups.
* @return list of hosts
*/
filterHosts(hosts, hostFilter) {
return this.$q.when(
findByFilter(hosts, hostFilter)
);
}
filterApps(apps, appFilter) {
return this.$q.when(
findByFilter(apps, appFilter)
);
}
/**
* Build query - convert target filters to array of Zabbix items
*/
buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype, showDisabledItems) {
return this.getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems)
.then(items => {
return getByFilter(items, itemFilter);
});
}
getGroups() {
return this.cache.getGroups();
}
/**
* Get list of host belonging to given groups.
* @return list of hosts
*/
getHosts(groupFilter) {
var self = this;
return this.cache
.getGroups()
.then(groups => {
return findByFilter(groups, groupFilter);
})
.then(groups => {
var groupids = _.map(groups, 'groupid');
return self.cache.getHosts(groupids);
});
}
/**
* Get list of applications belonging to given groups and hosts.
* @return list of applications belonging to given hosts
*/
getApps(groupFilter, hostFilter) {
var self = this;
return this.getHosts(groupFilter)
.then(hosts => {
return findByFilter(hosts, hostFilter);
})
.then(hosts => {
var hostids = _.map(hosts, 'hostid');
return self.cache.getApps(hostids);
});
}
getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems) {
var self = this;
return this.getHosts(groupFilter)
.then(hosts => {
return findByFilter(hosts, hostFilter);
})
.then(hosts => {
var hostids = _.map(hosts, 'hostid');
if (appFilter) {
return self.cache
.getApps(hostids)
.then(apps => {
// Use getByFilter for proper item filtering
return getByFilter(apps, appFilter);
});
} else {
return {
appFilterEmpty: true,
hostids: hostids
};
}
})
.then(apps => {
if (apps.appFilterEmpty) {
return self.cache
.getItems(apps.hostids, undefined, itemtype)
.then(items => {
if (showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
});
} else {
var appids = _.map(apps, 'applicationid');
return self.cache
.getItems(undefined, appids, itemtype)
.then(items => {
if (showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
});
}
});
}
/**
* Build query - convert target filters to array of Zabbix items
*/
buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter) {
var promises = [
this.cache.getGroups().then(function(groups) {
return _.filter(groups, function(group) {
if (utils.isRegex(groupFilter)) {
return utils.buildRegex(groupFilter).test(group.name);
} else {
return group.name === groupFilter;
}
});
}),
this.getHosts(groupFilter).then(function(hosts) {
return _.filter(hosts, function(host) {
if (utils.isRegex(hostFilter)) {
return utils.buildRegex(hostFilter).test(host.name);
} else {
return host.name === hostFilter;
}
});
}),
this.getApps(groupFilter, hostFilter).then(function(apps) {
return _.filter(apps, function(app) {
if (utils.isRegex(appFilter)) {
return utils.buildRegex(appFilter).test(app.name);
} else {
return app.name === appFilter;
}
});
})
];
return this.$q.all(promises).then(function(results) {
var filteredGroups = results[0];
var filteredHosts = results[1];
var filteredApps = results[2];
var query = {};
if (appFilter) {
query.applicationids = _.flatten(_.map(filteredApps, 'applicationid'));
}
if (hostFilter) {
query.hostids = _.map(filteredHosts, 'hostid');
}
if (groupFilter) {
query.groupids = _.map(filteredGroups, 'groupid');
}
return query;
});
}
/**
* Convert Zabbix API history.get response to Grafana format
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
convertHistory(history, items, addHostName, convertPointCallback) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }, ...
* ]
*/
// Group history by itemid
var grouped_history = _.groupBy(history, 'itemid');
var hosts = _.indexBy(_.flatten(_.map(items, 'hosts')), 'hostid');
return _.map(grouped_history, function(hist, itemid) {
var item = _.find(items, {'itemid': itemid});
var alias = item.name;
if (_.keys(hosts).length > 1 || addHostName) {
var host = hosts[item.hostid];
alias = host.name + ": " + alias;
}
return {
target: alias,
datapoints: _.map(hist, convertPointCallback)
};
});
}
handleHistory(history, items, addHostName) {
return this.convertHistory(history, items, addHostName, convertHistoryPoint);
}
handleTrends(history, items, addHostName, valueType) {
var convertPointCallback = _.partial(convertTrendPoint, valueType);
return this.convertHistory(history, items, addHostName, convertPointCallback);
}
handleSLAResponse(itservice, slaProperty, slaObject) {
var targetSLA = slaObject[itservice.serviceid].sla[0];
if (slaProperty.property === 'status') {
var targetStatus = parseInt(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]
]
};
}
}
}
return QueryProcessor;
});
/**
* Find group, host, app or item by given name.
* @param list list of groups, apps or other
* @param name visible name
* @return array with finded element or undefined
*/
function findByName(list, name) {
var finded = _.find(list, {'name': name});
if (finded) {
return [finded];
} else {
return undefined;
}
}
/**
* Different hosts can contains applications and items with same name.
* For this reason use _.filter, which return all elements instead _.find,
* which return only first finded.
* @param {[type]} list list of elements
* @param {[type]} name app name
* @return {[type]} array with finded element or undefined
*/
function filterByName(list, name) {
var finded = _.filter(list, {'name': name});
if (finded) {
return finded;
} else {
return undefined;
}
}
function findByRegex(list, regex) {
var filterPattern = utils.buildRegex(regex);
return _.filter(list, function (zbx_obj) {
return filterPattern.test(zbx_obj.name);
});
}
function findByFilter(list, filter) {
if (utils.isRegex(filter)) {
return findByRegex(list, filter);
} else {
return findByName(list, filter);
}
}
function getByFilter(list, filter) {
if (utils.isRegex(filter)) {
return findByRegex(list, filter);
} else {
return filterByName(list, filter);
}
}
function getFromIndex(index, objids) {
return _.map(objids, function(id) {
return index[id];
});
}
function convertHistoryPoint(point) {
// Value must be a number for properly work
return [
Number(point.value),
point.clock * 1000
];
}
function convertTrendPoint(valueType, point) {
var value;
switch (valueType) {
case "min":
value = point.value_min;
break;
case "max":
value = point.value_max;
break;
case "avg":
value = point.value_avg;
break;
default:
value = point.value_avg;
}
return [
Number(value),
point.clock * 1000
];
}

View File

@@ -0,0 +1,106 @@
import _ from 'lodash';
/**
* Convert Zabbix API history.get response to Grafana format
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
function convertHistory(history, items, addHostName, convertPointCallback) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }, ...
* ]
*/
// Group history by itemid
var grouped_history = _.groupBy(history, 'itemid');
var hosts = _.uniqBy(_.flatten(_.map(items, 'hosts')), 'hostid'); //uniqBy is needed to deduplicate
return _.map(grouped_history, function(hist, itemid) {
var item = _.find(items, {'itemid': itemid});
var alias = item.name;
if (_.keys(hosts).length > 1 && addHostName) { //only when actual multi hosts selected
var host = _.find(hosts, {'hostid': item.hostid});
alias = host.name + ": " + alias;
}
return {
target: alias,
datapoints: _.map(hist, convertPointCallback)
};
});
}
function handleHistory(history, items, addHostName = true) {
return convertHistory(history, items, addHostName, convertHistoryPoint);
}
function handleTrends(history, items, valueType, addHostName = true) {
var convertPointCallback = _.partial(convertTrendPoint, valueType);
return convertHistory(history, items, addHostName, convertPointCallback);
}
function handleSLAResponse(itservice, slaProperty, slaObject) {
var targetSLA = slaObject[itservice.serviceid].sla[0];
if (slaProperty.property === 'status') {
var targetStatus = parseInt(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]
]
};
}
}
function convertHistoryPoint(point) {
// Value must be a number for properly work
return [
Number(point.value),
point.clock * 1000
];
}
function convertTrendPoint(valueType, point) {
var value;
switch (valueType) {
case "min":
value = point.value_min;
break;
case "max":
value = point.value_max;
break;
case "avg":
value = point.value_avg;
break;
default:
value = point.value_avg;
}
return [
Number(value),
point.clock * 1000
];
}
export default {
handleHistory: handleHistory,
convertHistory: convertHistory,
handleTrends: handleTrends,
handleSLAResponse: handleSLAResponse
};

View File

@@ -0,0 +1,230 @@
import {Datasource} from "../module";
import {zabbixTemplateFormat} from "../datasource";
import Q from "q";
import sinon from 'sinon';
import _ from 'lodash';
describe('ZabbixDatasource', () => {
let ctx = {};
let defined = sinon.match.defined;
beforeEach(() => {
ctx.instanceSettings = {
jsonData: {
username: 'zabbix',
password: 'zabbix',
trends: true,
trendsFrom: '7d'
}
};
ctx.templateSrv = {};
ctx.alertSrv = {};
ctx.zabbix = () => {};
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv, ctx.zabbix);
});
describe('When querying data', () => {
beforeEach(() => {
ctx.ds.replaceTemplateVars = (str) => str;
});
ctx.options = {
targets: [
{
group: {filter: ""},
host: {filter: ""},
application: {filter: ""},
item: {filter: ""}
}
],
range: {from: 'now-7d', to: 'now'}
};
it('should return an empty array when no targets are set', (done) => {
let options = {
targets: [],
range: {from: 'now-6h', to: 'now'}
};
ctx.ds.query(options).then(result => {
expect(result.data).to.have.length(0);
done();
});
});
it('should use trends if it enabled and time more than trendsFrom', (done) => {
let ranges = ['now-7d', 'now-168h', 'now-1M', 'now-1y'];
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.query(ctx.options);
// Check that useTrends options is true
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, defined, true);
});
done();
});
it('shouldnt use trends if it enabled and time less than trendsFrom', (done) => {
let ranges = ['now-6d', 'now-167h', 'now-1h', 'now-30m', 'now-30s'];
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.query(ctx.options);
// Check that useTrends options is false
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, defined, false);
});
done();
});
});
describe('When replacing template variables', () => {
function testReplacingVariable(target, varValue, expectedResult, done) {
ctx.ds.templateSrv.replace = () => {
return zabbixTemplateFormat(varValue);
};
let result = ctx.ds.replaceTemplateVars(target);
expect(result).to.equal(expectedResult);
done();
}
/*
* Alphanumerics, spaces, dots, dashes and underscores
* are allowed in Zabbix host name.
* 'AaBbCc0123 .-_'
*/
it('should return properly escaped regex', (done) => {
let target = '$host';
let template_var_value = 'AaBbCc0123 .-_';
let expected_result = '/^AaBbCc0123 \\.-_$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
/*
* Single-value variable
* $host = backend01
* $host => /^backend01|backend01$/
*/
it('should return proper regex for single value', (done) => {
let target = '$host';
let template_var_value = 'backend01';
let expected_result = '/^backend01$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
/*
* Multi-value variable
* $host = [backend01, backend02]
* $host => /^(backend01|backend01)$/
*/
it('should return proper regex for multi-value', (done) => {
let target = '$host';
let template_var_value = ['backend01', 'backend02'];
let expected_result = '/^(backend01|backend02)$/';
testReplacingVariable(target, template_var_value, expected_result, done);
});
});
describe('When invoking metricFindQuery()', () => {
beforeEach(() => {
ctx.ds.replaceTemplateVars = (str) => str;
ctx.ds.zabbix = {
getGroups: () => Q.when([]),
getHosts: () => Q.when([]),
getApps: () => Q.when([]),
getItems: () => Q.when([])
};
});
it('should return groups', (done) => {
const tests = [
{query: '*', expect: '/.*/'},
{query: '', expect: ''},
{query: 'Backend', expect: 'Backend'},
{query: 'Back*', expect: 'Back*'}
];
let getGroups = sinon.spy(ctx.ds.zabbix, 'getGroups');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getGroups).to.have.been.calledWith(test.expect);
getGroups.reset();
}
done();
});
it('should return hosts', (done) => {
const tests = [
{query: '*.*', expect: '/.*/'},
{query: '.', expect: ''},
{query: 'Backend.*', expect: 'Backend'},
{query: 'Back*.', expect: 'Back*'}
];
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getHosts).to.have.been.calledWith(test.expect);
getHosts.reset();
}
done();
});
it('should return applications', (done) => {
const tests = [
{query: '*.*.*', expect: ['/.*/', '/.*/']},
{query: '.*.', expect: ['', '/.*/']},
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01']},
{query: 'Back*.*.', expect: ['Back*', '/.*/']}
];
let getApps = sinon.spy(ctx.ds.zabbix, 'getApps');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
getApps.reset();
}
done();
});
it('should return items', (done) => {
const tests = [
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '']},
{query: '.*.*.*', expect: ['', '/.*/', '']},
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '']},
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu']}
];
let getItems = sinon.spy(ctx.ds.zabbix, 'getItems');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getItems)
.to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
getItems.reset();
}
done();
});
it('should invoke method with proper arguments', (done) => {
let query = '*.*';
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
ctx.ds.metricFindQuery(query);
expect(getHosts).to.have.been.calledWith('/.*/');
done();
});
});
});

View File

@@ -0,0 +1,111 @@
import _ from 'lodash';
import moment from 'moment';
var units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
export function parse(text, roundUp) {
if (!text) { return undefined; }
if (moment.isMoment(text)) { return text; }
if (_.isDate(text)) { return moment(text); }
var time;
var mathString = '';
var index;
var parseString;
if (text.substring(0, 3) === 'now') {
time = moment();
mathString = text.substring('now'.length);
} else {
index = text.indexOf('||');
if (index === -1) {
parseString = text;
mathString = ''; // nothing else
} else {
parseString = text.substring(0, index);
mathString = text.substring(index + 2);
}
// We're going to just require ISO8601 timestamps, k?
time = moment(parseString, moment.ISO_8601);
}
if (!mathString.length) {
return time;
}
return parseDateMath(mathString, time, roundUp);
}
export function isValid(text) {
var date = parse(text);
if (!date) {
return false;
}
if (moment.isMoment(date)) {
return date.isValid();
}
return false;
}
export function parseDateMath(mathString, time, roundUp) {
var dateTime = time;
var i = 0;
var len = mathString.length;
while (i < len) {
var c = mathString.charAt(i++);
var type;
var num;
var unit;
if (c === '/') {
type = 0;
} else if (c === '+') {
type = 1;
} else if (c === '-') {
type = 2;
} else {
return undefined;
}
if (isNaN(mathString.charAt(i))) {
num = 1;
} else if (mathString.length === 2) {
num = mathString.charAt(i);
} else {
var numFrom = i;
while (!isNaN(mathString.charAt(i))) {
i++;
if (i > 10) { return undefined; }
}
num = parseInt(mathString.substring(numFrom, i), 10);
}
if (type === 0) {
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
if (num !== 1) {
return undefined;
}
}
unit = mathString.charAt(i++);
if (!_.includes(units, unit)) {
return undefined;
} else {
if (type === 0) {
if (roundUp) {
dateTime.endOf(unit);
} else {
dateTime.startOf(unit);
}
} else if (type === 1) {
dateTime.add(num, unit);
} else if (type === 2) {
dateTime.subtract(num, unit);
}
}
}
return dateTime;
}

View File

@@ -0,0 +1,49 @@
// JSHint options
/* globals global: false */
import prunk from 'prunk';
import {jsdom} from 'jsdom';
import chai from 'chai';
// import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as dateMath from './modules/datemath';
// Mock angular module
var angularMocks = {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
var datemathMock = {
parse: dateMath.parse,
parseDateMath: dateMath.parseDateMath,
isValid: dateMath.isValid
};
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
prunk.mock('./css/query-editor.css!', 'no css, dude.');
prunk.mock('app/plugins/sdk', {
QueryCtrl: null
});
prunk.mock('app/core/utils/datemath', datemathMock);
prunk.mock('angular', angularMocks);
prunk.mock('jquery', 'module not found');
// Setup jsdom
// Required for loading angularjs
global.document = jsdom('<html><head><script></script></head><body></body></html>');
global.window = global.document.parentWindow;
global.navigator = window.navigator = {};
global.Node = window.Node;
// Setup Chai
chai.should();
chai.use(sinonChai);
global.assert = chai.assert;
global.expect = chai.expect;

View File

@@ -1,7 +1,6 @@
import _ from 'lodash';
import moment from 'moment';
/**
* Expand Zabbix item name
*
@@ -35,7 +34,7 @@ export function isTemplateVariable(str, templateVariables) {
var variables = _.map(templateVariables, variable => {
return '$' + variable.name;
});
return _.contains(variables, str);
return _.includes(variables, str);
} else {
return false;
}
@@ -93,3 +92,27 @@ export function convertToZabbixAPIUrl(url) {
return url.replace(trimSlashPattern, "$1");
}
}
/**
* Wrap function to prevent multiple calls
* when waiting for result.
*/
export function callOnce(func, promiseKeeper) {
return function() {
if (!promiseKeeper) {
promiseKeeper = Promise.resolve(
func.apply(this, arguments)
.then(result => {
promiseKeeper = null;
return result;
})
);
}
return promiseKeeper;
};
}
// Fix for backward compatibility with lodash 2.4
if (!_.includes) {
_.includes = _.contains;
}

View File

@@ -0,0 +1,216 @@
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
import './zabbixAPI.service.js';
import './zabbixCachingProxy.service.js';
// Use factory() instead service() for multiple data sources support.
// Each Zabbix data source instance should initialize its own API instance.
/** @ngInject */
function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
class Zabbix {
constructor(url, username, password, basicAuth, withCredentials, cacheTTL) {
// Initialize Zabbix API
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
// Initialize caching proxy for requests
let cacheOptions = {
enabled: true,
ttl: cacheTTL
};
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions);
// Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAcknowledges = this.zabbixAPI.getAcknowledges.bind(this.zabbixAPI);
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
}
getItemsFromTarget(target, options) {
let parts = ['group', 'host', 'application', 'item'];
let filters = _.map(parts, p => target[p].filter);
return this.getItems(...filters, options);
}
getAllGroups() {
return this.cachingProxy.getGroups();
}
getGroups(groupFilter) {
return this.getAllGroups()
.then(groups => findByFilter(groups, groupFilter));
}
/**
* Get list of host belonging to given groups.
*/
getAllHosts(groupFilter) {
return this.getGroups(groupFilter)
.then(groups => {
let groupids = _.map(groups, 'groupid');
return this.cachingProxy.getHosts(groupids);
});
}
getHosts(groupFilter, hostFilter) {
return this.getAllHosts(groupFilter)
.then(hosts => findByFilter(hosts, hostFilter));
}
/**
* Get list of applications belonging to given groups and hosts.
*/
getAllApps(groupFilter, hostFilter) {
return this.getHosts(groupFilter, hostFilter)
.then(hosts => {
let hostids = _.map(hosts, 'hostid');
return this.cachingProxy.getApps(hostids);
});
}
getApps(groupFilter, hostFilter, appFilter) {
return this.getHosts(groupFilter, hostFilter)
.then(hosts => {
let hostids = _.map(hosts, 'hostid');
if (appFilter) {
return this.cachingProxy.getApps(hostids)
.then(apps => filterByQuery(apps, appFilter));
} else {
return {
appFilterEmpty: true,
hostids: hostids
};
}
});
}
getAllItems(groupFilter, hostFilter, appFilter, options = {}) {
return this.getApps(groupFilter, hostFilter, appFilter)
.then(apps => {
if (apps.appFilterEmpty) {
return this.cachingProxy.getItems(apps.hostids, undefined, options.itemtype);
} else {
let appids = _.map(apps, 'applicationid');
return this.cachingProxy.getItems(undefined, appids, options.itemtype);
}
})
.then(items => {
if (!options.showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
});
}
getItems(groupFilter, hostFilter, appFilter, itemFilter, options = {}) {
return this.getAllItems(groupFilter, hostFilter, appFilter, options)
.then(items => filterByQuery(items, itemFilter));
}
/**
* Build query - convert target filters to array of Zabbix items
*/
getTriggers(groupFilter, hostFilter, appFilter, showTriggers) {
let promises = [
this.getGroups(groupFilter),
this.getHosts(groupFilter, hostFilter),
this.getApps(groupFilter, hostFilter, appFilter)
];
return Promise.all(promises)
.then(results => {
let filteredGroups = results[0];
let filteredHosts = results[1];
let filteredApps = results[2];
let query = {};
if (appFilter) {
query.applicationids = _.flatten(_.map(filteredApps, 'applicationid'));
}
if (hostFilter) {
query.hostids = _.map(filteredHosts, 'hostid');
}
if (groupFilter) {
query.groupids = _.map(filteredGroups, 'groupid');
}
return query;
}).then(query => {
return this.zabbixAPI
.getTriggers(query.groupids, query.hostids, query.applicationids, showTriggers);
});
}
}
return Zabbix;
}
angular
.module('grafana.services')
.factory('Zabbix', ZabbixFactory);
///////////////////////////////////////////////////////////////////////////////
/**
* Find group, host, app or item by given name.
* @param list list of groups, apps or other
* @param name visible name
* @return array with finded element or undefined
*/
function findByName(list, name) {
var finded = _.find(list, {'name': name});
if (finded) {
return [finded];
} else {
return undefined;
}
}
/**
* Different hosts can contains applications and items with same name.
* For this reason use _.filter, which return all elements instead _.find,
* which return only first finded.
* @param {[type]} list list of elements
* @param {[type]} name app name
* @return {[type]} array with finded element or undefined
*/
function filterByName(list, name) {
var finded = _.filter(list, {'name': name});
if (finded) {
return finded;
} else {
return undefined;
}
}
function filterByRegex(list, regex) {
var filterPattern = utils.buildRegex(regex);
return _.filter(list, function (zbx_obj) {
return filterPattern.test(zbx_obj.name);
});
}
function findByFilter(list, filter) {
if (utils.isRegex(filter)) {
return filterByRegex(list, filter);
} else {
return findByName(list, filter);
}
}
function filterByQuery(list, filter) {
if (utils.isRegex(filter)) {
return filterByRegex(list, filter);
} else {
return filterByName(list, filter);
}
}

View File

@@ -4,7 +4,7 @@ import * as utils from './utils';
import './zabbixAPICore.service';
/** @ngInject */
function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
/**
* Zabbix API Wrapper.
@@ -25,8 +25,9 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
};
this.loginPromise = null;
this.loginErrorCount = 0;
this.maxLoginAttempts = 3;
this.$q = $q;
this.alertSrv = alertSrv;
this.zabbixAPICore = zabbixAPICoreService;
@@ -39,32 +40,32 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
//////////////////////////
request(method, params) {
var self = this;
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
.then(function(result) {
return result;
},
// Handle API errors
function(error) {
.catch(error => {
if (isNotAuthorized(error.data)) {
return self.loginOnce().then(
function() {
return self.request(method, params);
},
// Handle user.login method errors
function(error) {
self.alertAPIError(error.data);
});
// Handle auth errors
this.loginErrorCount++;
if (this.loginErrorCount > this.maxLoginAttempts) {
this.loginErrorCount = 0;
return null;
} else {
return this.loginOnce()
.then(() => this.request(method, params));
}
} else {
// Handle API errors
let message = error.data ? error.data : error.statusText;
this.alertAPIError(message);
}
});
}
alertAPIError(message) {
alertAPIError(message, timeout = 5000) {
this.alertSrv.set(
"Zabbix API Error",
message,
'error'
'error',
timeout
);
}
@@ -75,25 +76,16 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
* @return login promise
*/
loginOnce() {
var self = this;
var deferred = this.$q.defer();
if (!self.loginPromise) {
self.loginPromise = deferred.promise;
self.login().then(
function(auth) {
self.loginPromise = null;
self.auth = auth;
deferred.resolve(auth);
},
function(error) {
self.loginPromise = null;
deferred.reject(error);
}
if (!this.loginPromise) {
this.loginPromise = Promise.resolve(
this.login().then(auth => {
this.auth = auth;
this.loginPromise = null;
return auth;
})
);
} else {
return self.loginPromise;
}
return deferred.promise;
return this.loginPromise;
}
/**
@@ -114,6 +106,15 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
// Zabbix API method wrappers //
////////////////////////////////
acknowledgeEvent(eventid, message) {
var params = {
eventids: eventid,
message: message
};
return this.request('event.acknowledge', params);
}
getGroups() {
var params = {
output: ['name'],
@@ -185,13 +186,16 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
}
return this.request('item.get', params)
.then(items => {
return _.forEach(items, item => {
.then(expandItems);
function expandItems(items) {
items.forEach(item => {
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
});
return items;
}
}
getLastValue(itemid) {
@@ -199,48 +203,42 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
output: ['lastvalue'],
itemids: itemid
};
return this.request('item.get', params).then(function(items) {
if (items.length) {
return items[0].lastvalue;
} else {
return null;
}
});
return this.request('item.get', params)
.then(items => items.length ? items[0].lastvalue : null);
}
/**
* Perform history query from Zabbix API
*
* @param {Array} items Array of Zabbix item objects
* @param {Number} time_from Time in seconds
* @param {Number} time_till Time in seconds
* @param {Number} timeFrom Time in seconds
* @param {Number} timeTill Time in seconds
* @return {Array} Array of Zabbix history objects
*/
getHistory(items, time_from, time_till) {
var self = this;
getHistory(items, timeFrom, timeTill) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return this.$q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
// Group items by value type and perform request for each value type
let grouped_items = _.groupBy(items, 'value_type');
let promises = _.map(grouped_items, (items, value_type) => {
let itemids = _.map(items, 'itemid');
let params = {
output: 'extend',
history: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
time_from: timeFrom
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
if (timeTill) {
params.time_till = timeTill;
}
return self.request('history.get', params);
})).then(_.flatten);
return this.request('history.get', params);
});
return Promise.all(promises).then(_.flatten);
}
/**
@@ -252,31 +250,30 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
* @param {Number} time_till Time in seconds
* @return {Array} Array of Zabbix trend objects
*/
getTrend_ZBXNEXT1193(items, time_from, time_till) {
var self = this;
getTrend_ZBXNEXT1193(items, timeFrom, timeTill) {
// Group items by value type
var grouped_items = _.groupBy(items, 'value_type');
// Perform request for each value type
return this.$q.all(_.map(grouped_items, function (items, value_type) {
var itemids = _.map(items, 'itemid');
var params = {
// Group items by value type and perform request for each value type
let grouped_items = _.groupBy(items, 'value_type');
let promises = _.map(grouped_items, (items, value_type) => {
let itemids = _.map(items, 'itemid');
let params = {
output: 'extend',
trend: value_type,
itemids: itemids,
sortfield: 'clock',
sortorder: 'ASC',
time_from: time_from
time_from: timeFrom
};
// Relative queries (e.g. last hour) don't include an end time
if (time_till) {
params.time_till = time_till;
if (timeTill) {
params.time_till = timeTill;
}
return self.request('trend.get', params);
})).then(_.flatten);
return this.request('trend.get', params);
});
return Promise.all(promises).then(_.flatten);
}
getTrend_30(items, time_from, time_till, value_type) {
@@ -300,7 +297,7 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
return self.request('trend.get', params);
}
getITService(/* optional */ serviceids) {
getITService(serviceids) {
var params = {
output: 'extend',
serviceids: serviceids
@@ -308,12 +305,12 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
return this.request('service.get', params);
}
getSLA(serviceids, from, to) {
getSLA(serviceids, timeFrom, timeTo) {
var params = {
serviceids: serviceids,
intervals: [{
from: from,
to: to
from: timeFrom,
to: timeTo
}]
};
return this.request('service.getsla', params);
@@ -327,6 +324,7 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
applicationids: applicationids,
expandDescription: true,
expandData: true,
expandComment: true,
monitored: true,
skipDependent: true,
//only_true: true,
@@ -351,11 +349,11 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
return this.request('trigger.get', params);
}
getEvents(objectids, from, to, showEvents) {
getEvents(objectids, timeFrom, timeTo, showEvents) {
var params = {
output: 'extend',
time_from: from,
time_till: to,
time_from: timeFrom,
time_till: timeTo,
objectids: objectids,
select_acknowledges: 'extend',
selectHosts: 'extend',
@@ -376,10 +374,8 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
};
return this.request('event.get', params)
.then(function (events) {
return _.filter(events, function(event) {
return event.acknowledges.length;
});
.then(events => {
return _.filter(events, (event) => event.acknowledges.length);
});
}
@@ -398,4 +394,4 @@ function isNotAuthorized(message) {
angular
.module('grafana.services')
.factory('zabbixAPIService', ZabbixAPIService);
.factory('zabbixAPIService', ZabbixAPIServiceFactory);

View File

@@ -7,8 +7,7 @@ import angular from 'angular';
class ZabbixAPICoreService {
/** @ngInject */
constructor($q, backendSrv) {
this.$q = $q;
constructor(backendSrv) {
this.backendSrv = backendSrv;
}
@@ -17,8 +16,7 @@ class ZabbixAPICoreService {
* @return {object} response.result
*/
request(api_url, method, params, options, auth) {
var deferred = this.$q.defer();
var requestData = {
let requestData = {
jsonrpc: '2.0',
method: method,
params: params,
@@ -27,20 +25,19 @@ class ZabbixAPICoreService {
if (auth === "") {
// Reject immediately if not authenticated
deferred.reject({data: "Not authorised."});
return deferred.promise;
return Promise.reject(new ZabbixAPIError({data: "Not authorised."}));
} else if (auth) {
// Set auth parameter only if it needed
requestData.auth = auth;
}
var requestOptions = {
let requestOptions = {
method: 'POST',
url: api_url,
data: requestData,
headers: {
'Content-Type': 'application/json'
},
url: api_url,
data: requestData
}
};
// Set request options for basic auth
@@ -51,20 +48,23 @@ class ZabbixAPICoreService {
requestOptions.headers.Authorization = options.basicAuth;
}
this.backendSrv.datasourceRequest(requestOptions).then(function (response) {
// General connection issues
if (!response.data) {
deferred.reject(response);
return this.datasourceRequest(requestOptions);
}
datasourceRequest(requestOptions) {
return this.backendSrv.datasourceRequest(requestOptions)
.then(response => {
if (!response.data) {
return Promise.reject(new ZabbixAPIError({data: "General Error, no data"}));
} else if (response.data.error) {
// Handle Zabbix API errors
else if (response.data.error) {
deferred.reject(response.data.error);
return Promise.reject(new ZabbixAPIError(response.data.error));
}
deferred.resolve(response.data.result);
// Success
return response.data.result;
});
return deferred.promise;
}
/**
@@ -72,7 +72,7 @@ class ZabbixAPICoreService {
* @return {string} auth token
*/
login(api_url, username, password, options) {
var params = {
let params = {
user: username,
password: password
};
@@ -89,15 +89,18 @@ class ZabbixAPICoreService {
}
// Define zabbix API exception type
function ZabbixException(error) {
export class ZabbixAPIError {
constructor(error) {
this.code = error.code;
this.errorType = error.message;
this.name = error.data;
this.message = error.data;
}
this.data = error.data;
}
ZabbixException.prototype.toString = function() {
return this.errorType + ": " + this.message;
};
toString() {
return this.name + ": " + this.message;
}
}
angular
.module('grafana.services')

View File

@@ -1,238 +0,0 @@
import angular from 'angular';
import _ from 'lodash';
import * as utils from './utils';
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
/** @ngInject */
angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $interval) {
class ZabbixCachingProxy {
constructor(zabbixAPI, ttl) {
this.zabbixAPI = zabbixAPI;
this.ttl = ttl;
this.$q = $q;
// Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
this._applications = undefined;
this._items = undefined;
this.storage = {
history: {},
trends: {}
};
// Check is a service initialized or not
this._initialized = undefined;
this.refreshPromise = false;
this.historyPromises = {};
// Wrap _refresh() method to call it once.
this.refresh = callOnce(this._refresh, this.refreshPromise);
// Update cache periodically
$interval(_.bind(this.refresh, this), this.ttl);
// Don't run duplicated history requests
this.getHistory = callHistoryOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises);
// Don't run duplicated requests
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
this.groupPromises);
this.hostPromises = {};
this.getHostsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getHosts, this.zabbixAPI),
this.hostPromises);
this.appPromises = {};
this.getAppsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getApps, this.zabbixAPI),
this.appPromises);
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
this.itemPromises);
}
_refresh() {
var self = this;
var promises = [
this.zabbixAPI.getGroups()
];
return this.$q.all(promises).then(function(results) {
if (results.length) {
self._groups = results[0];
}
self._initialized = true;
});
}
getGroups() {
var self = this;
if (this._groups) {
return this.$q.when(self._groups);
} else {
return this.getGroupsOnce()
.then(groups => {
self._groups = groups;
return self._groups;
});
}
}
getHosts(groupids) {
//var self = this;
return this.getHostsOnce(groupids)
.then(hosts => {
// iss #196 - disable caching due performance issues
//self._hosts = _.union(self._hosts, hosts);
return hosts;
});
}
getApps(hostids) {
return this.getAppsOnce(hostids)
.then(apps => {
return apps;
});
}
getItems(hostids, appids, itemtype) {
//var self = this;
return this.getItemsOnce(hostids, appids, itemtype)
.then(items => {
// iss #196 - disable caching due performance issues
//self._items = _.union(self._items, items);
return items;
});
}
getHistoryFromCache(items, time_from, time_till) {
var deferred = this.$q.defer();
var historyStorage = this.storage.history;
var full_history;
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
return !historyStorage[itemid];
});
if (expired.length) {
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
var grouped_history = _.groupBy(history, 'itemid');
_.forEach(expired, function(item) {
var itemid = item.itemid;
historyStorage[itemid] = item;
historyStorage[itemid].time_from = time_from;
historyStorage[itemid].time_till = time_till;
historyStorage[itemid].history = grouped_history[itemid];
});
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
});
} else {
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
}
return deferred.promise;
}
getHistoryFromAPI(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till);
}
getHost(hostid) {
return _.find(this._hosts, {'hostid': hostid});
}
getItem(itemid) {
return _.find(this._items, {'itemid': itemid});
}
}
function callAPIRequestOnce(func, promiseKeeper) {
return function() {
var hash = getAPIRequestHash(arguments);
var deferred = $q.defer();
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper[hash] = null;
});
} else {
return promiseKeeper[hash];
}
return deferred.promise;
};
}
function callHistoryOnce(func, promiseKeeper) {
return function() {
var itemids = _.map(arguments[0], 'itemid');
var stamp = itemids.join() + arguments[1] + arguments[2];
var hash = stamp.getHash();
var deferred = $q.defer();
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper[hash] = null;
});
} else {
return promiseKeeper[hash];
}
return deferred.promise;
};
}
function callOnce(func, promiseKeeper) {
return function() {
var deferred = $q.defer();
if (!promiseKeeper) {
promiseKeeper = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper = null;
});
} else {
return promiseKeeper;
}
return deferred.promise;
};
}
return ZabbixCachingProxy;
});
function getAPIRequestHash(args) {
var requestStamp = _.map(args, arg => {
if (arg === undefined) {
return 'undefined';
} else {
return arg.toString();
}
}).join();
return requestStamp.getHash();
}
String.prototype.getHash = function() {
var hash = 0, i, chr, len;
if (this.length === 0) {
return hash;
}
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

View File

@@ -0,0 +1,191 @@
import angular from 'angular';
import _ from 'lodash';
// Use factory() instead service() for multiple datasources support.
// Each datasource instance must initialize its own cache.
/** @ngInject */
function ZabbixCachingProxyFactory() {
class ZabbixCachingProxy {
constructor(zabbixAPI, cacheOptions) {
this.zabbixAPI = zabbixAPI;
this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
// Internal objects for data storing
this.cache = {
groups: {},
hosts: {},
applications: {},
items: {},
history: {},
trends: {}
};
this.historyPromises = {};
// Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises, getHistoryRequestHash);
// Don't run duplicated requests
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
this.groupPromises, getRequestHash);
this.hostPromises = {};
this.getHostsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getHosts, this.zabbixAPI),
this.hostPromises, getRequestHash);
this.appPromises = {};
this.getAppsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getApps, this.zabbixAPI),
this.appPromises, getRequestHash);
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
this.itemPromises, getRequestHash);
}
isExpired(cacheObject) {
if (cacheObject) {
let object_age = Date.now() - cacheObject.timestamp;
return !(cacheObject.timestamp && object_age < this.ttl);
} else {
return true;
}
}
/**
* Check that result is present in cache and up to date
* or send request to API.
*/
proxyRequest(request, params, cacheObject) {
let hash = getRequestHash(params);
if (this.cacheEnabled && !this.isExpired(cacheObject[hash])) {
return Promise.resolve(cacheObject[hash].value);
} else {
return request(...params)
.then(result => {
cacheObject[hash] = {
value: result,
timestamp: Date.now()
};
return result;
});
}
}
getGroups() {
return this.proxyRequest(this.getGroupsOnce, [], this.cache.groups);
}
getHosts(groupids) {
return this.proxyRequest(this.getHostsOnce, [groupids], this.cache.hosts);
}
getApps(hostids) {
return this.proxyRequest(this.getAppsOnce, [hostids], this.cache.applications);
}
getItems(hostids, appids, itemtype) {
let params = [hostids, appids, itemtype];
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
}
getHistoryFromCache(items, time_from, time_till) {
var historyStorage = this.cache.history;
var full_history;
var expired = _.filter(_.keyBy(items, 'itemid'), (item, itemid) => {
return !historyStorage[itemid];
});
if (expired.length) {
return this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
var grouped_history = _.groupBy(history, 'itemid');
_.forEach(expired, item => {
var itemid = item.itemid;
historyStorage[itemid] = item;
historyStorage[itemid].time_from = time_from;
historyStorage[itemid].time_till = time_till;
historyStorage[itemid].history = grouped_history[itemid];
});
full_history = _.map(items, item => {
return historyStorage[item.itemid].history;
});
return _.flatten(full_history, true);
});
} else {
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
return Promise.resolve(_.flatten(full_history, true));
}
}
getHistoryFromAPI(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till);
}
}
return ZabbixCachingProxy;
}
angular
.module('grafana.services')
.factory('ZabbixCachingProxy', ZabbixCachingProxyFactory);
/**
* Wrap zabbix API request to prevent multiple calls
* with same params when waiting for result.
*/
function callAPIRequestOnce(func, promiseKeeper, argsHashFunc) {
return function() {
var hash = argsHashFunc(arguments);
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = Promise.resolve(
func.apply(this, arguments)
.then(result => {
promiseKeeper[hash] = null;
return result;
})
);
}
return promiseKeeper[hash];
};
}
function getRequestHash(args) {
var requestStamp = _.map(args, arg => {
if (arg === undefined) {
return 'undefined';
} else {
if (_.isArray(arg)) {
return arg.sort().toString();
} else {
return arg.toString();
}
}
}).join();
return requestStamp.getHash();
}
function getHistoryRequestHash(args) {
let itemids = _.map(args[0], 'itemid');
let stamp = itemids.join() + args[1] + args[2];
return stamp.getHash();
}
String.prototype.getHash = function() {
var hash = 0, i, chr, len;
if (this.length !== 0) {
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
}
return hash;
};
// Fix for backward compatibility with lodash 2.4
if (!_.keyBy) {_.keyBy = _.indexBy;}

View File

@@ -0,0 +1,113 @@
import angular from 'angular';
import $ from 'jquery';
import Drop from 'tether-drop';
/** @ngInject */
angular
.module('grafana.directives')
.directive('ackTooltip', function($sanitize, $compile) {
let buttonTemplate = '<a bs-tooltip="\'Acknowledges ({{trigger.acknowledges.length}})\'"' +
'<i ng-class="' +
"{'fa fa-comments': trigger.acknowledges.length, " +
"'fa fa-comments-o': !trigger.acknowledges.length, " +
'}"></i></a>';
return {
scope: {
ack: "=",
trigger: "=",
onAck: "=",
context: "="
},
link: function(scope, element) {
let acknowledges = scope.ack;
let $button = $(buttonTemplate);
$button.appendTo(element);
$button.click(function() {
let tooltip = '<div>';
if (acknowledges && acknowledges.length) {
tooltip += '<table class="table"><thead><tr>' +
'<th class="ack-time">Time</th>' +
'<th class="ack-user">User</th>' +
'<th class="ack-comments">Comments</th>' +
'</tr></thead><tbody>';
for (let ack of acknowledges) {
tooltip += '<tr><td>' + ack.time + '</td>' +
'<td>' + ack.user + '</td>' +
'<td>' + ack.message + '</td></tr>';
}
tooltip += '</tbody></table>';
} else {
tooltip += 'Add acknowledge';
}
let addAckButtonTemplate = '<div class="ack-add-button">' +
'<button id="add-acknowledge-btn"' +
'class="btn btn-mini btn-inverse gf-form-button">' +
'<i class="fa fa-plus"></i>' +
'</button></div>';
tooltip += addAckButtonTemplate;
tooltip += '</div>';
let drop = new Drop({
target: element[0],
content: tooltip,
position: "bottom left",
classes: 'drop-popover ack-tooltip',
openOn: 'hover',
hoverCloseDelay: 500,
tetherOptions: {
constraints: [{to: 'window', pin: true, attachment: "both"}]
}
});
drop.open();
drop.on('close', closeDrop);
$('#add-acknowledge-btn').on('click', onAddAckButtonClick);
function onAddAckButtonClick() {
let inputTemplate = '<div class="ack-input-group">' +
'<input type="text" id="ack-message">' +
'<button id="send-ack-button"' +
'class="btn btn-mini btn-inverse gf-form-button">' +
'Acknowledge </button>' +
'<button id="cancel-ack-button"' +
'class="btn btn-mini btn-inverse gf-form-button">' +
'Cancel' +
'</button></input></div>';
let $input = $(inputTemplate);
let $addAckButton = $('.ack-tooltip .ack-add-button');
$addAckButton.replaceWith($input);
$('.ack-tooltip #cancel-ack-button').on('click', onAckCancelButtonClick);
$('.ack-tooltip #send-ack-button').on('click', onAckSendlButtonClick);
}
function onAckCancelButtonClick() {
$('.ack-tooltip .ack-input-group').replaceWith(addAckButtonTemplate);
$('#add-acknowledge-btn').on('click', onAddAckButtonClick);
}
function onAckSendlButtonClick() {
let message = $('.ack-tooltip #ack-message')[0].value;
let onAck = scope.onAck.bind(scope.context);
onAck(scope.trigger, message).then(() => {
closeDrop();
});
}
function closeDrop() {
setTimeout(function() {
drop.destroy();
});
}
});
$compile(element.contents())(scope);
}
};
});

View File

@@ -150,7 +150,7 @@
<strong>Show fields</strong>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="hostField">Host</label>
<label class="checkbox-label" for="hostField">Host Name</label>
<input class="cr1"
id="hostField"
type="checkbox"
@@ -158,6 +158,15 @@
ng-checked="editor.panel.hostField">
<label for="hostField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="hostField">Host Technical Name</label>
<input class="cr1"
id="hostTechNameField"
type="checkbox"
ng-model="editor.panel.hostTechNameField"
ng-checked="editor.panel.hostTechNameField">
<label for="hostTechNameField" class="cr1"></label>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="statusField">Status</label>
<input class="cr1"
@@ -176,6 +185,14 @@
ng-checked="editor.panel.severityField">
<label for="severityField" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 100px">
<strong>&nbsp;</strong>
</li>
<li class="tight-form-item">
<label class="checkbox-label" for="lastChangeField">Last change</label>
<input class="cr1"
@@ -194,7 +211,7 @@
ng-checked="editor.panel.ageField">
<label for="ageField" class="cr1"></label>
</li>
<li class="tight-form-item last">
<li class="tight-form-item">
<label class="checkbox-label" for="infoField">Info</label>
<input class="cr1"
id="infoField"
@@ -206,6 +223,7 @@
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item">
@@ -273,7 +291,7 @@
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item"
ng-style="{background:editor.panel.okEventColor}"
@@ -289,5 +307,31 @@
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item"
ng-style="{background:editor.panel.ackEventColor}"
style="width: 160px; color: white">
<span style="padding-left: 25px"> Acknowledged color </span>
</li>
<li class="tight-form-item">
<spectrum-picker
ng-model="editor.panel.ackEventColor"
ng-change="editor.panelCtrl.refresh()">
</spectrum-picker>
</li>
<li class="tight-form-item last" style="width: 28px">
<label class="checkbox-label" for="ack-event-color"></label>
<input class="cr1"
id="ack-event-color"
type="checkbox"
ng-model="editor.panel.markAckEvents"
ng-checked="editor.panel.markAckEvents"
ng-change="editor.panelCtrl.refresh()">
<label for="ack-event-color" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>

View File

@@ -19,12 +19,11 @@ import '../datasource-zabbix/css/query-editor.css!';
class TriggerPanelEditorCtrl {
/** @ngInject */
constructor($scope, $rootScope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
constructor($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.$q = $q;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv;
@@ -62,8 +61,6 @@ class TriggerPanelEditorCtrl {
};
_.defaults(this, scopeDefaults);
var self = this;
// Get zabbix data sources
var datasources = _.filter(this.datasourceSrv.getMetricSources(), datasource => {
return datasource.meta.id === 'alexanderzobnin-zabbix-datasource';
@@ -75,59 +72,47 @@ class TriggerPanelEditorCtrl {
this.panel.datasource = this.datasources[0];
}
// Load datasource
this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
self.datasource = datasource;
self.initFilters();
self.panelCtrl.refresh();
this.datasourceSrv.get(this.panel.datasource)
.then(datasource => {
this.datasource = datasource;
this.queryBuilder = datasource.queryBuilder;
this.initFilters();
this.panelCtrl.refresh();
});
}
initFilters() {
var self = this;
return this.$q
.when(this.suggestGroups())
.then(() => {return self.suggestHosts();})
.then(() => {return self.suggestApps();});
return Promise.all([
this.suggestGroups(),
this.suggestHosts(),
this.suggestApps()
]);
}
suggestGroups() {
var self = this;
return this.datasource.zabbixCache
.getGroups()
return this.queryBuilder.getAllGroups()
.then(groups => {
self.metric.groupList = groups;
this.metric.groupList = groups;
return groups;
});
}
suggestHosts() {
var self = this;
var groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
return this.datasource.queryProcessor
.filterGroups(self.metric.groupList, groupFilter)
.then(groups => {
var groupids = _.map(groups, 'groupid');
return self.datasource.zabbixAPI
.getHosts(groupids)
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
return this.queryBuilder.getAllHosts(groupFilter)
.then(hosts => {
self.metric.hostList = hosts;
this.metric.hostList = hosts;
return hosts;
});
});
}
suggestApps() {
var self = this;
var hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
return this.datasource.queryProcessor
.filterHosts(self.metric.hostList, hostFilter)
.then(hosts => {
var hostids = _.map(hosts, 'hostid');
return self.datasource.zabbixAPI
.getApps(hostids)
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
let hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
return this.queryBuilder.getAllApps(groupFilter, hostFilter)
.then(apps => {
return self.metric.appList = apps;
});
this.metric.appList = apps;
return apps;
});
}

View File

@@ -9,6 +9,11 @@
Host
</div>
</th>
<th ng-if="ctrl.panel.hostTechNameField" style="width: 15%">
<div class="triggers-panel-table-header-inner pointer">
Technical Name
</div>
</th>
<th ng-if="ctrl.panel.statusField" style="width: 85px">
<div class="triggers-panel-table-header-inner pointer">Status</div>
</th>
@@ -31,21 +36,31 @@
</thead>
<tbody>
<tr ng-repeat="trigger in ctrl.triggerList">
<td ng-if="ctrl.panel.hostField">
<div>
<span><strong>{{trigger.host}}</strong></span>
</div>
</td>
<td ng-if="ctrl.panel.hostTechNameField">
<div>
<span><strong>{{trigger.hostTechName}}</strong></span>
</div>
</td>
<td ng-if="ctrl.panel.statusField" style="background-color: {{trigger.color}}; color: white">
<div>
{{ctrl.triggerStatusMap[trigger.value]}}
</div>
</td>
<td ng-if="ctrl.panel.severityField" style="background-color: {{trigger.color}}; color: white">
<div>
{{trigger.severity}}
</div>
</td>
<td style="background-color: {{trigger.color}}; color: white">
<div>
{{trigger.description}}
@@ -68,43 +83,16 @@
<small>{{trigger.comments}}</small>
</div>
</div>
</td>
<!-- Trigger acknowledges -->
<div class="collapse"
id="acknowledges-{{trigger.triggerid}}"
ng-if="trigger.showAcknowledges">
<div style="padding-top: 12px;">
<table class="table table-condensed">
<thead>
<tr>
<th><small>Time</small></th>
<th><small>User</small></th>
<th><small>Comments</small></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="ack in trigger.acknowledges">
<td>
<small>{{ack.time}}</small>
</td>
<td>
<small>{{ack.user}}</small>
</td>
<td>
<small>{{ack.message}}</small>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
<td ng-if="ctrl.panel.lastChangeField">
{{trigger.lastchange}}
</td>
<td ng-if="ctrl.panel.ageField">
{{trigger.age}}
</td>
<td ng-if="ctrl.panel.infoField">
<!-- Trigger Url -->
@@ -121,12 +109,12 @@
</span>
<!-- Trigger acknowledges -->
<a ng-if="trigger.acknowledges"
role="button"
ng-click="ctrl.switchAcknowledges(trigger)"
bs-tooltip="'Acknowledges ({{trigger.acknowledges.length}})'">
<i class="fa fa-comments"></i>
</a>
<ack-tooltip
ack="trigger.acknowledges"
trigger="trigger"
on-ack="ctrl.acknowledgeTrigger"
context="ctrl">
</ack-tooltip>
</td>
</tr>
</tbody>

View File

@@ -16,6 +16,7 @@ import moment from 'moment';
import * as utils from '../datasource-zabbix/utils';
import {MetricsPanelCtrl} from 'app/plugins/sdk';
import {triggerPanelEditor} from './editor';
import './ack-tooltip.directive';
import './css/panel_triggers.css!';
var defaultSeverity = [
@@ -47,6 +48,7 @@ var panelDefaults = {
showEvents: { text: 'Problems', value: '1' },
triggerSeverity: defaultSeverity,
okEventColor: 'rgba(0, 245, 153, 0.45)',
ackEventColor: 'rgba(0, 0, 0, 0)'
};
var triggerStatusMap = {
@@ -59,10 +61,11 @@ var defaultTimeFormat = "DD MMM YYYY HH:mm:ss";
class TriggerPanelCtrl extends MetricsPanelCtrl {
/** @ngInject */
constructor($scope, $injector, $q, $element, datasourceSrv, templateSrv) {
constructor($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv) {
super($scope, $injector);
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.contextSrv = contextSrv;
this.triggerStatusMap = triggerStatusMap;
this.defaultTimeFormat = defaultTimeFormat;
@@ -103,9 +106,9 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
var self = this;
// Load datasource
return this.datasourceSrv.get(this.panel.datasource).then(datasource => {
var zabbix = datasource.zabbixAPI;
var queryProcessor = datasource.queryProcessor;
return this.datasourceSrv.get(this.panel.datasource)
.then(datasource => {
var zabbix = datasource.zabbix;
var showEvents = self.panel.showEvents.value;
var triggerFilter = self.panel.triggers;
@@ -114,19 +117,14 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
var buildQuery = queryProcessor.buildTriggerQuery(groupFilter, hostFilter, appFilter);
return buildQuery.then(query => {
return zabbix.getTriggers(query.groupids,
query.hostids,
query.applicationids,
showEvents)
.then(triggers => {
var getTriggers = zabbix.getTriggers(groupFilter, hostFilter, appFilter, showEvents);
return getTriggers.then(triggers => {
return _.map(triggers, trigger => {
var triggerObj = trigger;
let triggerObj = trigger;
// Format last change and age
trigger.lastchangeUnix = Number(trigger.lastchange);
var timestamp = moment.unix(trigger.lastchangeUnix);
let timestamp = moment.unix(trigger.lastchangeUnix);
if (self.panel.customLastChangeFormat) {
// User defined format
triggerObj.lastchange = timestamp.format(self.panel.lastChangeFormat);
@@ -138,6 +136,7 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
// Set host that the trigger belongs
if (trigger.hosts.length) {
triggerObj.host = trigger.hosts[0].name;
triggerObj.hostTechName = trigger.hosts[0].host;
}
// Set color
@@ -171,11 +170,20 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
if (event) {
trigger.acknowledges = _.map(event.acknowledges, ack => {
var time = new Date(+ack.clock * 1000);
ack.time = time.toLocaleString();
let timestamp = moment.unix(ack.clock);
if (self.panel.customLastChangeFormat) {
ack.time = timestamp.format(self.panel.lastChangeFormat);
} else {
ack.time = timestamp.format(self.defaultTimeFormat);
}
ack.user = ack.alias + ' (' + ack.name + ' ' + ack.surname + ')';
return ack;
});
// Mark acknowledged triggers with different color
if (self.panel.markAckEvents && trigger.acknowledges.length) {
trigger.color = self.panel.ackEventColor;
}
}
});
@@ -209,7 +217,7 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
}
// Limit triggers number
self.triggerList = _.first(triggerList, self.panel.limit);
self.triggerList = triggerList.slice(0, self.panel.limit);
// Notify panel that request is finished
self.setTimeQueryEnd();
@@ -217,15 +225,23 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
});
});
});
});
}
switchComment(trigger) {
trigger.showComment = !trigger.showComment;
}
switchAcknowledges(trigger) {
trigger.showAcknowledges = !trigger.showAcknowledges;
acknowledgeTrigger(trigger, message) {
let self = this;
let eventid = trigger.lastEvent.eventid;
let grafana_user = this.contextSrv.user.name;
let ack_message = grafana_user + ' (Grafana): ' + message;
return this.datasourceSrv.get(this.panel.datasource).then(datasource => {
let zabbix = datasource.zabbixAPI;
return zabbix.acknowledgeEvent(eventid, ack_message).then(() => {
self.refresh();
});
});
}
}

View File

@@ -106,3 +106,37 @@ $grafanaListAccent: lighten($dark-2, 2%);
height: 0px;
line-height: 0px;
}
.ack-tooltip {
.drop-content {
// Rewrite tooltip width
max-width: 70rem !important;
min-width: 30rem !important;
}
.ack-comments {
width: 60%;
}
.ack-add-button {
padding-top: 1rem;
}
table td, th {
padding-right: 1rem;
}
.ack-input-group {
padding-top: 1rem;
input {
border: 1px solid;
border-radius: 2px;
width: 50%;
}
button {
margin-left: 1rem;
}
}
}

View File

@@ -31,8 +31,8 @@
{"name": "Metric Editor", "path": "img/screenshot-metric_editor.png"},
{"name": "Triggers", "path": "img/screenshot-triggers.png"}
],
"version": "3.0.0",
"updated": "2016-07-03"
"version": "3.1.2",
"updated": "2016-11-09"
},
"includes": [