-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpinky_redis.lua
195 lines (178 loc) · 5.15 KB
/
pinky_redis.lua
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
local p = require 'pinky'
local redis = require "redis"
local handle_list;
local handle_none;
local handle_set;
local handle_string;
local pinky_main;
local process_redis;
local redis_check;
local redis_connect;
local redis_getdata;
local resque_check;
local singularize;
local usage;
local function usage()
return [[
Check Redis:
Valid URLS:
/pinky/redis/up -- Check if redis is up locally.
/pinky/redis/workers -- Return resque worker information.
/pinky/redis/queues -- Return resque queues information.
/pinky/redis/failed -- Return resque failed job information. (max 2k)
/pinky/redis/stat -- Return resque stat entries
/pinky/redis/loners -- Return resque loners job entries
/pinky/redis/cmds -- Return stats for commands run
]];
end
local function pinky_main(uri,ps)
-- This function is the entry point.
local args = p.split(uri,"/")
if #args == 0 then -- Check if Redis is up return appropriate info.
ps = p.do_error(usage(),ps)
else
if args[1] == "up" then
ps = redis_check(ps)
elseif args[1] == "info" then
ps = info(ps)
elseif args[1] == "cmds" then
ps = cmds(ps)
-- elseif args[1] == "monitor" then -- Maybe later. can be dangerous
-- ps = monitor(ps)
elseif args[1] == "slowlog" then
ps = slowlog(ps)
elseif args[1] == "queues" then
ps = process_redis("resque:queues",nil,nil,ps)
elseif args[1] == "workers" then
ps = process_redis("resque:workers",nil,nil,ps)
elseif args[1] == "failed" then
ps = process_redis("resque:failed",nil,nil,ps)
elseif args[1] == "loners" then
ps = process_redis("resque:loners",nil,nil,ps)
elseif args[1] == "stat" then
ps = process_redis("resque:stat",nil,nil,ps)
elseif args[1] == "mobile_subscriptions" then
ps = process_redis("mobile_subscriptions:*",nil,nil,ps)
elseif args[1] == "keys" and #args > 1 then
ps = process_keys("mobile_subscriptions:*",nil,nil,ps)
else
ps.status.value,ps.status.error = "FAIL", usage()
end
end
return ps
end
function redis_connect(ip,port)
local ip = ip or '127.0.0.1'
local port = port or 6379
local client,err = redis.connect(ip,port)
return client,err
end
function redis_check(ps)
local client,err = redis_connect()
if err then
ps.status.value, ps.status.error = "FAIL", err
else
if client:ping() then
ps.data = "OK! Redis is alive"
else
ps.status.value,ps.status.error = "FAIL", "Redis is not pingable!"
end
end
return ps
end
function slowlog(ps)
local client,err = redis_connect()
if err then
ps.status.value, ps.status.error = "FAIL", err
else
ps.data = client:slowlog('get',10)
end
return ps
end
function cmds(ps)
local client,err = redis_connect()
if err then
ps.status.value, ps.status.error = "FAIL", err
else
ps.data = {}
for k,v in pairs(client:info("commandstats")) do
ps.data[k] = v
end
end
return ps
end
function info(ps)
local client,err = redis_connect()
if err then
ps.status.value, ps.status.error = "FAIL", err
else
ps.data = {}
for k,v in pairs(client:info()) do
ps.data[k] = v
end
end
return ps
end
-- function monitor(ps)
-- local client,err = redis_connect()
-- if err then
-- ps.status.value, ps.status.error = "FAIL", err
-- else
-- ps.data = {}
-- ps.data = client:monitor()
-- end
-- return ps
-- end
function handle_list(list,client,parent,ps)
print("handle_list: list:" .. list .. " parent:" .. parent)
parent = parent or ""
local len = client:llen(parent .. list)
if len then
len = len > 2000 and 2000 or len
for l=1,len,1 do
ps.data[parent .. list .. l] = client:lindex(parent .. list, l - 1)
end
end
end
function handle_set(set,client,parent,ps)
parent = parent or ""
for i, v in ipairs(client:smembers(set)) do
ps.data[parent .. set] = {}
v = singularize(set) .. ":" .. v
if v then
process_redis(v,client,parent)
end
end
return ps
end
function handle_string(string,client,parent,ps)
ps.data[parent .. string] = client:get(parent .. string)
return ps
end
function handle_none(none,client,parent,ps)
print("handle_none:" .. tostring(none) .. " client:" .. tostring(client) .. " parent:" .. tostring(parent) .. " ps:" .. tostring(ps))
ps.data[parent .. none] = client:get(parent .. none)
return ps
end
function singularize(name)
return string.sub(name,1,-2)
end
function process_redis(something,client,parent,ps)
local parent = parent or ""
local client = client or redis_connect()
local type = client:type(something)
print("type is :" .. type)
if type == "set" then
ps = handle_set(something,client,parent,ps)
elseif type == "list" then
ps = handle_list(something,client,parent,ps)
elseif type == "none" then
ps = handle_none(something,client,parent,ps)
elseif type == "string" then
ps = handle_string(something,client,parent,ps)
else
print("Unknown type:" .. type)
end
return ps
end
return { pinky_main = pinky_main }