-#!/bin/bash
-
-# Date: 2020-03-08T03:48Z; baltakatei>
-
-# Description: Log bitcoin block stats to file every minute for one
-# hour. Also adds stats to PRNG.
-
-# Usage: bkbtclog [path]
-
- # [path]: Path to write log file.
-
-# Dependencies: jq
-
-# Modify PATH to include some non-standard executable directories.
-PATH="/usr/local/bin/:$PATH" # for bitcoind and bitcoin-cli
-PATH="/usr/bin/:$PATH" # for jq
-
-# Exit if jq is unavailable or bitcoin-cli getblockcount not available.
-if ! ( ( command -v jq 1>/dev/null 2>&1 ) && ( bitcoin-cli getblockcount 1>/dev/null 2>&1 ) ) ; then echo "ERROR:Commands jq or bitcoin-cli not available." 1>&2; exit 1; fi
-
-# Set script time-to-live to 1 hour in seconds. See https://stackoverflow.com/a/11198713 .
-SCRIPT_TTL=3600
-SECONDS_END=$(( SECONDS + SCRIPT_TTL ))
-
-# Set output log file path
-LOG_PATH="$1"
-LOG_DIR=$(dirname "$LOG_PATH" )
-
-# Exit if LOG_DIR doesn't exist.
-if [ ! -d "$LOG_DIR" ]; then echo "ERROR:Specified log directory doesn't exist." 1>&2;
- echo "LOG_DIR is:""$LOG_DIR" 1>&2; exit 1; fi
-
-# Loop until script age exceeds SCRIPT_END.
-while [ $SECONDS -lt $SECONDS_END ]; do
- # Create log file and first line header if file at LOG_PATH doesn't exist.
- if [ ! -f "$LOG_PATH" ]; then
- echo "TIME","BTC_BESTBLOCKCOUNT","BTC_BESTBLOCKTIME","BTC_BESTBLOCKHASH","BTC_MEDIANFEE","BTC_TOTALOUTPUT","BTC_TXTOTALSIZE","BTC_TXCOUNT","BTC_TXIN","BTC_TXOUT","BTC_MAXTXFEERATE","BTC_MINTXFEERATE","BITCOINCORE_VERSION" >> "$LOG_PATH";
- fi
- TIME="$(date +%Y%m%dT%H%M%S%z)"
- BITCOINCORE_VERSION="$(bitcoin-cli -version)"
- BTC_BESTBLOCKSTATS_JSON="$(bitcoin-cli getblockstats "$(bitcoin-cli getblockcount)" '["height","time","blockhash","medianfee","total_out","total_size","txs","ins","outs","maxfeerate","minfeerate"]')"
- BTC_BESTBLOCKCOUNT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .height' )
- BTC_BESTBLOCKTIME_UNIX=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .time' )
- BTC_BESTBLOCKTIME=$(date --date=@"$BTC_BESTBLOCKTIME_UNIX" +%Y%m%dT%H%M%S%z )
- BTC_BESTBLOCKHASH=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .blockhash' | tr -dc "[:xdigit:]" )
- BTC_MEDIANFEE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .medianfee' )
- BTC_TOTALOUTPUT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .total_out' )
- BTC_TXTOTALSIZE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .total_size' )
- BTC_TXCOUNT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .txs' )
- BTC_TXIN=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .ins' )
- BTC_TXOUT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .outs' )
- BTC_MAXTXFEERATE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .maxfeerate' )
- BTC_MINTXFEERATE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .minfeerate' )
- OUTPUT=$(echo "$TIME","$BTC_BESTBLOCKCOUNT","$BTC_BESTBLOCKTIME","$BTC_BESTBLOCKHASH","$BTC_MEDIANFEE","$BTC_TOTALOUTPUT","$BTC_TXTOTALSIZE","$BTC_TXCOUNT","$BTC_TXIN","$BTC_TXOUT","$BTC_MAXTXFEERATE","$BTC_MINTXFEERATE","$BITCOINCORE_VERSION")
- echo "$OUTPUT" >> "$LOG_PATH";
- echo "$OUTPUT" >> /dev/random;
- sleep 60
-done
-exit 0