Changeset 14d983031447161dd01662030191c821275e40a3
- Timestamp:
- 11/01/11 01:38:52
(2 years ago)
- Author:
- Brian Clapper <bclapper@omniti.com>
- git-committer:
- Brian Clapper <bclapper@omniti.com> 1320111532 -0400
- git-parent:
[e520e98b8f3e66fdfa978ec0f096baad8a7dfb38]
- git-author:
- Brian Clapper <bclapper@omniti.com> 1320111532 -0400
- Message:
better support for hash commands, specifically HMGET and HGETALL. use the hash key in the metric name, HMGET will use the various fields as the rest of the metric, HGETALL will use every other return (the keys) as part of the metric name
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| rbe54f17 |
r14d9830 |
|
| 126 | 126 | end |
|---|
| 127 | 127 | |
|---|
| | 128 | metric_names = {} |
|---|
| | 129 | hash_key = nil |
|---|
| | 130 | need_key = 0 |
|---|
| | 131 | metric_name = nil |
|---|
| | 132 | |
|---|
| 128 | 133 | function get_info_metrics(conn, check) |
|---|
| 129 | 134 | local count = 0 |
|---|
| … | … | |
| 147 | 152 | count = count + 1 |
|---|
| 148 | 153 | met = string.split(db_metrics[idx], "=") |
|---|
| 149 | | add_check_metric(kv[1] .. "`" .. met[1], met[2], check) |
|---|
| | 154 | metric_name = kv[1] .. "`" .. met[1] |
|---|
| | 155 | add_check_metric(met[2], check) |
|---|
| 150 | 156 | end |
|---|
| 151 | 157 | elseif ( string.find(kv[1], "^allocation_stats$") ) then |
|---|
| … | … | |
| 162 | 168 | else |
|---|
| 163 | 169 | count = count + 1 |
|---|
| 164 | | add_check_metric(kv[1], kv[2], check) |
|---|
| | 170 | metric_name = kv[1] |
|---|
| | 171 | add_check_metric(kv[2], check) |
|---|
| 165 | 172 | end |
|---|
| 166 | 173 | end |
|---|
| … | … | |
| 168 | 175 | return count |
|---|
| 169 | 176 | end |
|---|
| 170 | | |
|---|
| 171 | | metric_names = {} |
|---|
| 172 | 177 | |
|---|
| 173 | 178 | function get_command_metrics(conn, check) |
|---|
| … | … | |
| 175 | 180 | metric_names = {} |
|---|
| 176 | 181 | local cs = string.split(check.config.command, "%s+") |
|---|
| 177 | | if ( string.find(cs[1]:upper(), "MGET") ) then |
|---|
| | 182 | if ( string.find(cs[1]:upper(), "HGET") ) then |
|---|
| | 183 | hash_key = cs[2] |
|---|
| | 184 | elseif ( string.find(cs[1]:upper(), "MGET") ) then |
|---|
| 178 | 185 | for idx in pairs(cs) do |
|---|
| 179 | 186 | if ( idx > 1 ) then |
|---|
| 180 | | metric_names[idx-2] = cs[idx] |
|---|
| | 187 | if ( string.find(cs[1]:upper(), "HMGET") and idx > 2 ) then |
|---|
| | 188 | metric_names[idx-3] = cs[2] .. '`' .. cs[idx] |
|---|
| | 189 | else |
|---|
| | 190 | metric_names[idx-2] = cs[idx] |
|---|
| | 191 | end |
|---|
| 181 | 192 | end |
|---|
| 182 | 193 | end |
|---|
| 183 | 194 | end |
|---|
| 184 | | return read_redis_response(conn, check, cs[2]) |
|---|
| 185 | | end |
|---|
| 186 | | |
|---|
| 187 | | function read_redis_response(conn, check, metric_name) |
|---|
| | 195 | metric_name = cs[2] |
|---|
| | 196 | return read_redis_response(conn, check) |
|---|
| | 197 | end |
|---|
| | 198 | |
|---|
| | 199 | function read_redis_response(conn, check) |
|---|
| 188 | 200 | local response_type = conn:read(1); |
|---|
| 189 | 201 | if ( response_type == "+" ) then |
|---|
| 190 | | return redis_response_string(conn, check, metric_name) |
|---|
| | 202 | return redis_response_string(conn, check) |
|---|
| 191 | 203 | elseif ( response_type == "-" ) then |
|---|
| 192 | 204 | return redis_response_error(conn, check) |
|---|
| 193 | 205 | elseif ( response_type == ":" ) then |
|---|
| 194 | | return redis_response_integer(conn, check, metric_name) |
|---|
| | 206 | return redis_response_integer(conn, check) |
|---|
| 195 | 207 | elseif ( response_type == "$" ) then |
|---|
| 196 | | return redis_bulk_response(conn, check, metric_name) |
|---|
| | 208 | return redis_bulk_response(conn, check) |
|---|
| 197 | 209 | elseif ( response_type == "*" ) then |
|---|
| 198 | | return redis_multibulk_response(conn, check, metric_name) |
|---|
| 199 | | end |
|---|
| 200 | | end |
|---|
| 201 | | |
|---|
| 202 | | function redis_bulk_response(conn, check, metric_name) |
|---|
| | 210 | return redis_multibulk_response(conn, check) |
|---|
| | 211 | end |
|---|
| | 212 | end |
|---|
| | 213 | |
|---|
| | 214 | function redis_bulk_response(conn, check) |
|---|
| 203 | 215 | local response |
|---|
| 204 | 216 | local response_len = conn:read("\r\n") |
|---|
| 205 | 217 | response_len = string.sub(response_len, 1, -2) |
|---|
| 206 | 218 | if ( -1 == tonumber(response_len) ) then |
|---|
| 207 | | return redis_response_null(check, metric_name) |
|---|
| | 219 | return redis_response_null(check) |
|---|
| 208 | 220 | else |
|---|
| 209 | 221 | response = conn:read(response_len) |
|---|
| 210 | | add_check_metric(metric_name, response, check) |
|---|
| | 222 | if ( 1 == need_key ) then |
|---|
| | 223 | metric_name = hash_key .. '`' .. response |
|---|
| | 224 | else |
|---|
| | 225 | add_check_metric(response, check) |
|---|
| | 226 | end |
|---|
| 211 | 227 | -- clean out rest of response |
|---|
| 212 | 228 | conn:read("\r\n") |
|---|
| … | … | |
| 215 | 231 | end |
|---|
| 216 | 232 | |
|---|
| 217 | | function redis_multibulk_response(conn, check, metric_name) |
|---|
| | 233 | function redis_multibulk_response(conn, check) |
|---|
| 218 | 234 | local responses = conn:read("\r\n") |
|---|
| 219 | 235 | responses = string.sub(responses, 1, -2) |
|---|
| 220 | 236 | |
|---|
| 221 | 237 | for i = 1, responses, 1 do |
|---|
| 222 | | local m_name = metric_name .. '`' .. (i-1) |
|---|
| 223 | | if ( metric_names[i-1] ) then |
|---|
| 224 | | m_name = metric_names[i-1] |
|---|
| 225 | | end |
|---|
| 226 | | read_redis_response(conn, check, m_name) |
|---|
| | 238 | if ( hash_key ~= nil ) then |
|---|
| | 239 | need_key = (need_key + 1) % 2 |
|---|
| | 240 | else |
|---|
| | 241 | metric_name = metric_name .. '`' .. (i-1) |
|---|
| | 242 | if ( metric_names[i-1] ) then |
|---|
| | 243 | metric_name = metric_names[i-1] |
|---|
| | 244 | end |
|---|
| | 245 | end |
|---|
| | 246 | read_redis_response(conn, check) |
|---|
| 227 | 247 | end |
|---|
| 228 | 248 | return tonumber(responses) |
|---|
| 229 | 249 | end |
|---|
| 230 | 250 | |
|---|
| 231 | | function redis_response_integer(conn, check, metric_name) |
|---|
| | 251 | function redis_response_integer(conn, check) |
|---|
| 232 | 252 | local response = conn:read("\r\n") |
|---|
| 233 | 253 | response = string.sub(response, 1, -2) |
|---|
| 234 | | add_check_metric(metric_name, response, check) |
|---|
| | 254 | add_check_metric(response, check) |
|---|
| 235 | 255 | return 1 |
|---|
| 236 | 256 | end |
|---|
| 237 | 257 | |
|---|
| 238 | | function redis_response_string(conn, check, metric_name) |
|---|
| | 258 | function redis_response_string(conn, check) |
|---|
| 239 | 259 | local response = conn:read("\r\n") |
|---|
| 240 | 260 | response = string.sub(response, 1, -2) |
|---|
| 241 | | add_check_metric(metric_name, response, check) |
|---|
| | 261 | add_check_metric(response, check) |
|---|
| 242 | 262 | return 1 |
|---|
| 243 | 263 | end |
|---|
| … | … | |
| 251 | 271 | end |
|---|
| 252 | 272 | |
|---|
| 253 | | function redis_response_null(check, metric_name) |
|---|
| | 273 | function redis_response_null(check) |
|---|
| 254 | 274 | check.metric_string(metric_name, nil) |
|---|
| 255 | 275 | return 1 |
|---|
| 256 | 276 | end |
|---|
| 257 | 277 | |
|---|
| 258 | | function add_check_metric(name, value, check) |
|---|
| | 278 | function add_check_metric(value, check) |
|---|
| 259 | 279 | if ( string.find(value, "^%d+$") ) then |
|---|
| 260 | | check.metric_uint64(name, value) |
|---|
| | 280 | check.metric_uint64(metric_name, value) |
|---|
| 261 | 281 | elseif ( string.find(value, "^%d+?.%d+$") ) then |
|---|
| 262 | | check.metric_double(name, value) |
|---|
| | 282 | check.metric_double(metric_name, value) |
|---|
| 263 | 283 | else |
|---|
| 264 | | check.metric(name, value) |
|---|
| | 284 | check.metric(metric_name, value) |
|---|
| 265 | 285 | end |
|---|
| 266 | 286 | end |
|---|