Skip to content

Instantly share code, notes, and snippets.

@enko
Created June 1, 2013 20:11
Show Gist options
  • Save enko/5691584 to your computer and use it in GitHub Desktop.
Save enko/5691584 to your computer and use it in GitHub Desktop.
A Zabbix-check for prosody.
var xmpp = require('node-xmpp');
var util = require('util');
var sys = require('sys');
var ZabbixSender = require('zabbix-sender');
var config = {
server : "192.168.0.5",
real_server : "boese-ban.de",
user : "[email protected]/check",
password : "",
debug: false,
zabbix: {
hostname : 'xmpp'
}
};
var start = process.hrtime();
var elapsed_time = function(note){
var precision = 3; // 3 decimal places
var elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
return elapsed.toFixed(precision);
start = process.hrtime(); // reset the timer
};
var c = new xmpp.Client({ jid: config.user,
password: config.password,
host: config.host,
port: 5222
});
c.on('online', function() {
if (config.debug) util.log('connected')
var node = new xmpp.Element('iq', {
xmlns:"jabber:client",
to: config.real_server,
type: 'set',
id: 116
}).c('command', {
node : "http://jabber.org/protocol/admin#get-online-users",
xmlns: "http://jabber.org/protocol/commands",
action: "execute"
}).up();
if (config.debug) util.log(node);
c.send(node);
// nodejs has nothing left to do and will exit
});
c.on('error', function(e) {
console.error(e);
process.exit(1);
});
c.on('stanza',function(stanza){
if (config.debug) util.log(stanza)
if (stanza.attrs.type == 'result') {
var command = stanza.children[0];
var sessionid = command.attrs.sessionid;
if (command.attrs.status == 'executing') {
var node = new xmpp.Element('iq',{
xmlns:"jabber:client",
type: 'set',
to: config.real_server,
id: 117
}).c('command',{
node : "http://jabber.org/protocol/admin#get-online-users",
xmlns: "http://jabber.org/protocol/commands",
action: "next",
sessionid:sessionid
}).c('x',{
xmlns:"jabber:x:data",
type:"submit"
}).c('field',{
"var": 'FORM_TYPE',
type: 'hidden'
}).c('value').t('http://jabber.org/protocol/admin').up().up()
.c('field',{
"var": 'max_items',
type: 'list-single'
})
.c('value').t('all').up().up()
.c('field',{
"var": "details",
type: "boolean"
}).c('value').t('false').up().up().up().up();
if (config.debug) util.log(node);
c.send(node);
} else if (command.attrs.status == 'completed') {
var count = command.children[0].children[1].children.length;
var sender = new ZabbixSender({
'hostname' : config.zabbix.hostname,
'bin' : '/opt/zabbix/bin/zabbix_sender'
});
sender.send({
'prosody.users': count,
'prosody.users_time': elapsed_time()
}, function(err,stdout,stderr) {
if (err) throw err;
if (config.debug) util.log('Wrote keys to zabbix');
c.end();
});
}
}
});
{ "name": "xmpp-check"
, "version": "0.0.1"
, "engines": ["node >= 0.8.0"]
, "main": "index.js"
, "dependencies":
{ "node-xmpp": "*",
"node-stringprep": "*",
"zabbix-sender": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment