Projet

Général

Profil

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

root / plugins / network / traffic_ipt @ be7d3f82

Historique | Voir | Annoter | Télécharger (3,53 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
STAT_FILE=$MUNIN_PLUGSTATE/plugin-traffic_ipt.state
55

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

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

    
106
if [ ! -r $STAT_FILE ]; then
107
	oldv4=0
108
	oldv6=0
109
else
110
	oldv4=$(grep IPv4 $STAT_FILE | cut -f2 -d '=')
111
	oldv6=$(grep IPv6 $STAT_FILE | cut -f2 -d '=')
112
fi
113

    
114
ipv4=0
115
ipv6=0
116
diffv4=0
117
diffv6=0
118

    
119
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)
120
if [ -z "$IPv4_bytes" ];
121
then
122
	echo "W: Unable to read rule from iptables, please add rules" >&2
123
else
124
	ipv4=$(echo $IPv4_bytes | sed -e 's/ / + /' | bc -l)
125
fi
126

    
127
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)
128
if [ -z "$IPv6_bytes" ];
129
then
130
	echo "W: Unable to read rule from ip6tables, please add rules" >&2
131
else
132
	ipv6=$(echo $IPv6_bytes | sed -e 's/ / + /' | bc -l)
133
fi
134

    
135
if [ $ipv4 -ge $oldv4 ];
136
then
137
	diffv4=$(($ipv4 - $oldv4))
138
else
139
	diffv4=$ipv4
140
fi
141

    
142
if [ $ipv6 -ge $oldv6 ];
143
then
144
	diffv4=$(($ipv6 - $oldv6))
145
else
146
	diffv4=$ipv6
147
fi
148

    
149
echo "IPv4=$ipv4" > $STAT_FILE
150
echo "IPv6=$ipv6" >> $STAT_FILE
151

    
152
echo "IPv4.value $diffv4"
153
echo "IPv6.value $diffv6"
154
echo "total.value $( echo $diffv4 + $diffv6 | bc )"
155

    
156
exit 0