File: cps_.in

package info (click to toggle)
munin 1.2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,940 kB
  • ctags: 98
  • sloc: sh: 4,215; makefile: 452; perl: 135
file content (220 lines) | stat: -rwxr-xr-x 4,461 bytes parent folder | download | duplicates (3)
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
#!@@PERL@@
#
# Plugin to monitor connections per second, for LVS loadbalancers.
#
# Magic name:
#
# 	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)
#
# $Log$
# Revision 1.8  2004/12/10 18:55:14  jimmyo
# Removed hardcoded host_name in linux/cps_.
#
# Revision 1.7  2004/12/10 18:51:44  jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.6  2004/12/10 11:48:42  jimmyo
# linux/cps_ plugin now groks high numbers.
#
# Revision 1.5  2004/12/10 10:47:49  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.4  2004/12/09 22:12:56  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.3  2004/11/21 00:16:57  jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.2  2004/05/20 19:02:37  jimmyo
# Set categories on a bunch of plugins
#
# Revision 1.1  2004/01/02 18:50:01  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.4  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers - optional - used by installation scripts and munin-config:
#
#%# family=manual
#%# capabilities=autoconf suggest
#

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 "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 --stats >/dev/null 2>/dev/null");

	if ($? == 0)
	{
		print "yes\n";
		exit 0;
	}
	elsif (($?>>8) == 2)
	{
		print "no (permission denied)\n";
		exit 1;
	}
	elsif ($? == 127)
	{
		print "no (ipvsadm not found)\n";
		exit 1;
	}
	else
	{
		print "no\n";
		exit 1;
	}
}
sub suggest
{
	my $ipvs = shift;
	exit 0 unless $ipvs;

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

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

	print "graph_title Loadbalanced ",($vip?$vip:"*"),"->",$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 --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