-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
151 lines (116 loc) · 3.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const net = require('net');
/**
* Zabbix sender class
*/
class ZabbixSender {
/**
*
* @param options
* @param options.agentHost
* @param options.serverHost
* @param [options.serverPort]
* @param [options.timeout]
* @param [options.withTimestamps]
*/
constructor(options) {
options = (typeof options !== 'undefined') ? options : {};
this.serverHost = options.serverHost || 'localhost';
this.serverPort = parseInt(options.serverPort) || 10051;
this.timeout = parseInt(options.timeout) || 5000;
this.withTimestamps = options.withTimestamps || false;
this.agentHost = options.agentHost;
// prepare items array
this.clearItems();
}
addItem(host, key, value) {
if (arguments.length < 3) {
if (arguments.length < 2) {
throw new Error('addItem requires 2 or 3 arguments');
}
// if just 2 args provided
value = key;
key = host;
host = this.agentHost;
}
const length = this.items.push({
host: host,
key: key,
value: value
});
if (this.withTimestamps) {
this.items[length - 1].clock = Date.now() / 1000 | 0;
}
}
clearItems() {
this.items = [];
}
countItems() {
return this.items.length;
}
prepareData(items, withTimestamps) {
const data = {
request: 'sender data',
data: items
};
if (withTimestamps) {
data.clock = Date.now() / 1000 | 0;
}
// console.log(data); // DEBUG
const payload = new Buffer(JSON.stringify(data), 'utf8');
const header = new Buffer(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]);
}
send(callback) {
callback = (typeof callback === 'function') ? callback : () => {
};
let self = this,
error = false,
items = this.items,
data = this.prepareData(items, this.withTimestamps),
client = new net.Socket(),
response = new Buffer(0);
// uncoment when debugging
// console.log(data.slice(13).toString());
// reset items array
this.clearItems();
// set socket timeout
client.setTimeout(this.timeout);
client.connect(this.serverPort, this.serverHost, () => {
client.write(data);
});
client.on('data', data => {
response = Buffer.concat([response, data]);
});
client.on('timeout', () => {
error = new Error("socket timed out after " + self.timeout / 1000 + " seconds");
client.destroy();
});
client.on('error', err => {
error = err;
});
client.on('close', () => {
// bail out on any error
if (error) {
// in case of error, put the items back
self.items = self.items.concat(items);
return callback(error, {});
}
// bail out if got wrong response
if (response.slice(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"), {});
}
// all clear, return the result
callback(null, JSON.parse(response.slice(13)), items);
});
}
}
/**
*
* @type {ZabbixSender}
*/
module.exports = ZabbixSender;