-
Notifications
You must be signed in to change notification settings - Fork 101
/
zbx_redis_stats.js
executable file
·92 lines (79 loc) · 2.63 KB
/
zbx_redis_stats.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
// This software is licensed GNU GPL v2
// Author: Alexey Dubkov <[email protected]>
var host = process.argv[2] || 'localhost',
port = 6379,
metric = process.argv[3],
db = process.argv[4] || 'none';
var client = require('redis').createClient(port, host);
var llenall = i = rlen = 0;
client.on('llenall', function(v) {
llenall += v;
if (i == rlen) {
console.log(llenall);
client.emit('quit');
}
});
client.on('ready', function(err) {
if (metric) {
if (db && client.server_info.hasOwnProperty(db)) {
var t = client.server_info[db]
.match('keys=(\\d+),expires=(\\d+),avg_ttl=(\\d+)');
client.server_info.key_space_db_keys = t[1];
client.server_info.key_space_db_expires = t[2];
client.server_info.key_space_db_avg_ttl = t[3];
}
switch (metric) {
case 'llen':
client.llen(db, function(err, res) {
console.log(res);
client.emit('quit');
});
break;
case 'llenall':
client.keys('*', function(err, res) {
rlen = res.length;
res.map(function(v) {
client.llen(v, function(err, res) {
i++;
client.emit('llenall', res);
});
});
});
break;
case 'list_key_space_db':
if (client.server_info.db0) {
console.log('db0');
} else
console.log('database_detect');
client.emit('quit');
break;
case 'dbsize':
if (client.server_info.db0) {
var res = {};
client.server_info.db0.split(',').map(function(el){
var _el = el.split('=');
res[_el[0]] = _el[1];
});
console.log(res.keys);
} else
console.log('database_detect');
client.emit('quit');
break;
default:
if (client.server_info.hasOwnProperty(metric))
console.log(client.server_info[metric]);
client.emit('quit');
break;
}
} else {
console.log('Not selected metric');
client.emit('quit');
}
});
client.on('error', function(err) {
console.log('Error: ' + err);
});
client.on('quit', function() {
client.quit();
});