-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod_spdstats.sh
executable file
·198 lines (161 loc) · 5.07 KB
/
mod_spdstats.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
#!/bin/sh
[ -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_spdstats"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
readonly SCRIPT_CONF="$SCRIPT_DIR/config.conf"
#########################################################################################
#InfluxDB Settings
EXTS_URL=$(grep "EXTS_URL" "$SCRIPT_CONF" | cut -f2 -d"=")
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"=")
#Script Settings
readonly TEMP_FOLDER="/opt/tmp"
readonly DBFILE_ORG="/jffs/addons/spdmerlin.d/spdstats.db"
readonly DBFILE_COPY="$TEMP_FOLDER/$MOD_NAME.db"
readonly CSV_TEMP_FILE="$TEMP_FOLDER/$MOD_NAME.csv"
readonly TABLE="spdstats_WAN"
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)
readonly LOCK_FILE="/tmp/$MOD_NAME.lock"
readonly INFLUX_DB_METRIC_NAME="router.spdstats"
if [ -z "${SCRIPT_MODE}" ]; then
echo "Usage: ./$MOD_NAME.sh [update | full] [debug true/false] [send debug to syslog true/false] [true|false SCRIPT_DEBUG_FULL ] "
echo "like Usage: ./$MOD_NAME.sh update true false 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
}
if [ "$1" = "ext" ]; then
EXTS_SPDSTATS_ENABLED=$(grep "EXTS_SPDSTATS_ENABLED" "$SCRIPT_CONF" | cut -f2 -d"=")
if [ "$EXTS_SPDSTATS_ENABLED" = "true" ]; then
name="$INFLUX_DB_METRIC_NAME"
CURDATE=`date +%s`
columns="host=${ROUTER_MODEL}"
dl=${2}
ul=${3}
ext_data="$name,${columns} Download=$dl,Upload=$ul ${CURDATE}000000000"
logger -t "$SCRIPT_NAME:$MOD_NAME" "$ext_data"
$dir/export.sh "${ext_data}" "true"
fi
exit 0 ;
fi
if [ -f "$CSV_TEMP_FILE" ]; then
rm -f $CSV_TEMP_FILE
fi
if [ -f "$DBFILE_COPY" ]; then
rm $DBFILE_COPY
fi
lock()
{
while [ -f $LOCK_FILE ]; do
if [ ! -d /proc/$(cat $LOCK_FILE) ]; then
echo "WARNING : Lockfile detected but process $(cat $LOCK_FILE) does not exist !"
rm -f $LOCK_FILE
fi
sleep 1
done
echo $$ > $LOCK_FILE
}
unlock()
{
rm -f $LOCK_FILE
}
lock
CURDATE=`date +%s`
STARTDATE=`expr $CURDATE - 3600`
#echo $CURDATE
#echo $STARTDATE
if [ ! -r "$DBFILE_ORG" ]; then
Print_Output "$DBFILE_ORG not readable. spdStats not activ?" true true
exit 1
fi
cp $DBFILE_ORG $DBFILE_COPY
if [ ! -r "$DBFILE_COPY" ]; then
Print_Output "$DBFILE_COPY not readable" true true
exit 1
fi
#select mac,app_name,cat_name, strftime('%Y-%m-%d %H:%M:%S', datetime(timestamp, 'unixepoch')) as timestamp,tx,rx from $TABLE ;
#all data
if [ "$1" = "full" ]; then
sqlite3 $DBFILE_COPY <<!
.headers on
.mode csv
.output $CSV_TEMP_FILE
select StatID,strftime('%Y-%m-%d %H:%M:%S', datetime(Timestamp, 'unixepoch')) as Timestamp,Download,Upload,Latency,Jitter,IFNULL(PktLoss,0) from $TABLE;
!
fi
if [ "$1" = "update" ]; then
#last hour
sqlite3 $DBFILE_COPY <<!
.headers on
.mode csv
.output $CSV_TEMP_FILE
select StatID,strftime('%Y-%m-%d %H:%M:%S', datetime(Timestamp, 'unixepoch')) as Timestamp,Download,Upload,Latency,Jitter,IFNULL(PktLoss,0) from $TABLE WHERE Timestamp >= $STARTDATE ;
!
fi
if [ "$SCRIPT_DEBUG_FULL" = "true" ]; then
Print_Output "data from $DBFILE_COPY $TABLE:"
#debug
sqlite3 $DBFILE_COPY <<!
SELECT * from $TABLE;
!
Print_Output "Debug $CSV_TEMP_FILE:"
cat $CSV_TEMP_FILE
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 Speedtest data into InfluxDB. $lines entrys" true true
fi
if [ "$EXTS_USESSH" = "true" ]; then
SSL_MODE="--ssl"
fi
if [ "$EXTS_NOVERIFIY" = "true" ]; then
SSL_VERIFY="--verify_ssl"
fi
#--tagcolumns "host=${ROUTER_MODEL},table=${TABLE}" \ //ToDo
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 \
--fieldcolumns StatID,Timestamp,Download,Upload,Latency,Jitter,PktLoss \
--metricname $INFLUX_DB_METRIC_NAME \
--batchsize 6000 \
-tc Timestamp \
-tf "%Y-%m-%d %H:%M:%S" -g \
-tz "UTC"
rm -f $CSV_TEMP_FILE
fi
#cleanup
# Free some memory
unlock
rm -f $DBFILE_COPY