Add haproxy module from tor
[mirror/dsa-puppet.git] / modules / haproxy / files / monitoring-munin-haproxy / haproxyng
1 #!/usr/bin/perl
2
3 # haproxyng Munin Plugin
4 # Multigraph plugin which monitors the haproxy service.
5 #  (c) 2014-2015 Jonathan Wright <jon@than.io>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 # Configure the Perl Environment
22 use warnings;
23 use Carp;
24 use Switch;
25
26 # Import Modules
27 use Munin::Plugin;
28 use IO::Socket::UNIX qw( SOCK_STREAM );
29 use Data::Dumper;
30
31 # Configure Program details
32 our ($_program, $_version, $_author);
33 $_program = 'haproxyng';
34 $_version = '1.0.1';
35 $_author  = 'Jonathan Wright <jon@than.io>';
36
37 use constant {
38   # Field names to locations for all the CSV data provided by HAProxy
39   STAT_PROXY_NAME               => 0,
40   STAT_SERVICE_NAME             => 1,
41   STAT_QUEUED_REQUESTS          => 2,
42   STAT_QUEUED_MAX               => 3,
43   STAT_SESSIONS_CURRENT         => 4,
44   STAT_SESSIONS_MAX             => 5,
45   STAT_SESSIONS_LIMIT           => 6,
46   STAT_CONNECTIONS_TOTAL        => 7,
47   STAT_BYTES_IN                 => 8,
48   STAT_BYTES_OUT                => 9,
49   STAT_REQUESTS_DENIED          => 10,
50   STAT_RESPONSES_DENIED         => 11,
51   STAT_REQUESTS_ERROR           => 12,
52   STAT_CONNECTIONS_ERROR        => 13,
53   STAT_RESPONSES_ERROR          => 14,
54   STAT_CONNECTIONS_RETRIED      => 15,
55   STAT_CONNECTIONS_REDISPATCHED => 16,
56   STAT_STATUS                   => 17,
57   STAT_WEIGHT                   => 18,
58   STAT_SERVER_ACTIVE            => 19, STAT_SERVERS_ACTIVE => 19,
59   STAT_SERVER_BACKUP            => 20, STAT_SERVERS_BACKUP => 20,
60   STAT_CHECKS_FAIL              => 21,
61   STAT_CHECKS_GO_DOWN           => 22,
62   STAT_CHECKS_LAST_CHANGE       => 23,
63   STAT_CHECKS_DOWNTIME          => 24,
64   STAT_QUEUED_LIMIT             => 25,
65   STAT_PID                      => 26,
66   STAT_UID                      => 27,
67   STAT_SID                      => 28,
68   STAT_THROTTLE                 => 29,
69   STAT_SESSIONS_TOTAL           => 30,
70   STAT_TRACKED                  => 31,
71   STAT_SERVICE_TYPE             => 32,
72   STAT_SESSIONS_RATE_CURRENT    => 33,
73   STAT_SESSIONS_RATE_LIMIT      => 34,
74   STAT_SESSIONS_RATE_MAX        => 35,
75   STAT_CHECK_STATUS             => 36,
76   STAT_CHECK_CODE               => 37,
77   STAT_CHECK_DURATION           => 38,
78   STAT_RESPONSES_HTTP_1XX       => 39,
79   STAT_RESPONSES_HTTP_2XX       => 40,
80   STAT_RESPONSES_HTTP_3XX       => 41,
81   STAT_RESPONSES_HTTP_4XX       => 42,
82   STAT_RESPONSES_HTTP_5XX       => 43,
83   STAT_RESPONSES_HTTP_XXX       => 44,
84   STAT_CHECK_FAILED_DETAILS     => 45,
85   STAT_REQUESTS_RATE_CURRENT    => 46,
86   STAT_REQUESTS_RATE_MAX        => 47,
87   STAT_REQUESTS_TOTAL           => 48,
88   STAT_ABORTS_CLIENT            => 49,
89   STAT_ABORTS_SERVER            => 50,
90   STAT_COMPRESSOR_IN            => 51,
91   STAT_COMPRESSOR_OUT           => 52,
92   STAT_COMPRESSOR_BYPASSED      => 53,
93   STAT_COMPRESSOR_REQUESTS      => 54,
94   STAT_SESSIONS_LAST            => 55,
95   STAT_CHECK_HEALTH_LAST        => 56,
96   STAT_CHECK_AGENT_LAST         => 57,
97   STAT_TIME_QUEUE               => 58,
98   STAT_TIME_CONNECT             => 59,
99   STAT_TIME_RESPONSE            => 60,
100   STAT_TIME_TOTAL               => 61,
101   # Types used by HAProxy for some fields
102   TYPE_FRONTEND                 => 0,
103   TYPE_BACKEND                  => 1,
104   TYPE_SERVER                   => 2,
105   TYPE_LISTENER                 => 3,
106   # Types used to define which attributed to give a graph
107   TYPE_BANDWIDTH                => 0,
108   TYPE_TIMING                   => 1,
109   TYPE_SESSIONS                 => 2,
110   TYPE_RESPONSES                => 3,
111   TYPE_BACKENDS                 => 4,
112   # Program-specific constants
113   GRAPH_CATEGORY                => 'haproxyng',
114   GRAPH_WIDTH                   => 500,
115   FETCH                         => 0,
116   CONFIG                        => 1,
117   # Configurable limits
118   MAX_BANDWIDTH                 => (1024**3),
119   MAX_TIMING                    => (1200000),
120   MAX_SESSIONS                  => (10000),
121   MAX_RESPONSES                 => (100000),
122   MAX_BACKENDS                  => (1000),
123   # Constants to match Munin keywords
124   DERIVE                        => 'DERIVE',
125   GAUGE                         => 'GAUGE',
126   AREASTACK                     => 'AREASTACK',
127   LINE1                         => 'LINE1',
128   COLOUR_BLACK                  => '000000',
129 };
130
131 # Configure basic variables for program operation
132 our ($_socket, $_clean, %data);
133 # Get the location of the socket we'll connect to
134 $_socket = exists $ENV{'socket'} ? $ENV{'socket'} : '/var/run/haproxy.sock';
135 $_clean  = (exists $ENV{'clean'} ? quotemeta $ENV{'clean'} : '');
136
137 MAIN: {
138   # This is a special multi-graph plugin, so make sure that whatever version
139   # of Munin we're running in is enabled and configured for it.
140   need_multigraph();
141
142   # Now work out what we are being asked to do, then run it.
143   my $request = $ARGV[0] || "fetch";
144   eval "do_${request}();"
145     or croak "do_${request}: $@";
146   # and then finish
147   exit 0;
148 }
149
150 sub get_data {
151   my ($t, %v); $t = shift;
152
153   my $s = IO::Socket::UNIX->new(
154     Peer    => $_socket,
155     Type    => SOCK_STREAM,
156     Timeout => 1
157   ) or croak "Cannot connect to socket ".$_socket;
158
159   print $s "show stat\n";
160   while (<$s>) {
161     chomp; next unless length;
162     my @stat = split (',');
163
164     switch ($stat[STAT_SERVICE_TYPE]) {
165       # Process all FRONTEND type entries; these are singular and are totals
166       # only for each of the frontends configured
167       case TYPE_FRONTEND {
168         $v{$stat[STAT_PROXY_NAME]} = {
169           type => TYPE_FRONTEND,
170           connections => ($stat[STAT_CONNECTIONS_TOTAL] || 0),
171           sessions    => ($stat[STAT_SESSIONS_CURRENT] || 0),
172           queued      => 0,
173           bandwidth   => {
174             in => ($stat[STAT_BYTES_IN] || 0),
175             out => ($stat[STAT_BYTES_OUT] || 0),
176           },
177           responses   => {
178             total     => ($stat[STAT_REQUESTS_TOTAL] || 0),
179             http1xx   => ($stat[STAT_RESPONSES_HTTP_1XX] || 0),
180             http2xx   => ($stat[STAT_RESPONSES_HTTP_2XX] || 0),
181             http3xx   => ($stat[STAT_RESPONSES_HTTP_3XX] || 0),
182             http4xx   => ($stat[STAT_RESPONSES_HTTP_4XX] || 0),
183             http5xx   => ($stat[STAT_RESPONSES_HTTP_5XX] || 0),
184             httpxxx   => ($stat[STAT_RESPONSES_HTTP_XXX] || 0),
185           },
186         };
187       }
188       # Process all BACKEND type entries; these are the totals for each backend
189       # and don't have the same amount of information as SERVERs
190       case TYPE_BACKEND {
191         # We can't 'set' the hash here as the backend totals are normally after
192         # the backend's servers, so would override anything previously set
193         # in TYPE_SERVER
194         $v{$stat[STAT_PROXY_NAME]}{'type'} = TYPE_BACKEND;
195         $v{$stat[STAT_PROXY_NAME]}{'connections'} = ($stat[STAT_CONNECTIONS_TOTAL] || 0);
196         $v{$stat[STAT_PROXY_NAME]}{'sessions'} = ($stat[STAT_SESSIONS_CURRENT] || 0);
197         $v{$stat[STAT_PROXY_NAME]}{'queued'} = ($stat[STAT_QUEUED_REQUESTS] || 0);
198         $v{$stat[STAT_PROXY_NAME]}{'active'} = ($stat[STAT_SERVERS_ACTIVE] || 0);
199         $v{$stat[STAT_PROXY_NAME]}{'backup'} = ($stat[STAT_SERVERS_BACKUP] || 0);
200         $v{$stat[STAT_PROXY_NAME]}{'bandwidth'} = {
201           in => ($stat[STAT_BYTES_IN] || 0),
202           out => ($stat[STAT_BYTES_OUT] || 0),
203         };
204         $v{$stat[STAT_PROXY_NAME]}{'responses'} = {
205             total     => ($stat[STAT_RESPONSES_HTTP_1XX] || 0)
206                       +  ($stat[STAT_RESPONSES_HTTP_2XX] || 0)
207                       +  ($stat[STAT_RESPONSES_HTTP_3XX] || 0)
208                       +  ($stat[STAT_RESPONSES_HTTP_4XX] || 0)
209                       +  ($stat[STAT_RESPONSES_HTTP_5XX] || 0)
210                       +  ($stat[STAT_RESPONSES_HTTP_XXX] || 0),
211             http1xx   => ($stat[STAT_RESPONSES_HTTP_1XX] || 0),
212             http2xx   => ($stat[STAT_RESPONSES_HTTP_2XX] || 0),
213             http3xx   => ($stat[STAT_RESPONSES_HTTP_3XX] || 0),
214             http4xx   => ($stat[STAT_RESPONSES_HTTP_4XX] || 0),
215             http5xx   => ($stat[STAT_RESPONSES_HTTP_5XX] || 0),
216             httpxxx   => ($stat[STAT_RESPONSES_HTTP_XXX] || 0),
217         };
218       }
219       # Process all SERVER type entries, which are the most details and give
220       # information about how each server is responding
221       case TYPE_SERVER {
222         # Only set the server itself directly, otherwise we may override
223         # anything previously set
224         $v{$stat[STAT_PROXY_NAME]}{'servers'}{$stat[STAT_SERVICE_NAME]} = {
225           active      => ($stat[STAT_SERVER_ACTIVE] ? 1 : 0),
226           backup      => ($stat[STAT_SERVER_BACKUP] ? 1 : 0),
227           up          => ($stat[STAT_STATUS] eq 'UP' ? 1 : 0),
228           down        => ($stat[STAT_STATUS] eq 'DOWN' ? 1 : 0),
229           disabled    => ($stat[STAT_STATUS] =~ /^(MAINT|DRAIN|NOLB)/i ? 1 : 0),
230           connections => ($stat[STAT_CONNECTIONS_TOTAL] || 0),
231           sessions    => ($stat[STAT_SESSIONS_CURRENT] || 0),
232           queued      => ($stat[STAT_QUEUED_REQUESTS] || 0),
233           bandwidth   => {
234             in => ($stat[STAT_BYTES_IN] || 0),
235             out => ($stat[STAT_BYTES_OUT] || 0),
236           },
237           status      => ($stat[STAT_STATUS] || 0),
238           timing      => {
239             queue     => ($stat[STAT_TIME_QUEUE] || 0),
240             connect   => ($stat[STAT_TIME_CONNECT] || 0),
241             response  => ($stat[STAT_TIME_RESPONSE] || 0),
242             total     => ($stat[STAT_TIME_TOTAL] || 0)
243           },
244           responses   => {
245             total     => ($stat[STAT_RESPONSES_HTTP_1XX] || 0)
246                       +  ($stat[STAT_RESPONSES_HTTP_2XX] || 0)
247                       +  ($stat[STAT_RESPONSES_HTTP_3XX] || 0)
248                       +  ($stat[STAT_RESPONSES_HTTP_4XX] || 0)
249                       +  ($stat[STAT_RESPONSES_HTTP_5XX] || 0)
250                       +  ($stat[STAT_RESPONSES_HTTP_XXX] || 0),
251             http1xx   => ($stat[STAT_RESPONSES_HTTP_1XX] || 0),
252             http2xx   => ($stat[STAT_RESPONSES_HTTP_2XX] || 0),
253             http3xx   => ($stat[STAT_RESPONSES_HTTP_3XX] || 0),
254             http4xx   => ($stat[STAT_RESPONSES_HTTP_4XX] || 0),
255             http5xx   => ($stat[STAT_RESPONSES_HTTP_5XX] || 0),
256             httpxxx   => ($stat[STAT_RESPONSES_HTTP_XXX] || 0),
257           },
258         };
259       }
260     }
261   }
262
263   # print Dumper(\%v);
264   return %v;
265 }
266
267 sub mg_fetch {
268   my ($type, $name, $values) = (shift, shift, shift);
269
270   my %values_ = %{$values};
271   my $type_ = $type eq TYPE_FRONTEND ? '_fe' :
272               $type eq TYPE_BACKEND  ? '_be' : '';
273
274   my $name_;
275   foreach my $level (@{$name}) {
276     $level =~ s/[^a-z0-9]+/_/g;
277     $name_ .= ($name_ ? '.' : '').$level;
278   }
279
280   printf "multigraph %s%s_%s\n", GRAPH_CATEGORY, $type_, $name_;
281   foreach my $key (sort keys %values_) {
282     (my $key_ = $key) =~ s/[^a-z0-9]+/_/g;
283     printf "%s.value %0.0f\n", $key_, $values_{$key};
284   }
285   print "\n";
286 }
287
288 sub mg_config {
289   my ($graph, $type, $name, $title, $metrics) = (shift, shift, shift, shift, shift, shift);
290
291   my %metrics_ = %{$metrics};
292
293   my $type_  = $type eq TYPE_FRONTEND ? '_fe' :
294                $type eq TYPE_BACKEND  ? '_be' : '';
295
296      $title  =~ s/$_clean//g if $_clean;
297   my $title_ = $type eq TYPE_FRONTEND ? ' Frontend' :
298                $type eq TYPE_BACKEND  ? ' Backend' : '';
299
300   my $name_;
301   foreach my $level (@{$name}) {
302     next unless $level;
303     $level =~ s/[^a-z0-9]+/_/g;
304     $name_ .= ($name_ ? '.' : '').$level;
305   }
306
307   my $max = undef;
308
309   printf "multigraph %s%s_%s\n", GRAPH_CATEGORY, $type_, $name_;
310   printf "graph_category %s\n", GRAPH_CATEGORY;
311   printf "graph_title HAProxy%s %s\n", $title_, $title;
312   printf "graph_width %d\n", GRAPH_WIDTH;
313
314   switch ($graph) {
315     case TYPE_BANDWIDTH {
316       print "graph_args --base 1024\n".
317             "graph_vlabel bits/second [in(-); out(+)]\n";
318       $max = MAX_BANDWIDTH;
319     }
320     case TYPE_TIMING {
321       print "graph_args --base 1000\n".
322             "graph_vlabel Time in Seconds\n";
323       $max = MAX_TIMING;
324     }
325     case TYPE_SESSIONS {
326       print "graph_args --base 1000\n".
327             "graph_vlabel [Queued(-); Sessions(+)]\n";
328       $max = MAX_SESSIONS;
329     }
330     case TYPE_RESPONSES {
331       print "graph_args --base 1000\n".
332             "graph_vlabel Responses per Second\n";
333       $max = MAX_RESPONSES;
334     }
335     case TYPE_BACKENDS {
336       print "graph_args --base 1000\n".
337             "graph_vlabel Total Backends\n";
338       $max = MAX_BACKENDS;
339     }
340   }
341
342   foreach my $metric (sort keys %metrics_) {
343     (my $metric_ = $metric) =~ s/[^a-z0-9]+/_/g;
344     foreach my $key (keys %{$metrics_{$metric}}) {
345       switch ($key) {
346         case 'label' {
347           my $value = $metrics_{$metric}{$key};
348              $value =~ s/$_clean//g if $_clean;
349           printf "%s.%s %s\n", $metric_, $key, $value;
350         }
351         case 'cdef' {
352           printf "%s.%s %s\n", $metric_, $key, $metric_.','.$metrics_{$metric}{$key};
353         }
354         case 'negative' {
355           my $value = $metrics_{$metric}{$key};
356              $value =~ s/[^a-z0-9]+/_/g;
357           printf "%s.%s %s\n", $metric_, $key, $value;
358         }
359
360         printf "%s.%s %s\n", $metric_, $key, $metrics_{$metric}{$key};
361       }
362     }
363     # Always add min/max if max has been defined
364     if ($max) {
365       printf "%s.%s %s\n", $metric_, 'min', 0;
366       printf "%s.%s %s\n", $metric_, 'max', $max if $max;
367     }
368   }
369   print "\n";
370 }
371
372 # frontend(bandwidth)
373 # backend(bandwidth)
374 # frontend(bandwidth/service)
375 # backend(bandwidth/service)
376 # backend(bandwidth/service/host)
377
378 sub haproxy_bandwidth {
379   my ($operation, $data_) = (shift, shift);
380   my %data = %{$data_};
381
382   switch ($operation) {
383
384     case CONFIG {
385
386       my (%fe_metrics, %be_metrics, %submetrics);
387
388       foreach my $service (sort keys %data) {
389         undef %submetrics;
390
391         # Prepare the field configuration
392         my %in = (
393           label     => $service,
394           type      => DERIVE,
395           cdef      => '8,*',
396           graph     => 'no',
397         );
398         my %out = (
399           label     => $service,
400           type      => DERIVE,
401           cdef      => '8,*',
402           draw      => AREASTACK,
403           negative  => "${service}_in",
404         );
405
406         # This will copy the hash for the main metric
407         switch ($data{$service}{'type'}) {
408           case TYPE_FRONTEND {
409             %{$fe_metrics{"${service}_in"}} = %in;
410             %{$fe_metrics{"${service}_out"}} = %out;
411
412           }
413           case TYPE_BACKEND {
414             %{$be_metrics{"${service}_in"}} = %in;
415             %{$be_metrics{"${service}_out"}} = %out;
416           }
417         }
418
419         # next unless $data{$service}{'type'} eq TYPE_BACKEND;
420
421         # Override these for the totals in the sub metric graph
422         $in{'label'} = $out{'label'} = 'Total';
423         $out{'negative'} = 'total_in';
424         $out{'draw'} = LINE1;
425
426         # This will copy the hash for the submetric
427         %{$submetrics{'total_in'}} = %in;
428         %{$submetrics{'total_out'}} = %out;
429           $submetrics{'total_out'}{'colour'} = COLOUR_BLACK;
430
431         # Loop through each of the available servers
432         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
433           next if $data{$service}{'servers'}{$server}{'backup'};
434           # Override these for the per-Service graph
435           $in{'label'} = $out{'label'} = $server;
436           $out{'negative'} = "${server}_in";
437           $out{'draw'} = AREASTACK;
438
439           # This will copy the hash for the submetric
440           %{$submetrics{"${server}_in"}} = %in;
441           %{$submetrics{"${server}_out"}} = %out;
442
443           # Override these for the per-Host graph
444           $in{'label'} = $out{'label'} = 'Bandwidth';
445           $out{'negative'} = 'total_in';
446           $out{'draw'} = LINE1;
447           $out{'colour'} = COLOUR_BLACK;
448
449           mg_config(TYPE_BANDWIDTH, $data{$service}{'type'}, ['bandwidth', $service, $server],
450                     'Bandwidth for '.$service.' via '.$server, {
451             total_in  => \%in,
452             total_out => \%out,
453           });
454
455           delete $out{'colour'};
456         }
457
458         mg_config(TYPE_BANDWIDTH, $data{$service}{'type'}, ['bandwidth', $service],
459                   'Bandwidth for '.$service, \%submetrics);
460       }
461
462       mg_config(TYPE_BANDWIDTH, TYPE_FRONTEND, ['bandwidth'],
463                 'Bandwidth Overview', \%fe_metrics);
464       mg_config(TYPE_BANDWIDTH, TYPE_BACKEND, ['bandwidth'],
465                 'Bandwidth Overview', \%be_metrics);
466
467     }
468
469     case FETCH {
470
471       my (%values);
472       foreach my $service (sort keys %data) {
473         next unless $data{$service}{'type'} eq TYPE_FRONTEND;
474         $values{"${service}_in"}  = $data{$service}{'bandwidth'}{'in'};
475         $values{"${service}_out"} = $data{$service}{'bandwidth'}{'out'};
476       }
477
478       mg_fetch(TYPE_FRONTEND, ['bandwidth'], \%values);
479
480       undef %values;
481
482       foreach my $service (sort keys %data) {
483         next unless $data{$service}{'type'} eq TYPE_BACKEND;
484         $values{"${service}_in"}  = $data{$service}{'bandwidth'}{'in'};
485         $values{"${service}_out"} = $data{$service}{'bandwidth'}{'out'};
486       }
487
488       mg_fetch(TYPE_BACKEND, ['bandwidth'], \%values);
489
490       foreach my $service (sort keys %data) {
491         %values = (
492           total_in  => $data{$service}{'bandwidth'}{'in'},
493           total_out => $data{$service}{'bandwidth'}{'out'},
494         );
495
496         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
497           next if $data{$service}{'servers'}{$server}{'backup'};
498           $values{"${server}_in"}  = $data{$service}{'servers'}{$server}{'bandwidth'}{'in'};
499           $values{"${server}_out"} = $data{$service}{'servers'}{$server}{'bandwidth'}{'out'};
500           mg_fetch($data{$service}{'type'}, ['bandwidth', $service, $server], {
501             total_in  => $data{$service}{'servers'}{$server}{'bandwidth'}{'in'},
502             total_out => $data{$service}{'servers'}{$server}{'bandwidth'}{'out'},
503           });
504         }
505
506         mg_fetch($data{$service}{'type'}, ['bandwidth', $service], \%values);
507
508       }
509
510     }
511   }
512 }
513
514 # backend(timing)
515 # backend(timing/service)
516 # backend(timing/service/host)
517
518 sub haproxy_timing {
519   my ($operation, $data_) = (shift, shift);
520   my %data = %{$data_};
521
522   switch ($operation) {
523
524     case CONFIG {
525
526       my (%metrics, %submetrics);
527
528       foreach my $service (sort keys %data) {
529         next unless $data{$service}{'type'} eq TYPE_BACKEND;
530
531         undef %submetrics;
532
533         # Prepare the field configuration
534         my %time = (
535           label     => $service,
536           type      => GAUGE,
537           cdef      => '1000,/',
538           draw      => LINE1,
539         );
540
541         # This will copy the hash for the main metric
542         %{$metrics{$service}} = %time;
543
544         # Loop through each of the available servers
545         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
546           next if $data{$service}{'servers'}{$server}{'backup'};
547           # Override these for the per-Service graph
548           $time{'label'} = $server;
549
550           # This will copy the hash for the submetric
551           %{$submetrics{$server}} = %time;
552
553           # Copy and override these for the per-Host graph
554           my %queue     = %time; $queue{'label'}    = 'Queue';
555           my %connect   = %time; $connect{'label'}  = 'Connect';
556           my %response  = %time; $response{'label'} = 'Response';
557           my %total     = %time; $total{'label'}    = 'Total';
558
559           mg_config(TYPE_TIMING, TYPE_BACKEND, ['timing', $service, $server],
560                     'Server Timing for '.$service.' via '.$server, {
561             queue     => \%queue,
562             connect   => \%connect,
563             response  => \%response,
564             total     => \%total,
565           });
566         }
567
568         mg_config(TYPE_TIMING, TYPE_BACKEND, ['timing', $service],
569                   'Server Timing for '.$service, \%submetrics);
570       }
571
572       mg_config(TYPE_TIMING, TYPE_BACKEND, ['timing'],
573                 'Server Timing Overview', \%metrics);
574
575     }
576
577     case FETCH {
578
579       my (%values);
580
581       foreach my $service (sort keys %data) {
582         next unless $data{$service}{'type'} eq TYPE_BACKEND;
583         my ($count, $total) = (0,0);
584         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
585           ++$count;
586           $total+=$data{$service}{'servers'}{$server}{'timing'}{'total'};
587         }
588
589         $values{$service} = ($count > 0 ? $total/$count : 0);
590       }
591
592       mg_fetch(TYPE_BACKEND, ['timing'], \%values);
593
594
595       foreach my $service (sort keys %data) {
596         next unless $data{$service}{'type'} eq TYPE_BACKEND;
597         undef %values;
598
599         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
600           next if $data{$service}{'servers'}{$server}{'backup'};
601           $values{$server} = $data{$service}{'servers'}{$server}{'timing'}{'total'};
602           mg_fetch(TYPE_BACKEND, ['timing', $service, $server],
603                    $data{$service}{'servers'}{$server}{'timing'});
604         }
605
606         mg_fetch(TYPE_BACKEND, ['timing', $service], \%values);
607
608       }
609
610     }
611   }
612 }
613
614 # frontend(sessions)
615 # frontend(sessions/service)
616 # backend(sessions)
617 # backend(sessions/service)
618 # backend(sessions/service/host)
619
620 sub haproxy_sessions {
621   my ($operation, $data_) = (shift, shift);
622   my %data = %{$data_};
623
624   switch ($operation) {
625
626     case CONFIG {
627
628       my (%metrics, %submetrics);
629
630       foreach my $service (sort keys %data) {
631         next unless $data{$service}{'type'} eq TYPE_FRONTEND;
632
633         my %sessions = (
634           label     => $service,
635           type      => GAUGE,
636           draw      => LINE1,
637         );
638
639         %{$metrics{"${service}_sessions"}} = %sessions;
640
641         # Override these for the per-Service graph
642         $sessions{'label'} = 'Sessions';
643
644         mg_config(TYPE_SESSIONS, TYPE_FRONTEND, ['sessions', $service],
645                   'Sessions for '.$service, {
646           sessions => \%sessions,
647         });
648       }
649
650       mg_config(TYPE_SESSIONS, TYPE_FRONTEND, ['sessions'],
651                 'Sessions Overview', \%metrics);
652
653       undef %metrics;
654
655       foreach my $service (sort keys %data) {
656         next unless $data{$service}{'type'} eq TYPE_BACKEND;
657
658         undef %submetrics;
659
660         # Prepare the field configuration
661         my %queued    = (
662           label     => $service,
663           type      => GAUGE,
664           graph     => 'no',
665         );
666         my %sessions  = (
667           label     => $service,
668           type      => GAUGE,
669           draw      => LINE1,
670           negative  => "${service}_queued",
671         );
672
673         # This will copy the hash for the main metric
674         %{$metrics{"${service}_queued"}} = %queued;
675         %{$metrics{"${service}_sessions"}} = %sessions;
676
677         # Override these for the totals in the sub metric graph
678         $queued{'label'} = $sessions{'label'} = 'Total';
679         $out{'negative'} = 'total_queued';
680         $out{'draw'} = LINE1;
681
682         # This will copy the hash for the submetric
683         %{$submetrics{'total_queued'}} = %queued;
684         %{$submetrics{'total_sessions'}} = %sessions;
685
686         # Loop through each of the available servers
687         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
688           next if $data{$service}{'servers'}{$server}{'backup'};
689           # Override these for the per-Service graph
690           $in{'label'} = $out{'label'} = $server;
691           $out{'negative'} = "${server}_queued";
692           $out{'draw'} = AREASTACK;
693
694           # This will copy the hash for the submetric
695           %{$submetrics{"${server}_queued"}} = %queued;
696           %{$submetrics{"${server}_sessions"}} = %sessions;
697
698           # Override these for the per-Host graph
699           $in{'label'} = $out{'label'} = 'Sessions';
700           $out{'negative'} = 'queued';
701           $out{'draw'} = LINE1;
702
703           mg_config(TYPE_SESSIONS, TYPE_BACKEND, ['sessions', $service, $server],
704                     'Sessions for '.$service.' via '.$server, {
705             queued    => \%queued,
706             sessions  => \%sessions,
707           });
708         }
709
710         mg_config(TYPE_SESSIONS, TYPE_BACKEND, ['sessions', $service],
711                   'Sessions for '.$service, \%submetrics);
712       }
713
714       mg_config(TYPE_SESSIONS, TYPE_BACKEND, ['sessions'],
715                 'Sessions Overview', \%metrics);
716
717     }
718
719     case FETCH {
720
721       my (%values);
722
723       foreach my $service (sort keys %data) {
724         next unless $data{$service}{'type'} eq TYPE_FRONTEND;
725         $values{"${service}_sessions"} = $data{$service}{'sessions'};
726         mg_fetch(TYPE_FRONTEND, ['sessions', $service], {
727           sessions => $data{$service}{'sessions'},
728         });
729       }
730
731       mg_fetch(TYPE_FRONTEND, ['sessions'], \%values);
732
733       undef %values;
734
735       foreach my $service (sort keys %data) {
736         next unless $data{$service}{'type'} eq TYPE_BACKEND;
737         $values{"${service}_sessions"} = $data{$service}{'sessions'};
738         $values{"${service}_queued"} = $data{$service}{'queued'};
739       }
740
741       mg_fetch(TYPE_BACKEND, ['sessions'], \%values);
742
743       foreach my $service (sort keys %data) {
744         next unless $data{$service}{'type'} eq TYPE_BACKEND;
745
746         %values = (
747           total_sessions => $data{$service}{'sessions'},
748           total_queued => $data{$service}{'queued'},
749         );
750
751         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
752           next if $data{$service}{'servers'}{$server}{'backup'};
753
754           $values{"${server}_sessions"} = $data{$service}{'servers'}{$server}{'sessions'};
755           $values{"${server}_queued"} = $data{$service}{'servers'}{$server}{'queued'};
756
757           mg_fetch(TYPE_BACKEND, ['sessions', $service, $server], {
758             sessions  => $data{$service}{'servers'}{$server}{'sessions'},
759             queued    => $data{$service}{'servers'}{$server}{'queued'},
760           });
761         }
762
763         mg_fetch(TYPE_BACKEND, ['sessions', $service], \%values);
764
765       }
766
767     }
768   }
769 }
770
771 # frontend(responses)
772 # frontend(responses/service(status))
773 # frontend(responses/service(status)/status)
774 # backend(responses)
775 # backend(responses/service(server))
776 # backend(responses/service(server)/server)
777 # backend(responses/service(status))
778 # backend(responses/service(status)/status)
779 # backend(responses/service(status)/status/server)
780
781 sub haproxy_responses {
782   my ($operation, $data_) = (shift, shift);
783   my %data = %{$data_};
784
785   switch ($operation) {
786
787     case CONFIG {
788
789       my (%fe_metrics, %be_metrics, %submetrics, $subsubmetrics);
790
791       foreach my $service (sort keys %data) {
792         my %response = (
793           label     => $service,
794           type      => DERIVE,
795           draw      => LINE1,
796         );
797
798         switch ($data{$service}{'type'}) {
799           case TYPE_FRONTEND {
800             %{$fe_metrics{$service}} = %response;
801           }
802           case TYPE_BACKEND {
803             %{$be_metrics{$service}} = %response;
804           }
805         }
806
807         undef %submetrics;
808
809         foreach my $response (sort keys %{$data{$service}{'responses'}}) {
810           $response_ = ($response  =~ /([12345])xx$/ ? "HTTP ${1}xx" :
811                         $response  eq 'total'        ? "Total"       : "Other");
812           $draw_     = ($response_ eq 'Total'        ? LINE1       : AREASTACK);
813
814           %{$submetrics{$response}} = (
815             label   => $response_,
816             type    => DERIVE,
817             draw    => $draw_,
818           );
819
820           $submetrics{$response}{'colour'} = COLOUR_BLACK if $response eq 'total';
821
822           switch ($data{$service}{'type'}) {
823             case TYPE_FRONTEND {
824               mg_config(TYPE_RESPONSES, $data{$service}{'type'}, ['responses', $service, $response],
825                         "${response_} Responses for ${service}", {
826                 responses => {
827                   label   => 'Responses',
828                   type    => DERIVE,
829                   draw    => LINE1,
830                 }
831               });
832             }
833             case TYPE_BACKEND {
834
835               undef %subsubmetrics;
836
837               foreach my $server (sort keys %{$data{$service}{'servers'}}) {
838                 next if $data{$service}{'servers'}{$server}{'backup'};
839                 %{$subsubmetrics{$server}} = (
840                   label   => $server,
841                   type    => DERIVE,
842                   draw    => AREASTACK,
843                 );
844                 mg_config(TYPE_RESPONSES, $data{$service}{'type'}, ['responses', $service, $response, $server],
845                           "${response_} Responses for ${service} via ${server}", {
846                   responses => {
847                     label   => 'Responses',
848                     type    => DERIVE,
849                     draw    => LINE1,
850                   }
851                 });
852               }
853
854               %{$subsubmetrics{'total'}} = (
855                 label   => 'Total',
856                 type    => DERIVE,
857                 draw    => LINE1,
858                 colour  => COLOUR_BLACK,
859               );
860
861               mg_config(TYPE_RESPONSES, $data{$service}{'type'}, ['responses', $service, $response],
862                         "${response_} Responses for ${service}", \%subsubmetrics);
863             }
864           }
865
866         }
867
868         mg_config(TYPE_RESPONSES, $data{$service}{'type'}, ['responses', $service],
869                   'Responses for '.$service, \%submetrics);
870       }
871
872       mg_config(TYPE_RESPONSES, TYPE_FRONTEND, ['responses'],
873                 'Responses Overview', \%fe_metrics);
874       mg_config(TYPE_RESPONSES, TYPE_BACKEND, ['responses'],
875                 'Responses Overview', \%be_metrics);
876
877     }
878
879     case FETCH {
880
881       my (%values, %subvalues);
882
883       foreach my $service (sort keys %data) {
884         next unless $data{$service}{'type'} eq TYPE_FRONTEND;
885         $values{$service} = $data{$service}{'responses'}{'total'};
886         mg_fetch(TYPE_FRONTEND, ['responses', $service], $data{$service}{'responses'});
887         foreach my $response (sort keys %{$data{$service}{'responses'}}) {
888           mg_fetch(TYPE_FRONTEND, ['responses', $service, $response], {
889             responses => $data{$service}{'responses'}{$response}
890           });
891         }
892       }
893
894       mg_fetch(TYPE_FRONTEND, ['responses'], \%values);
895
896       undef %values;
897
898       foreach my $service (sort keys %data) {
899         next unless $data{$service}{'type'} eq TYPE_BACKEND;
900
901         $values{$service} = $data{$service}{'responses'}{'total'};
902
903         %subvalues = (
904           total => $data{$service}{'responses'}{'total'},
905         );
906
907         mg_fetch(TYPE_BACKEND, ['responses', $service], $data{$service}{'responses'});
908
909         foreach my $response (sort keys %{$data{$service}{'responses'}}) {
910           undef %subvalues;
911
912           foreach my $server (sort keys %{$data{$service}{'servers'}}) {
913             next if $data{$service}{'servers'}{$server}{'backup'};
914
915             $subvalues{$server} = $data{$service}{'servers'}{$server}{'responses'}{$response};
916             mg_fetch(TYPE_BACKEND, ['responses', $service, $response, $server], {
917               responses => $data{$service}{'servers'}{$server}{'responses'}{$response},
918             });
919           }
920
921           $subvalues{'total'} = $data{$service}{'responses'}{$response};
922
923           mg_fetch(TYPE_BACKEND, ['responses', $service, $response], \%subvalues);
924         }
925       }
926
927       mg_fetch(TYPE_BACKEND, ['responses'], \%values);
928
929     }
930
931   }
932 }
933
934 sub haproxy_count {
935   my ($operation, $data_) = (shift, shift);
936   my %data = %{$data_};
937
938   switch ($operation) {
939
940     case CONFIG {
941
942       my (%metrics, %submetrics);
943
944       foreach my $service (sort keys %data) {
945         next unless $data{$service}{'type'} eq TYPE_BACKEND;
946
947         # Prepare the field configuration
948         my %count = (
949           label     => $service,
950           type      => GAUGE,
951           draw      => AREASTACK,
952         );
953
954         # This will copy the hash for the main metric
955         %{$metrics{$service}} = %count;
956           $metrics{$service}{'draw'} = LINE1;
957
958         # Copy and override these for the per-Service graph
959         my %backup    = %count; $backup{'label'}    = 'Backup';
960         my %down      = %count; $down{'label'}      = 'Down';
961         my %disabled  = %count; $disabled{'label'}  = 'Disabled';
962         my %up        = %count; $up{'label'}        = 'Up';
963
964         mg_config(TYPE_BACKEND, TYPE_BACKEND, ['count', $service],
965                   'Server Count for '.$service, {
966           backup    => \%backup,
967           down      => \%down,
968           disabled  => \%disabled,
969           up        => \%up,
970         });
971       }
972
973       mg_config(TYPE_BACKEND, TYPE_BACKEND, ['count'],
974                 'Server Count Overview', \%metrics);
975
976     }
977
978     case FETCH {
979
980       my (%values);
981
982       foreach my $service (sort keys %data) {
983         next unless $data{$service}{'type'} eq TYPE_BACKEND;
984
985         my ($backup, $disabled, $down, $up) = (0,0,0,0);
986         foreach my $server (sort keys %{$data{$service}{'servers'}}) {
987           ++$backup   if $data{$service}{'servers'}{$server}{'backup'}
988                      and not $data{$service}{'servers'}{$server}{'down'}
989                      and not $data{$service}{'servers'}{$server}{'disabled'};
990           ++$disabled if $data{$service}{'servers'}{$server}{'disabled'};
991           ++$down     if $data{$service}{'servers'}{$server}{'down'};
992           ++$up       if $data{$service}{'servers'}{$server}{'up'}
993                      and not $data{$service}{'servers'}{$server}{'backup'};
994         }
995
996         $values{$service} = $up;
997
998         mg_fetch(TYPE_BACKEND, ['count', $service], {
999           backup    => $backup,
1000           disabled  => $disabled,
1001           down      => $down,
1002           up        => $up,
1003         });
1004       }
1005
1006       mg_fetch(TYPE_BACKEND, ['count'], \%values);
1007
1008     }
1009
1010   }
1011 }
1012
1013 sub do_fetch {
1014   my %data = get_data;
1015   exit 1 unless scalar(keys %data);
1016
1017   haproxy_bandwidth FETCH, \%data;
1018   haproxy_timing FETCH, \%data;
1019   haproxy_sessions FETCH, \%data;
1020   haproxy_count FETCH, \%data;
1021   haproxy_responses FETCH, \%data;
1022
1023   # Return something or the eval() will fail above
1024   return 1;
1025 }
1026
1027 sub do_config {
1028   my %data = get_data;
1029   exit 1 unless scalar(keys %data);
1030
1031   haproxy_bandwidth CONFIG, \%data;
1032   haproxy_timing CONFIG, \%data;
1033   haproxy_sessions CONFIG, \%data;
1034   haproxy_count CONFIG, \%data;
1035   haproxy_responses CONFIG, \%data;
1036
1037   return 1;
1038 }
1039
1040 sub do_autoconf {
1041   # All Munin wants to know is will this plugin provide all the configuration
1042   # settings and options itself? Well, yes...
1043   print "".(-S $_socket ? "yes" : "no")."\n";
1044   return 1;
1045 }