File: cps_.in

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (178 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (5)
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
#!@@PERL@@
#
# Plugin to monitor connections per second, for LVS loadbalancers.
#
# Wildcard names:
#
#      cps_<port>
#      cps_<vip>_<port>
#
# Examples:
#
#      cps_smtp
#      cps_mail.foo.boo_smtp
#      cps_pop3
#      cps_www.foo.boo_www
#      cps_vvv.foo.boo_www
#
# Parameters understood:
#
#      config   (required)
#      autoconf (optional - used by munin-config)
#      suggest  (optional - used by munin-config)
#
# Magic markers - optional - used by installation scripts and munin-config:
#
#%# family=auto
#%# capabilities=autoconf suggest
#

use warnings;
use strict;

if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
    autoconf ();
}

if ( defined $ARGV[0] and $ARGV[0] eq "suggest" ) {
    my $sipvs;
    $sipvs = &ipvs (".", ".", $sipvs);
    exit 0 if $sipvs == undef;
    suggest ($sipvs);
}

unless ($0 =~ /cps(?:_([^_]+)|)_(.+)\s*$/) {
    die "Could not parse name $0.\n";
}

my $vip  = $1;
my $port = $2;
my $ipvs;

#print "$vip:$port\n";

use Socket;
my $name;
my $address;
$address = inet_aton($vip);
$name = gethostbyaddr($address,AF_INET);

#print "$vip:$port\n";
#print "Name: $0\nPort: $port\nVip : $vip\n";

# Read ipvsadm-output

$ipvs = &ipvs ($vip, $port, $ipvs);

if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
    config ($vip, $port, $ipvs);
}

$vip = $vip || "";
if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port})) {
    foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) {
	(my $fname = $host) =~ s/[.-]/_/g;
	print "$fname.value ", $ipvs->{$vip}->{$port}->{$host}, "\n";;
    }
}


sub autoconf {
    system ("/sbin/ipvsadm -L -n --stats >/dev/null 2>/dev/null");

    if ($? == 0) {
	print "yes\n";
	exit 0;
    }
    elsif ($? & 127) {
	    print "no (system call exited with %d)", $? & 127;
    }
    elsif (($?>>8) == 2) {
	print "no (permission denied)\n";
	exit 0;
    }
    elsif (($?>>8) == 127) {
	print "no (ipvsadm not found)\n";
	exit 0;
    } else {
	print "no (unknown ipvsadm return value: $?, %d)\n", $? >> 8;
	exit 0;
    }
}


sub suggest {
    my $ipvs = shift;
    exit 0 unless $ipvs;

    foreach my $vip (sort keys %{$ipvs}) {
	foreach my $port (sort keys %{$ipvs->{$vip}}) {
	    print "${vip}_$port\n";
	}
    }
    exit 0;
}


sub config {
    my $vip  = shift;
    my $port = shift;
    my $ipvs = shift;

    print "graph_title Loadbalanced ",($name?$name:"*"),"->",$port," connections\n";
    print "graph_args -l 0\n";
    print "graph_total total\n";
    print "graph_vlabel connections / \${graph_period}\n";
    print "graph_category network\n";
    my $first=1;
    $vip = $vip || "";
    if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port})) {
	foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) {
	    (my $fname = $host) =~ s/[.-]/_/g;
	    if ( $first == 1 ) {
		print "$fname.draw AREA\n";
		$first=0
	    } else {
		print "$fname.draw STACK\n";
	    }
	    print "$fname.type DERIVE\n";
	    $host =~ s/-bak//;
	    print "$fname.label $host\n";
	    print "$fname.max 1000\n";
	    print "$fname.min 0\n";
	}
    }
    exit 0;
}

sub ipvs {
    my $vip  = shift;
    my $port = shift;
    my $ipvs = shift;
    open (IPVS, "/sbin/ipvsadm -L -n --stats 2>/dev/null|") or return undef;
    my $cvip  = "";
    my $cport = "";
    while (<IPVS>) {
	next if /^IP Virtual Server/;
	next if /^Prot\s+LocalAddress/;
	if (/^(\w+)\s+([\w\.-]+):([\w\d]+)\s+(\d+)[KMG]?\s+/) {
	    $cvip  = ($vip?$2:"");
	    $cport = $3;
	}
	elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)G\s+/) {
	    $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000000);
	}
	elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)M\s+/) {
	    $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000);
	}
	elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)K\s+/) {
	    $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000);
	}
	elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)\s+/) {
	    $ipvs->{$cvip}->{$cport}->{$1} += $3;
	}
    }
    close (IPVS) or return undef;
    return $ipvs;
}
# vim:syntax=perl