-
Notifications
You must be signed in to change notification settings - Fork 2
/
munin-node.js
executable file
·144 lines (121 loc) · 4.23 KB
/
munin-node.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
#!/usr/local/bin/node
///////////////////////////////////////////////////////////////////////
// Simple munin-node server
// Written by Harald Nesland
///////////////////////////////////////////////////////////////////////
// Settings
var allowedHosts = ['192.168.5.0/24', /192\.168\.5\.\d+/];
var pluginPath = '/Users/hn/munin-nodejs/plugins/';
var MUNIN_LIBDIR = '/usr/share/munin/';
var tcpPort = 4949;
// TLS settings
// Create private key: openssl genrsa -out server-key.pem 1024
// Create a cert sign request: openssl req -new -key server-key.pem -out server-csr.pem
// Self-sign cert with csr: openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem
// (Or send CSR to your CA etc)
// This will force TLS/SSL. Node-js does not support STARTTLS.
var useTLS = false;
var tlsOptions = { keyFile: 'server-key.pem', certFile: 'server-cert.pem' };
// Node-server
var net = require('net');
var os = require('os');
var fs = require('fs');
var spawn = require('child_process').spawn;
net.ipv4 = require('./net.ipv4.js');
// Enum plugins
var availablePlugins = {};
fs.readdir(pluginPath, function(err, files) {
for(var n in files) {
availablePlugins[files[n]] = true;
}
});
// Socket listener
var muninNodeHandler = function(s) {
var remoteAddress = '0.0.0.0';
if(s.remoteAddress) {
remoteAddress = s.remoteAddress;
} else if(s.socket.remoteAddress) {
remoteAddress = s.socket.remoteAddress;
}
var allowHost = false;
for(var _i = 0; _i < allowedHosts.length; _i++) {
var iprange = allowedHosts[_i];
if(typeof iprange == 'function') {
if( iprange.exec(remoteAddress) != null ) {
console.log("Host allowed by " + iprange);
allowHost = true;
}
} else {
if(net.ipv4.ipInRange(remoteAddress, iprange)) {
console.log("Host allowed by " + iprange);
allowHost = true;
}
}
}
if(!allowHost) {
s.destroy();
return;
}
s.write("# munin node at " + os.hostname() + "\n");
s.on("data", function(data) {
var commandMatch = /^(\S+)\s(.*)/;
var commandResult;
if(commandResult = commandMatch.exec(data)) {
var command = commandResult[1];
switch(command) {
case 'STARTTLS':
s.write('# Shhhhhhhhhhhhhhhiiiiiiiiieeeeeeeeeeeet\n');
s.destroy();
return;
break;
case "cap":
s.write("cap multigraph\n");
break;
case "list":
for(var plugin in availablePlugins) {
s.write(plugin + " ");
}
s.write("\n");
break;
case "nodes":
s.write(os.hostname() + "\n.\n");
break;
case "autoconf":
case "detect":
case "fetch":
case "config":
var plugin = commandResult[2];
if(plugin in availablePlugins) {
console.log("Executing: " + pluginPath + plugin + " " + command);
var pluginProc = spawn(pluginPath + plugin, [command], {MUNIN_LIBDIR:MUNIN_LIBDIR});
pluginProc.stdout.on('data', function(pluginLine) {
s.write(pluginLine);
});
pluginProc.on('exit', function(code) {
s.write(".\n");
});
} else {
s.write("# Unknown service\n.\n");
}
break;
case "version":
s.write("munins node on " + os.hostname() + " version: 1.4.4\n");
break;
case "quit":
s.destroy();
break;
default:
s.write("# Unknown command. Try cap, list, nodes, config, fetch, version or quit\n");
break;
}
}
});
};
if(useTLS) {
var tls = require('tls');
var options = {key: fs.readFileSync(tlsOptions.keyFile), cert: fs.readFileSync(tlsOptions.certFile)};
var server = tls.createServer(options, muninNodeHandler);
} else {
var server = net.createServer(muninNodeHandler);
}
server.listen(tcpPort,'0.0.0.0');