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

Custom timestamps in addItem #6

Open
wants to merge 3 commits 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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ Sender.addItem('dbserver', 'mysql.ping', 1);
// Add item with default host
Sender.addItem('httpd.logs.size', 1024);

// Add item with timestamp
Sender.addItem('webserver', 'httpd.running', 1, 1634645976);

// Add item with timestamp and nanoseconds
Sender.addItem('dbserver', 'mysql.ping', 0, 1634645976, 758000000);

// Add item with default host with timestamp. The host has to be set to an empty string in this case.
Sender.addItem('','httpd.logs.size', 1024, 1634645976);

// Send the items to zabbix trapper
Sender.send(function(err, res) {
if (err) {
Expand Down Expand Up @@ -69,9 +78,11 @@ here are the options defaults:

### Instance methods

**`addItem([host], key, value)`**
**`addItem([host], key, value, [timestamp], [ns])`**

Adds an item to the request payload. The item data won't be sent until the `send` method invoked.
Adds an item to the request payload. `ns` can only be set if `host` and `timestamp` are also set.
If a custom timestamp is set, `with_timestamps` and `with_ns` are ignored.
If `host` is an empty string `items_host` is used. The item data won't be sent until the `send` method invoked.
The `return` value is self instance, so chaining can be used.

**`clearItems()`**
Expand All @@ -92,8 +103,8 @@ In case of `error`, the previously added items will be kept, for the next `send`

### Protocol References

- https://www.zabbix.org/wiki/Docs/protocols/zabbix_sender/3.4
- https://www.zabbix.org/wiki/Docs/protocols/zabbix_agent/3.4
- https://www.zabbix.com/documentation/6.0/manual/appendix/items/trapper
- https://www.zabbix.com/documentation/6.0/manual/appendix/protocols/header_datalen

### License

Expand Down
35 changes: 23 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var ZabbixSender = module.exports = function(opts) {
this.clearItems();
}

ZabbixSender.prototype.addItem = function(host, key, value) {
ZabbixSender.prototype.addItem = function(host, key, value, timestamp, ns) {
if (arguments.length < 3) {
if (arguments.length < 2) {
throw new Error('addItem requires 2 or 3 arguments');
throw new Error('addItem requires at least 2 arguments');
}

// if just 2 args provided
Expand All @@ -27,18 +27,29 @@ ZabbixSender.prototype.addItem = function(host, key, value) {
host = this.items_host;
}

if (host === "")
host = this.items_host;

var length = this.items.push({
host: host,
key: key,
value: value
});

if (this.with_timestamps) {
var ts = Date.now() / 1000;
this.items[length - 1].clock = ts | 0;

if (this.with_ns) {
this.items[length - 1].ns = (ts % 1) * 1000 * 1000000 | 0;
// we don't care about configuration if time or ns is set.
if (arguments.length > 3) {
this.items[length - 1].clock = timestamp
if (arguments.length > 4) {
this.items[length - 1].ns = ns;
}
} else {
if (this.with_timestamps) {
var ts = Date.now() / 1000;
this.items[length - 1].clock = ts | 0;

if (this.with_ns) {
this.items[length - 1].ns = (ts % 1) * 1000 * 1000000 | 0;
}
}
}

Expand All @@ -63,7 +74,7 @@ ZabbixSender.prototype.send = function(callback) {
items = this.items,
data = prepareData(items, this.with_timestamps, this.with_ns),
client = new Net.Socket(),
response = new Buffer(0);
response = Buffer.alloc(0);

// uncoment when debugging
//console.log(data.slice(13).toString());
Expand Down Expand Up @@ -127,10 +138,10 @@ function prepareData(items, with_timestamps, with_ns) {
}
}

var payload = new Buffer(JSON.stringify(data), 'utf8'),
header = new Buffer(5 + 4); // ZBXD\1 + packed payload.length
var 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, new Buffer('\x00\x00\x00\x00'), payload]);
return Buffer.concat([header, Buffer.from('\x00\x00\x00\x00'), payload]);
}