diff --git a/example-tls.js b/example-tls.js new file mode 100644 index 0000000..ecb4f65 --- /dev/null +++ b/example-tls.js @@ -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); +}); \ No newline at end of file diff --git a/example.js b/example.js index 5f28343..555157a 100644 --- a/example.js +++ b/example.js @@ -17,4 +17,3 @@ Sender.send(function(err, res) { // print the response object console.dir(res); }); - diff --git a/index.js b/index.js index 7b13d75..47127dc 100644 --- a/index.js +++ b/index.js @@ -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(); @@ -62,11 +74,15 @@ 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(); @@ -74,7 +90,7 @@ ZabbixSender.prototype.send = function(callback) { // set socket timeout client.setTimeout(this.timeout); - client.connect(this.port, this.host, function() { + client.connect(this.socket_options, function() { client.write(data); }); @@ -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]); -} +} \ No newline at end of file