Class: RedisStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_status.rb

Constant Summary collapse

SERVER_PROCESS_NAME =
"redis-server"
CLIENT_PROCESS_NAME =
"redis-cli"
GUESS_HOST =
`ifconfig | grep -o -P "(192|127)\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | head -n 1`.strip
CONFIG_LOCATIONS =
["/etc/redis.conf", "/etc/redis/redis.conf", "/usr/local/etc/redis.conf"]
@@redis_status =
{}

Class Method Summary collapse

Class Method Details

.cli_exec(command, *args) ⇒ Object



69
70
71
# File 'lib/redis_status.rb', line 69

def self.cli_exec(command, *args)
  `#{CLIENT_PROCESS_NAME} -h #{status[:bind] || GUESS_HOST} #{command.to_s} #{args.join}`
end

.cli_infoObject



73
74
75
# File 'lib/redis_status.rb', line 73

def self.cli_info
   parse_settings(cli_exec :info)
end

.confObject



44
45
46
# File 'lib/redis_status.rb', line 44

def self.conf
   parse_settings(`cat #{self.config_filename}`)
end

.config(key) ⇒ Object



77
78
79
80
81
82
# File 'lib/redis_status.rb', line 77

def self.config(key)
  output = cli_exec "config get", key
  result = output.split(/\n|\r/)[1]
  set_status(key, result)
  return result
end

.config_filenameObject



40
41
42
# File 'lib/redis_status.rb', line 40

def self.config_filename
  process_info[:args].nil? ? self.find_config : process_info[:args][0]
end

.db_fileObject



84
85
86
# File 'lib/redis_status.rb', line 84

def self.db_file
  File.join(config(:dir),config(:dbfilename))
end

.dbsObject



88
89
90
# File 'lib/redis_status.rb', line 88

def self.dbs
  self.cli_info.select{|k,v| k.to_s =~ /db/}
end

.errors?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/redis_status.rb', line 104

def self.errors?
  !recent_errors.empty?
end

.find_configObject



36
37
38
# File 'lib/redis_status.rb', line 36

def self.find_config
   CONFIG_LOCATIONS.select{|f| File.exist?(f) }.first
end

.last_saveObject



100
101
102
# File 'lib/redis_status.rb', line 100

def self.last_save
  Time.at(cli_exec(:lastsave).match(/\d+$/).to_s.to_i)
end

.memory_allocationObject



108
109
110
# File 'lib/redis_status.rb', line 108

def self.memory_allocation
  self.cli_info[:allocation_stats].split(",").map{|e| e.split("=").select{|i| i =~ /\d+/}}
end

.method_missing(m, *args, &block) ⇒ Object



112
113
114
# File 'lib/redis_status.rb', line 112

def self.method_missing(m, *args, &block)
  return @@redis_status[m.to_sym]
end

.parse_settings(settings) ⇒ Object

Accepts output from ‘redis-cli info` or the contents of redis.conf and parses it, populating a hash with the results



57
58
59
60
61
62
63
# File 'lib/redis_status.rb', line 57

def self.parse_settings(settings)
  set = {}
  settings.split(/\n|\r/).reject{|line| line =~ /^#/ || line.strip == ""}.map{|line| line.split(/:|\s+/)}.reject{|kv| kv.size != 2 }.each do |keypair|
    set[keypair[0].to_sym] = keypair[1]
  end
  set
end

.pidObject



28
29
30
# File 'lib/redis_status.rb', line 28

def self.pid
  process_info[:pid]
end

.process_infoObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis_status.rb', line 10

def self.process_info
  @info = `ps aux | grep "#{SERVER_PROCESS_NAME}" | grep -v "grep"`.strip.split(/\s+/)
  {
    :user => @info[0],
    :pid => @info[1],
    :cpu=> @info[2],
    :mem=> @info[3],
    :vsz=> @info[4],
    :rss=> @info[5],
    :tt=> @info[6],
    :stat=> @info[7],
    :started=> @info[8],
    :time=> @info[9],
    :command=> @info[10],
    :args=> @info[11..-1]
    }
end

.recent_errorsObject



96
97
98
# File 'lib/redis_status.rb', line 96

def self.recent_errors
   tail.select{|line| line.match(/#/)}
end

.running?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/redis_status.rb', line 32

def self.running?
  !self.pid.nil?
end

.set_status(*args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/redis_status.rb', line 48

def self.set_status(*args)
  if args.size == 1 && args[0].is_a?(Hash)
    @@redis_status.merge!(args[0])
  elsif args.size == 2 && [Symbol, String].include?(args[0].class)
    @@redis_status[args[0].to_sym] = args[1]
  end
end

.status(key = nil) ⇒ Object



65
66
67
# File 'lib/redis_status.rb', line 65

def self.status(key=nil)
  key.nil? ? @@redis_status : @@redis_status[key.to_sym]
end

.tailObject



92
93
94
# File 'lib/redis_status.rb', line 92

def self.tail
  `tail -n 50 #{self.conf[:logfile]}`.split(/\n|\r/)
end