root / plugins / network / host_traffic @ 92483a04
Historique | Voir | Annoter | Télécharger (2,46 ko)
| 1 |
#!/bin/bash |
|---|---|
| 2 |
|
| 3 |
# Plugin to monitor traffic rate to specific host. |
| 4 |
# |
| 5 |
# Requirements: |
| 6 |
# - tcpdump |
| 7 |
# - should be run as root or any other user for tcpdump |
| 8 |
# |
| 9 |
# Parameters supported: |
| 10 |
# config |
| 11 |
# autoconf |
| 12 |
# |
| 13 |
# Configurable variables |
| 14 |
# |
| 15 |
# type - monitor type. Available values: packets, bytes. Defaults to packets because it's faster |
| 16 |
# hostname - mandatory. Hostname (ip) to monitor connections to. Actually just a part of tcpdump expr. |
| 17 |
# Thus a hack: '1.2.3.4 and port 80' will monitor port 80 only. Bingo! |
| 18 |
# interface - self-explanatory |
| 19 |
# |
| 20 |
# IMPORTANT! This plugin spawns 'tcpdump' process along with bash shell piping tcpdump output there |
| 21 |
# PLEASE DO NOT FORGET TO KILL when removing plugin/stopping/etc. You can kill any of three, the others should die |
| 22 |
# |
| 23 |
# Revision 0.1 2011/08/06 Artem Sheremet |
| 24 |
# Revision 0.2 2011/10/23 - |
| 25 |
|
| 26 |
INTERVAL=300 |
| 27 |
|
| 28 |
if [ -z "$type" ]; then |
| 29 |
type=packets |
| 30 |
fi |
| 31 |
|
| 32 |
if [ -z "$hostname" ]; then |
| 33 |
echo "Configuration problem" >&2 |
| 34 |
exit 1 |
| 35 |
fi |
| 36 |
|
| 37 |
if [ "$interface" ]; then |
| 38 |
interface="-i $interface" |
| 39 |
fi |
| 40 |
|
| 41 |
TCPDUMP="tcpdump $interface -nqt host $hostname" |
| 42 |
|
| 43 |
if [ "$1" = "config" ]; then |
| 44 |
echo "graph_title Number of $type for host $hostname" |
| 45 |
if [ "$type" == "bytes" ]; then |
| 46 |
echo "graph_args --base 1024" |
| 47 |
fi |
| 48 |
echo "graph_category network" |
| 49 |
echo "graph_vlabel $type per second" |
| 50 |
echo "graph_info This plugin shows number of $type within host $hostname through the tcp protocol using tcpdump" |
| 51 |
echo "rate.label $type within $hostname" |
| 52 |
exit 0 |
| 53 |
fi |
| 54 |
|
| 55 |
TMP_DIR="/tmp/host_traffic_${hostname}_${type}"
|
| 56 |
|
| 57 |
if [ ! -d "$TMP_DIR" ]; then |
| 58 |
mkdir -p "$TMP_DIR" |
| 59 |
fi |
| 60 |
|
| 61 |
if [ -f "$TMP_DIR/pid" ]; then |
| 62 |
PID=`cat "$TMP_DIR/pid"` |
| 63 |
|
| 64 |
if [ -z "$PID" -o ! -d /proc/$PID ]; then |
| 65 |
unset PID |
| 66 |
fi |
| 67 |
fi |
| 68 |
|
| 69 |
if [ "$PID" ]; then |
| 70 |
rm -f "$TMP_DIR/value" |
| 71 |
# IPC: call data print & reset |
| 72 |
kill -USR1 $PID |
| 73 |
# wait 10 sec for data to arrive |
| 74 |
for i in {1..10}; do
|
| 75 |
if [ -r "$TMP_DIR/value" ]; then |
| 76 |
VALUE=`cat "$TMP_DIR/value"` |
| 77 |
echo $VALUE $INTERVAL | awk '{ printf("rate.value %.3f\n", $1/$2) }'
|
| 78 |
exit 0 |
| 79 |
fi |
| 80 |
sleep 1 |
| 81 |
done |
| 82 |
echo "rate.value U" |
| 83 |
else |
| 84 |
$TCPDUMP 2>/dev/null | ( |
| 85 |
COUNTER=0 |
| 86 |
# IPC: install SIGUSR1 handler |
| 87 |
trap 'echo $COUNTER > "$TMP_DIR/value"; COUNTER=0' SIGUSR1 |
| 88 |
echo $BASHPID >"$TMP_DIR/pid" |
| 89 |
while read line; do |
| 90 |
if [[ "$type" == "bytes" ]]; then |
| 91 |
# get the last field separated by space |
| 92 |
COUNTER=$(($COUNTER+${line/* /})) 2>/dev/null
|
| 93 |
else |
| 94 |
COUNTER=$(($COUNTER+1)) |
| 95 |
fi |
| 96 |
done |
| 97 |
) 2>/dev/null & |
| 98 |
fi |
