/usr/share/munin/plugins/vlan_inetuse_ is in munin-plugins-core 2.0.19-3.
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 | #!/usr/bin/perl -w
# Wildcard-script to monitor network interfaces. To monitor an
# interface, link vlan_<interface> to this file. E.g.
#
# ln .vlan_inetuse_ vlan_inetuse_eth1-200
#
# ...will monitor eth1.200 <=> eth0
#
# The interface must also have an accounting iptables rules defined, _before_
# any action rules. E.g., in /etc/network/vlan-firewall.d/eth1-200-out, you
# will find:
#
# --out-interface eth0 -p tcp --sport http
# --out-interface eth0 -p tcp --sport smtp
# --out-interface eth0 -p tcp --sport ssh
# --out-interface eth0 -p tcp --sport domain
# --out-interface eth0 -p tcp
# --out-interface eth0 -p icmp
# --out-interface eth0 -p udp --sport domain
# --out-interface eth0 -p udp
# --out-interface eth0
#
# ...which will make the out-traffic graphable, separated into the categories
# mentioned above. (Both in and out-files must have such rules. Look at the
# existing for examples.
#
#%# family=manual
use strict;
my $INTERFACE=`basename $0 | sed 's/^vlan_inetuse_//g' | tr '_' '-'` ;
#my $INTERFACE="eth1-200";
chomp $INTERFACE;
my %contraries = ("dpt" => "spt", "spt" => "dpt");
my %in_octets = ();
my %out_octets = ();
open (IN, "/sbin/iptables -v -x -L $INTERFACE-in |") or
die "Could not run iptables: $!\n";
while (<IN>)
{
if (/^\s*\d+\s+(\d+) +([a-z]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+(?:\s+|)(.+|)$/)
{
my ($octets, $proto, $comment) = ($1, $2, $3);
chop $comment;
$in_octets{$proto}{$comment} = $octets;
}
}
close IN;
die "Error running iptables. Dying\n" if $?;
open (IN, "/sbin/iptables -v -x -L $INTERFACE-out |") or
die "Could not run iptables: $!\n";
while (<IN>)
{
if (/^\s*\d+\s+(\d+) +([a-z]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+(?:\s+|)(.+|)$/)
{
my ($octets, $proto, $comment) = ($1, $2, $3);
chop $comment;
$out_octets{$proto}{$comment} = $octets;
if (!exists $in_octets{$proto}{&contrary($comment)})
{
$in_octets{$proto}{&contrary($comment)} = "U";
}
}
}
close IN;
die "Error running iptables. Dying\n" if $?;
if ($ARGV[0] and $ARGV[0] eq "config")
{
my @in_fields;
my @out_fields;
my @out;
foreach my $proto (sort keys %in_octets)
{
my $in_subtotal = 0;
my $out_subtotal = 0;
my $counter = 0;
next if $proto eq "all";
foreach my $comment (sort keys %{$in_octets{$proto}})
{
my $in = "in_" . &field($comment);
my $out = "out_" . &field(&contrary($comment));
my $label = $comment;
$label =~ s/:/=/g;
if (! $comment)
{
$in = "in_" . $proto . "_other";
$out = "out_" . $proto . "_other";
$label = "$proto other";
}
if (@in_fields)
{
push (@out , "$in.draw STACK\n");
}
else
{
push (@out , "$in.draw AREA\n");
}
push (@in_fields, "$in");
push (@out , "$in.label $label\n");
push (@out , "$in.cdef $in,8,*\n");
push (@out , "$in.graph no\n");
push (@out , "$in.type DERIVE\n");
push (@out , "$in.min 0\n");
if (@out_fields)
{
push (@out , "$out.draw STACK\n");
}
else
{
push (@out , "$out.draw AREA\n");
}
push (@out_fields, "$out");
push (@out , "$out.label $label\n");
push (@out , "$out.cdef $out,8,*\n");
push (@out , "$out.negative $in\n");
push (@out , "$out.type DERIVE\n");
push (@out , "$out.min 0\n");
}
}
if (@in_fields)
{
push (@out , "in_other.draw STACK\n");
}
else
{
push (@out , "in_other.draw AREA\n");
}
push (@in_fields, "in_other");
push (@out , "in_other.label other\n");
push (@out , "in_other.cdef in_other,8,*\n");
push (@out , "in_other.graph no\n");
push (@out , "in_other.type DERIVE\n");
push (@out , "in_other.min 0\n");
if (@out_fields)
{
push (@out , "out_other.draw STACK\n");
}
else
{
push (@out , "out_other.draw AREA\n");
}
push (@out_fields, "out_other");
push (@out , "out_other.label other\n");
push (@out , "out_other.cdef out_other,8,*\n");
push (@out , "out_other.negative in_other\n");
push (@out , "out_other.type DERIVE\n");
push (@out , "out_other.min 0\n");
print "graph_order ", join (' ', @in_fields, @out_fields), "\n";
print "graph_title VLAN $INTERFACE internet usage\n";
print "graph_total total\n";
print "graph_args --base 1000\n";
print "graph_vlabel bits per \${graph_period} in (-) / out (+)\n";
print "graph_category network\n";
print @out;
exit 0;
}
my $in_total = 0;
my $out_total = 0;
foreach my $proto (sort keys %in_octets)
{
my $in_subtotal = 0;
my $out_subtotal = 0;
my $counter = 0;
next if $proto eq "all";
foreach my $comment (sort keys %{$in_octets{$proto}})
{
if (! $comment)
{
$in_subtotal = $in_octets{$proto}{$comment}
unless $in_octets{$proto}{$comment} eq "U";
$in_total -= $in_subtotal
unless $in_octets{$proto}{$comment} eq "U";
$out_subtotal = $out_octets{$proto}{&contrary($comment)}
unless $out_octets{$proto}{&contrary($comment)} eq "U";
$out_total -= $out_subtotal
unless $out_octets{$proto}{&contrary($comment)} eq "U";
next;
}
$counter++;
print "in_", &field($comment), ".value ", $in_octets{$proto}{$comment}, "\n";
if (exists $out_octets{$proto}{&contrary($comment)})
{
print "out_", &field(&contrary($comment)), ".value ", $out_octets{$proto}{&contrary($comment)}, "\n";
}
else
{
print "out_", &field(&contrary($comment)), ".value U\n";
}
$in_subtotal -= $in_octets{$proto}{$comment}
unless $in_octets{$proto}{$comment} eq "U";
$out_subtotal -= $out_octets{$proto}{&contrary($comment)}
unless $out_octets{$proto}{&contrary($comment)} eq "U";
}
print "in_", $proto, "_other.value $in_subtotal\n";
print "out_", $proto, "_other.value $out_subtotal\n";
}
if (exists $in_octets{"all"}{""})
{
print "in_other.value ", ($in_octets{"all"}{""}+$in_total), "\n";
}
if (exists $out_octets{"all"}{""})
{
print "out_other.value ", ($out_octets{"all"}{""}+$out_total), "\n";
}
sub contrary
{
my $string = shift;
foreach my $key (keys %contraries)
{
last if ($string =~ s/(\b)$key(\b)/$1$contraries{$key}$2/g);
}
return $string;
}
sub field
{
my $string = shift;
$string =~ s/[: ]/_/g;
return $string;
}
# vim:syntax=perl
|