-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics-twemproxy.rb
executable file
·106 lines (96 loc) · 2.58 KB
/
metrics-twemproxy.rb
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
#! /usr/bin/env ruby
#
# twemproxy-metrics
#
# DESCRIPTION:
# This plugin gets the stats data provided by twemproxy
# and sends it to graphite.
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: json
# gem: socket
#
# USAGE:
# #YELLOW
#
# NOTES:
#
# LICENSE:
# Copyright 2014 Toni Reina <areina0@gmail.com>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'sensu-plugin/metric/cli'
require 'socket'
require 'timeout'
require 'json'
#
# Twemproxy metrics
#
class Twemproxy2Graphite < Sensu::Plugin::Metric::CLI::Graphite
SKIP_KEYS = %w(service source version uptime timestamp total_connections curr_connections).freeze
option :host,
description: 'Twemproxy stats host to connect to',
short: '-h HOST',
long: '--host HOST',
required: false,
default: '127.0.0.1'
option :port,
description: 'Twemproxy stats port to connect to',
short: '-p PORT',
long: '--port PORT',
required: false,
proc: proc(&:to_i),
default: 22_222
option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-s SCHEME',
long: '--scheme SCHEME',
required: false,
default: "#{Socket.gethostname}.twemproxy"
option :timeout,
description: 'Timeout in seconds to complete the operation',
short: '-t SECONDS',
long: '--timeout SECONDS',
required: false,
proc: proc(&:to_i),
default: 5
option :skip_keys,
description: 'Keys to skip when looping through metrics (Comma seperated)',
short: '-k KEYS',
long: '--skip-keys KEYS'
def run
skip_keys = if config[:skip_keys]
config[:skip_keys].split(',')
else
SKIP_KEYS
end
Timeout.timeout(config[:timeout]) do
sock = TCPSocket.new(config[:host], config[:port])
data = JSON.parse(sock.read)
pools = data.keys - skip_keys
pools.each do |pool_key|
data[pool_key].each do |key, value|
if value.is_a?(Hash)
value.each do |key_server, value_server|
output "#{config[:scheme]}.#{key}.#{key_server}", value_server
end
else
output "#{config[:scheme]}.#{key}", value
end
end
end
end
ok
rescue Timeout::Error
warning 'Connection timed out'
rescue Errno::ECONNREFUSED
warning "Can't connect to #{config[:host]}:#{config[:port]}"
end
end