@bettercorp/node-zabbix-sender
Version:
A zabbix_sender implementation, to send zabbix item(s) using the zabbix trapper protocol in NodeJS/Typescript.
150 lines • 5.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const net_1 = require("net");
const os_1 = require("os");
class ZabbixSender {
constructor(host, port = 10051, timeout = 5000, timestamps = false, nsTiming = false, hostname = (0, os_1.hostname)()) {
this._hostname = "localhost";
this._host = "localhost";
this._host = host;
this._port = port;
this._timeout = timeout;
this._nsTiming = nsTiming;
this._timestamps = timestamps;
this._hostname = hostname;
if (this.nsTiming && !this.timestamps) {
this._timestamps = true;
}
}
get hostname() {
return this._hostname;
}
get host() {
return this._host;
}
get port() {
return this._port;
}
get timeout() {
return this._timeout;
}
get nsTiming() {
return this._nsTiming;
}
get timestamps() {
return this._timestamps;
}
add(key, value, timestamp, ns, hostname) {
return new ZabbixSenderRequest(this).add(key, value, timestamp, ns, hostname);
}
send(key, value, timestamp, ns, hostname) {
return new ZabbixSenderRequest(this)
.add(key, value, timestamp, ns, hostname)
.send();
}
}
exports.default = ZabbixSender;
class ZabbixSenderRequest {
constructor(sender) {
this.closed = false;
this.listOfItems = [];
this.sender = sender;
}
get length() {
return this.listOfItems.length;
}
get items() {
return this.listOfItems;
}
add(key, value, timestamp, ns, hostname) {
if (this.closed)
throw "Data has already been sent. use ZabbixSender.add() to start a new chain.";
if (typeof key === "string") {
let newItem = {
host: this.sender.hostname,
key: key,
value: value,
};
if (typeof timestamp === "string") {
newItem.host = timestamp;
}
else if (typeof ns === "string") {
newItem.host = ns;
}
else if (typeof hostname === "string") {
newItem.host = hostname;
}
if (this.sender.timestamps) {
if (typeof timestamp === "number") {
newItem.clock = timestamp;
}
else {
newItem.clock = Date.now() / 1000;
}
if (this.sender.nsTiming) {
if (typeof ns === "number") {
newItem.ns = ns;
}
else {
newItem.ns = (newItem.clock % 1) * 1000 * 1000000;
}
}
}
this.listOfItems.push(newItem);
return this;
}
this.listOfItems.push(key);
return this;
}
async send() {
if (this.closed)
throw "Data has already been sent. use ZabbixSender.add() to start a new chain.";
const self = this, client = new net_1.Socket();
let response = Buffer.alloc(0);
let error = null;
return new Promise((resolve, reject) => {
client.setTimeout(self.sender.timeout);
client.connect(self.sender.port, self.sender.host, function () {
client.write(self.prepareData());
});
client.on("data", (data) => {
response = Buffer.concat([response, data]);
});
client.on("timeout", () => {
client.destroy();
error = new Error("socket timed out after " + self.sender.timeout / 1000 + " seconds");
});
client.on("error", (err) => (error = err));
client.on("close", function () {
// bail out on any error
if (error) {
return reject(error);
}
// bail out if got wrong response
if (response.subarray(0, 5).toString() !== "ZBXD\x01") {
return reject(new Error("got invalid response from server"));
}
// all clear, return the result
self.closed = true;
resolve(JSON.parse(response.subarray(13).toString("utf8")));
});
});
}
prepareData() {
let data = {
request: "sender data",
data: this.listOfItems,
};
if (this.sender.timestamps) {
data.clock = Date.now() / 1000;
}
if (this.sender.nsTiming) {
data.ns = (data.clock % 1) * 1000 * 1000000;
}
let payload = Buffer.from(JSON.stringify(data), "utf8"), header = Buffer.alloc(5 + 4); // ZBXD\1 + packed payload.length
header.write("ZBXD\x01");
header.writeInt32LE(payload.length, 5);
return Buffer.concat([header, Buffer.from("\x00\x00\x00\x00"), payload]);
}
}
//# sourceMappingURL=sender.js.map