| 1 | #!/usr/bin/perl | 
|---|
| 2 | # | 
|---|
| 3 | # Plugin to monitor connections per second, for LVS loadbalancers. | 
|---|
| 4 | # | 
|---|
| 5 | # Magic name: | 
|---|
| 6 | # | 
|---|
| 7 | #       cps_<port> | 
|---|
| 8 | #       cps_<vip>_<port> | 
|---|
| 9 | # | 
|---|
| 10 | # Examples: | 
|---|
| 11 | # | 
|---|
| 12 | #       cps_smtp | 
|---|
| 13 | #       cps_mail.foo.boo_smtp | 
|---|
| 14 | #       cps_pop3 | 
|---|
| 15 | #       cps_www.foo.boo_www | 
|---|
| 16 | #       cps_vvv.foo.boo_www | 
|---|
| 17 | # | 
|---|
| 18 | # Parameters understood: | 
|---|
| 19 | # | 
|---|
| 20 | #       config   (required) | 
|---|
| 21 | #       autoconf (optional - used by munin-config) | 
|---|
| 22 | #       suggest  (optional - used by munin-config) | 
|---|
| 23 | # | 
|---|
| 24 | # $Log$ | 
|---|
| 25 | # Revision 1.8  2004/12/10 18:55:14  jimmyo | 
|---|
| 26 | # Removed hardcoded host_name in linux/cps_. | 
|---|
| 27 | # | 
|---|
| 28 | # Revision 1.7  2004/12/10 18:51:44  jimmyo | 
|---|
| 29 | # linux/apt* has been forced to LANG=C, to get predictable output. | 
|---|
| 30 | # | 
|---|
| 31 | # Revision 1.6  2004/12/10 11:48:42  jimmyo | 
|---|
| 32 | # linux/cps_ plugin now groks high numbers. | 
|---|
| 33 | # | 
|---|
| 34 | # Revision 1.5  2004/12/10 10:47:49  jimmyo | 
|---|
| 35 | # Change name from ${scale} to ${graph_period}, to be more consistent. | 
|---|
| 36 | # | 
|---|
| 37 | # Revision 1.4  2004/12/09 22:12:56  jimmyo | 
|---|
| 38 | # Added "graph_period" option, to make "graph_sums" usable. | 
|---|
| 39 | # | 
|---|
| 40 | # Revision 1.3  2004/11/21 00:16:57  jimmyo | 
|---|
| 41 | # Changed a lot of plugins so they use DERIVE instead of COUNTER. | 
|---|
| 42 | # | 
|---|
| 43 | # Revision 1.2  2004/05/20 19:02:37  jimmyo | 
|---|
| 44 | # Set categories on a bunch of plugins | 
|---|
| 45 | # | 
|---|
| 46 | # Revision 1.1  2004/01/02 18:50:01  jimmyo | 
|---|
| 47 | # Renamed occurrances of lrrd -> munin | 
|---|
| 48 | # | 
|---|
| 49 | # Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo | 
|---|
| 50 | # Import of LRRD CVS tree after renaming to Munin | 
|---|
| 51 | # | 
|---|
| 52 | # Revision 1.4  2003/11/07 17:43:16  jimmyo | 
|---|
| 53 | # Cleanups and log entries | 
|---|
| 54 | # | 
|---|
| 55 | # | 
|---|
| 56 | # | 
|---|
| 57 | # Magic markers - optional - used by installation scripts and munin-config: | 
|---|
| 58 | # | 
|---|
| 59 | #%# family=manual | 
|---|
| 60 | #%# capabilities=autoconf suggest | 
|---|
| 61 | # | 
|---|
| 62 |  | 
|---|
| 63 | use strict; | 
|---|
| 64 |  | 
|---|
| 65 | if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) | 
|---|
| 66 | { | 
|---|
| 67 |         &autoconf (); | 
|---|
| 68 | } | 
|---|
| 69 | if ( defined $ARGV[0] and $ARGV[0] eq "suggest" ) | 
|---|
| 70 | { | 
|---|
| 71 |         my $sipvs; | 
|---|
| 72 |         $sipvs = &ipvs (".", ".", $sipvs); | 
|---|
| 73 |         exit 0 if $sipvs == undef; | 
|---|
| 74 |         &suggest ($sipvs); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | unless ($0 =~ /cps(?:_([^_]+)|)_(.+)\s*$/) | 
|---|
| 78 | { | 
|---|
| 79 |         die "Could not parse name $0.\n"; | 
|---|
| 80 | } | 
|---|
| 81 | my $vip  = $1; | 
|---|
| 82 | my $port = $2; | 
|---|
| 83 | my $ipvs; | 
|---|
| 84 |  | 
|---|
| 85 | #print "Name: $0\nPort: $port\nVip : $vip\n"; | 
|---|
| 86 |  | 
|---|
| 87 | # Read ipvsadm-output | 
|---|
| 88 |  | 
|---|
| 89 | $ipvs = &ipvs ($vip, $port, $ipvs); | 
|---|
| 90 |  | 
|---|
| 91 | if ( defined $ARGV[0] and $ARGV[0] eq "dump" ) | 
|---|
| 92 | { | 
|---|
| 93 |         use Data::Dumper; | 
|---|
| 94 |         print Dumper($ipvs); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | if ( defined $ARGV[0] and $ARGV[0] eq "config" ) | 
|---|
| 98 | { | 
|---|
| 99 |         &config ($vip, $port, $ipvs); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | $vip = $vip || ""; | 
|---|
| 103 | if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port})) | 
|---|
| 104 | { | 
|---|
| 105 |         foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) | 
|---|
| 106 |         { | 
|---|
| 107 |                 (my $fname = $host) =~ s/[.-]/_/g; | 
|---|
| 108 |                 print "$fname.value ", $ipvs->{$vip}->{$port}->{$host}, "\n";; | 
|---|
| 109 |         } | 
|---|
| 110 | } | 
|---|
| 111 | else | 
|---|
| 112 | { | 
|---|
| 113 |         foreach my $vip (sort keys %{$ipvs}) | 
|---|
| 114 |         { | 
|---|
| 115 |                 foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) | 
|---|
| 116 |                 { | 
|---|
| 117 |                         (my $fname = ($vip . "-" . $host)) =~ s/[.-]/_/g; | 
|---|
| 118 |                         print "$fname.value ", $ipvs->{$vip}->{$port}->{$host}, "\n";; | 
|---|
| 119 |                 } | 
|---|
| 120 |         } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | sub autoconf | 
|---|
| 124 | { | 
|---|
| 125 |         system ("/sbin/ipvsadm -L --stats >/dev/null 2>/dev/null"); | 
|---|
| 126 |  | 
|---|
| 127 |         if ($? == 0) | 
|---|
| 128 |         { | 
|---|
| 129 |                 print "yes\n"; | 
|---|
| 130 |                 exit 0; | 
|---|
| 131 |         } | 
|---|
| 132 |         elsif (($?>>8) == 2) | 
|---|
| 133 |         { | 
|---|
| 134 |                 print "no (permission denied)\n"; | 
|---|
| 135 |                 exit 1; | 
|---|
| 136 |         } | 
|---|
| 137 |         elsif ($? == 127) | 
|---|
| 138 |         { | 
|---|
| 139 |                 print "no (ipvsadm not found)\n"; | 
|---|
| 140 |                 exit 1; | 
|---|
| 141 |         } | 
|---|
| 142 |         else | 
|---|
| 143 |         { | 
|---|
| 144 |                 print "no\n"; | 
|---|
| 145 |                 exit 1; | 
|---|
| 146 |         } | 
|---|
| 147 | } | 
|---|
| 148 | sub suggest | 
|---|
| 149 | { | 
|---|
| 150 |         my $ipvs = shift; | 
|---|
| 151 |         exit 0 unless $ipvs; | 
|---|
| 152 |  | 
|---|
| 153 |         foreach my $vip (sort keys %{$ipvs}) | 
|---|
| 154 |         { | 
|---|
| 155 |                 foreach my $port (sort keys %{$ipvs->{$vip}}) | 
|---|
| 156 |                 { | 
|---|
| 157 |                         print "cps_${vip}_$port\n"; | 
|---|
| 158 |                 } | 
|---|
| 159 |         } | 
|---|
| 160 |         exit 0; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | sub config | 
|---|
| 164 | { | 
|---|
| 165 |         my $vip  = shift; | 
|---|
| 166 |         my $port = shift; | 
|---|
| 167 |         my $ipvs = shift; | 
|---|
| 168 |  | 
|---|
| 169 |         print "graph_title Loadbalanced ",($vip?$vip:"*"),"->",$port," connections\n"; | 
|---|
| 170 |         print "graph_args -l 0\n"; | 
|---|
| 171 |         print "graph_total total\n"; | 
|---|
| 172 |         print "graph_vlabel connections / \${graph_period}\n"; | 
|---|
| 173 |         print "graph_category network\n"; | 
|---|
| 174 |         my $first=1; | 
|---|
| 175 |         $vip = $vip || ""; | 
|---|
| 176 |         if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port})) | 
|---|
| 177 |         { | 
|---|
| 178 |                 foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) | 
|---|
| 179 |                 { | 
|---|
| 180 |                         (my $fname = $host) =~ s/[.-]/_/g; | 
|---|
| 181 |                         if ( $first == 1 ) | 
|---|
| 182 |                         { | 
|---|
| 183 |                                 print "$fname.draw AREA\n"; | 
|---|
| 184 |                                 $first=0 | 
|---|
| 185 |                         } | 
|---|
| 186 |                         else | 
|---|
| 187 |                         { | 
|---|
| 188 |                                 print "$fname.draw STACK\n"; | 
|---|
| 189 |                         } | 
|---|
| 190 |                         print "$fname.type DERIVE\n"; | 
|---|
| 191 |                         $host =~ s/-bak//; | 
|---|
| 192 |                         print "$fname.label $host\n"; | 
|---|
| 193 |                         print "$fname.max 1000\n"; | 
|---|
| 194 |                         print "$fname.min 0\n"; | 
|---|
| 195 |                 } | 
|---|
| 196 |         } | 
|---|
| 197 |         else | 
|---|
| 198 |         { | 
|---|
| 199 |                 foreach my $vip (reverse sort keys %{$ipvs}) | 
|---|
| 200 |                 { | 
|---|
| 201 |                         foreach my $host (sort keys %{$ipvs->{$vip}->{$port}}) | 
|---|
| 202 |                         { | 
|---|
| 203 |                                 (my $fname = ($vip . "-" . $host)) =~ s/[.-]/_/g; | 
|---|
| 204 |                                 if ( $first == 1 ) | 
|---|
| 205 |                                 { | 
|---|
| 206 |                                         print "$fname.draw AREA\n"; | 
|---|
| 207 |                                         $first=0 | 
|---|
| 208 |                                 } | 
|---|
| 209 |                                 else | 
|---|
| 210 |                                 { | 
|---|
| 211 |                                         print "$fname.draw STACK\n"; | 
|---|
| 212 |                                 } | 
|---|
| 213 |                                 print "$fname.type DERIVE\n"; | 
|---|
| 214 |                                 $host =~ s/-bak//; | 
|---|
| 215 |                                 my $label = "$vip -> $host"; | 
|---|
| 216 |                                 $label =~ s/\.MIT\.EDU//g; | 
|---|
| 217 |                                 print "$fname.label $label\n"; | 
|---|
| 218 |                                 print "$fname.max 1000\n"; | 
|---|
| 219 |                                 print "$fname.min 0\n"; | 
|---|
| 220 |                         } | 
|---|
| 221 |                 } | 
|---|
| 222 |         } | 
|---|
| 223 |         exit 0; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | sub ipvs | 
|---|
| 227 | { | 
|---|
| 228 |         my $vip  = shift; | 
|---|
| 229 |         my $port = shift; | 
|---|
| 230 |         my $ipvs = shift; | 
|---|
| 231 |         open (IPVS, "/sbin/ipvsadm -L --stats 2>/dev/null|") or return undef; | 
|---|
| 232 |         my $cvip  = ""; | 
|---|
| 233 |         my $cport = ""; | 
|---|
| 234 |         while (<IPVS>) | 
|---|
| 235 |         { | 
|---|
| 236 |                 next if /^IP Virtual Server/; | 
|---|
| 237 |                 next if /^Prot\s+LocalAddress/; | 
|---|
| 238 |                 if (/^(\w+)\s+([\w\.-]+):([\w\d]+)\s+(\d+)[KMG]?\s+/) | 
|---|
| 239 |                 { | 
|---|
| 240 |                         $cvip  = $2; | 
|---|
| 241 |                         $cport = $3; | 
|---|
| 242 |                 } | 
|---|
| 243 |                 elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)G\s+/) | 
|---|
| 244 |                 { | 
|---|
| 245 |                         $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000000); | 
|---|
| 246 |                 } | 
|---|
| 247 |                 elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)M\s+/) | 
|---|
| 248 |                 { | 
|---|
| 249 |                         $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000); | 
|---|
| 250 |                 } | 
|---|
| 251 |                 elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)K\s+/) | 
|---|
| 252 |                 { | 
|---|
| 253 |                         $ipvs->{$cvip}->{$cport}->{$1} += ($3*1000); | 
|---|
| 254 |                 } | 
|---|
| 255 |                 elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)\s+/) | 
|---|
| 256 |                 { | 
|---|
| 257 |                         $ipvs->{$cvip}->{$cport}->{$1} += $3; | 
|---|
| 258 |                 } | 
|---|
| 259 |         } | 
|---|
| 260 |         close (IPVS) or return undef; | 
|---|
| 261 |         return $ipvs; | 
|---|
| 262 | } | 
|---|
| 263 | # vim:syntax=perl | 
|---|