Skip to content

Instantly share code, notes, and snippets.

@taiyoh
Last active December 17, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taiyoh/5603373 to your computer and use it in GitHub Desktop.
Save taiyoh/5603373 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var io = require('socket.io-client')
, argv = require('optimist').argv
, w, c, h, u;
w = argv.w, c = argv.c, h = argv.h, u = argv.u;
w = w || 500; // value for "WARNING" notification
c = c || 4000; // value for "CRITICAL" notification
h = h || "localhost"; // hostname
u = u || "/"; // resource path of socket.io
var socket = io.connect("http://" + h + u, {
resource: u.replace(/^\//, '')
});
var reqt;
socket.on('connect', function (data) {
reqt = new Date;
// send some data to server if needs
socket.emit("foobar");
});
socket.on('hogefuga', function (data) {
var rest = new Date
, exit_code = 0
, exit_state = "OK"
, exit_msg = "no problem: "
, diff_t = rest - reqt;
try {
// type check here
// if (invalid) throw "error";
if (w <= diff_t && diff_t < c) {
exit_code = 1;
exit_state = "WARNING";
exit_msg = "slow response: ";
}
else if (c <= diff_t) {
exit_code = 2;
exit_state = "CRITICAL";
exit_msg = "very slow response: ";
}
exit_msg += String(diff_t) + "ms";
console.log(exit_state + " - " + exit_msg + "|node=valid;" + w + ";" + c);
socket.disconnect();
process.exit(exit_code);
} catch(e) {
console.log("CRITICAL - " + e + "|node=invalid;" + w + ";" + c);
socket.disconnect();
process.exit(2);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment