This repository was archived by the owner on Nov 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhaproxy-stats
More file actions
executable file
·207 lines (178 loc) · 5.47 KB
/
haproxy-stats
File metadata and controls
executable file
·207 lines (178 loc) · 5.47 KB
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env ruby
#
# haproxy-stats
#
# Copyright 2011 BankSimple. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'rubygems'
require 'optparse'
require 'socket'
require 'fastercsv'
options = {
:socket => "/var/run/haproxy.stats",
:interval => 10,
:hostname => Socket.gethostname
}
class Stats
def initialize(hostname, socket)
@hostname = hostname
@socket = socket
@types = {
0 => :frontend,
1 => :backend,
2 => :server
}
@metrics = {
"status" => {
:on => [ :backend, :server ],
:fields => ['status', 'downtime' ]
},
"sessions" => {
:on => [ :frontend, :server, :backend ],
:fields => ['scur', 'smax', 'slim', 'stot']
},
"session_rates" => {
:on => [ :frontend, :backend, :server],
:fields => [ 'rate', 'rate_max', 'rate_lim']
},
"traffic" => {
:on => [ :frontend, :server, :backend ],
:fields => ['bin', 'bout']
},
"errors" => {
:on => [ :frontend, :server, :backend ],
:fields => [ 'ereq', 'econ', 'eresp', 'dreq', 'dresp']
}
}
# A map of column name to list of
@cleanups = {
"status" => [
[/^UP$/, "2"],
[/^DOWN.*$/, "1"], #Going Up
[/^no check$/, "0"],
[/^UP.*$/, "-1"], #Going Down
[/^DOWN$/, "-2"]
]
}
end
# Read in a haproxy 'show stat' output from a file
def from_file(filename)
data = File.read(filename)
clean(data)
end
def from_socket
socket = UNIXSocket.open(@socket)
socket.write("show stat\n")
data = socket.read
socket.close
clean(data)
end
# Clean up the 'csv' provided by haproxy stats
# - We remove the leading '# ' for the headers
# - We remove the trailing ',' on all lines
def clean(data)
l = data.split("\n")
l.each do |line|
if line.start_with?("# ")
line.slice!("# ")
end
line.slice!(-1..-1)
line
end
data = l.join("\n")
end
def parse(data, start_time)
output = ""
table = FasterCSV.parse(data, {:headers=> true, :converters => :all }) do |row|
# Do cleanups and calculations
@cleanups.each do |field, matches|
matches.each do |from, to|
if row[field] =~ from
row[field] = to
end
end
end
@metrics.each do |name, data|
if data[:on].include?(@types[row['type']])
output << "PUTVAL #{@hostname}/haproxy-#{row['pxname'].gsub('-','_')}/haproxy_#{name}-#{row['svname'].gsub('-','_')} #{start_time}:"
output << data[:fields].collect { |f| if row[f].nil? then 0 else row[f] end }.join(':')
output << "\n"
end
end
end
output
end
end
opts = OptionParser.new
opts.banner = "Usage: haproxy-stats [options]"
opts.separator ""
opts.on("-sSOCKET", "--socket=SOCKET", "Location of the haproxy stats socket",
"Default: #{options[:socket]}"){ |str|
options[:socket] = str
}
opts.on("-iINTERVAL", "--interval=INTERVAL", "Interval between data collection",
"Default: #{options[:interval]}"){ |str|
options[:wait_time] = str.to_i
}
opts.on("-HHOSTNAME", "--hostname=HOSTNAME", "Hostname",
"Default: #{options[:hostname]}"){ |str|
options[:hostname] = str
}
opts.on("--single", "Run in single-shot mode"){|s| options[:single] = s}
opts.separator ""
opts.separator "Debugging options"
opts.on("-fFILE", "--file=FILE", "Read sample data from a CSV file"){ |str|
options[:file] = str
}
opts.separator ""
opts.separator "Common Options"
opts.on_tail("-h", "--help", "Shows this message") {
exit
}
begin
opts.parse(ARGV)
# Reading from a file implies single-shot mode
if options[:file]
options[:single] = true
end
rescue SystemExit
$stderr.puts opts
exit
rescue Exception => e
$stderr.puts "Error: #{e}"
$stderr.puts opts
exit
end
begin
stats = Stats.new(options[:hostname], options[:socket])
while true do
start_run = Time.now.to_i
next_run = start_run + options[:interval]
data = ""
if options[:file]
data = stats.from_file(options[:file])
else
data = stats.from_socket
end
puts stats.parse(data, start_run)
if options[:single]
break
end
# sleep to make the interval
while((time_left = (next_run - Time.now.to_i)) > 0) do
sleep(time_left)
end
end
end