-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod_client_traffic.sh
executable file
·254 lines (204 loc) · 6.53 KB
/
mod_client_traffic.sh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/sh
#
# Traffic logging tool for DD-WRT-based routers using InfluxDB
#
# Based on work from Emmanuel Brucy (e.brucy AT qut.edu.au)
# Based on work from Fredrik Erlandsson (erlis AT linux.nu)
# Based on traff_graph script by twist - http://wiki.openwrt.org/RrdTrafficWatch
# Edit by Corgan
# 1 https://www.instructables.com/id/How-to-graph-home-router-metrics/
#
[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL=$(nvram get productid) || ROUTER_MODEL=$(nvram get odmpid)
dir=`dirname $0`
readonly SCRIPT_NAME="extStats"
readonly SCRIPT_debug=$2
readonly DHCP_HOSTNAMESMAC="/opt/tmp/dhcp_clients_mac.txt"
readonly MOD_NAME="mod_client_traffic"
readonly DATA_TEMP_FILE="/tmp/$MOD_NAME.influx"
readonly CLIENTLIST="/opt/tmp/client-list.txt"
#generate new clientlist
$SCRIPT_DIR/helper_dhcpstaticlist.sh >/dev/null 2>&1
function lc() {
if [ $# -eq 0 ]; then
python -c 'import sys; print(sys.stdin.read().lower())'
else
for i in "$@"; do
echo $i | python -c 'import sys; print(sys.stdin.read().lower())'
done
fi
}
# $1 = print to syslog, $2 = message to print, $3 = log level
Print_Output(){
if [ "$SCRIPT_debug" = "true" ]; then
logger -t "$MOD_NAME" "$2"
printf "\\e[1m$3%s: $2\\e[0m\\n\\n" "$MOD_NAME"
else
printf "\\e[1m$3%s: $2\\e[0m\\n\\n" "$MOD_NAME"
fi
}
lock()
{
while [ -f /tmp/$MOD_NAME.lock ]; do
if [ ! -d /proc/$(cat /tmp/$MOD_NAME.lock) ]; then
echo "WARNING : Lockfile detected but process $(cat /tmp/$MOD_NAME.lock) does not exist !"
rm -f /tmp/$MOD_NAME.lock
fi
sleep 1
done
echo $$ > /tmp/$MOD_NAME.lock
}
unlock()
{
rm -f /tmp/$MOD_NAME.lock
}
LAN_IFNAME=$(nvram get lan_ifname)
WAN_IFNAME=$(nvram get wan_ifname)
rm -f $DATA_TEMP_FILE
case ${1} in
"setup" )
# Create the RRDIPT2 CHAIN (it doesn't matter if it already exists).
# This one is for the whole LAN -> WAN and WAN -> LAN traffic measurement
iptables -N RRDIPT2 #2> /dev/null
# Add the RRDIPT2 CHAIN to the FORWARD chain (if non existing).
iptables -L FORWARD -n | grep RRDIPT2 #> /dev/null
if [ $? -ne 0 ]; then
iptables -L FORWARD -n | grep "RRDIPT2" #> /dev/null
if [ $? -eq 0 ]; then
echo "DEBUG : iptables chain misplaced, recreating it..."
iptables -D FORWARD -j RRDIPT2
fi
iptables -I FORWARD -j RRDIPT2
fi
# Add the LAN->WAN and WAN->LAN rules to the RRDIPT2 chain
iptables -nvL RRDIPT2 | grep ${WAN_IFNAME}.*${LAN_IFNAME} #>/dev/null
if [ $? -ne 0 ]; then
iptables -I RRDIPT2 -i ${WAN_IFNAME} -o ${LAN_IFNAME} -j RETURN
fi
iptables -nvL RRDIPT2 | grep ${LAN_IFNAME}.*${WAN_IFNAME} #>/dev/null
if [ $? -ne 0 ]; then
iptables -I RRDIPT2 -i ${LAN_IFNAME} -o ${WAN_IFNAME} -j RETURN
fi
# Create the RRDIPT CHAIN (it doesn't matter if it already exists).
iptables -N RRDIPT #2> /dev/null
# Add the RRDIPT CHAIN to the FORWARD chain (if non existing).
iptables -L FORWARD -n | grep RRDIPT[^2] #> /dev/null
if [ $? -ne 0 ]; then
iptables -L FORWARD -n | grep "RRDIPT" #> /dev/null
if [ $? -eq 0 ]; then
echo "DEBUG : iptables chain misplaced, recreating it..."
iptables -D FORWARD -j RRDIPT
fi
iptables -I FORWARD -j RRDIPT
fi
# For each host in the ARP table
grep ${LAN_IFNAME} /proc/net/arp | while read IP TYPE FLAGS MAC MASK IFACE
do
# Add iptable rules (if non existing).
#echo "${IP}"
iptables -nL RRDIPT | grep "${IP} " #> /dev/null
if [ $? -ne 0 ]; then
iptables -I RRDIPT -d ${IP} -j RETURN
iptables -I RRDIPT -s ${IP} -j RETURN
fi
done
;;
"update" )
lock
# Read and reset counters
iptables -L RRDIPT -vnxZ -t filter > /tmp/traffic_$$.tmp
iptables -L RRDIPT2 -vnxZ -t filter > /tmp/global_$$.tmp
CURDATE=`date +%s`
grep -Ev "0x0|IP" /proc/net/arp | while read IP TYPE FLAGS MAC MASK IFACE
do
IN=0
OUT=0
# Add new data to the graph.
grep ${IP} /tmp/traffic_$$.tmp | while read PKTS BYTES TARGET PROT OPT IFIN IFOUT SRC DST
do
if [ "${DST}" = "${IP}" ]; then
IN=${BYTES}
fi
if [ "${SRC}" = "${IP}" ]; then
OUT=${BYTES}
fi
client_lc=$(lc "$MAC")
ip=$(grep $client_lc /proc/net/arp | awk '{print $1}' | head -1)
host=$(grep -i $client_lc $DHCP_HOSTNAMESMAC | awk '{print $1}' | head -1)
#$ipp=$(echo $ip | awk '{print $1}')
#echo $ipp
if [ -z "$ip" ]; then
ip=$MAC
fi
# echo $ip
if [ "$host" = "" ]; then
host=$(grep -i $client_lc $CLIENTLIST | head -1 | awk '{print $1}')
fi
if [ -z "$host" ]; then
host=$MAC
fi
if [[ -n "$ip" ]] && [[ -n "$host" ]] && [[ -n "$MAC" ]]; then
columns="host=${ROUTER_MODEL},mac=$MAC,ip=$ip,hostname=$host"
points="inBytes=${IN},outBytes=${OUT}"
name="router.client_traffic"
CURDATE=`date +%s`
#echo "1 host $host ip $ip mac $client_lc"
data="$name,$columns $points ${CURDATE}000000000"
echo $data >> $DATA_TEMP_FILE
#printf "$client_lc \t $ip \t $host \n"
#echo $data
#curl -is -XPOST "https://${DBURL}/write?db=${DBNAME}&u=${USER}&p=${PASS}" --data-binary "$data" >/dev/null 2>&1
#Print_Output "$SCRIPT_debug" "$data" "$WARN"
#$dir/export.sh "$data" "$SCRIPT_debug" "file"
fi
done
done
# Chain RRDIPT2 Processing
IN=0
OUT=0
grep ${LAN_IFNAME} /tmp/global_$$.tmp | while read PKTS BYTES TARGET PROT OPT IFIN IFOUT SRC DST
do
if [ "${IFIN}" = "${LAN_IFNAME}" ]; then
IN=${BYTES}
fi
if [ "${IFIN}" = "${WAN_IFNAME}" ]; then
OUT=${BYTES}
fi
client_lc=$(lc "$MAC")
ip=$(grep $client_lc /proc/net/arp | awk '{print $1}' | head -1)
host=$(grep -i $client_lc $DHCP_HOSTNAMESMAC | awk '{print $1}' | head -1)
if [ -z "$ip" ]; then
ip=$MAC
fi
if [ "$host" = "" ]; then
host=$(grep -i $client_lc $CLIENTLIST | head -1 | awk '{print $1}')
fi
if [ -z "$host" ]; then
host=$MAC
fi
if [[ -n "$ip" ]] && [[ -n "$host" ]] && [[ -n "$MAC" ]]; then
columns="host=${ROUTER_MODEL},mac=$MAC,ip=$ip,hostname=$host"
points="inBytes=${IN},outBytes=${OUT}"
name="router.client_traffic"
CURDATE=`date +%s`
#echo "2 host $host ip $ip mac $client_lc"
#data="$name,$columns $points ${CURDATE}000000000"
data="$name,$columns $points ${CURDATE}000000000"
echo $data >> $DATA_TEMP_FILE
#Print_Output "$SCRIPT_debug" "$data" "$WARN"
#$dir/export.sh "$data" "$SCRIPT_debug"
fi
done
$dir/export.sh "$DATA_TEMP_FILE" "$SCRIPT_debug" "file"
# Free some memory
rm -f /tmp/*_$$.tmp
rm -f $DATA_TEMP_FILE
unlock
;;
*)
echo "Usage : $0 {setup|update}"
echo "Options : "
echo " $0 setup"
echo " $0 update"
exit
;;
esac