]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/fw_conntrack
Relax warning threshold for minutely planet diffs
[chef.git] / cookbooks / munin / files / default / plugins / fw_conntrack
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 fw_conntrack - Plugin to monitor the number of tracked connections
6 through a Linux 2.4/2.6 firewall
7
8 =head1 CONFIGURATION
9
10 This plugin must run with root privileges
11
12 =head2 CONFIGURATION EXAMPLE
13
14 /etc/munin/plugin-conf.d/global or other file in that dir must contain:
15
16  [fw_*]
17   user root
18
19 =head1 NOTES
20
21 ESTABLISHED+FIN_WAIT+TIME_WAIT+SYN_SENT+UDP are the most interesting
22 connections.
23
24 The total list also includes SYN_RECV, CLOSE, CLOSE_WAIT, LAST_ACK and
25 LISTEN, but these were not (often) observed on my firewall.
26
27 TOTAL is the total number of tracked connections.
28
29 ASSURED and UNREPLIED connections are complimentary subsets of
30 ESTABLISHED.
31
32 ASSURED is after ACK is seen after SYN_RECV.  Therefore ASSURED is
33 plotted but not UNREPLIED.
34
35 Note that the plugin depends on the netfilter "conntrack" userspace tool.
36 It comes from http://conntrack-tools.netfilter.org/
37
38 =head1 AUTHORS
39
40 =over
41
42 =item 2004.05.05: Initial version by Nicolai Langfeldt, Linpro AS, Oslo, Norway
43
44 =item 2004.05.06: Enhanced to count NATed connections after input from Xavier on munin-users list
45
46 =item 2011.09.23: Perl version by Alex Tomlins
47
48 =back
49
50 =head1 LICENSE
51
52 GPL
53
54 =head1 MAGIC MARKERS
55
56  #%# family=auto
57  #%# capabilities=autoconf
58
59 =cut
60
61 use strict;
62 use Munin::Plugin;
63
64 my $conntrack = '/usr/sbin/conntrack';
65 my $nf_conntrack_file = '/proc/net/nf_conntrack';
66 my $ip_conntrack_file = '/proc/net/ip_conntrack';
67 my @conntrack_max_files = qw(
68         /proc/sys/net/nf_conntrack_max
69         /proc/sys/net/netfilter/nf_conntrack_max
70         /proc/sys/net/ipv4/ip_conntrack_max
71         /proc/sys/net/ipv4/netfilter/ip_conntrack_max
72 );
73
74 if ( defined($ARGV[0]) and $ARGV[0] eq "autoconf" ) {
75     if ( -x $conntrack or -r $nf_conntrack_file or -r $ip_conntrack_file) {
76         print "yes\n";
77     } else {
78         print "no\n";
79     }
80     exit 0;
81 }
82
83 if ( defined($ARGV[0]) and $ARGV[0] eq "config" ) {
84     print <<EOF;
85 graph_title Connections through firewall
86 graph_vlabel Connections
87 graph_category network
88 graph_args -l 0
89 established.label Established
90 established.type GAUGE
91 established.draw AREA
92 fin_wait.label FIN_WAIT
93 fin_wait.type GAUGE
94 fin_wait.draw STACK
95 time_wait.label TIME_WAIT
96 time_wait.type GAUGE
97 time_wait.draw STACK
98 syn_sent.label SYN_SENT
99 syn_sent.type GAUGE
100 syn_sent.draw STACK
101 udp.label UDP connections
102 udp.type GAUGE
103 udp.draw STACK
104 assured.label Assured
105 assured.type GAUGE
106 assured.draw LINE2
107 nated.label NATed
108 nated.type GAUGE
109 nated.draw LINE1
110 total.label Total
111 total.type GAUGE
112 total.graph no
113 EOF
114     my $max;
115     foreach (@conntrack_max_files) {
116         if ( -r $_) {
117             chomp($max = `cat $_`);
118             last;
119         }
120     }
121     if ($max) {
122         print "total.warning ", $max * 8 / 10, "\n";
123         print "total.critical ", $max * 9 / 10, "\n";
124     }
125     exit 0;
126 }
127
128 my $command;
129 if ( -x $conntrack) {
130     $command = "$conntrack -L -o extended -f ipv4 2>/dev/null; $conntrack -L -o extended -f ipv6 2>/dev/null";
131 } elsif ( -r $nf_conntrack_file ) {
132     $command = "cat $nf_conntrack_file";
133 } else {
134     $command = "cat $ip_conntrack_file";
135 }
136
137 my %state = (
138     'ESTABLISHED' => 0,
139     'FIN_WAIT' => 0,
140     'TIME_WAIT' => 0,
141     'SYN_SENT' => 0,
142     'UDP' => 0,
143     'ASSURED' => 0,
144     'NATTED' => 0,
145     'TOTAL' => 0
146 );
147 open CMD, "$command|";
148 while (<CMD>) {
149     $state{'TOTAL'} ++;
150     $state{'UDP'} ++ if /udp /;
151     $state{'ASSURED'} ++ if /ASSURED/;
152     if (/tcp \s*\d+\s+\d+\s+(\S+)/) {
153          $state{$1} ++;
154     }
155     if (/src=(\S+)\s+dst=(\S+)\s+sport.*src=(\S+)\s+dst=(\S+)/) {
156         $state{'NATTED'} ++ if $1 ne $4 or $2 ne $3;
157     }
158 }
159 close CMD;
160
161 print "established.value $state{'ESTABLISHED'}\n";
162 print "fin_wait.value $state{'FIN_WAIT'}\n";
163 print "time_wait.value $state{'TIME_WAIT'}\n";
164 print "syn_sent.value $state{'SYN_SENT'}\n";
165 print "udp.value $state{'UDP'}\n";
166 print "assured.value $state{'ASSURED'}\n";
167 print "nated.value $state{'NATTED'}\n";
168 print "total.value $state{'TOTAL'}\n";