forked from corgan2222/extstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_constats.sh
executable file
·198 lines (159 loc) · 5.36 KB
/
mod_constats.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
#!/bin/sh
#./router_constats_influx.sh full create
#./router_constats_influx.sh update
#cru a connstats "45 * * * * /mnt/routerUSB/scripts/scripts/asuswrt/metrics2influx/router_constats_influx.sh update "
[ -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_constats"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
readonly SCRIPT_CONF="$SCRIPT_DIR/config.conf"
#########################################################################################
#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"=")
#Script Settings
readonly TEMP_FOLDER="/opt/tmp"
readonly DBFILE_ORG="/jffs/addons/connmon.d/connstats.db"
readonly DBFILE_COPY="$TEMP_FOLDER/$MOD_NAME.db"
readonly CSV_TEMP_FILE="$TEMP_FOLDER/$MOD_NAME.csv"
readonly TABLE="connstats"
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"
INFLUX_DB_METRIC_NAME="router.uptime"
#INFLUX_DB_NAME="router.office"
#OUT_FOLDER="/mnt/routerUSB/exports"
#DBFILE="/jffs/addons/connmon.d/connstats.db"
#DBFILE_tmp="/mnt/routerUSB/tmp/connstats_tmp.db"
#TABLE="connstats"
#CSV_FILE="/mnt/routerUSB/exports/connstats.csv"
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
}
if [ -f "$CSV_TEMP_FILE" ]; then
rm -f $CSV_FILE
fi
if [ -f "$DBFILE_COPY" ]; then
rm -f $DBFILE_tmp
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`
if [ ! -r "$DBFILE_ORG" ]; then
Print_Output "$DBFILE_ORG not readable. Connmon 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,Ping,Jitter,Packet_Loss 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,Ping,Jitter,Packet_Loss from $TABLE WHERE Timestamp >= $STARTDATE ;
!
fi
if [ "$SCRIPT_DEBUG_FULL" = "true" ]; then
Print_Output "data from $DBFILE_COPY:"
#debug
sqlite3 $DBFILE_COPY <<!
SELECT * from $TABLE;
!
fi
if [ "$EXTS_USESSH" = "true" ]; then
SSL_MODE="--ssl"
fi
if [ "$EXTS_NOVERIFIY" = "true" ]; then
SSL_NOVERIFIY="--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 Uptime data into InfluxDB. $lines entrys" true true
fi
if [ "$2" = "create" ]; then
createDB="--create"
fi
if [ "$SCRIPT_DEBUG_FULL" = "true" ]; then
Print_Output "Debug $CSV_TEMP_FILE:"
cat $CSV_TEMP_FILE
fi
if [ -f "$CSV_TEMP_FILE" ]; then
#python $dir/csv2influx.py --input /mnt/routerUSB/exports/connstats.csv --dbname router.office $createDB --tagcolumns hostname,mac,app_name,cat_name --fieldcolumns mac,app_name,cat_name,timestamp,tx,rx,hostname --metricname router.traffic --batchsize 6000 -tc timestamp -tf "%Y-%m-%d %H:%M:%S" -g
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_NOVERIFIY \
--fieldcolumns Timestamp,Ping,Jitter,Packet_Loss \
--metricname $INFLUX_DB_METRIC_NAME \
--batchsize 6000 \
-tc Timestamp \
-tf "%Y-%m-%d %H:%M:%S" -g \
rm -f $CSV_TEMP_FILE
fi
#cleanup
# Free some memory
rm -f $DBFILE_COPY
unlock
#cru a trafA "15 * * * * /mnt/routerUSB/scripts/scripts/asuswrt/metrics2influx/router_TrafficAnalyzer_influx.sh update "