This file is indexed.

/usr/share/munin/plugins/haproxy_ng is in munin-plugins-extra 2.0.25-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/perl -w
#
# haproxy_ng - Munin plugin HAProxy
# Copyright (C) 2012 Redpill Linpro AS
#
# Author: Trygve Vea <tv@redpill-linpro.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Graph stats from the haproxy daemon
#
# Parameters supported:
#
#     config
#
# Configurable variables
#
#     url      - Override default status-url
#     username - Basic authentication username
#     password - Basic authentication password
#
# Example:
#
#   [haproxy_backend]
#   env.url http://backend.example.com/haproxy-status;csv;norefresh
#   env.username monitor
#   env.password somepasswordorother
#
# Magic markers:
#%# family=contrib

use Munin::Plugin;

need_multigraph();

my $ret = undef;
if (! eval "require LWP::UserAgent;")
{
    $ret = "LWP::UserAgent not found";
}

my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://localhost/haproxy-status;csv;norefresh";

{
    package MyLWP;
    @ISA = qw(LWP::UserAgent);

    sub new {
        my $self = LWP::UserAgent::new(@_);
        $self;
    }
    sub get_basic_credentials {
        return ($ENV{"username"}, $ENV{"password"});
    }
}

my $ua = MyLWP->new(timeout => 30);

my @types = qw(frontend backend server);
my $url = $URL;
my $response = $ua->request(HTTP::Request->new('GET',$url));
my $content = $response->content;
my $set;
my @columns;

my %datas = ();

my @graph_parameters = ('total','order','scale','vlabel','args', 'category');
my @field_parameters = ('graph', 'min', 'max', 'draw', 'cdef', 'warning', 'colour', 'info', 'type', 'negative');

my %aspects = (
    'Bandwidth' => {
        'title' => 'Bandwidth usage',
        'category' => 'HAProxy',
        'vlabel' => 'per second',
        'values' => {
            'bin' => {
                'type' => 'DERIVE',
                'min' => 0,
                'graph' => 'no'
            },
            'bout' => {
                'type' => 'DERIVE',
                'draw' => 'AREASTACK',
                'min' => 0,
                'label' => 'bytes',
                'info' => 'Bytes transferred',
                'negative' => 'bin'
            }
        }
    },
    'Sessions' => {
        'title' => 'Sessions',
        'category' => 'HAProxy',
        'vlabel' => 'current',
        'values' => {
            'scur' => {
                'type' => 'GAUGE',
                'draw' => 'AREASTACK',
                'min' => 0,
                'label' => 'sessions',
                'info' => 'Current sessions',
            }
        }
    },
    'Uptime' => {
        'title' => 'Uptime',
        'category' => 'HAProxy',
        'vlabel' => 'seconds',
        'values' => {
            'lastchg' => {
                'type' => 'GAUGE',
                'draw' => 'LINE2',
                'min' => 0,
                'label' => 'seconds',
                'info' => 'Seconds',
            }
        }
    },
    'Active' => {
        'title' => 'Active Servers',
        'category' => 'HAProxy',
        'vlabel' => 'servers',
        'values' => {
            'act' => {
                'type' => 'GAUGE',
                'draw' => 'LINE2',
                'min' => 0,
                'label' => 'servers',
                'info' => 'Number of active servers'
            }
        }
    },
    'ErrorReq' => {
        'title' => 'Request errors',
        'category' => 'HAProxy',
        'vlabel' => 'per second',
        'values' => {
            'ereq' => {
                'type' => 'DERIVE',
                'draw' => 'LINE2',
                'min' => 0,
                'label' => 'requests',
                'info' => 'Number of request errors'
            }
        }
    },
    'ErrorResp' => {
        'title' => 'Response errors',
        'category' => 'HAProxy',
        'vlabel' => 'per second',
        'values' => {
            'eresp' => {
                'type' => 'DERIVE',
                'draw' => 'LINE2',
                'min' => 0,
                'label' => 'requests',
                'info' => 'Number of response errors'
            }
        }
    }
);

my %graphs = (
    'BandwidthFront' => {
        'lines' => 'frontend',
        'title' => 'Bandwidth usage by Frontend',
        'aspect' => 'Bandwidth'
    },
    'BandwidthBack' => {
        'lines' => 'backend',
        'title' => 'Bandwidth usage by Backend',
        'aspect' => 'Bandwidth',
    },
    'BandwidthBack.BACKEND' => {
        'lines' => 'server',
        'title' => 'Bandwidth usage by backend: BACKEND',
        'aspect' => 'Bandwidth',
    },
    'SessionsFront' => {
        'lines' => 'frontend',
        'title' => 'Session rate by Frontend',
        'aspect' => 'Sessions'
    },
    'SessionsBack' => {
        'lines' => 'backend',
        'title' => 'Session rate by Backend',
        'aspect' => 'Sessions'
    },
    'SessionsBack.BACKEND' => {
        'lines' => 'server',
        'title' => 'Session rate by backend: BACKEND',
        'aspect' => 'Sessions'
    },
    'Uptime' => {
        'lines' => 'backend',
        'title' => 'Uptime by Backend',
        'aspect' => 'Uptime'
    },
    'Uptime.BACKEND' => {
        'lines' => 'server',
        'title' => 'Uptime by servers in BACKEND',
        'aspect' => 'Uptime'
    },
    'Active' => {
        'lines' => 'backend',
        'title' => 'Active servers by Backend',
        'aspect' => 'Active'
    },
    'ErrorsFront' => {
        'lines' => 'frontend',
        'title' => 'Request errors by Frontend',
        'aspect' => 'ErrorReq'
    },
    'ErrorsBack' => {
        'lines' => 'backend',
        'title' => 'Response errors by Backend',
        'aspect' => 'ErrorResp'
    },
    'ErrorsBack.BACKEND' => {
        'lines' => 'server',
        'title' => 'Response errors by backend: BACKEND',
        'aspect' => 'ErrorResp'
    }
);

my @lines = split(/\n/,$content);

foreach ( @lines )
{
    my $i = 0;
    my @line = split(/,/,$_);
    if ( !$set )
    {
        $set++;
        @columns = @line;
        $columns[0] =~ s/^# //;
        next;
    }

    foreach(@line)
    {
        $datas{$types[$line[32]]}{$line[0]}{$line[1]}{$columns[$i]} = $_;
        $i++;
    }
}

sub fetch_graph
{
    my $name = shift;
    my $graphkey = shift;
    my @names = split(/\./,$name);
    my $backend = $names[1];
    my $data;
    my $aspect = $graphs{$graphkey}{'aspect'};

    print "multigraph HAP$name\n";

    if(!defined $ARGV[0]){ $data = 'values'; }
    elsif ( $ARGV[0] eq 'config' ) { $data = 'config' }


    if ( $data eq 'config' ) {
        my $title = $graphs{$graphkey}{'title'};
        $title =~ s/BACKEND/$backend/g;

        print "graph_title $title\n";
        foreach (@graph_parameters) {
            if (!defined($aspects{$aspect}{$_})) {
                next;
            }
            print "graph_$_ $aspects{$aspect}{$_}\n";
        }
    }

    foreach $val (sort keys %{$aspects{$aspect}{'values'}}) {
        if ( $graphs{$graphkey}{'lines'} eq 'frontend' )
        {
            foreach $fe ( sort keys %{$datas{'frontend'}} )
            {
                if ( $data eq 'config' ) {
                    print $fe."_$val.label $fe\n";
                    foreach (@field_parameters) {
                        if (!defined($aspects{$aspect}{'values'}{$val}{$_})) {
                            next;
                        }
                        if ( $_ eq 'negative' )
                        {
                            print $fe."_$val.$_ $fe"."_$aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                        else
                        {
                            print $fe."_$val.$_ $aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                    }
                }
                else {
                    if ( $val eq 'lastchg' and $datas{'frontend'}{$fe}{'FRONTEND'}{'status'} eq 'DOWN' ) {
                        print $fe."_".$val.".value 0\n";
                    }
                    else {
                        print $fe."_".$val.".value ".$datas{'frontend'}{$fe}{'FRONTEND'}{$val}."\n";
                    }
                }
            }
        }
        elsif ( $graphs{$graphkey}{'lines'} eq 'backend' )
        {
            foreach $be ( sort keys %{$datas{'backend'}} )
            {
                if ( $data eq 'config' ) {
                    print $be."_$val.label $be\n";
                    foreach (@field_parameters) {
                        if (!defined($aspects{$aspect}{'values'}{$val}{$_})) {
                            next;
                        }
                        if ( $_ eq 'negative' )
                        {
                            print $be."_$val.$_ $be"."_$aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                        else
                        {
                            print $be."_$val.$_ $aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                    }
                }
                else {
                    if ( $val eq 'lastchg' and $datas{'backend'}{$be}{'BACKEND'}{'status'} eq 'DOWN' ) {
                        print $be."_".$val.".value 0\n";
                    }
                    else {
                        print $be."_".$val.".value ".$datas{'backend'}{$be}{'BACKEND'}{$val}."\n";
                    }
                }
            }
        }
        elsif ( $graphs{$graphkey}{'lines'} eq 'server' )
        {
            foreach $server ( sort keys %{$datas{'server'}{$backend}} )
            {
                if ( $data eq 'config' ) {
                    print $server."_$val.label $server\n";
                    foreach (@field_parameters) {
                        if (!defined($aspects{$aspect}{'values'}{$val}{$_})) {
                            next;
                        }
                        if ( $_ eq 'negative' )
                        {
                            print $server."_$val.$_ $server"."_$aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                        else
                        {
                            print $server."_$val.$_ $aspects{$aspect}{'values'}{$val}{$_}\n";
                        }
                    }
                }
                else {
                    if ( $val eq 'lastchg' and $datas{'server'}{$backend}{$server}{'status'} eq 'DOWN' ) {
                        print $server."_".$val.".value 0\n";
                    }
                    else {
                        print $server."_".$val.".value ".$datas{'server'}{$backend}{$server}{$val}."\n";
                    }
                }
            }
        }
    }
    print "\n";
}

sub proc_graph
{
    my $g = shift;
    my $pre = shift;
    my $level = shift;

    foreach( sort keys %{$g} )
    {
        my $name = $_;
        my @spects = split(/\./,$name);
        my $aspect = $spects[0];
        my $graphkey = $name;

        if ( $name =~ /BACKEND/ )
        {
            foreach $be (sort keys %{$datas{'backend'}})
            {
                my $name = $name;
                $name =~ s/BACKEND/$be/;
                fetch_graph($name, $graphkey, $be);
            }
        }
        else
        {
            fetch_graph($name, $graphkey);
        }
    }
}

proc_graph(\%graphs, "", 0);

# vim:syntax=perl