I'm trying to get the status from Monit from a NodeJS program. In my monitrc I have it set to use port 2812, but I'm not sure what to do in my node program. Any advice would be highly appreciated.
I'll add that I am currently clueless, but I've tried:
var net = require('net');
var client = net.connect({port: 2812},
function() { //'connect' listener
console.log('client connected');
client.write('monit status');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
Which outputs:
client connected
HTTP/1.0 400 Bad Request
Date: Tue, 04 Dec 2012 17:03:15 GMT
Server: monit 5.3.2
Content-Type: text/html
Connection: close
<html><head><title>Bad Request</title></head><body bgcolor=#FFFFFF><h2>Bad Request</h2>Cannot parse request<p><hr><a href='http://mmonit.com/monit/'><font size=-1>monit 5.3.2</font></a></body></html>
client disconnected
This is more than nothing, since it actually lists monit as the server, but I have no idea how to make it work.
I figured it out. Turns out what I wanted to do was:
var http = require('http');
var options = {
hostname: '127.0.0.1',
port: 2812,
method: 'GET'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (data) {
console.log('BODY: ' + data);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();