1 |
15c2e494
|
joshuasign
|
#!/bin/sh
|
2 |
|
|
|
3 |
|
|
# Usage
|
4 |
|
|
if [ $# -ne 1 -a $# -ne 2 ]; then
|
5 |
|
|
echo "Usage : $0 iface"
|
6 |
|
|
exit
|
7 |
|
|
fi
|
8 |
|
|
|
9 |
|
|
# files paths
|
10 |
|
|
pid_file=/var/run/iftop_${1}.pid
|
11 |
|
|
cache_file=/var/db/iftop_${1}.log
|
12 |
|
|
awk_script=/usr/local/bin/iftop_parse.awk
|
13 |
|
|
|
14 |
|
|
# Binaries paths
|
15 |
|
|
DATE=/bin/date
|
16 |
|
|
STAT=/usr/bin/stat
|
17 |
|
|
CUT=/usr/bin/cut
|
18 |
|
|
PS=/bin/ps
|
19 |
|
|
GREP=/usr/bin/grep
|
20 |
|
|
CAT=/bin/cat
|
21 |
|
|
RM=/bin/rm
|
22 |
|
|
IFTOP=/usr/local/sbin/iftop
|
23 |
|
|
AWK=/usr/bin/awk
|
24 |
|
|
|
25 |
|
|
# test if pid file exist
|
26 |
|
|
if [ -f $pid_file ]; then
|
27 |
|
|
# check how old is the file
|
28 |
|
|
curTS=`$DATE +%s`
|
29 |
|
|
pidTS=`$STAT -r $pid_file | $CUT -d " " -f 10`
|
30 |
|
|
# if more than 10 seconds,
|
31 |
|
|
# it must be a dead pid file (process killed?)
|
32 |
|
|
# or a stucked process that we should kill
|
33 |
|
|
if [ $(( curTS - pidTS )) -gt 10 ]; then
|
34 |
|
|
oldPID=`$CAT $pid_file`
|
35 |
|
|
# test if pid still exist
|
36 |
|
|
run=`$PS -p $oldPID | $GREP -F $0`
|
37 |
|
|
if [ "$run" != "" ]; then
|
38 |
|
|
kill -9 $oldPID
|
39 |
|
|
fi
|
40 |
|
|
$RM $pid_file
|
41 |
|
|
$RM $cache_file 2>> /dev/null
|
42 |
|
|
else
|
43 |
|
|
if [ -s $cache_file ]; then
|
44 |
|
|
$CAT $cache_file
|
45 |
|
|
fi
|
46 |
|
|
fi
|
47 |
|
|
else
|
48 |
|
|
echo -n $$ > $pid_file
|
49 |
|
|
$IFTOP -nNb -i $1 -s 3 -o 2s -t 2>> /dev/null | $AWK -f $awk_script > ${cache_file}.tmp
|
50 |
|
|
$CAT ${cache_file}.tmp > $cache_file
|
51 |
|
|
$CAT $cache_file
|
52 |
|
|
$RM $pid_file
|
53 |
|
|
fi
|