-
Notifications
You must be signed in to change notification settings - Fork 65
/
haproxy.rb
executable file
·128 lines (114 loc) · 3.45 KB
/
haproxy.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
## Rackspace Cloud Monitoring Plug-In
## HAProxy Stats
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return
# ----------------------------------------------------------------------------
#
# haproxy.rb
# - Takes HAProxy stats and grabs connections, rate, and check time
# for every listener and every backend server, and prints it using
# Rackspace Cloud Montioring metric lines
#
# Usage:
# Place plug-in in /usr/lib/rackspace-monitoring-agent/plugins
#
# The following is an example 'criteria' for a Rackspace Monitoring Alarm:
#
# if (metric['connections'] == 0) {
# return new AlarmStatus(CRITICAL, 'No connections to your HAProxy!
#
# if (metric['connections'] < '10') {
# return new AlarmStatus(WARNING, 'Less than 10 connections to your HAProxy!');
# }
#
# return new AlarmStatus(OK, 'HAProxy connections normal');
#
# Please note that you will need to adjust the thresholds based on workload.
# Also, there are other metrics this plugin reports you may find useful, but
# the metricnames for these will vary based on your HAProxy cluster name.
#
def fail(status = 'Unknown failure')
puts "status #{status}"
exit 1
end
def metric(name, type, value)
@metrics[name] = {
:type => type,
:value => value
}
end
def output_success
puts 'status HAProxy is running and reporting metrics'
@metrics.each do |name, v|
puts "metric #{name} #{v[:type]} #{v[:value]}"
end
end
begin
require 'optparse'
require 'socket'
rescue
fail 'Failed to load required ruby gems'
end
@metrics = {}
options = {
:limit => 10
}
args = ARGV.dup
OptionParser.new do |o|
o.banner = "Usage: #{$PROGRAM_NAME} [options]"
o.on('-s', '--stats-socket SOCKET', 'Specify the HAProxy stats socket') do |s|
options[:sock] = s
end
o.on('-l', '--limit BACKEND_COUNT', 'Specify a limit of how many backends to report. Default is 10.') do |l|
options[:limit] = l.to_i
end
o.on_tail('-h', '--help', 'Show this message') { puts o; exit }
o.parse!(args)
end
fail 'You must specify the haproxy stats socket' if options[:sock].nil?
pid = `pidof haproxy`.chomp.to_i || fail('HAProxy is not running')
# get global frontend stats
begin
ctl = UNIXSocket.new(options[:sock])
ctl.puts 'show info'
while (line = ctl.gets)
if line =~ /^CurrConns:/
line = line.split(':')
metric('connections', 'int', line[1].to_i)
end
if line =~ /^ConnRate:/
line = line.split(':')
metric('connection_rate', 'int', line[1].to_i)
end
end
ctl.close
rescue
fail "Problem reading global stats from #{options[:sock]}"
end
# get per-backend stats
begin
ctl = UNIXSocket.new(options[:sock])
ctl.puts 'show stat'
i = 0
while (line = ctl.gets)
next unless line =~ /^[^#]\w+/
line = line.split(',')
host = "#{line[0]}_#{line[1]}".tr('-', '_').tr('.', '_')
if i < options[:limit]
metric("#{host}_request_rate", 'int', line[47].to_i)
metric("#{host}_total_requests", 'gauge', line[49].to_i)
metric("#{host}_current_queue", 'int', line[3].to_i)
metric("#{host}_health_check_duration","int",line[35].to_i)
i += 1
end
end
ctl.close
rescue
fail "Problem reading backend stats from #{options[:sock]}"
end
output_success