/usr/share/munin/plugins/port_ is in munin-plugins-core 2.0.25-1+deb8u3.
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 | #!/usr/bin/perl
# -*- perl -*-
=head1 NAME
port_ - Munin wildcard plugin to monitor netwok port usage.
=head1 CONFIGURATION
To monitor a port link port_<service> to this file. E.g.
ln -s /usr/share/munin/plugins/port_ /etc/munin/plugins/port_ssh
will monitor ssh connections. Services are those from /etc/services or
a numeric port number.
=head1 MAGIC MARKERS
#%# family=manual
#%# capabilities=autoconf suggest
=head1 BUGS
The plugin doesnt count the tcp6 connections if tcp and tcp6 is used.
=head1 VERSION
$Id$
=head1 AUTHOR
Unknown
=head1 LICENSE
GPLv2
=cut
use Munin::Plugin;
use constant {
ST_ESTABLISHED => 1,
ST_LISTEN => 10,
};
sub cache_open
{
my ($fd, $file) = @_;
my $cache_dir = "$ENV{MUNIN_PLUGSTATE}";
my $cache = $file;
$cache =~ s:/:_:g;
$cache = "$cache_dir/$cache";
my $ttl = $ENV{'cache_ttl'};
$ttl = 60 unless (defined $ttl);
# The user can disable caching by setting cache_ttl to something
# other than a positive integer.
if ($ttl !~ /^\d+$/ || $ttl == 0)
{
return open($fd, $file);
}
if (-e $cache && -M _ < $ttl / 86400)
{
return open($fd, $cache);
}
unless (-d $cache_dir)
{
mkdir($cache_dir) || die "$cache_dir: mkdir: $!\n";
}
my $retval;
$retval = open(CACHE_INPUT, $file);
return $retval unless ($retval);
my $tmpfile = "$cache.$$";
open(CACHE_OUTPUT, ">$tmpfile") || die "$tmpfile: open: $!\n";
while (<CACHE_INPUT>)
{
print CACHE_OUTPUT;
}
close(CACHE_OUTPUT) || die "$tmpfile: close: $!\n";
close(CACHE_INPUT);
rename($tmpfile, $cache) || die "$cache: rename: $!\n";
# Okay, we have a fresh copy in our cache.
return open($fd, $cache);
}
if ($ARGV[0] and $ARGV[0] =~ /^\s*autoconf\s*$/i)
{
if (-r "/proc/net/tcp" or -r "/proc/net/tcp6")
{
print "yes\n";
exit 0;
}
else
{
print "no\n";
exit 0;
}
}
sub count_tcp_ports
{
my ($state, $ports) = @_;
cache_open("NETSTAT", "/proc/net/tcp") or exit 1;
# Skip header line, else hex($curstate) will complain
my $header = <NETSTAT>;
# 32/4 + 1 corresponds to the IP address in hex + ":"
my $local_port_offset = 32/4 + 1;
while (<NETSTAT>)
{
my ($linenr, $local, $remote, $curstate) = split;
next unless (hex($curstate) == $state);
++$ports->{hex(substr($local, $local_port_offset, 4))};
}
close NETSTAT;
}
sub count_tcp6_ports
{
my ($state, $ports) = @_;
cache_open("NETSTAT", "/proc/net/tcp6") or exit 1;
# Skip header line, else hex($curstate) will complain
my $header = <NETSTAT>;
# 128/4 + 1 corresponds to the IP address in hex + ":"
my $local_port_offset = 128/4 + 1;
while (<NETSTAT>)
{
my ($linenr, $local, $remote, $curstate) = split;
next unless (hex($curstate) == $state);
++$ports->{hex(substr($local, $local_port_offset, 4))};
}
close(NETSTAT);
}
if ($ARGV[0] and $ARGV[0] =~ /^\s*suggest\s*$/i)
{
if (-r "/proc/net/tcp" or -r "/proc/net/tcp6")
{
my %ports = ();
count_tcp_ports(ST_LISTEN, \%ports) if (-r "/proc/net/tcp");
count_tcp6_ports(ST_LISTEN, \%ports) if (-r "/proc/net/tcp6");
foreach my $port (keys %ports)
{
my $p = (getservbyport($port, "tcp"))[0];
print $p || $port, "\n";
}
exit 0;
}
exit 1;
}
my ($name, $port);
if ($0 =~ /port_(.+)*$/) {
$name = $port = $1;
if ($port =~ /\D/) {
$port = (getservbyname ($name, "tcp"))[2];
}
} else {
exit 2;
}
if ($ARGV[0] and $ARGV[0] =~ /^\s*config\s*$/i)
{
if (int($name)) {
print "graph_title TCP port $name connection count\n";
} else {
print "graph_title $name connection count\n";
}
print "graph_args --base 1000 -l 0\n";
print "graph_vlabel concurrent connections\n";
print "graph_category network\n";
print "count.label $name\n";
print_thresholds("count");
exit 0;
}
my %ports = ();
count_tcp_ports(ST_ESTABLISHED, \%ports) if (-r "/proc/net/tcp");
count_tcp6_ports(ST_ESTABLISHED, \%ports) if (-r "/proc/net/tcp6");
# If there aint any the right answer is 0, not blank or U.
$ports{$port} = 0 if (!exists $ports{$port}) or ($ports{$port} eq '');
print "count.value ", $ports{$port}, "\n";
# vim:syntax=perl
|