-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.sh
More file actions
executable file
·180 lines (154 loc) · 5.95 KB
/
telemetry.sh
File metadata and controls
executable file
·180 lines (154 loc) · 5.95 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
#!/bin/sh
#####################################################################
# (C) Unmukti Technology Pvt Ltd
# support@hopbox.in
#
# This is a free software which comes with absolutely no warranty.
# Please use at your own risk.
#
# License: GNU General Public License - GPL v3.0 or later
####################################################################
# Include network.sh lib
. /lib/functions/network.sh
#
date="$( date +%s )000000000"
#deviceid=`uci show system | grep hostname | cut -d "=" -f 2 | sed -e "s/'//g"`
deviceid=`uci get system.@system[0].hostname`
server=`uci get hopcloud.statistics.telemetry_host`
port=`uci get hopcloud.statistics.telemetry_port`
slug=`uci get hopcloud.credentials.slug`
location=`uci get hopcloud.credentials.location`
POST_CMD="curl --connect-timeout 5 --retry 2 --retry-delay 2 -k -i -XPOST https://$server:$port/write"
## CPU
#cpu=`cat /proc/stat | head -n1 | sed 's/cpu //'`
cpu=`/bin/busybox top -b -n 1 | grep CPU | grep irq | sed 's/%//g'`
user=`echo $cpu | awk '{print $2}'`
nice=`echo $cpu | awk '{print $6}'`
system=`echo $cpu | awk '{print $4}'`
idle=`echo $cpu | awk '{print $8}'`
iowait=`echo $cpu | awk '{print $10}'`
irq=`echo $cpu | awk '{print $12}'`
softirq=`echo $cpu | awk '{print $14}'`
$POST_CMD --data-binary "cpu,host=$deviceid,slug=$slug,location=$location user=$user,nice=$nice,system=$system,idle=$idle,iowait=$iowait,irq=$irq,softirq=$softirq $date"
## Memory
mem=`cat /proc/meminfo`
total=`echo "$mem" | grep ^MemTotal | awk '{print $2}'`
free=`echo "$mem" | grep ^MemFree | awk '{print $2}'`
$POST_CMD --data-binary "memory,host=$deviceid,slug=$slug,location=$location total=$total,free=$free $date"
## Disk
# Checkif overlayfs else report root usage
total=`df -k | grep -E 'overlayfs|/root' | tail -1 | awk '{print $2}'`
used=`df -k | grep -E 'overlayfs|/root' | tail -1 | awk '{print $5}' | sed -e 's/%//'`
$POST_CMD --data-binary "disk,host=$deviceid,slug=$slug,location=$location total=$total,used=$used $date"
## Load
load=`cat /proc/loadavg`
load1=`echo "$load" | awk '{print $1}'`
load5=`echo "$load" | awk '{print $2}'`
load15=`echo "$load" | awk '{print $3}'`
proc_run=`echo "$load" | awk '{print $4}' | awk -F '/' '{print $1}'`
proc_total=`echo "$load" | awk '{print $4}' | awk -F '/' '{print $2}'`
$POST_CMD --data-binary "load,host=$deviceid,slug=$slug,location=$location load1=$load1,load5=$load5,load15=$load15,proc_run=$proc_run,proc_total=$proc_total $date"
## Check WAN Bandwidth
for i in `uci get hopcloud.statistics.wan`
do
int=`network_get_device l3_dev $i;echo $l3_dev`
net=$i
echo $int, $net
rx=0
tx=0
rx=`cat /sys/class/net/$int/statistics/rx_bytes`
res=$?
if [ "$res" -ne 0 ]
then
continue
fi
tx=`cat /sys/class/net/$int/statistics/tx_bytes`
# Get interface alias
int_alias=`uci get hopcloud.alias.$i`
if [ -z $int_alias ]
then
int_alias=$i
fi
$POST_CMD --data-binary "wanbw,host=$deviceid,slug=$slug,location=$location,interface=$net,intalias=$int_alias rx=$rx,tx=$tx $date"
done
## Check OpenVPN Bandwidth
for i in `uci get hopcloud.statistics.ovpn`
do
rx=0
tx=0
rx=`cat /sys/class/net/$i/statistics/rx_bytes`
res=$?
if [ "$res" -ne 0 ]
then
continue
fi
tx=`cat /sys/class/net/$i/statistics/tx_bytes`
$POST_CMD --data-binary "ovpnbw,host=$deviceid,slug=$slug,location=$location,interface=$i rx=$rx,tx=$tx $date"
done
## Get Wireless data
# Wireless bandwidth & Station counts
for i in `iwinfo | grep ESSID | sed -e 's/ESSID: //' | awk '{print $1}'`
do
int=$i
ssid=`iw $i info | grep ssid | sed -e 's/.*ssid //'`
if [ "$ssid" = "" ];then
ssid="unknown"
fi
echo "WBW,ASSOC: $ssid, $int"
tx=0
rx=0
assoc=0
tx=`cat /sys/class/net/$int/statistics/tx_bytes`
res=$?
if [ "$res" -ne 0 ]
then
continue
fi
rx=`cat /sys/class/net/$int/statistics/rx_bytes`
assoc=`iw $int station dump | grep Station | wc -l`
$POST_CMD --data-binary "wbw,host=$deviceid,slug=$slug,location=$location,interface=$int,ssid=$ssid rx=$rx,tx=$tx $date"
$POST_CMD --data-binary "wireless,host=$deviceid,slug=$slug,location=$location,interface=$int,ssid=$ssid count=$assoc $date"
done
## Number of Connections
tcp=`/usr/sbin/conntrack -L -p tcp | wc -l`
udp=`/usr/sbin/conntrack -L -p udp | wc -l`
icmp=`/usr/sbin/conntrack -L -p icmp | wc -l`
total=`/usr/sbin/conntrack -C`
$POST_CMD --data-binary "connections,host=$deviceid,slug=$slug,location=$location tcp=$tcp,udp=$udp,icmp=$icmp,total=$total $date"
## OpenVPN tunnel status
for i in `uci show openvpn | grep status | cut -d "=" -f2 | sed -e "s/'//g"`
do
devq=`uci show openvpn | grep $i | cut -d "=" -f 1 | sed -e 's/status/dev/'`
tun=`uci get $devq`
count=`grep -v '^[0-9]' $i | grep -v -E 'UNDEF|Common|ROUTING|GLOBAL|CLIENT|Updated|bcast|END' | wc -l`
$POST_CMD --data-binary "ovpntunnelscount,host=$deviceid,slug=$slug,location=$location,tunnel=$tun count=$count $date"
done
## Latency & Packet loss and WAN Status
for i in `uci get hopcloud.statistics.wan`
do
# interface=`uci get network.$i.ifname`
sIP=`network_get_device l3_dev $i;echo $l3_dev`
int_alias=`uci get hopcloud.alias.$i`
if [ -z $int_alias ]
then
int_alias=$i
fi
ping_destination=`uci get hopcloud.destination.$i`
if [ "$sIP" == "" ]
then
status=0
$POST_CMD --data-binary "wanstatus,host=$deviceid,slug=$slug,location=$location,interface=$i,intalias=$int_alias status=$status $date"
continue
fi
pingResult=`ping -W 5 -I $sIP -c 10 $ping_destination | tail -2`
packet=`echo "$pingResult" |grep "packet loss" | cut -d "," -f 3 | cut -d " " -f 2| sed 's/.$//'`
latency=`echo "$pingResult" |grep -E 'rtt|round-trip' | cut -d "=" -f 2 | cut -d "/" -f 2`
status=1
if [ "$packet" == 100 ]
then
status=0
latency=0
fi
$POST_CMD --data-binary "wanstatus,host=$deviceid,slug=$slug,location=$location,interface=$i,intalias=$int_alias status=$status $date"
$POST_CMD --data-binary "ping,host=$deviceid,slug=$slug,location=$location,interface=$i,intalias=$int_alias,destination=$ping_destination packetloss=$packet,latency=$latency $date"
done