-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodechecker.sh
86 lines (79 loc) · 4.6 KB
/
nodechecker.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
#!/bin/bash
CRONTAB=/etc/cron.d/nodechecker
cat >$CRONTAB <<EOF
*/15 * * * * root bash /opt/ghpb-bin/nodechecker.sh > /dev/null 2>&1
EOF
REQUIRED_PKG="jq"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
sudo apt-get --yes install $REQUIRED_PKG
fi
REQUIRED_PKG="speedtest-cli"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
sudo apt-get --yes install gnupg1 apt-transport-https dirmngr
INSTALL_KEY=379CE192D401AB61
DEB_DISTRO=$(lsb_release -sc)
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $INSTALL_KEY
echo "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" | sudo tee /etc/apt/sources.list.d/speedtest.list
sudo apt-get --yes install $REQUIRED_PKG
fi
NODENAME="CHANGEME"
PASSWORD="CHANGEME"
BINARY=/opt/ghpb-bin/ghpb
PORT="8545"
HOUR=`date +"%H"`
MINUTE=`date +"%M"`
COUNTRY=`curl -s ipinfo.io/ | jq ".country" | sed 's/"//g'`
FUNCTIONING=false
MINING=false
PEERCOUNT=0
BLOCKNUMBER=0
STARTED=false
NODETYPE=""
TIME=`date -u +%Y-%m-%dT%H:%M:%S`
VERSION=`$BINARY version | sed -n 2p |cut -d ' ' -f2`
FILE=/tmp/blockNumber.log
STARTED=`nc -z 127.0.0.1 $PORT`
STARTED=$?
if [ $STARTED -lt 1 ]; then
STARTED=true
HEXPEERCOUNT=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"net_peerCount","params": ["latest"],"id":2}' http://127.0.0.1:$PORT | jq '.result'`
PEERCOUNT=$((16#`echo ${HEXPEERCOUNT//\"} | cut -c 3-`))
NODETYPE=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"admin_nodeInfo","params": ["latest"],"id":2}' http://127.0.0.1:$PORT | jq '.result' | jq '.local' |sed 's/"//g'`
if test -f "$FILE"; then
hex=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"hpb_blockNumber","params": ["latest"],"id":2}' http://127.0.0.1:$PORT | jq '.result'`
BLOCKNUMBER=$((16#`echo ${hex//\"} | cut -c 3-`))
if (( $BLOCKNUMBER == `cat /tmp/blockNumber.log` )); then
FUNCTIONING=false
else
FUNCTIONING=true
echo $BLOCKNUMBER > $FILE
fi
else
hex=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"hpb_blockNumber","params": ["latest"],"id":2}' http://127.0.0.1:$PORT | jq '.result'`
BLOCKNUMBER=$((16#`echo ${hex//\"} | cut -c 3-`))
echo $BLOCKNUMBER > $FILE
fi
if [ $FUNCTIONING == true ]; then
BOEADDRESS=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"hpb_coinbase","params": ["latest"],"id":64}' http://127.0.0.1:$PORT | jq '.result' | sed 's/"//g'`
MINING=`curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"hpb_mining","params": ["latest"],"id":2}' http://127.0.0.1:$PORT | jq '.result' |sed 's/"//g'`
if [ $MINING == "false" ]; then
FUNCTIONING=false
fi
fi
else
STARTED=false
FUNTIONING=false
fi
curl -s -XPOST "https://$NODENAME:$PASSWORD@669a7da94fbb453f80a520d28bbb3662.us-central1.gcp.cloud.es.io:9243/my-map-index/_doc/" -H 'Content-Type: application/json' -d '{ "@timestamp": "'$TIME'", "country": "'$COUNTRY'", "name": "'$NODENAME'", "functioning": '$FUNCTIONING', "nodeType": "'$NODETYPE'", "mining": '$MINING', "peers": "'$PEERCOUNT'", "blocknumber": "'$BLOCKNUMBER'", "running": '$STARTED', "version": "'$VERSION'", "boeAddress": "'$BOEADDRESS'" }'
if [ $HOUR == 00 ] && [ $MINUTE == 00 ]; then
BANDWIDTH=`speedtest-cli --json`
DOWNLOAD=`echo $BANDWIDTH | jq '.download' | cut -d '.' -f1`
DOWNLOAD=`echo $(( $DOWNLOAD / 1000000 ))`
UPLOAD=`echo $BANDWIDTH | jq '.upload' | cut -d '.' -f1`
UPLOAD=`echo $(( $UPLOAD / 1000000 ))`
curl -s -XPOST "https://$NODENAME:$PASSWORD@669a7da94fbb453f80a520d28bbb3662.us-central1.gcp.cloud.es.io:9243/bandwidth-index/_doc/" -H 'Content-Type: application/json' -d '{ "@timestamp": "'$TIME'", "country": "'$COUNTRY'", "name": "'$NODENAME'", "functioning": '$FUNCTIONING', "nodeType": "'$NODETYPE'", "mining": '$MINING', "peers": "'$PEERCOUNT'", "blocknumber": "'$BLOCKNUMBER'", "running": '$STARTED', "version": "'$VERSION'", "boeAddress": "'$BOEADDRESS'", "upload": "'$UPLOAD'", "download": "'$DOWNLOAD'" }'
fi