Class: MemcacheStats

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger = nil) ⇒ MemcacheStats

Returns a new instance of MemcacheStats.



9
10
11
12
13
14
15
16
17
# File 'lib/memcachestats.rb', line 9

def initialize(config, logger = nil)
  @config = config
  
  initialize_log unless logger
  @log = logger if logger
  @log.error("Logging started")
  
  initialize_metrics
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/memcachestats.rb', line 5

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/memcachestats.rb', line 5

def connection
  @connection
end

#countersObject

Returns the value of attribute counters.



7
8
9
# File 'lib/memcachestats.rb', line 7

def counters
  @counters
end

#gaugesObject

Returns the value of attribute gauges.



7
8
9
# File 'lib/memcachestats.rb', line 7

def gauges
  @gauges
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/memcachestats.rb', line 5

def host
  @host
end

#logObject

Returns the value of attribute log.



5
6
7
# File 'lib/memcachestats.rb', line 5

def log
  @log
end

#metricsObject

Returns the value of attribute metrics.



7
8
9
# File 'lib/memcachestats.rb', line 7

def metrics
  @metrics
end

#statsObject

Returns the value of attribute stats.



7
8
9
# File 'lib/memcachestats.rb', line 7

def stats
  @stats
end

Instance Method Details

#add_derived_metrics(stats) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/memcachestats.rb', line 187

def add_derived_metrics(stats)
  stats['get_hit_percentage'] = stats['get_hits_total'].to_f / (stats['get_hits_total'] + stats['get_misses_total'] + 1)
  stats['get_miss_percentage'] = stats['get_misses_total'].to_f / (stats['get_hits_total'] + stats['get_misses_total'] + 1)
  stats['delete_hit_percentage'] = stats['delete_hits_total'].to_f / (stats['delete_hits_total'] + stats['delete_misses_total'] + 1)
  stats['delete_miss_percentage'] = stats['delete_misses_total'].to_f / (stats['delete_hits_total'] + stats['delete_misses_total'] + 1)
  stats['used_percentage'] = stats['bytes'].to_i / (stats['limit_maxbytes'].to_f + 1)
end

#connect(host, port = '11211') ⇒ Object



159
160
161
162
163
164
165
# File 'lib/memcachestats.rb', line 159

def connect(host, port = '11211')
  return if @connection and ((@host == host) && (@port == port))
  
  @connection = TCPSocket.new(host, port)
  @host = host
  @port = port
end

#get_memcached_stats(host, port = '11211') ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/memcachestats.rb', line 167

def get_memcached_stats(host, port = '11211')
  @stats = {}
  matched = {}

  begin
    connect(host, port)

    @connection.puts("stats")

    while ((line = @connection.gets.chomp) != "END")
      if (matched = /STAT (?<key>\w+) (?<value>\d+)/.match(line))
        @stats[matched['key']] = matched['value']
        @stats["#{matched['key']}_total"] = matched['value'] if @gauges["#{matched['key']}_total"]
      end
    end
  rescue Exception => e
    pp ["ex", e]
  end
end

#initialize_logObject



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
# File 'lib/memcachestats.rb', line 19

def initialize_log
  @config['log'] = {
     'file' => STDOUT,
     'level' => 'INFO'
   }.merge(@config['log'] || {})

   log_initialize = [@config['log']['file']]
   log_initialize << @config['log']['shift_age'] if @config['log']['shift_age']
   log_initialize << @config['log']['shift_size'] if @config['log']['shift_size']

   begin
     @log = Logger.new(*log_initialize)
     @log.level = Logger.const_get(@config['log']['level'])
   rescue Exception => e
     @config['log'] = {
       'file' => STDOUT,
       'level' => 'INFO'
     }
     @log = Logger.new(@config['log']['file'])
     @log.level = Logger.const_get(@config['log']['level'])
     @log.error("Caught a problem with log settings")
     @log.error("#{e.message}")
     @log.error("Setting log settings to defaults")
   end
end

#initialize_metricsObject



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
# File 'lib/memcachestats.rb', line 45

def initialize_metrics
  @gauges = {
    'curr_connections' => {
      'units' => 'conn',
      'type' => 'uint32'
    },
    'curr_items' => {
      'units' => 'keys',
      'type' => 'double'
    },
    'threads' => {
      'units' => 'threads',
      'type' => 'uint32'
    },
    'bytes' => {
      'units' => 'bytes',
      'publish' => 'no'
    },
    'limit_maxbytes' => {
      'units' => 'bytes',
      'publish' => 'no'
    },
    'total_connections_total' => {
      'units' => 'conn',
      'publish' => 'no'
    },
    'total_items_total' => {
      'units' => 'key',
      'publish' => 'no'
    },
    'get_hits_total' => {
      'units' => 'r',
      'publish' => 'no'
    },
    'get_misses_total' => {
      'units' => 'r',
      'publish' => 'no'
    },
    'delete_misses_total' => {
      'units' => 'r',
      'publish' => 'no'
    },
    'delete_hits_total' => {
      'units' => 'r',
      'publish' => 'no'
    },
    'bytes_read_total' => {
      'units' => 'bytes',
      'publish' => 'no'
    },
    'bytes_written_total' => {
      'units' => 'bytes',
      'publish' => 'no'
    },
    "get_hit_percentage" => {
      'units' => '%',
      'type' => 'double'
    },
    "get_miss_percentage" => {
      'units' => '%',
      'type' => 'double'
    },
    "delete_hit_percentage" => {
      'units' => '%',
      'type' => 'double'
    },
    "delete_miss_percentage" => {
      'units' => '%',
      'type' => 'double'
    },
    "used_percentage" => {
      'units' => '%',
      'type' => 'double'
    }
  }
  
  @counters = {
    'total_connections' => {
      'units' => 'conn/s',
      'type' => 'uint32'
    },
    'total_items' => {
      'units' => 'keys/s',
      'type' => 'uint32'
    },
    'get_hits' => {
      'units' => 'r/s',
      'type' => 'uint32'
    },
    'get_misses' => {
      'units' => 'r/s',
      'type' => 'uint32'
    },
    'delete_misses' => {
      'units' => 'r/s',
      'type' => 'uint32'
    },
    'delete_hits' => {
      'units' => 'r/s',
      'type' => 'uint32'
    },
    'bytes_read' => {
      'units' => 'bytes/s',
      'type' => 'double'
    },
    'bytes_written' => {
      'units' => 'bytes/s',
      'type' => 'double'
    }
  }
  
  @metrics = @counters.merge(@gauges)
end