UNPKG

tsc-zabbix-sender

Version:

A Typescript implementation to sent data to Zabbix using the Zabbix trapper protocol.

117 lines (116 loc) 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZabbixSender = void 0; var console_1 = require("console"); var Net = require("net"); var buffer_1 = require("buffer"); /** * Zabbix sender class */ var ZabbixSender = /** @class */ (function () { function ZabbixSender(options) { this.items = []; // Check if the `options` parameter is defined or not using the `typeof` operator // If `options` is not defined, set it to an empty object using a ternary operator options = (typeof options !== 'undefined') ? options : {}; this._serverHost = options.host || 'localhost'; this._serverPort = options.port || 10051; this._timeout = options.timeout || 5000; this._withNs = options.withNs || false; this._withTimestamps = options.withTimeStamp || false; this._agentHost = options.agentHost || ' '; // TODO: Clear Items this.clearItems(); } /** * Clear the items array */ ZabbixSender.prototype.clearItems = function () { this.items = []; }; ZabbixSender.prototype.addItem = function (key, value, host) { if (typeof host === 'undefined') { host = this._agentHost; } var item = { host: host, key: key, value: value, }; var length = this.items.push(item); if (this._withTimestamps) { this.items[length - 1]['clock'] = Date.now() / 1000 | 0; } }; ZabbixSender.prototype.send = function (callback) { var _this = this; callback = (typeof callback !== 'undefined') ? callback : function () { }; // Create a new TCP socket var self = this, error = false, items = this.items, data = this.prepareData(items, this._withTimestamps), client = new Net.Socket(), response = buffer_1.Buffer.alloc(0); this.clearItems(); // Set the socket timeout client.setTimeout(this._timeout); // Connect to the Zabbix server client.connect(this._serverPort, this._serverHost, function () { // Send the data to the Zabbix server client.write(data); }); client.on('data', function (chunk) { // Concatenate the response response = buffer_1.Buffer.concat([response, chunk]); }); client.on('timeout', function () { client.destroy(); throw new Error('Socket timed out after' + _this._timeout / 1000 + 'seconds'); }); client.on('error', function (err) { client.destroy(); throw new Error('Socket error: ' + err.message); }); client.on('close', function () { // Quit on any type of error if (error) { self.items = self.items.concat(items); return callback(error, {}); } // Check if the response is valid if (response.subarray(0, 5).toString() !== 'ZBXD\x01') { // in case of bad response, put the items back self.items = self.items.concat(items); return callback(new Error("got invalid response from server"), {}); } var jsonData = response.slice(13); // Convert the buffer to a string and parse it as JSON var responseString = jsonData.toString(); callback(null, JSON.parse(responseString), items); }); }; ZabbixSender.prototype.prepareData = function (items, withTimeStamp) { // Create a new object with the `request` and `data` properties var data = { "request": 'sender data', "data": items, }; if (withTimeStamp) { data['clock'] = Date.now() / 1000 | 0; ; } // Return the `data` object as a JSON string var payload = buffer_1.Buffer.from(JSON.stringify(data), 'utf8'); // Create a new buffer with a length of 5 + 4 bytes var header = buffer_1.Buffer.alloc(5 + 4); // Write the Zabbix protocol header to the `header` buffer header.write('ZBXD\x01'); // Write the payload length to the `header` buffer header.writeInt32LE(payload.length, 5); // Return the concatenated `header` and `payload` buffers (0, console_1.log)(buffer_1.Buffer.concat([header, buffer_1.Buffer.from("\x00\x00\x00\x00"), payload]).toString('ascii')); return buffer_1.Buffer.concat([header, buffer_1.Buffer.from("\x00\x00\x00\x00"), payload]); }; ZabbixSender.prototype._test_prepareData = function (items, withTimeStamp) { return this.prepareData(items, withTimeStamp); }; return ZabbixSender; }()); exports.ZabbixSender = ZabbixSender; ;