Skip to content

Commit

Permalink
preliminary working version using node-zabbix-sender.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steev Hise committed Sep 1, 2017
1 parent 29a3a6f commit c17eaf7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -3,6 +3,7 @@
## Overview

This is a pluggable backend for [StatsD](https://github.com/etsy/statsd), which publishes stats to Zabbix.
This fork adds the option of using the node-zabbix-sender module instead of a separate zabbix-sender binary.

## Installation

Expand Down Expand Up @@ -39,6 +40,7 @@ hostname to be sent to zabbix. For example, you may run statsd on each
zabbix monitored host and configure statsd-zabbix-backend to always send the
hostname of the current host. This is useful for sources other than logstash
which do not encode the hostname in the statsd key.
Leave zabbixSender config variable unset to use the node-zabbix-sender module.

### Logstash

Expand Down
60 changes: 45 additions & 15 deletions lib/zabbix.js
Expand Up @@ -10,7 +10,7 @@
*
* zabbixHost: Hostname of zabbix server.
* zabbixPort: Port to contact zabbix server at.
* zabbixSender: Path to zabbix_sender utility.
* zabbixSender: Path to zabbix_sender utility. (leave blank to use node-zabbix-sender module instead)
*
* Optional
* hostname: Static hostname associated with the stats to send to zabbix
Expand All @@ -26,30 +26,53 @@ var debug;
var flushInterval;
var zabbixHost;
var zabbixPort;
var zabbixSender;
var zabbixCmd;
var zabbixSender = null;
var senderModule;

var zabbixStats = {};

var post_stats = function zabbix_post_stats(statString) {
if (zabbixHost) {
if (debug) {
util.log(statString);
util.log('statsString: ' + statString);
}

try {
var zabbixExec = proc.exec(zabbixCmd, function(error, stdout, stderr) {
if (error && error.code !== 0 && debug) {
util.log(zabbixSender + ': ' + stderr);
if(zabbixCmd) {
try {
var zabbixExec = proc.exec(zabbixCmd, function (error, stdout, stderr) {
if (error && error.code !== 0 && debug) {
util.log(zabbixSender + ': ' + stderr);
}
});

zabbixExec.stdin.write(statString);
zabbixExec.stdin.end();
} catch (e) {
if (debug) {
util.log(e);
}
zabbixStats.last_exception = Math.round(new Date().getTime() / 1000);
}
}
else {
// we're using the module, not a shell command
var Sender = new senderModule({host: zabbixHost, with_timestamps: true, items_host: 'test'});
// statString is a bunch of lines to be parsed and added one at a time.
var statArray = statString.split('\n');
statArray.forEach( function (v, i, array) {
var data = v.split(' ',4);
// third one is always the timestamp so leave that out.
Sender.addItem([data[0], data[1]].join('.'), data[3]);
});
Sender.send( function (err,res) {
if (err) {
throw err;
}

zabbixExec.stdin.write(statString);
zabbixExec.stdin.end();
} catch(e) {
if (debug) {
util.log(e);
}
zabbixStats.last_exception = Math.round(new Date().getTime() / 1000);
// print the response object - for testing
console.dir(res);
});
}
}
}
Expand Down Expand Up @@ -98,6 +121,7 @@ var zabbix_host_key_from_config = function (host, key) {
}

var flush_stats = function zabbix_flush(zabbix_host_key, ts, metrics) {
console.log('flushing stats...');
var statString = '';
var numStats = 0;
var key;
Expand Down Expand Up @@ -193,7 +217,13 @@ exports.init = function zabbix_init(startup_time, config, events) {
zabbixPort = config.zabbixPort;
zabbixSender = config.zabbixSender;
zabbix_host_key = config.hostname ? zabbix_host_key_from_config.bind(undefined, config.hostname) : zabbix_host_key_from_encoding;
zabbixCmd = zabbixSender + ' -T -i - -z ' + zabbixHost + ' -p ' + zabbixPort;

if(zabbixSender) {
zabbixCmd = zabbixSender + ' -T -i - -z ' + zabbixHost + ' -p ' + zabbixPort;
}
else {
senderModule = require('node-zabbix-sender');
}

zabbixStats.last_flush = startup_time;
zabbixStats.last_exception = startup_time;
Expand Down
9 changes: 6 additions & 3 deletions package.json
@@ -1,5 +1,5 @@
{
"name": "statsd-zabbix-backend",
"name": "@freecycle/statsd-zabbix-backend",
"description": "A backend for StatsD to emit stats to Zabbix",
"author": "Parker DeBardelaben",
"repository": {
Expand All @@ -8,10 +8,13 @@
},
"version": "0.2.0",
"engine": {
"node": ">=0.10"
"node": ">=8.x"
},
"main": "lib/zabbix.js",
"devDependencies" : {
"devDependencies": {
"unit.js": "*"
},
"dependencies": {
"node-zabbix-sender": "^1.0.8"
}
}

0 comments on commit c17eaf7

Please sign in to comment.