Project

General

Profile

Bug #3334 » iftop_parser.sh

Joshua Sign, 01/18/2019 06:17 PM

 
1
#!/bin/sh
2

    
3
# Usage
4
if [ $# -ne 1 ]; 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=/root/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
42
		exit
43
	fi
44
	if [ -f $cache_file ]; then
45
		$CAT $cache_file
46
	fi
47
	exit 	
48
fi
49

    
50
echo -n $$ > $pid_file
51

    
52
$IFTOP -nNb -i $1 -s 1 -o 2s -t 2>> /dev/null | $AWK -f $awk_script > $cache_file.1
53
$CAT $cache_file.1 > $cache_file
54
$CAT $cache_file
55
$RM $pid_file
(3-3/9)