Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravibhure/1ffd8423f7ab81f8bd4b to your computer and use it in GitHub Desktop.
Save ravibhure/1ffd8423f7ab81f8bd4b to your computer and use it in GitHub Desktop.
All about haproxy
#!/usr/bin/perl
# HAProxy Performance Statistics
# by Steve Kamerman
#
# To use, pipe your HAProxy log with timing information (like "0/0/1/1/3 200") to
# this script. It will output min, max, med, avg and a latency distribution graph.
#
# Info on timing logging in HAProxy: http://code.google.com/p/haproxy-docs/wiki/TimingEvents
#
my @timings;
@dist_breakpoints = (5,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200);
my %dist;
foreach (@dist_breakpoints) {
$dist{$_} = 0;
}
$dist{'Over'} = 0;
while (<>) {
if (m#\d+/\d+/\d+/(\d+)/\d+ (\d+)#){
next if ($2 >= 300);
push(@timings, $1);
$dist{get_breakpoint($1)}++;
}
}
sort(@timings);
$total = scalar(@timings);
$med = $timings[int($total/2)];
$sum = 0;
$min = 1000;
$max = 0;
foreach (@timings) {
$sum += $_;
if ($_ < $min) {$min=$_;}
if ($_ > $max) {$max=$_;}
}
$avg = int($sum / $total);
$max_count = 0;
foreach (@dist_breakpoints) {
if ($dist{$_} > $max_count) {$max_count=$dist{$_};}
}
if ($dist{'Over'} > $max_count) {$max_count=$dist{'Over'};}
print "Total Requests: $total\n";
print "Min: $min ms\n";
print "Max: $max ms\n";
print "Med: $med ms\n";
print "Avg: $avg ms\n";
print "+----------------------\n";
print "| Latency Distribution \n";
print "+----------------------\n";
foreach (@dist_breakpoints) {
printf("%5s: %s\n", $_, dist_line($dist{$_}, $total, $max_count));
}
printf("%5s: %s\n", 'Over', dist_line($dist{'Over'}, $total, $max_count));
sub dist_line {
($count, $total, $max_count) = @_;
$max_char = 100;
$pct = ($count/$total)*100;
$chars = ($count/$max_count)*$max_char;
$line = '';
foreach (0..$chars) {$line .= '#';}
$line .= sprintf(" (%.2f%%, %s)", $pct, $count);
return $line;
}
sub get_breakpoint {
$val = shift;
foreach (@dist_breakpoints) {
if ($val < $_) {return $_;}
}
return 'Over';
}
#!/usr/bin/env ruby
# because CloudKick wants a license to merge...
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <lo@petalphile.com> 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
# ----------------------------------------------------------------------------
### the script:
# takes HAProxy stats and grabs connections, rate, and check time
# for every listener and every backend server, using a format like
# "metric groupname_servername_request_rate int 10
# variables you may actually need to change
# change this if the file is elsewhere
config = "/etc/haproxy/haproxy.cfg" || raise("Expecting haproxy configuration file in /etc/haproxy/haproxy.cfg")
# grab the statistics socket from above
socket = `awk '/stats socket/ {print $3}' #{config}`.chomp || raise("Expecting \'stats socket <UNIX_socket_path>\' in #{config}")
# where haproxy lives and pid (either in conf or static location)
exec = `which haproxy`.chomp || raise("Where the F is haproxy?")
pid = `pidof haproxy`.chomp.to_i || nil
if ( pid )
puts "status ok haproxy is running"
conn = `lsof -ln -i |grep -c #{pid}`.chomp.to_i
# removes the listener and stats socket
conn = conn - 2
puts "metric connections int #{conn}"
# grab statistics from the socket
require 'socket'
ctl=UNIXSocket.new(socket)
ctl.puts "show stat"
while (line = ctl.gets) do
if (line =~ /^[^#]\w+/)
line = line.split(",")
host = "#{line[0]}_#{line[1]}"
puts "metric #{host}_request_rate int #{line[47]}" if line[47].to_i > 0
puts "metric #{host}_total_requests gauge #{line[49]}" if line[49].to_i > 0
puts "metric #{host}_health_check_duration int #{line[35]}" if line[35].to_i > 0
puts "metric ${host}_current_queue int #{line[3]}" if line[3].to_i > 0
end
end
ctl.close
else
puts "status err haproxy is not running!"
end
# cat haproxy.log.1 | ./analyze_haproxy_performance.pl
Total Requests: 502199
Min: 0 ms
Max: 2563 ms
Med: 5 ms
Avg: 14 ms
+----------------------
| Latency Distribution
+----------------------
5: ##################################################################################### (33.33%, 167369)
10: ##################################################################################################### (39.40%, 197883)
20: ##### (1.77%, 8887)
30: ###### (1.98%, 9937)
40: #################### (7.59%, 38097)
50: ################################### (13.71%, 68852)
60: ##### (1.71%, 8605)
70: # (0.35%, 1733)
80: # (0.08%, 406)
90: # (0.02%, 124)
100: # (0.02%, 104)
110: # (0.01%, 73)
120: # (0.01%, 36)
130: # (0.01%, 38)
140: # (0.00%, 12)
150: # (0.00%, 3)
160: # (0.00%, 4)
170: # (0.00%, 7)
180: # (0.00%, 0)
190: # (0.00%, 1)
200: # (0.00%, 2)
Over: # (0.01%, 26)
[root@host75 ~]# ./haproxy.rb
status ok haproxy is running
metric connections int 0
metric host75_FRONTEND_request_rate int 78
metric host75_FRONTEND_health_check_duration int 78
metric www_REAL:127.0.0.1:8080_total_requests gauge 958
metric www_REAL:127.0.0.1:8080_health_check_duration int 62
metric www_BACKEND_total_requests gauge 958
metric www_BACKEND_health_check_duration int 62
metric hotjob.co.in_REAL:127.0.0.1:8080_health_check_duration int 6
metric hotjob.co.in_BACKEND_total_requests gauge 4
metric hotjob.co.in_BACKEND_health_check_duration int 6
metric acefarm.co.in_REAL:127.0.0.1:8080_total_requests gauge 20
metric acefarm.co.in_REAL:127.0.0.1:8080_health_check_duration int 46
metric acefarm.co.in_BACKEND_total_requests gauge 20
metric acefarm.co.in_BACKEND_health_check_duration int 46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment