-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnstat.sh
220 lines (208 loc) · 6 KB
/
nnstat.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
#!/bin/bash
# A script for checking Linux CLI NOIA node data, status and statistics
# Version 191001-1
# Fetch log messages to temporary files
tac /var/log/syslog |grep -m 1 "changed bandwidth" > tnn.log
tac /var/log/syslog |grep -m 1 "Received statistics" > tno.log
# Define variables
logDate=`cat tnn.log |cut -c 1-15`
logDay=`cat tnn.log |cut -c 5-6 |sed 's/^0*//'`
logHour=`cat tnn.log |cut -c 8-9| sed 's/^0*//'`
logMinute=`cat tnn.log |cut -c 11-12| sed 's/^0*//'`
curDay=`date '+%d' | sed 's/^0*//'`
curHour=`date '+%H' | sed 's/^0*//'`
curMinute=`date '+%M' | sed 's/^0*//'`
nodeName=`cat tnn.log |cut -f 3 -d ":" |cut -f 2 -d " "`
nodeIp=`dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'`
airDrop=`grep airdropAddress .noia-node/node.settings |cut -f 2 -d "="`
storeDir=`grep dir= .noia-node/node.settings |cut -f 2 -d "="`
storeSize=`grep size= .noia-node/node.settings |cut -f 2 -d "="`
ispName=` cat tnn.log |cut -f 9 -d "=" |cut -f 1 -d ","`
cityName=` cat tnn.log |cut -f 18 -d "=" |cut -f 1 -d ","`
countryName=` cat tnn.log |cut -f 19 -d "=" |cut -f 1 -d ","`
testHost=`cat tnn.log |cut -f 15 -d "=" |cut -f 1 -d "," |cut -f 1 -d ":"`
upTime=`cat /var/log/syslog |grep -c "changed bandwidth"`
downLoad=`cat tnn.log |cut -f 2 -d "=" |cut -f 1 -d "," |cut -f 1 -d "."`
upLoad=`cat tnn.log |cut -f 3 -d "=" |cut -f 1 -d "," |cut -f 1 -d "."`
pingTime=`cat tnn.log |cut -f 24 -d "=" |cut -f 1 -d "," |cut -f 1 -d "."`
upTotal=`cat tno.log |cut -f 3 -d "," |cut -f 4 -d " "`
upDays=$(( $upTotal / 24 ))
upHours=$(($upTotal % 24 ))
downBytes=`cat tno.log |cut -f 2 -d "=" |cut -f 1 -d ","`
upBytes=`cat tno.log |cut -f 3 -d "=" |cut -f 1 -d ","`
downGbytes=$(( $downBytes / 1073741824 ))
upGbytes=$(( $upBytes / 1073741824 ))
downMbytes=$(( $downBytes / 1048567 ))
upMbytes=$(( $upBytes / 1048567 ))
downKbytes=$(( $downBytes / 1024 ))
upKbytes=$(( $upBytes / 1024 ))
storeGbytes=$(( $storeSize / 1073741824 ))
storeMbytes=$(( $storeSize / 1048567 ))
storeKbytes=$(( $storeSize / 1024 ))
# Define color names
noColor='\033[0m'
green='\033[0;32m'
yellow='\033[0;33m'
red='\033[0;31m'
blue='\033[0;34m'
# Calculate log age
upDiff=$(($curHour-$upTime))
curHourMin=$(( $curHour * 60 ))
curHourMin=$(( $curHourMin + $curMinute ))
logHourMin=$(( $logHour * 60 ))
logHourMin=$(( $logHourMin + $logMinute ))
logAge=$(( $curHourMin - $logHourMin ))
# Define IP address type
ipLength=`expr length $nodeIp`
# Define value based colors
ipColor=$red
if [ "$ipLength" -gt "15" ]; then
ipColor=$green
fi
uptimeColor=$yellow
if [ "$upDiff" -ge "3" ]; then
uptimeColor=$red
fi
if [ "$upDiff" -le "1" ]; then
uptimeColor=$green
fi
if [ "$logAge" -le "0" ]; then
logAge=$(($logAge + 60))
fi
if [ "$logAge" -le "60" ]; then
dateColor=$green
else
dateColor=$red
fi
downColor=$yellow
if [ "$downLoad" -ge "70" ]; then
downColor=$green
fi
if [ "$downLoad" -le "20" ]; then
downColor=$red
fi
upColor=$yellow
if [ "$upLoad" -ge "70" ]; then
upColor=$green
fi
if [ "$upLoad" -le "20" ]; then
upColor=$red
fi
pingColor=$yellow
if [ "$pingTime" -ge "70" ]; then
pingColor=$red
fi
if [ "$pingTime" -le "30" ]; then
pingColor=$green
fi
# Define data units
downLoaded=$downGbytes
downUnit="GB"
if [ "$downGbytes" = "0" ]; then
downLoaded=$downMbytes
downUnit="MB"
fi
if [ "$downMbytes" = "0" ]; then
downLoaded=$downKbytes
downUnit="kB"
fi
if [ "$downKbytes" = "0" ]; then
downLoaded=$downBytes
downUnit="bytes"
fi
upLoaded=$upGbytes
upUnit="GB"
if [ "$upGbytes" = "0" ]; then
upLoaded=$upMbytes
upUnit="MB"
fi
if [ "$upMbytes" = "0" ]; then
upLoaded=$upKbytes
downUnit="kB"
fi
if [ "$upKbytes" = "0" ]; then
upLoaded=$upBytes
upUnit="bytes"
fi
storage=$storeGbytes
storeUnit="GB"
if [ "$storeGbytes" = "0" ]; then
storage=$storeMbytes
storeUnit="MB"
fi
if [ "$storeMbytes" = "0" ]; then
storage=$storeKbytes
downUnit="kB"
fi
if [ "$storeKbytes" = "0" ]; then
storage=$storeBytes
storeUnit="bytes"
fi
# Print data
clear
echo
# If node verified OK, print fetched data...
if [ "$logAge" != "" ]; then
headColor=$blue
printf "${headColor}-------------------------------------------------------------------\n"
printf "${headColor}NOIA node data, status and statistics"
echo ' - '$logDate
printf "${headColor}-------------------------------------------------------------------${noColor}\n"
echo
printf '%-25s' 'Node name'
echo $nodeName
printf '%-25s' 'External IP address'
printf "${ipColor}$nodeIp${noColor}\n"
printf '%-25s' 'Beneficiary address'
echo $airDrop
printf '%-25s' 'System location'
echo $cityName', '$countryName
printf '%-25s' 'Service provider'
echo $ispName
printf '%-25s' 'Storage directory'
echo $storeDir
printf '%-25s' 'Storage size'
echo $storage' '$storeUnit
printf '%-25s' 'Uptime today'
printf "${uptimeColor}$upTime"
printf "${uptimeColor} hours${noColor}\n"
printf '%-25s' 'Status checked OK'
printf "${dateColor}$logAge"
printf "${dateColor} min ago${noColor}\n"
printf '%-25s' 'Test server address'
echo $testHost
printf '%-25s' 'Measured download speed'
printf "${downColor}$downLoad"
printf "${downColor} Mb/s${noColor}\n"
printf '%-25s' 'Measured upload speed'
printf "${upColor}$upLoad"
printf "${upColor} Mb/s${noColor}\n"
printf '%-25s' 'Network ping time'
printf "${pingColor}$pingTime"
printf "${pingColor} ms${noColor}\n"
printf '%-25s' 'Cumulative uptime'
echo $upDays' days '$upHours' hours'
printf '%-25s' 'Total downloaded'
echo $downLoaded' '$downUnit
printf '%-25s' 'Total uploaded'
echo $upLoaded' '$upUnit
echo
echo '(Legend: Green: OK/Good, Yellow: Acceptable, Red: Not OK/Poor)'
echo
# If node not up, check TCP 8048 port and print notifications
else
echo
echo "The node is down or not verified yet. (Can take up to 1 h)"
echo
portChk=`timeout 1s telnet $nodeIp 8048 |grep Connected |cut -f 1 -d " "`
if [ "$portChk" = "Connected" ]; then
echo "Port 8048 tested OK"
else
clear
echo
echo "The node is down for TCP port 8048 being blocked!"
fi
echo
fi
# Remove the temporary files
rm tn?.log