-
Notifications
You must be signed in to change notification settings - Fork 0
/
haproxy_check_duration
executable file
·161 lines (146 loc) · 4.29 KB
/
haproxy_check_duration
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
#!/usr/bin/perl
# -*- perl -*-
#
# $Id: haproxy_check_duration 79237 2010-01-17 01:18:14Z bvdschans $
# Plugin to monitor haproxy health check duration
#
# Author: Bart van der Schans <[email protected]>
#
# Parameters understood:
#
# config (required)
# autoconf (optional - used by munin-config)
# dump dump hastats to stdout
#
# Configuration:
# - Make sure the check user can read and write to the unix socket of haproxy
# - Configure the location of the unix socket of haproxy
# - Backends to skip (optional)
# - Servers to skip (optional)
#
# Configuration example:
# [haproxy*]
# user haproxy
# env.socket /var/run/haproxy.sock
# env.skip_backends stats hidden
# env.skip_servers FRONTEND BACKEND
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf
use strict;
use Munin::Plugin;
use IO::Socket::UNIX;
my $socketfile = $ENV{'socket'} || '/var/run/haproxy.sock';
my @skip_backends = exists $ENV{'skip_backends'} ? split(' ', $ENV{'skip_backends'}) : qw(stats public-http);
my @skip_servers = exists $ENV{'skip_servers'} ? split(' ', $ENV{'skip_servers'}) : qw(FRONTEND BACKEND);
# the stats hash
my %haproxy;
# cmd line parsing
if ( $ARGV[0] eq "autoconf" ) {
print_autoconf();
exit 0;
} elsif ( $ARGV[0] eq "config" ) {
print_config();
exit 0;
} elsif ( $ARGV[0] eq "dump" ) {
dump_stats();
exit 1;
} else {
print_values();
exit 0;
}
sub print_values() {
read_stats();
for my $pxname (sort keys %haproxy) {
next if (grep {m|^$pxname?$|} @skip_backends);
my @svnames = sort keys %{ $haproxy{$pxname} };
foreach my $svname (@svnames) {
next if (grep {m|^$svname?$|} @skip_servers);
my $field = clean_fieldname($pxname . "_" . $svname);
if ($haproxy{$pxname}{$svname}{"check_code"} == 200) {
print "$field.value " . $haproxy{$pxname}{$svname}{"check_duration"} . "\n";
} else {
print "$field.value 0\n";
}
}
}
}
sub print_config() {
read_stats();
print "graph_title check duration per server\n";
print "graph_args --base 1000\n";
print "graph_vlabel check duration in ms\n";
print "graph_category haproxy\n";
print "graph_info Check duration of health checks of the servers.\n";
for my $pxname (sort keys %haproxy) {
next if (grep {m|^$pxname?$|} @skip_backends);
my @svnames = sort keys %{ $haproxy{$pxname} };
foreach my $svname (@svnames) {
next if (grep {m|^$svname?$|} @skip_servers);
my $field = clean_fieldname($pxname . "_" . $svname);
print "$field.label $pxname.$svname\n";
print "$field.draw LINE2\n";
print "$field.min 0\n";
}
}
}
sub print_autoconf() {
unless ( -S $socketfile ) {
print "no\n";
exit 1;
}
if ( `/usr/bin/perl $0` eq "" ) {
print "no\n";
exit 1;
}
print "yes\n";
exit 0;
}
sub dump_stats() {
read_stats();
foreach my $pxname (sort keys %haproxy) {
my @svnames = sort keys %{ $haproxy{$pxname} };
foreach my $svname (@svnames) {
my %stats = %{ $haproxy{$pxname}{$svname} };
for my $field (sort keys %stats) {
print $pxname . "." . $svname . "." . $field . "=" . $stats{$field} . "\n" if $field;
}
}
print "\n";
}
}
sub read_stats() {
unless ( -S $socketfile ) {
die "Socket not found: $socketfile"
}
my $socket = IO::Socket::UNIX->new(Peer => $socketfile,
Type => SOCK_STREAM ) or die $!;
print $socket "show stat\n";
my @headers;
while (<$socket>) {
# skip empty lines
next if /^\s*$/;
# find headers
if ( /^\# (.+)/ ) {
@headers = split(",", $1);
} else {
# build stats hash
my $i = 0;
my %stats;
my @values = split(",");
my $pxname = @values[0];
my $svname = @values[1];
foreach my $value (@values) {
$stats{@headers[$i]} = $value;
$i++;
}
%{ $haproxy{$pxname}{$svname} } = %stats;
}
}
# cleanup
$socket->flush;
$socket->close;
}
# should not be reached
exit -1;