forked from corgan2222/extstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_trafficAnalyzer.sh
More file actions
executable file
·242 lines (196 loc) · 6.76 KB
/
mod_trafficAnalyzer.sh
File metadata and controls
executable file
·242 lines (196 loc) · 6.76 KB
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
#!/bin/sh
#set -x
#how it works:
# 1. get copy of the original /jffs/.sys/TrafficAnalyzer/TrafficAnalyzer.db
# 2. create a new table clients in the copy
# 3. on full: import all data stored in the table
# on update: import only the latest data (one hour)
# 4. export as csv
# 4. call the python importer to export the csv via line protocoll into influx, 6k points at once
[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL=$(nvram get productid) || ROUTER_MODEL=$(nvram get odmpid)
dir=`dirname $0`
readonly SCRIPT_NAME="extstats"
readonly MOD_NAME="mod_trafficAnalyzer"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
readonly SCRIPT_CONF="$SCRIPT_DIR/config.conf"
readonly CLIENTLIST="/opt/tmp/client-list.txt"
#InfluxDB Settings
readonly EXTS_URL=$(grep "EXTS_URL" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_DATABASE=$(grep "EXTS_DATABASE" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_USERNAME=$(grep "EXTS_USERNAME" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_PASSWORD=$(grep "EXTS_PASSWORD" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_USESSH=$(grep "EXTS_USESSH" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_NOVERIFIY=$(grep "EXTS_NOVERIFIY" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly EXTS_PORT=$(grep "EXTS_PORT" "$SCRIPT_CONF" | cut -f2 -d"=")
readonly INFLUX_DB_METRIC_NAME="router.trafficAnalyzer"
#Script Settings
readonly TEMP_FOLDER="/tmp"
readonly DHCP_HOSTNAMESMAC_CSV="/opt/tmp/dhcp_clients_mac.csv"
readonly CLIENTLIST_CSV="/opt/tmp/client-list.csv"
readonly DBFILE_ORG="/jffs/.sys/TrafficAnalyzer/TrafficAnalyzer.db"
readonly DBFILE_COPY="$TEMP_FOLDER/$MOD_NAME.db"
readonly CSV_TEMP_FILE="$TEMP_FOLDER/$MOD_NAME.csv"
readonly TABLE="traffic"
readonly SCRIPT_MODE="${1}" #(update or full)
readonly SCRIPT_DEBUG="${2}" #(true/false)
readonly SCRIPT_DEBUG_SYSLOG="${3}" #(true/false)
readonly SCRIPT_DEBUG_FULL="${4}" #(true/false)
#generate new clientlist
$SCRIPT_DIR/helper_dhcpstaticlist.sh >/dev/null 2>&1
#check if dhcp_clientlist or nvram client list has more than one line
dhcp_client_lc=$(cat $DHCP_HOSTNAMESMAC_CSV | wc -l)
nvram_client_lc=$(cat $CLIENTLIST_CSV | wc -l)
if [ $dhcp_client_lc -lt 3 ]; then
HOSTNAMESMAC_CSV="/opt/tmp/client-list.csv"
else
HOSTNAMESMAC_CSV="/opt/tmp/dhcp_clients_mac.csv"
fi
#########################################################################################################
if [ -z "${SCRIPT_MODE}" ]; then
echo "Usage: ./$MOD_NAME.sh [update | full] [debug true/false] [send debug to syslog true/false] "
echo "like Usage: ./$MOD_NAME.sh update true false"
echo "update gets the data from the last hour"
echo "full imports all data"
return 1
fi
Print_Output(){
#$1 = message to print, $2 = log level
if [ "$SCRIPT_DEBUG" = "true" ]; then
printf "\\e[1m%s: $1\\e[0m\\n" "$SCRIPT_NAME:$MOD_NAME"
if [ "$SCRIPT_DEBUG_SYSLOG" = "true" ]; then
logger -t "$SCRIPT_NAME:$MOD_NAME" "$1"
fi
if [ "$3" = "true" ]; then
logger -t "$SCRIPT_NAME:$MOD_NAME" "$1"
fi
fi
}
#delete old temp data if something went wrong the last run, we dont want to have old stuff
if [ -f "$CSV_TEMP_FILE" ]; then
rm -f "$CSV_TEMP_FILE"
fi
if [ -f "$DBFILE_COPY" ]; then
rm -f "$DBFILE_COPY"
fi
lock()
{
while [ -f /tmp/$MOD_NAME.lock ]; do
if [ ! -d /proc/$(cat /$TEMP_FOLDER/$MOD_NAME.lock) ]; then
echo "WARNING : Lockfile detected but process $(cat /tmp/$MOD_NAME.lock) does not exist !"
rm -f /$TEMP_FOLDER/$MOD_NAME.lock
fi
sleep 1
done
echo $$ > /$TEMP_FOLDER/$MOD_NAME.lock
}
unlock()
{
rm -f /$TEMP_FOLDER/$MOD_NAME.lock
}
CURDATE=`date +%s`
STARTDATE=`expr $CURDATE - 3600`
if [ ! -r "$DBFILE_ORG" ]; then
Print_Output "$DBFILE_ORG not readable. TrafficAnalyser inactiv?" true true
exit 1
fi
#only working on a copy
cp $DBFILE_ORG $DBFILE_COPY
if [ ! -r "$DBFILE_COPY" ]; then
Print_Output "$DBFILE_COPY not readable" true true
exit 1
fi
sqlite3 $DBFILE_COPY <<!
CREATE TABLE clients(
hostname TEXT NOT NULL,
mac TEXT NOT NULL
);
!
#inject the hostnames to the mac adresses
sqlite3 $DBFILE_COPY <<!
.mode csv
.import $HOSTNAMESMAC_CSV clients
!
if [ "$SCRIPT_DEBUG_FULL" = "true" ]; then
Print_Output "Clientlist from $HOSTNAMESMAC_CSV imported into $DBFILE_COPY:"
#debug
sqlite3 $DBFILE_COPY <<!
SELECT * from clients;
!
fi
#get the traffic data
sqlite3 $DBFILE_COPY "select * from clients;"
#sql statement for manual use
#select mac,app_name,cat_name, strftime('%Y-%m-%d %H:%M:%S', datetime(timestamp, 'unixepoch')) as timestamp,tx,rx from $TABLE ;
#sqlite3 /opt/tmp/mod_trafficAnalyzer.db "select clients.hostname, traffic.mac,app_name,cat_name, strftime('%Y-%m-%d %H:%M:%S', datetime(timestamp, 'unixepoch')) as timestamp,tx,rx from traffic left join clients on traffic.mac = clients.mac;"
#import all historical data
if [ "$1" = "full" ]; then
Print_Output "Full import, this can take some time"
sqlite3 $DBFILE_COPY <<!
.headers on
.mode csv
.output $CSV_TEMP_FILE
select clients.hostname, traffic.mac,app_name,cat_name, strftime('%Y-%m-%d %H:%M:%S', datetime(timestamp, 'unixepoch')) as timestamp,tx,rx from $TABLE left join clients on traffic.mac = clients.mac;
!
fi
#import only the data from last hour
if [ "$1" = "update" ]; then
Print_Output "Update import"
Print_Output "date: $CURDATE"
Print_Output "startdate: $STARTDATE"
#last hour
sqlite3 $DBFILE_COPY <<!
.headers on
.mode csv
.output $CSV_TEMP_FILE
select clients.hostname, traffic.mac,app_name,cat_name, strftime('%Y-%m-%d %H:%M:%S', datetime(timestamp, 'unixepoch')) as timestamp,tx,rx from $TABLE left join clients on traffic.mac = clients.mac WHERE timestamp >= $STARTDATE ;
!
fi
if [ "$SCRIPT_DEBUG_FULL" = "true" ]; then
Print_Output "Debug $CSV_TEMP_FILE:"
cat $CSV_TEMP_FILE
fi
#create new table
if [ "$2" = "create" ]; then
DB_MODE="--create"
fi
if [ "$EXTS_USESSH" = "true" ]; then
SSL_MODE="--ssl"
fi
if [ "$EXTS_NOVERIFIY" = "true" ]; then
SSL_VERIFY="--noverify"
fi
if [ ! -r "$CSV_TEMP_FILE" ]; then
Print_Output "$CSV_TEMP_FILE not readable." true true
exit 1
else
lines=$(cat $CSV_TEMP_FILE | wc -l)
Print_Output "Export Traffic Analyser into InfluxDB. $lines entrys" true true
fi
#call the python script to do the work
if [ -f "$CSV_TEMP_FILE" ]; then
python $dir/export_py.py \
--input $CSV_TEMP_FILE \
-s "$EXTS_URL" \
-u "$EXTS_USERNAME" \
-p "$EXTS_PASSWORD" \
--port "$EXTS_PORT" \
--dbname "$EXTS_DATABASE" \
$DB_MODE \
$SSL_MODE \
$SSL_VERIFY \
--tagcolumns hostname,mac,app_name,cat_name \
--fieldcolumns mac,app_name,cat_name,timestamp,tx,rx,hostname \
--metricname $INFLUX_DB_METRIC_NAME \
--batchsize 6000 \
-tc timestamp \
-tf "%Y-%m-%d %H:%M:%S" -g \
#rm $CSV_TEMP_FILE
else
echo "no $CSV_TEMP_FILE"
fi
#cleanup
# Free some memory
unlock
#rm -f $CSV_TEMP_FILE
#rm -f $DBFILE_COPY
#cru a trafA "15 * * * * /mnt/routerUSB/scripts/scripts/asuswrt/metrics2influx/router_TrafficAnalyzer_influx.sh update "