Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / traffic_ipt @ 8589c6df

Historique | Voir | Annoter | Télécharger (3,39 ko)

1
#!/bin/bash
2
# -*- bash -*-
3

    
4
: << =cut
5

    
6
=head1 NAME
7

    
8
traffic - Plugin to monitor the traffic (throughput) by IP protocols.
9

    
10
=head1 CONFIGURATION
11

    
12
To make this plugin work, you need to add rules to your firewall.
13
They are empty rules, we only use them to count traffic, not do anything
14
with them. To make this plugin work correctly, these rules have to
15
in the beginning of the chain(s), or else traffic that matches rules
16
above will not be counted (you can use this to your advantage of course).
17

    
18
The rules can be added with:
19
iptables -I INPUT
20
iptables -I OUTPUT
21
ip6tables -I INPUT
22
ip6tables -I OUTPUT
23

    
24
If trouble reading output, use:
25

    
26
 [traffic_ipt]
27
 user root
28

    
29
=head1 AUTHORS
30

    
31
=over
32

    
33
=item 2012.09.20: Initial version by Arturo Borrero Gonzalez <aborrero@cica.es>
34

    
35
=item 2013.01.12: Added percentage graphing by Michiel Holtkamp <michiel@supermind.nl>
36

    
37
=item 2013.02.03: Converted to use iptables/ip6tables by Michiel Holtkamp <michiel@supermind.nl>
38

    
39
=item 2015.01.25: Fixed regexp and make use of a status file by Pierre Schweitzer <pierre@reactos.org>
40

    
41
=back
42

    
43
=head1 LICENSE
44

    
45
GPLv2
46

    
47
=head1 MAGIC MARKERS
48

    
49
 #%# family=auto
50
 #%# capabilities=autoconf
51

    
52
=cut
53

    
54
if [ "$1" == "config" ]
55
then
56
        cat <<'EOF'
57
multigraph traffic_ipt
58
graph_title Throughput by IP protocol
59
graph_vlabel bits per ${graph_period}
60
graph_category network
61
graph_args --base 1000 --upper-limit 100 -l 0
62
IPv4.label IPv4 bps
63
IPv4.min 0
64
IPv4.type DERIVE
65
IPv4.draw AREA
66
IPv6.label IPv6 bps
67
IPv6.min 0
68
IPv6.type DERIVE
69
IPv6.draw STACK
70
total.label Total bps
71
total.min 0
72
total.type DERIVE
73
total.draw LINE1
74
EOF
75

    
76
		# Adapted from http://munin-monitoring.org/wiki/PercentGraphHowto
77
        cat << 'EOF'
78
multigraph traffic_ipt_percent
79
update no
80
graph_category network
81
graph_args --base 1000 -l 0 -u 100 -r
82
graph_scale no
83
graph_title Throughput of IP protocols by percentage
84
graph_vlabel Percentage
85
graph_order IPv4=traffic_ipt.IPv4 IPv6=traffic_ipt.IPv6 total=traffic_ipt.total IPv4_percent=traffic_ipt.total IPv6_percent=traffic_ipt.total
86
IPv4.graph no
87
IPv6.graph no
88
total.graph no
89
total.cdef IPv4,IPv6,0.00001,+,+
90
IPv4_percent.cdef IPv4,total,/,100,*
91
IPv4_percent.label IPv4
92
IPv4_percent.draw AREASTACK
93
IPv6_percent.cdef IPv6,total,/,100,*
94
IPv6_percent.label IPv6
95
IPv6_percent.draw AREASTACK
96
EOF
97
        exit 0
98
fi
99

    
100
if [ ! -r $MUNIN_STATEFILE ]; then
101
	oldv4=0
102
	oldv6=0
103
else
104
	oldv4=$(grep IPv4 $MUNIN_STATEFILE | cut -f2 -d '=')
105
	oldv6=$(grep IPv6 $MUNIN_STATEFILE | cut -f2 -d '=')
106
fi
107

    
108
ipv4=0
109
ipv6=0
110
diffv4=0
111
diffv6=0
112

    
113
IPv4_bytes=$(iptables -L -n -v -x | egrep '^\W*[0-9]+\W+[0-9]+\W+all\W+--\W+\*\W+\*\W+0.0.0.0/0\W+0.0.0.0/0\W*$' | while read pkts bytes rest; do echo $bytes; done)
114
if [ -z "$IPv4_bytes" ];
115
then
116
	echo "W: Unable to read rule from iptables, please add rules" >&2
117
else
118
	ipv4=$(echo $IPv4_bytes | sed -e 's/ / + /' | bc -l)
119
fi
120

    
121
IPv6_bytes=$(ip6tables -L -n -v -x | egrep '^\W*[0-9]+\W+[0-9]+\W+all\W+\*\W+\*\W+::/0\W+::/0\W*$' | while read pkts bytes rest; do echo $bytes; done)
122
if [ -z "$IPv6_bytes" ];
123
then
124
	echo "W: Unable to read rule from ip6tables, please add rules" >&2
125
else
126
	ipv6=$(echo $IPv6_bytes | sed -e 's/ / + /' | bc -l)
127
fi
128

    
129
if [ $ipv4 -ge $oldv4 ];
130
then
131
	diffv4=$(($ipv4 - $oldv4))
132
else
133
	diffv4=$ipv4
134
fi
135

    
136
if [ $ipv6 -ge $oldv6 ];
137
then
138
	diffv6=$(($ipv6 - $oldv6))
139
else
140
	diffv6=$ipv6
141
fi
142

    
143
echo "IPv4=$ipv4" > $MUNIN_STATEFILE
144
echo "IPv6=$ipv6" >> $MUNIN_STATEFILE
145

    
146
echo "IPv4.value $diffv4"
147
echo "IPv6.value $diffv6"
148
echo "total.value $( echo $diffv4 + $diffv6 | bc )"
149

    
150
exit 0