File: port_.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 (187 lines) | stat: -rwxr-xr-x 3,330 bytes parent folder | download
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
#!@@PERL@@
#
# Wildcard-script to monitor network port usage. To monitor a
# port, link port_<service> to this file. E.g.
#
#    ln -s /usr/share/munin/node/plugins-auto/port_ /etc/munin/node.d/port_www
#
# ...will monitor www connections. Services are those listed in
# /etc/services.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
# 	suggest  (optional - used by munin-config)
#
# $Log$
# Revision 1.2  2004/05/20 13:57:12  jimmyo
# Set categories to some of the 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/12/16 16:22:36  jimmyo
# Typo fix.
#
# Revision 1.3  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#%# family=manual
#%# capabilities=autoconf suggest


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 1;
    }
}

if ($ARGV[0] and $ARGV[0] =~ /^\s*suggest\s*$/i)
{
    if (-r "/proc/net/tcp" or -r "/proc/net/tcp6")
    {
	my %ports = ();
	if (-r "/proc/net/tcp")
	{
	    open (NETSTAT, "/proc/net/tcp") or exit 1;

	    while (<NETSTAT>)
	    {
		    chomp;
		    next unless (hex (substr($_,35,2)) == 10);
		    my $p = substr( $_,15,4);
		    if (defined $ports{hex($p)})
		    {
			$ports{hex($p)}++;
		    }
		    else
		    {
			$ports{hex($p)} = 1;
		    }
	    }
	    close NETSTAT;
	}

	if (-r "/proc/net/tcp6")
	{
		open (NETSTAT, "/proc/net/tcp6") or exit 1;

		while (<NETSTAT>)
		{
			chomp;
			next unless (hex (substr($_,83,2)) == 10);
			my $p = substr( $_,40,4);
			if (defined $ports{hex($p)})
			{
			    $ports{hex($p)}++;
			}
			else
			{
			    $ports{hex($p)} = 1;
			}
		}

		close NETSTAT;

	}

	foreach my $port (keys %ports)
	{
	    my $p = (getservbyport ($port, "tcp"))[0];
	    if ($p)
	    {
		print "$p\n";
	    }
	    else
	    {
		print "$port\n";
	    }
	}
	exit 0;
    }
    else
    {
	exit 1;
    }
}

$0 =~ /port_(.+)*$/;
unless ($name = $1)
{
	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";
	exit 0;
}

if ($name =~ /\D/)
{
  $port = (getservbyname ($name, "tcp"))[2];
}
else
{
  $port = $name;
}


$count = 0;

if (-r "/proc/net/tcp")
{
    open (NETSTAT, "/proc/net/tcp") or exit 1;

    while (<NETSTAT>)
    {
	    chomp;
	    next unless (hex (substr($_,35,2)) == 1);
	    my $p = substr( $_,15,4);
	    $count++ if hex($p) == $port;
    }

    close NETSTAT;
}

if (-r "/proc/net/tcp6")
{
        open (NETSTAT, "/proc/net/tcp6") or exit 1;

        while (<NETSTAT>)
        {
                chomp;
                next unless (hex (substr($_,83,2)) == 1);
                my $p = substr( $_,40,4);
                $count++ if hex($p) == $port;
        }

        close NETSTAT;
}

print "count.value $count\n";
# vim:syntax=perl