-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridcoingeo.sh
109 lines (104 loc) · 3.38 KB
/
gridcoingeo.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
#!/bin/sh
#--------------------------------------------------
# This script intended to run as service addition to gridcoin_netdata or as a seperate service if more charts are added
#
# Will leave option of using online API (15000 requests per hour max (15000 / 60 = 250 per minute))
# Online API: https://freegeoip.net/json/1.1.1.1
# Took 30 seconds to figure out the 60 ips through web API
# Default setup with local API method which i've setup to port 5000 in service http://127.0.0.1:5000/json/1.1.1.1
# freegeoip requires freegeoip.license to be included with release when using binary. No complaints here.
#
# Locally Requires Dependencies
# jq
# freegeoip binary (included in service install)
#
# READ enviroment variables from config
GRCCONF='/usr/local/bin/grc-netdata.conf'
GRCGEO='/usr/local/bin/geo.json'
# Read an config ini file for GRC info:)
while read -r GRCCONFDATA; do
if [[ ${GRCCONFDATA%%=*} == "GRCPATH" ]]
then
GRCPATH=${GRCCONFDATA#*=}
elif [[ ${GRCCONFDATA%%=*} == "GRCAPP" ]]
then
GRCAPP=${GRCCONFDATA#*=}
elif [[ ${GRCCONFDATA%%=*} == "FREEGEOIPPORT" ]]
then
FREEGEOIPPORT=${GRCCONFDATA#*=}
else
continue
fi
done < $GRCCONF
APIADDRESS="http://127.0.0.1:$FREEGEOIPPORT/json"
# END enviroment variables
if pgrep "gridcoin" > /dev/null
then
peerinfo=$($GRCAPP getpeerinfo | jq -r '.[].addr' | rev | cut -d':' -f2- | rev | tr -d '[' | tr -d ']')
contNA=0 # North America
contSA=0 # South America
contEU=0 # Europe
contAF=0 # Africe
contAS=0 # Asia
contOC=0 # Australia (Oceania)
# contAN=0 # Antarctica // Removed as antarctica has no decent internet to support an application such as gridcoin. In event of one add to contOT.
contOT=0 # Other, yes they exist
# Do loop to run through peerinfo string line by line.
while read -r line; do
# Curl each ip address wheather IPV4 or Ipv6 through API
curldata=$(curl -s $APIADDRESS/$line | jq -r '.country_code')
# Cross reference country_code to geo.json to return continent and increase value by one
curlcont=$(jq -r '.[].'"$curldata" $GRCGEO)
if [[ $curlcont == "NA" ]]
then
contNA=$(($contNA + 1))
fi
if [[ $curlcont == "SA" ]]
then
contSA=$(($contSA + 1))
fi
if [[ $curlcont == "EU" ]]
then
contEU=$(($contEU + 1))
fi
if [[ $curlcont == "AF" ]]
then
contAF=$(($contAF + 1))
fi
if [[ $curlcont == "AS" ]]
then
contAS=$(($contAS + 1))
fi
if [[ $curlcont == "OC" ]]
then
contOC=$(($contOC + 1))
fi
if [[ $curlcont == "AN" ]]
then
contAN=$(($contOT + 1))
fi
if [[ $curlcont == "XX" ]]
then
contOT=$(($contOT + 1))
fi
#No reply
if [[ $curlcont == "" ]]
then
contOT=$(($contOT + 1))
fi
done <<< "$peerinfo"
# take data and make temporary json file with results
echo "[{" > $GRCPATH/geooutput.tmp
echo '"contNA"': '"'$contNA'",' >> $GRCPATH/geooutput.tmp
echo '"contSA"': '"'$contSA'",' >> $GRCPATH/geooutput.tmp
echo '"contEU"': '"'$contEU'",' >> $GRCPATH/geooutput.tmp
echo '"contAF"': '"'$contAF'",' >> $GRCPATH/geooutput.tmp
echo '"contAS"': '"'$contAS'",' >> $GRCPATH/geooutput.tmp
echo '"contOC"': '"'$contOC'",' >> $GRCPATH/geooutput.tmp
echo '"contOT"': '"'$contOT'"' >> $GRCPATH/geooutput.tmp
echo "}]" >> $GRCPATH/geooutput.tmp
# Finally move data to geooutput.tmp
mv -f $GRCPATH/geooutput.tmp $GRCPATH/geooutput.json
else
exit 1
fi