Created
April 4, 2014 17:33
-
-
Save itamarhaber/1c251a031ee7c0651fda to your computer and use it in GitHub Desktop.
An introspective stats collector for Redis, in Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- infospect.lua - an introspective stats collector for Redis, in Redis | |
-- | |
-- Each time you run this script, INFO lines are added to lists with | |
-- their respective names. | |
-- Suggested usage: | |
-- SCRIPT LOAD infospect.lua | |
-- cron redis-cli EVALSHA <infospect-sha1> | |
-- list of stat names/prefixes to collect | |
local collected_stats = | |
'cmdstat_,'.. | |
'connected_clients,'.. | |
'db0,'.. | |
'evicted_keys,'.. | |
'expired_keys,'.. | |
'instantaneous_ops_per_sec,'.. | |
'keyspace_hits,'.. | |
'keyspace_misses,'.. | |
'rejected_connections,'.. | |
'uptime_in_seconds,'.. | |
'used_memory,' | |
local samples_max = 100 -- the number of samples to keep | |
local samples_prefix = 'infospect' -- the prefix for samples' keys | |
local function store(key, value) | |
redis.call('lpush', key, value) | |
redis.call('ltrim', key, 0, samples_max - 1) | |
end | |
local function collect(l) | |
if l:len() > 0 then -- ignore empty lines | |
if l:sub(1,1) == '#' then -- section line | |
stats_section = l:match('# (.*)') | |
else | |
local name, value = l:match('([%a%d_]+):(.*)') | |
local pre, post = name:match('([%a%d_]+)_(.*)') | |
if (collected_stats:find(name) ~= nil or | |
(pre ~= nil and collected_stats:find(pre..'_') ~= nil)) then | |
local key = samples_prefix..':'..stats_section..':'..name | |
if value:find(',') == nil then | |
store(key, value) | |
else | |
for n,v in value:gmatch('([%a%d_]*)=([%a%d_]*),?') do | |
store(key..':'..n, v) | |
end | |
end | |
end | |
end | |
end | |
return '' | |
end | |
local stats_section = '' | |
local raw_info = redis.call('INFO', 'all') | |
collect(raw_info:gsub('(.-)\r?\n', collect)) | |
return 'OK' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Won't work anymore, because
INFO
returns random stuff... unless used on Redis 3.2 + redis.replicate_commands()