Using Node.js To Discover Jenkins On The Network

I’ve just added a new feature to Nestor to discover Jenkins on the network, and as it turned out, it’s pretty simple to do thanks to Node.js Datagram sockets API (hat tip Paul Querna).

Jenkins has a discovery feature as part of its remote access API where it listens on UDP port 33848, and whenever it receives a message, Jenkins will respond with an XML containing the instance’s URL, version number, and slave port information.

So how do you send a UDP message using NodeJS?
Here’s a sample function adapted from Nestor’s lib/service.js:

function sendUdp(message, host, port, cb) {
    var socket = require('dgram').createSocket('udp4'),
        buffer = new Buffer(message);
    socket.on("error", function (err) {
        cb(err);
    });
    socket.on("message", function (data) {
        cb(null, data);
    });
    socket.send(buffer, 0, buffer.length, port, host, function (err, message) {
        if (err) {
            cb(err);
        }
    });
}

For Jenkins discovery purpose, send any message to any hostname on port 33848:

sendUdp('Long live Jenkins!', 'localhost', 33848, function () { ... });

and if there’s any Jenkins instance running on localhost, it will respond with an XML like this:

<hudson>
  <version>1.414</version>
  <url>http://localhost:8080</url>
  <slave-port>12345</slave-port>
</hudson>

Simple!

konan cliffano$ nestor discover
Jenkins 1.414 running at http://localhost:8080/