-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod_basic.sh
executable file
·301 lines (242 loc) · 12 KB
/
mod_basic.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/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_basic"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
readonly SCRIPT_CONF="$SCRIPT_DIR/config.conf"
readonly SCRIPT_debug=$1
readonly TEMP_FOLDER="/opt/tmp"
#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"=")
# $1 = print to syslog, $2 = message to print, $3 = log level
Print_Output(){
if [ "$1" = "true" ]; then
logger -t "$SCRIPT_NAME" "$2"
printf "\\e[1m$3%s: $2\\e[0m\\n\\n" "$SCRIPT_NAME"
else
printf "\\e[1m$3%s: $2\\e[0m\\n\\n" "$SCRIPT_NAME"
fi
}
mod_cpu(){
#http://www.linuxhowtos.org/System/procstat.htm
CURDATE=`date +%s`
name="router.cpu"
columns="usr sys nic idle io irq sirq"
points_top=$(top -bn1 | head -3 )
points2=`top -bn1 | head -3 | awk '/CPU/ {print $2}' | sed 's/%//g'`
points4=`top -bn1 | head -3 | awk '/CPU/ {print $4}' | sed 's/%//g'`
points6=`top -bn1 | head -3 | awk '/CPU/ {print $6}' | sed 's/%//g'`
points8=`top -bn1 | head -3 | awk '/CPU/ {print $8}' | sed 's/%//g'`
points10=`top -bn1 | head -3 | awk '/CPU/ {print $10}' | sed 's/%//g'`
points12=`top -bn1 | head -3 | awk '/CPU/ {print $12}' | sed 's/%//g'`
points14=`top -bn1 | head -3 | awk '/CPU/ {print $14}' | sed 's/%//g'`
load1=`top -bn1 | head -3 | awk '/Load average:/ {print $3}' | sed 's/%//g'`
load5=`top -bn1 | head -3 | awk '/Load average:/ {print $4}' | sed 's/%//g'`
load15=`top -bn1 | head -3 | awk '/Load average:/ {print $5}' | sed 's/%//g'`
processes=$(ps | wc -l)
columns="host=${ROUTER_MODEL}"
points="usr=$points2,sys=$points4,nic=$points6,idle=$points8,io=$points10,irq=$points12,sirq=$points14,load1=$load1,load5=$load5,load15=$load15,processes=$processes"
mod_cpu_data="$name,$columns $points ${CURDATE}000000000"
Print_Output "$SCRIPT_debug" "$mod_cpu_data" "$WARN"
$dir/export.sh "$mod_cpu_data" "$SCRIPT_debug"
}
mod_mem(){
name="router.mem"
columns="host=${ROUTER_MODEL}"
CURDATE=`date +%s`
total=$(free -h | grep "Mem:" | awk '{print $2}')
used_kb=`top -bn1 | head -3 | awk '/Mem/ {print $2}' | sed 's/K//g'`
free_kb=`top -bn1 | head -3 | awk '/Mem/ {print $4}' | sed 's/K//g'`
shrd_kb=`top -bn1 | head -3 | awk '/Mem/ {print $6}' | sed 's/K//g'`
buff_kb=`top -bn1 | head -3 | awk '/Mem/ {print $8}' | sed 's/K//g'`
cached_kb=`top -bn1 | head -3 | awk '/Mem/ {print $10}' | sed 's/K//g'`
#ToDo: how to get rid of the first line
#size: 78844 bytes (52228 left)
#sysinfo | grep MemTotal | awk '/Mem/ {print $2}' | head -1
points="total=$total,used_kb=${used_kb},free_kb=${free_kb},shrd_kb=${shrd_kb},buff_kb=${buff_kb},cached_kb=${cached_kb}"
mod_mem_data="$name,$columns $points ${CURDATE}000000000"
Print_Output "$SCRIPT_debug" "$mod_mem_data" "$WARN"
$dir/export.sh "$mod_mem_data" "$SCRIPT_debug"
}
mod_temp(){
name="router.temp"
CURDATE=`date +%s`
columns="host=${ROUTER_MODEL}"
p1=`wl -i eth6 phy_tempsense | awk '{ print $1 * .5 + 20 }'` # 2.4GHz
p2=`wl -i eth7 phy_tempsense | awk '{ print $1 * .5 + 20 }'` # 5.0GHz
cpu=$(cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1 / 1000}') # cpu thanks helkaluin
mod_temp_data="$name,${columns} temp_24=$p1,temp_50=$p2,temp_cpu=$cpu ${CURDATE}000000000"
Print_Output "$SCRIPT_debug" "$mod_temp_data" "$WARN"
$dir/export.sh "$mod_temp_data" "$SCRIPT_debug"
}
mod_net(){
maxint=4294967295
scriptname=`basename $0`
old="/tmp/$scriptname.data.old"
new="/tmp/$scriptname.data.new"
old_epoch_file="/tmp/$scriptname.epoch.old"
DATA_TEMP_FILE="/tmp/mod_net.influx"
old_epoch=`cat $old_epoch_file`
new_epoch=`date "+%s"`
echo $new_epoch > $old_epoch_file
interval=`expr $new_epoch - $old_epoch` # seconds since last sample
if [ -f $new ]; then
awk -v old=$old -v interval=$interval -v maxint=$maxint '{
getline line < old
split(line, a)
if( $1 == a[1] ){
recv_bytes = $2 - a[2]
trans_bytes = $5 - a[5]
if(recv_bytes < 0) {recv_bytes = recv_bytes + maxint} # maxint counter rollover
if(trans_bytes < 0) {trans_bytes = trans_bytes + maxint} # maxint counter rollover
recv_mbps = (8 * (recv_bytes) / interval) / 1048576 # mbits per second
trans_mbps = (8 * (trans_bytes) / interval) / 1048576 # mbits per second
print $1, recv_mbps, $3 - a[3], $4 - a[4], trans_mbps, $6 - a[6], $7 - a[7]
}
}' $new | while read line; do
#echo $line
interface=$(echo $line | cut -d' ' -f1)
recv_mbps=$(echo $line | cut -d' ' -f2)
recv_errs=$(echo $line | cut -d' ' -f3)
recv_drop=$(echo $line | cut -d' ' -f4)
trans_mbps=$(echo $line | cut -d' ' -f5)
trans_errs=$(echo $line | cut -d' ' -f6)
trans_drop=$(echo $line | cut -d' ' -f7)
name="router.network"
CURDATE=`date +%s`
columns="host=${ROUTER_MODEL},interface=${interface}"
points="recv_mbps=${recv_mbps},recv_errs=${recv_errs},recv_drop=${recv_drop},trans_mbps=${trans_mbps},trans_errs=${trans_errs},trans_drop=${trans_drop}"
mod_net_data="$name,${columns} ${points} ${CURDATE}000000000"
#echo $mod_net_data
echo $mod_net_data >> $DATA_TEMP_FILE
#$dir/export.sh "$mod_net_data" "$SCRIPT_debug"
#echo "$mod_net_data"
Print_Output "$SCRIPT_debug" "$mod_net_data" "$WARN"
done
mv $new $old
fi
cat /proc/net/dev | tail +3 | tr ':|' ' ' | awk '{print $1,$2,$4,$5,$10,$12,$13}' > $new
$dir/export.sh "$DATA_TEMP_FILE" "$SCRIPT_debug" "file"
}
mod_connections(){
CURDATE=`date +%s`
active_dhcp_leases=$(cat /var/lib/misc/dnsmasq.leases| wc -l)
mod_connected_clients=$(arp -a -n | awk '$4!="<incomplete>"' | wc -l)
wifi_24=`wl -i eth6 assoclist | awk '{print $2}' | wc -l`
wifi_5=`wl -i eth7 assoclist | awk '{print $2}' | wc -l`
name="router.connections"
columns="host=${ROUTER_MODEL},type=connections"
mod_active_dhcp_leases_data="$name,$columns dhcp_leases=$active_dhcp_leases,connected_clients=$mod_connected_clients,wifi_24=$wifi_24,wifi_5=$wifi_5 ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}_mod_connections" "$mod_active_dhcp_leases_data" "$WARN"
$dir/export.sh "$mod_active_dhcp_leases_data" "$SCRIPT_debug"
}
mod_uptime(){
CURDATE=`date +%s`
uptime=$(cat /proc/uptime | cut -d' ' -f1)
uptime_idle=$(cat /proc/uptime | cut -d' ' -f2)
name="router.uptime"
columns="host=${ROUTER_MODEL}"
uptime_data="$name,$columns uptime=$uptime,uptime_idle=$uptime_idle ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}" "$uptime_data" "$WARN"
$dir/export.sh "$uptime_data" "$SCRIPT_debug"
}
mod_filesystem()
{
#CURDATE=`date +%F" "%T`
CURDATE=`date +%s`
readonly DF_CSV="/opt/tmp/DF.csv"
readonly DF_CSV_TMP="/opt/tmp/DF_TEMP.csv"
INFLUX_DB_METRIC_NAME="router.filesystem"
rm -f $DF_CSV
#filesystem info, with filter and remove % from used
df | tr -s ' ' ',' | grep -vE '^Filesystem|cdrom|www' | sed 's/%//' > $DF_CSV
while read line
do
Filesystem=$(echo ${line} | cut -d',' -f1) #Filesystem
blocks=$(echo ${line} | cut -d',' -f2) #1K-blocks
Used=$(echo ${line} | cut -d',' -f3) #Used
Available=$(echo ${line} | cut -d',' -f4) #Available
Use=$(echo ${line} | cut -d',' -f5) #Use
Free=$(echo "scale=0;(100-$Use)" | bc)
Mounted=$(echo ${line} | cut -d',' -f6) #Mounted on
columns="host=${ROUTER_MODEL},Filesystem=$Filesystem,MountedOn=$Mounted"
filesystem_data="$INFLUX_DB_METRIC_NAME,$columns blocks=$blocks,Used=$Used,Available=$Available,UsedPercent=$Use,FreePercent=$Free ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}_mod_filesystem" "$filesystem_data" "$WARN"
$dir/export.sh "$filesystem_data" "$SCRIPT_debug"
done < $DF_CSV
}
mod_swap()
{
CURDATE=`date +%s`
INFLUX_DB_METRIC_NAME="router.swap"
if [ "$(wc -l < /proc/swaps)" -ge "2" ]; then
swap_filename=$(cat "/proc/swaps" | grep "file" | awk '{print $1}')
swap_total=$(free -h | grep "Swap" | awk '{print $2}')
swap_used=$(free -h | grep "Swap" | awk '{print $3}')
swap_free=$(free -h | grep "Swap" | awk '{print $4}')
if [ $swap_total -gt 0 ]; then
swap_used_percent=$(echo "scale=2;(($swap_used/$swap_total)*100)" | bc)
swap_free_percent=$(echo "scale=2;(($swap_free/$swap_total)*100)" | bc)
swap_per=",swap_used_percent=$swap_used_percent,swap_free_percent=$swap_free_percent"
fi
columns="host=${ROUTER_MODEL},MountedOn=$swap_filename"
filesystem_data="$INFLUX_DB_METRIC_NAME,$columns swap_total=$swap_total,swap_used=$swap_used,swap_free=$swap_free${swap_per} ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}_mod_swap" "$filesystem_data" "$WARN"
$dir/export.sh "$filesystem_data" "$SCRIPT_debug"
else
Print_Output "${SCRIPT_debug}" "no swap found" "$WARN"
fi
}
mod_divstats(){
divstats="/jffs/addons/uiDivStats.d/uidivstats.txt"
if [ -r "$divstats" ]; then
CURDATE=`date +%s`
TOTAL_D_BL=$(cat $divstats | grep "domains in total are blocked" | awk '{print $1}' | sed "s/,//g" )
BLOCKED_BL=$(cat $divstats | grep "blocked by blocking list" | awk '{print $1}' | sed "s/,//g")
BLOCKED_BLACKLIST=$(cat $divstats | grep "blocked by blacklist" | awk '{print $1}' | sed "s/,//g" )
BLOCKED_WILDCARD=$(cat $divstats | grep "blocked by wildcard blacklist" | awk '{print $1}' | sed "s/,//g" )
ADS_TOTAL_BL=$(cat $divstats | grep "ads in total blocked" | awk '{print $1}' | sed "s/,//g" )
ADS_THIS_WEEK=$(cat $divstats | grep "ads this week, since last Monday" | awk '{print $1}' | sed "s/,//g" )
NEW_ADDS=$(cat $divstats | grep "new ads, since" | awk '{print $1}' | sed "s/,//g" )
name="router.uidivstats"
columns="host=${ROUTER_MODEL}"
divstats_data="$name,$columns domain_total_blocked=$TOTAL_D_BL,blocked_by_blocking_list=$BLOCKED_BL,blocked_by_blacklist=$BLOCKED_BLACKLIST,blocked_by_wildcard_blacklist=$BLOCKED_WILDCARD,ads_total_blocked=$ADS_TOTAL_BL,ads_this_week=$ADS_THIS_WEEK,new_ads=$NEW_ADDS ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}" "$divstats_data" "$WARN"
$dir/export.sh "$divstats_data" "$SCRIPT_debug"
else
Print_Output "${SCRIPT_debug}" "$uidivstats not found" "$WARN"
fi
}
mod_skynet(){
EXT_skynetloc="$(grep -ow "skynetloc=.* # Skynet" /jffs/scripts/firewall-start 2>/dev/null | grep -vE "^#" | awk '{print $1}' | cut -c 11-)"
EXT_skynetipset="${EXT_skynetloc}/skynet.ipset"
if [ -r "$EXT_skynetloc" ]; then
CURDATE=`date +%s`
EXT_blacklist1count="$(grep -Foc "add Skynet-Black" "$EXT_skynetipset" 2> /dev/null)"
EXT_blacklist2count="$(grep -Foc "add Skynet-Block" "$EXT_skynetipset" 2> /dev/null)"
name="router.skynet"
columns="host=${ROUTER_MODEL}"
skynet_data="$name,$columns IPs_blocked=$EXT_blacklist1count,IPs_ranged_blocked=$EXT_blacklist2count ${CURDATE}000000000"
Print_Output "${SCRIPT_debug}" "$skynet_data" "$WARN"
$dir/export.sh "$skynet_data" "$SCRIPT_debug"
else
Print_Output "${SCRIPT_debug}" "$EXT_skynetipset not found" "$WARN"
fi
}
mod_cpu
mod_mem
mod_temp
mod_net
mod_connections
mod_uptime
mod_filesystem
mod_swap
mod_divstats
mod_skynet