Skip to content

Commit 13adf54

Browse files
author
mda
committed
Added memory check
1 parent db3cf0f commit 13adf54

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## nagios-check-redis
22

3-
Simple plugin for nagios to check status of a redis server.
3+
Simple plugin for nagios to check status or memory usage of a redis server.
44

55
## Requirements
66

@@ -15,29 +15,47 @@ To download the latest version:
1515

1616
## Usage
1717

18-
nagios-redis -H localhost -p 6379
18+
# check ping
19+
check_redis -H localhost -p 6379 -t ping
20+
21+
# check ping
22+
check_redis -H localhost -p 6379 -t memory -w 1GB -c 2GB
23+
24+
Note on units: when memory size is needed, it is possible to specify it in the usual form of 1k 5GB 4M and so forth.
1925

2026
Edit your commands.cfg and add the following:
2127

2228
define command {
23-
command_name check_redis
24-
command_line $USER1$/nagios-redis/check_redis.js -H $HOSTADDRESS$ -p $ARG1$
29+
command_name check_redis_ping
30+
command_line $USER1$/nagios-redis/check_redis.js -H $HOSTADDRESS$ -p $ARG1$ -t ping
31+
}
32+
33+
define command {
34+
command_name check_redis_memory
35+
command_line $USER1$/nagios-redis/check_redis.js -H $HOSTADDRESS$ -p $ARG1$ -t memory -w $ARG2$ -c $ARG3$
2536
}
2637

2738
Now you can monitor redis servers by adding:
2839

2940
define service {
3041
use generic-service
3142
hostgroup_name Redis servers
32-
service_description Redis server check
33-
check_command check_redis!6379
43+
service_description Redis ping
44+
check_command check_redis_ping!6379
45+
}
46+
47+
define service {
48+
use generic-service
49+
hostgroup_name Redis servers
50+
service_description Redis memory
51+
check_command check_redis_memory!6379!1GB!2GB
3452
}
3553

3654
## Usage monitoring remote host with NRPE
3755

3856
Add the command to your NRPE configuration on your remote host:
3957

40-
command[check_redis]=check_redis
58+
command[check_redis_ping]=check_redis_ping
4159

4260
Monitor the service on your Nagios host:
4361

check_redis.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,53 @@
22
'use strict';
33

44
var optimist = require('optimist')
5-
.usage('Usage: $0 -H [hostname] -p [port] --help')
5+
.usage('Usage: $0 -H [hostname] -p [port] -t [check_type] -w -c --help')
66
.default('H', 'localhost')
77
.default('p', 6379)
8+
.default('t', 'ping')
9+
.default('w', '1GB')
10+
.default('c', '2GB')
811
.describe('H', 'hostname of the redis server')
912
.describe('p', 'port of the redis server')
13+
.describe('t', 'type to check, ping or memory')
14+
.describe('w', 'warning threshold (e.g. 100MB or 1GB, only used when type is memory)')
15+
.describe('c', 'critical threshold (e.g. 100MB or 1GB, only used when type is memory)')
1016
.describe('help', 'prints this help message');
1117
var argv = optimist.argv;
1218
var redis = require('redis');
19+
var bytes = require('bytes');
20+
21+
var exitCode = 0;
1322

1423
if (argv.help) {
1524
optimist.showHelp();
16-
process.exit(0);
25+
process.exit(exitCode);
1726
}
1827

1928
var client = redis.createClient(argv.p, argv.H);
2029

2130
client.on("error", function (err) {
2231
console.log('CRITICAL - ', err.toString());
23-
process.exit(3);
32+
exitCode = 2;
2433
});
2534

2635
client.on('ready', function() {
27-
console.log('OK -', 'uptime_in_days:', client.server_info.uptime_in_days);
28-
client.end();
29-
process.exit(0);
36+
if (argv.t === 'ping')
37+
console.log('OK -', 'uptime_in_days:', client.server_info.uptime_in_days);
38+
if (argv.t === 'memory') {
39+
var usage = parseInt(client.server_info.used_memory);
40+
var message = bytes(usage) + ' of memory used.';
41+
42+
if (usage >= bytes(argv.c)) {
43+
console.log('CRITICAL -', message);
44+
exitCode = 2;
45+
} else if (usage >= bytes(argv.w)) {
46+
console.log('WARNING -', message);
47+
exitCode = 1;
48+
} else
49+
console.log('OK -', message);
50+
}
51+
52+
client.end();
53+
process.exit(exitCode);
3054
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"author": "Michel Hiemstra <info@michelhiemstra.nl>",
1414
"license": "none",
1515
"dependencies": {
16+
"bytes": "^2.1.0",
1617
"hiredis": "^0.3.0",
1718
"optimist": "^0.6.1",
1819
"redis": "^0.12.1"

0 commit comments

Comments
 (0)