Skip to content

Instantly share code, notes, and snippets.

@austgate austgate/monitor.js
Created Dec 1, 2013

Embed
What would you like to do?
Quick Node.js script to look at the XML feed on ActiveMQ to demonstrate a way of doing this if not hooked into something like Nagios or antoehr monitoring service
var http = require('http'),
util = require('util'),
url = require('url'),
fs = require('fs'),
xml2js = require('xml2js');
try {
var amq_client = http.request(8161, 'localhost');
var parser = new xml2js.Parser();
var MAX_PENDING = 5; //high water mark set for pending messages
var MAX_DIFF = 5; //high water mark variable set
var LOG_HOME = '/path/to/amq.log';
} catch (err) {
console.log('set up error: '+err);
}
function get_queue() {
var request = amq_client.request("GET", "/admin/xml/queues.jsp", {"host": "localhost"}); //must be changed to read the timelines
request.addListener("response", function (response) {
var body = "";
response.addListener("data", function(data) {
body += data;
});
response.addListener("end", function() {
parser.on('end', function(result) {
var queues = JSON.parse(JSON.stringify(result));
for(var i in queues.queues.queue) {
var name = queues.queues.queue[i].$.name;;
var q = queues.queues.queue[i];
for (var k in q.stats) {
var size = q.stats[k].$.size;
var enq = q.stats[k].$.enqueueCount;
var deq = q.stats[k].$.dequeueCount;
if (size > MAX_PENDING) {
var writeStream = fs.createWriteStream(LOG_HOME, {'flags':'a'});
writeStream.write('ERROR: Queue: '+name+'. Pending queue size is too large. Pending size is '+ size + "\n");
}
if (enq + MAX_DIFF > deq) {
var writeStream = fs.createWriteStream(LOG_HOME, {'flags':'a'});
writeStream.write('ERROR: Queue: '+name+'. Messages are building up. Enqueue size: '+ enq +' dequeue is '+deq + "\n");
}
}
}
});
parser.parseString(body);
});
});
request.end();
} //end get_queue
setInterval(get_queue, 5000);
http.createServer(function(req, resp){}).listen(8002);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.