chore(archive):Move legacy log to archive dir
[BK-2020-03.git] / unitproc / bkbtclog
... / ...
CommitLineData
1#!/bin/bash
2
3# Date: 2020-03-08T03:48Z; baltakatei>
4
5# Description: Log bitcoin block stats to file every minute for one
6# hour. Also adds stats to PRNG.
7
8# Usage: bkbtclog [path]
9
10 # [path]: Path to write log file.
11
12# Dependencies: jq
13
14# Modify PATH to include some non-standard executable directories.
15PATH="/usr/local/bin/:$PATH" # for bitcoind and bitcoin-cli
16PATH="/usr/bin/:$PATH" # for jq
17
18# Exit if jq is unavailable or bitcoin-cli getblockcount not available.
19if ! ( ( 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
20
21# Set script time-to-live to 1 hour in seconds. See https://stackoverflow.com/a/11198713 .
22SCRIPT_TTL=3600
23SECONDS_END=$(( SECONDS + SCRIPT_TTL ))
24
25# Set output log file path
26LOG_PATH="$1"
27LOG_DIR=$(dirname "$LOG_PATH" )
28
29# Exit if LOG_DIR doesn't exist.
30if [ ! -d "$LOG_DIR" ]; then echo "ERROR:Specified log directory doesn't exist." 1>&2;
31 echo "LOG_DIR is:""$LOG_DIR" 1>&2; exit 1; fi
32
33# Loop until script age exceeds SCRIPT_END.
34while [ $SECONDS -lt $SECONDS_END ]; do
35 # Create log file and first line header if file at LOG_PATH doesn't exist.
36 if [ ! -f "$LOG_PATH" ]; then
37 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";
38 fi
39 TIME="$(date +%Y%m%dT%H%M%S%z)"
40 BITCOINCORE_VERSION="$(bitcoin-cli -version)"
41 BTC_BESTBLOCKSTATS_JSON="$(bitcoin-cli getblockstats "$(bitcoin-cli getblockcount)" '["height","time","blockhash","medianfee","total_out","total_size","txs","ins","outs","maxfeerate","minfeerate"]')"
42 BTC_BESTBLOCKCOUNT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .height' )
43 BTC_BESTBLOCKTIME_UNIX=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .time' )
44 BTC_BESTBLOCKTIME=$(date --date=@"$BTC_BESTBLOCKTIME_UNIX" +%Y%m%dT%H%M%S%z )
45 BTC_BESTBLOCKHASH=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .blockhash' | tr -dc "[:xdigit:]" )
46 BTC_MEDIANFEE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .medianfee' )
47 BTC_TOTALOUTPUT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .total_out' )
48 BTC_TXTOTALSIZE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .total_size' )
49 BTC_TXCOUNT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .txs' )
50 BTC_TXIN=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .ins' )
51 BTC_TXOUT=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .outs' )
52 BTC_MAXTXFEERATE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .maxfeerate' )
53 BTC_MINTXFEERATE=$(echo "$BTC_BESTBLOCKSTATS_JSON" | jq '. | .minfeerate' )
54 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")
55 echo "$OUTPUT" >> "$LOG_PATH";
56 echo "$OUTPUT" >> /dev/random;
57 sleep 60
58done
59exit 0