root / plugins / network / traffic_ipt @ 9eeaa526
Historique | Voir | Annoter | Télécharger (2,98 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 |
=back |
| 40 |
|
| 41 |
=head1 LICENSE |
| 42 |
|
| 43 |
GPLv2 |
| 44 |
|
| 45 |
=head1 MAGIC MARKERS |
| 46 |
|
| 47 |
#%# family=auto |
| 48 |
#%# capabilities=autoconf |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
|
| 53 |
if [ "$1" == "config" ] |
| 54 |
then |
| 55 |
cat <<'EOF' |
| 56 |
multigraph traffic_ipt |
| 57 |
graph_title Throughput by IP protocol |
| 58 |
graph_vlabel bits per ${graph_period}
|
| 59 |
graph_category network |
| 60 |
graph_args --base 1000 --upper-limit 100 -l 0 |
| 61 |
IPv4.label IPv4 bps |
| 62 |
IPv4.min 0 |
| 63 |
IPv4.type DERIVE |
| 64 |
IPv4.draw AREA |
| 65 |
IPv6.label IPv6 bps |
| 66 |
IPv6.min 0 |
| 67 |
IPv6.type DERIVE |
| 68 |
IPv6.draw STACK |
| 69 |
total.label Total bps |
| 70 |
total.min 0 |
| 71 |
total.type DERIVE |
| 72 |
total.draw LINE1 |
| 73 |
EOF |
| 74 |
|
| 75 |
# Adapted from http://munin-monitoring.org/wiki/PercentGraphHowto |
| 76 |
cat <<'EOF' |
| 77 |
multigraph traffic_ipt_percent |
| 78 |
graph_scale no |
| 79 |
graph_title Throughput of IP protocols by percentage |
| 80 |
graph_vlabel Percentage |
| 81 |
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 |
| 82 |
graph_category network |
| 83 |
graph_args --upper-limit 100 -l 0 -r |
| 84 |
IPv4.label no |
| 85 |
IPv6.label no |
| 86 |
total.label no |
| 87 |
total_percent.label no |
| 88 |
IPv4.graph no |
| 89 |
IPv6.graph no |
| 90 |
total.graph no |
| 91 |
total_percent.graph no |
| 92 |
total_percent.cdef total,0.0000001,+ |
| 93 |
IPv4_percent.label IPv4 |
| 94 |
IPv4_percent.cdef IPv4,total_percent,/,100,* |
| 95 |
IPv4_percent.draw AREASTACK |
| 96 |
IPv6_percent.label IPv6 |
| 97 |
IPv6_percent.cdef IPv6,total_percent,/,100,* |
| 98 |
IPv6_percent.draw AREASTACK |
| 99 |
EOF |
| 100 |
exit 0 |
| 101 |
fi |
| 102 |
|
| 103 |
|
| 104 |
ipv4=0 |
| 105 |
ipv6=0 |
| 106 |
|
| 107 |
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) |
| 108 |
if [ -z "$IPv4_bytes" ]; |
| 109 |
then |
| 110 |
echo "W: Unable to read rule from iptables, please add rules" >&2 |
| 111 |
else |
| 112 |
ipv4=$(echo $IPv4_bytes | sed -e 's/ / + /' | bc -l) |
| 113 |
fi |
| 114 |
|
| 115 |
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) |
| 116 |
if [ -z "$IPv6_bytes" ]; |
| 117 |
then |
| 118 |
echo "W: Unable to read rule from ip6tables, please add rules" >&2 |
| 119 |
else |
| 120 |
ipv6=$(echo $IPv6_bytes | sed -e 's/ / + /' | bc -l) |
| 121 |
fi |
| 122 |
|
| 123 |
echo "IPv4.value $ipv4" |
| 124 |
echo "IPv6.value $ipv6" |
| 125 |
echo "total.value $( echo $ipv4 + $ipv6 | bc )" |
| 126 |
|
| 127 |
exit 0 |
| 128 |
|
