-
Notifications
You must be signed in to change notification settings - Fork 0
/
netmonitor-daemon
executable file
·113 lines (100 loc) · 2.87 KB
/
netmonitor-daemon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
source /usr/netmonitor/netmonitor.exports
traf_rx()
{
interface=${1:-ppp0}
rx=`ifconfig $interface | grep 'RX bytes' | awk '{ print $2 }'`
rx=${rx:$(expr index $rx :)}
echo $rx
}
traf_tx()
{
interface=${1:-ppp0}
tx=`ifconfig $interface | grep 'TX bytes' | awk '{ print $6 }'`
tx=${tx:$(expr index $tx :)}
echo $tx
}
traf()
{
interface=${1:-ppp0}
(ifconfig $interface &> /dev/null) && echo $(( $(traf_tx $interface)+$(traf_rx $interface) ))
}
curdate()
{
date +%Y%m%d%H
}
filedate()
{
cat $NETMONITOR_TRAFFIC_FILE | awk '{print $2}'
}
filetraffic()
{
cat $NETMONITOR_TRAFFIC_FILE | awk '{print $1}'
}
wasturnedoff()
{
[[ `cat $NETMONITOR_TRAFFIC_FILE | awk '{print $3}'` = "1" ]] && return 0 || return 1
}
day()
{
expr substr $1 7 2
}
hour()
{
expr substr $1 9 2
}
is-same-session()
{
first=`hour-ago $1`
second=`hour-ago $2`
if [[ `day $first` = `day $second` ]]; then
[ `hour $first` -ge 10 -a `hour $second` -ge 10 ] && return 0 || return 1
else
return 1
fi
}
netmonitor-start()
{
while true; do
if ifconfig ppp0 &> /dev/null; then
if ! isunlim `curdate`; then
temp=`traf`
sleep $delay
now=`traf`
result=$(( $now-$temp ))
if is-same-session `filedate` `curdate`; then
result=$(( $result + $(filetraffic) ))
fi
if [ $result -ge 0 ]; then
echo "$result `curdate`" > $NETMONITOR_TRAFFIC_FILE
fi
[[ $result -ge $TRAFFIC_LIMIT ]] && poff netberry && echo "`filetraffic` `curdate` 1" > $NETMONITOR_TRAFFIC_FILE
else
echo "0 `curdate`" > $NETMONITOR_TRAFFIC_FILE
sleep $delay
fi
else
isunlim `curdate` && wasturnedoff && pon netberry
sleep $delay
fi
done
}
if [[ $0 = *"netmonitor-daemon" ]]; then
# for i in `seq 40`; do echo -n _; done; echo
# echo Checking exports...
# echo traffic file: $NETMONITOR_TRAFFIC_FILE
# echo log file: $NETMONITOR_LOG_FILE
# echo pid file: $NETMONITOR_PID_FILE
# echo limit: $TRAFFIC_LIMIT
# echo delay: $delay
# for i in `seq 40`; do echo -n _; done; echo
#echo Testing features...
# echo Testing wether hour-ago works correctly...
# [[ `hour-ago 2011010100` = "2010123123" ]] && echo hour-ago works || echo "ERROR!!! hour-ago 2011010100 - $(hour-ago 2011010100)"
# echo Testing is-same-session...
# is-same-session 2012050715 2012050800 && echo is-same-session works || echo "ERROR!!! is-same-session 2012050715 2012050800"
#echo Testing isunlim...
# isunlim 2012050801 && echo isunlim works || echo "ERROR!!! isunlim 2012050801"
# for i in `seq 40`; do echo -n _; done; echo
netmonitor-start
fi