Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS support. #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions example-tls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var ZabbixSender = require('./index');
var Sender = new ZabbixSender({
host: 'zabbix.example.com',
key_file_path: 'private-key.pem',
cert_file_path: 'public-cert.pem',
// optional socket_options
// socket_options: {
// rejectUnauthorized: false,
// }
});

// Add items to request
Sender.addItem('webserver', 'httpd.running', 0);
Sender.addItem('dbserver', 'mysql.ping', 1);

// Add item with default host
Sender.addItem('httpd.logs.size', 1024);

// Send the items to zabbix trapper
Sender.send(function(err, res) {
if (err) {
throw err;
}

// print the response object
console.dir(res);
});
1 change: 0 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ Sender.send(function(err, res) {
// print the response object
console.dir(res);
});

30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
var Net = require('net'),
hostname = require("os").hostname();
var tls = require('tls');
var fs = require('fs');
var hostname = require("os").hostname();
var net = require('net');

var ZabbixSender = module.exports = function(opts) {
opts = (typeof opts !== 'undefined') ? opts : {};

this.host = opts.host || 'localhost';
this.port = parseInt(opts.port) || 10051;
this.timeout = parseInt(opts.timeout) || 5000;
this.with_ns = opts.with_ns || false;
this.with_timestamps = this.with_ns || opts.with_timestamps || false;
this.items_host = opts.items_host || hostname;
this.items = [];
this.socket_options = opts.socket_options || {};
this.socket_options.host = opts.host || 'localhost';
this.socket_options.port = parseInt(opts.port) || 10051;

if (typeof opts.key_file_path !== 'undefined') {
this.socket_options.key = fs.readFileSync(opts.key_file_path);
}

if (typeof opts.cert_file_path !== 'undefined') {
this.socket_options.cert = fs.readFileSync(opts.cert_file_path);
}

// prepare items array
this.clearItems();
Expand Down Expand Up @@ -62,19 +74,23 @@ ZabbixSender.prototype.send = function(callback) {
error = false,
items = this.items,
data = prepareData(items, this.with_timestamps, this.with_ns),
client = new Net.Socket(),
response = new Buffer(0);

// uncoment when debugging
//console.log(data.slice(13).toString());
if (typeof this.socket_options.key !== 'undefined' && typeof this.socket_options.cert !== 'undefined') {
var client = new tls.TLSSocket();
} else {
var client = new net.Socket();
}

// reset items array
this.clearItems();

// set socket timeout
client.setTimeout(this.timeout);

client.connect(this.port, this.host, function() {
client.connect(this.socket_options, function() {
client.write(data);
});

Expand Down Expand Up @@ -133,4 +149,4 @@ function prepareData(items, with_timestamps, with_ns) {
header.write('ZBXD\x01');
header.writeInt32LE(payload.length, 5);
return Buffer.concat([header, new Buffer('\x00\x00\x00\x00'), payload]);
}
}