-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_redis.js
54 lines (46 loc) · 1.48 KB
/
check_redis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
'use strict';
var optimist = require('optimist')
.usage('Usage: $0 -H [hostname] -p [port] -t [check_type] -w -c --help')
.default('H', 'localhost')
.default('p', 6379)
.default('t', 'ping')
.default('w', '1GB')
.default('c', '2GB')
.describe('H', 'hostname of the redis server')
.describe('p', 'port of the redis server')
.describe('t', 'type to check, ping or memory')
.describe('w', 'warning threshold (e.g. 100MB or 1GB, only used when type is memory)')
.describe('c', 'critical threshold (e.g. 100MB or 1GB, only used when type is memory)')
.describe('help', 'prints this help message');
var argv = optimist.argv;
var redis = require('redis');
var bytes = require('bytes');
var exitCode = 0;
if (argv.help) {
optimist.showHelp();
process.exit(exitCode);
}
var client = redis.createClient(argv.p, argv.H);
client.on("error", function (err) {
console.log('CRITICAL - ', err.toString());
exitCode = 2;
});
client.on('ready', function() {
if (argv.t === 'ping')
console.log('OK -', 'uptime_in_days:', client.server_info.uptime_in_days);
if (argv.t === 'memory') {
var usage = parseInt(client.server_info.used_memory);
var message = bytes(usage) + ' of memory used.';
if (usage >= bytes(argv.c)) {
console.log('CRITICAL -', message);
exitCode = 2;
} else if (usage >= bytes(argv.w)) {
console.log('WARNING -', message);
exitCode = 1;
} else
console.log('OK -', message);
}
client.end();
process.exit(exitCode);
});