From 21371cc330053b1fcea3617c577579ae0b6c357c Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Tue, 10 Jul 2018 18:28:10 -0300
Subject: [PATCH] Better tools and starting to monitor

---
 .gitignore     |  3 +++
 bin/cron.sh    | 13 ++++++++++
 bin/env        |  2 ++
 bin/libbfa.sh  | 19 ++++++++------
 bin/log.sh     |  2 ++
 bin/monitor.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++
 bin/start.sh   | 15 ++++++++---
 7 files changed, 110 insertions(+), 11 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 bin/cron.sh
 create mode 100644 bin/monitor.sh

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5cce60f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*/lastseen
+*/node*
+*/status
diff --git a/bin/cron.sh b/bin/cron.sh
new file mode 100644
index 0000000..9b643e6
--- /dev/null
+++ b/bin/cron.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+trap 'exit 1' ERR
+cd `dirname $0`
+source ./env
+exec < /dev/null > /dev/null 2> /dev/null
+(
+    ./start.sh &
+    while ./monitor.sh
+    do
+	sleep 60
+    done &
+)
diff --git a/bin/env b/bin/env
index e6e2ace..2139b84 100644
--- a/bin/env
+++ b/bin/env
@@ -1,2 +1,4 @@
 export BFAHOME=${HOME}/bfa
 export BFANETWORKDIR="${BFAHOME}/network5445"
+export BFANETWORKID=5445
+PATH=${PATH}:${BFAHOME}/bin
diff --git a/bin/libbfa.sh b/bin/libbfa.sh
index eab03fa..e004e2d 100644
--- a/bin/libbfa.sh
+++ b/bin/libbfa.sh
@@ -2,6 +2,7 @@
 # 20180626 Robert Martin-Legene <robert@nic.ar>
 
 trap "echo Argh! ; exit 1" ERR
+set -e -o errtrace
 
 function    fatal()
 {
@@ -54,16 +55,18 @@ function    geth_exec_js
 
 function    getnetworkid
 {
+    test -n "${BFANETWORKID}" &&
+	return
     local   dir="${BFANETWORKDIR}"
     for dir in $( ls -1d "${BFAHOME}"/* | sort --version-sort )
     do
         if [ -d "${dir}" ]
         then
             local   base=$( basename "${dir}" )
-            base=$( echo "${base}" | grep -E '^network[0-9]+$' )
-            if [ -n "$base" ]
+            base=$( echo "${base}" | grep -E '^network[0-9]+$' ) || true
+            if [ -n "${base:7}" ]
             then
-                BFANETWORKID=${network:7}
+                BFANETWORKID=${base:7}
                 return
             fi
         fi
@@ -98,8 +101,7 @@ function    networkdir
         stderr  "Consider running: mkdir ${BFAHOME}/network${num} or set BFANETWORKDIR in ${BFAHOME}/bin/env"
         exit 1
     fi
-    # Get network id
-    BFANETWORKID=${BFANETWORKDIR:7}
+    getnetworkid
 }
 
 function    nodedir
@@ -124,9 +126,10 @@ function    nodedir
         fatal "$BFANODEDIR is not a directory."
     test -d "${BFANODEDIR}/geth/chaindata"  ||
         fatal "Node is not initialised. Consider running: geth --datadir $BFANODEDIR init ${BFANETWORKDIR}/genesis.json"
-    test -r "${BFANODEDIR}/port"            ||
+    test -r "${BFANODEDIR}/netport"         ||
         echo $(( $RANDOM / 2 + 12345 )) > ${BFANODEDIR}/netport
-    httpport=$(     cat ${BFANODEDIR}/netport )
+    netport=$(     cat ${BFANODEDIR}/netport )
+    test $? = 0
     test -r "${BFANODEDIR}/rpcport"         ||
         echo $(( $RANDOM / 2 + 12345 )) > ${BFANODEDIR}/rpcport
     rpcport=$(      cat ${BFANODEDIR}/rpcport )
@@ -140,7 +143,7 @@ function    account
         test -d "${BFANODEDIR}/keystore"
         account=$( ls -1dl "${BFANODEDIR}"/keystore/*--* | head -1 | sed 's/.*--//' )
     fi
-    if [ -n "$account" ]
+    if [ -z "$account" ]
     then
         fatal "No accounts found. Consider running: geth --datadir ${BFANETWORKDIR}/node1 --password /dev/null account new"
     fi
diff --git a/bin/log.sh b/bin/log.sh
index 0d64483..43e3068 100755
--- a/bin/log.sh
+++ b/bin/log.sh
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+trap "echo Argh; exit 1" ERR
+
 true ${MAXSIZE:=$((10*1024*1024))}
 
 log=$1
diff --git a/bin/monitor.sh b/bin/monitor.sh
new file mode 100644
index 0000000..3ecd354
--- /dev/null
+++ b/bin/monitor.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/env ?" >&2; exit 1; fi
+source ${BFAHOME}/bin/libbfa.sh || exit 1
+
+function json_query
+{
+  cmd=$1
+  params=$2
+  if [ -n "$params" ]
+  then
+    params=",\"parameters\":[$2]"
+  fi
+  json=$(
+    curl -s -H 'Content-type: application/json' -X POST --data "{\"jsonrpc\":\"2.0\",\"method\":\"${cmd}\"${params},\"id\":1}" http://127.0.0.1:$rpcport
+  )
+}
+
+function json_err
+{
+  echo "$json" | jq -r '.result'
+}
+
+function json_arraylength
+{
+  jq -r length
+}
+
+function json_result
+{
+  echo "$json" | jq -r '.result'
+}
+
+bfaconfig node
+json_query admin_peers
+numpeers=$( json_result | json_arraylength )
+statusfile=$( bfamktemp )
+(
+  echo -n "time: "
+  date
+  echo "total-peer-count: ${numpeers}"
+) > $statusfile
+peercount=0
+for idx in $( seq 0 $(( $numpeers - 1 )) )
+do
+  eth=$( echo "$json" | jq -r .result[$idx].protocols.eth )
+  if [ "$eth" = "handshake" ]
+  then
+    continue
+  fi
+  #echo -n "$idx: "; echo "$eth" | jq -c
+  remoteid=$(
+	echo "$json" |
+	jq -r .result[$idx].id
+  )
+  remoteip=$(
+	echo "$json" |
+	jq -r .result[$idx].network.remoteAddress |
+	sed 's/:[0-9][0-9]*$//'i
+  )
+  mkdir -p "${BFANETWORKDIR}/lastseen/${remoteid}"
+  touch    "${BFANETWORKDIR}/lastseen/${remoteid}/${remoteip}"
+  echo "peer: ${remoteid}" >> $statusfile
+  peercount=$(( $peercount + 1 ))
+done
+echo "valid-peer-count: ${peercount}" >> $statusfile
+mv -f $statusfile ${BFANETWORKDIR}/status
diff --git a/bin/start.sh b/bin/start.sh
index 52fcf7f..896f2c2 100755
--- a/bin/start.sh
+++ b/bin/start.sh
@@ -64,9 +64,13 @@ bootnodeport=5445
 bootnodes="enode://${bootnodeid}@[$bootnodev6]:${bootnodeport},enode://${bootnodeid}@[$bootnodev4]:${bootnodeport}"
 
 bfaconfig max
-set -x
-# Start the miner.
-exec flock --nonblock --exclusive ${BFANODEDIR}/rpcport \
+(
+  echo
+  echo '***'
+  echo
+  set -x
+  # Start the miner.
+  exec flock --nonblock --exclusive ${BFANODEDIR}/rpcport \
     geth			    \
 	--datadir ${BFANODEDIR}	    \
 	--unlock "$account"	    \
@@ -75,8 +79,13 @@ exec flock --nonblock --exclusive ${BFANODEDIR}/rpcport \
 	--networkid $BFANETWORKID   \
 	--rpc			    \
 	--rpcport $rpcport	    \
+	--rpcapi "eth,net,web3,admin,clique,miner"	\
 	--port $netport		    \
 	--syncmode full		    \
 	--nousb			    \
 	--bootnodes "${bootnodes}"  \
+	--gcmode archive	    \
+	--cache 512		    \
 	--verbosity 3
+) 2>&1 |
+    ${BFAHOME}/bin/log.sh ${BFANODEDIR}/log
-- 
GitLab