2019-04-16 21:25:27 +01:00
#!/bin/bash
source /home/admin/raspiblitz.info
source /mnt/hdd/raspiblitz.conf
# command info
if [ " $1 " = "-h" ] || [ " $1 " = "-help" ] ; then
echo "# script to scan the state of the system after setup"
exit 1
fi
# measure time of scan
startTime = $( date +%s)
2019-04-16 21:31:52 +01:00
# macke sure temp folder on HDD is available and fro all usable
sudo mkdir /mnt/hdd/temp 2>/dev/null
sudo chmod 777 -R /mnt/hdd/temp 2>/dev/null
2019-04-16 21:25:27 +01:00
# localIP
localip = $( ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/' )
echo " localIP=' ${ localip } ' "
2019-04-16 22:00:49 +01:00
# temp
tempC = $( echo " scale=1; $( cat /sys/class/thermal/thermal_zone0/temp) /1000 " | bc)
echo " tempCelsius=' ${ tempC } ' "
2019-04-16 23:34:53 +01:00
# uptime in seconds
uptime = $( awk '{printf("%d\n",$1 + 0.5)}' /proc/uptime)
echo " uptime= ${ uptime } "
2019-04-24 13:47:58 +02:00
# count restarts of bitcoind/litecoind
2019-04-25 01:27:26 +02:00
startcountBlockchain = $( cat /home/admin/systemd.blockchain.log 2>/dev/null | grep -c "STARTED" )
2019-04-24 13:47:58 +02:00
echo " startcountBlockchain= ${ startcountBlockchain } "
2019-04-16 21:25:27 +01:00
# is bitcoind running
bitcoinRunning = $( systemctl status ${ network } d.service 2>/dev/null | grep -c running)
echo " bitcoinActive= ${ bitcoinRunning } "
if [ ${ bitcoinRunning } -eq 1 ] ; then
# get blockchain info
2019-04-17 00:17:25 +01:00
sudo -u bitcoin ${ network } -cli -datadir= /home/bitcoin/.${ network } getblockchaininfo 1>/mnt/hdd/temp/.bitcoind.out 2>/mnt/hdd/temp/.bitcoind.error
2019-04-16 21:25:27 +01:00
# check if error on request
2019-04-17 00:17:25 +01:00
blockchaininfo = $( cat /mnt/hdd/temp/.bitcoind.out 2>/dev/null)
2019-04-17 00:03:36 +01:00
bitcoinError = $( cat /mnt/hdd/temp/.bitcoind.error 2>/dev/null)
2019-04-24 23:56:43 +02:00
#rm /mnt/hdd/temp/.bitcoind.error 2>/dev/null
2019-04-16 21:26:37 +01:00
if [ ${# bitcoinError } -gt 0 ] ; then
2019-04-17 00:26:30 +01:00
bitcoinErrorShort = $( echo ${ bitcoinError /error* : / } | sed 's/[^a-zA-Z0-9 ]//g' )
echo " bitcoinErrorShort=' ${ bitcoinErrorShort } ' "
2019-04-24 23:56:43 +02:00
bitcoinErrorFull = $( echo ${ bitcoinError } | tr -d "'" )
2019-04-24 23:28:45 +02:00
echo " bitcoinErrorFull=' ${ bitcoinErrorFull } ' "
2019-04-16 21:25:27 +01:00
else
##############################
# Get data from blockchaininfo
##############################
# get total number of blocks
total = $( echo ${ blockchaininfo } | jq -r '.blocks' )
echo " blockchainHeight= ${ total } "
# is initial sync of blockchain
initialSync = $( echo ${ blockchaininfo } | jq -r '.initialblockdownload' | grep -c 'true' )
echo " initialSync= ${ initialSync } "
# get blockchain sync progress
syncProgress = " $( echo ${ blockchaininfo } | jq -r '.verificationprogress' ) "
2019-04-16 21:39:49 +01:00
syncProgress = $( echo $syncProgress | awk '{printf( "%.2f%%", 100 * $1)}' | tr '%' ' ' | tr -s " " )
2019-04-16 21:25:27 +01:00
echo " syncProgress= ${ syncProgress } "
fi
2019-04-17 18:34:20 +01:00
else
# find out why Bitcoin not running
pathAdd = ""
if [ " ${ chain } " = "test" ] ; then
pathAdd = "/testnet3"
fi
#### POSSIBLE/SOFT PROBLEMS
# place here in future analysis
#### HARD PROBLEMS
# LOW DISK SPACE
lowDiskSpace = $( sudo tail -n 100 /mnt/hdd/${ network } ${ pathAdd } /debug.log | grep -c "Error: Disk space is low!" )
if [ ${ lowDiskSpace } -gt 0 ] ; then
bitcoinErrorShort = "HDD DISK SPACE LOW"
bitcoinErrorFull = "HDD DISK SPACE LOW - check what data you can delete on HDD and restart"
fi
#### GENERIC ERROR FIND
2019-05-01 12:00:22 +02:00
# if still no error identified - search logs for generic error (after 4min uptime)
2019-04-25 02:30:24 +02:00
if [ ${# bitcoinErrorShort } -eq 0 ] && [ ${ uptime } -gt 240 ] ; then
2019-04-24 23:56:43 +02:00
bitcoinErrorFull = $( sudo tail -n 100 /mnt/hdd/${ network } ${ pathAdd } /debug.log | grep -c "Error:" | tail -1 | tr -d "'" )
2019-04-24 15:52:24 +02:00
if [ ${# bitcoinErrorFull } -gt 0 ] ; then
2019-04-17 18:34:20 +01:00
bitcoinErrorShort = "Error found in Logs"
fi
fi
# output error if found
if [ ${# bitcoinErrorShort } -gt 0 ] ; then
echo " bitcoinErrorShort=' ${ bitcoinErrorShort } ' "
echo " bitcoinErrorFull=' ${ bitcoinErrorFull } ' "
2019-04-24 16:42:16 +02:00
/home/admin/config.scripts/blitz.systemd.sh log blockchain " ERROR: ${ bitcoinErrorShort } "
2019-04-17 18:34:20 +01:00
fi
2019-04-16 21:25:27 +01:00
fi
2019-04-24 16:23:15 +02:00
# count restarts of bitcoind/litecoind
2019-04-25 01:27:26 +02:00
startcountLightning = $( cat /home/admin/systemd.lightning.log 2>/dev/null | grep -c "STARTED" )
2019-04-24 16:23:15 +02:00
echo " startcountLightning= ${ startcountLightning } "
2019-04-16 21:25:27 +01:00
# is LND running
lndRunning = $( systemctl status lnd.service 2>/dev/null | grep -c running)
echo " lndActive= ${ lndRunning } "
if [ ${ lndRunning } -eq 1 ] ; then
# get LND info
2019-04-26 21:38:54 +02:00
lndRPCReady = 1
2019-04-16 21:31:52 +01:00
lndinfo = $( sudo -u bitcoin lncli getinfo 2>/mnt/hdd/temp/.lnd.error)
2019-04-16 21:25:27 +01:00
# check if error on request
2019-04-17 00:03:36 +01:00
lndErrorFull = $( cat /mnt/hdd/temp/.lnd.error 2>/dev/null)
2019-04-24 23:28:45 +02:00
lndErrorShort = ''
2019-04-24 23:56:43 +02:00
#rm /mnt/hdd/temp/.lnd.error 2>/dev/null
2019-04-24 23:28:45 +02:00
2019-04-25 00:02:19 +02:00
if [ ${# lndErrorFull } -gt 0 ] ; then
2019-04-24 23:28:45 +02:00
2019-04-26 21:38:54 +02:00
# flag if error could be resoled by analysis
errorResolved = 0
### analyse LND logs since start
2019-04-26 23:49:45 +02:00
# find a the line number in logs of start of LND
2019-04-26 21:38:54 +02:00
# just do this on error case to save on processing memory
2019-04-26 23:49:45 +02:00
lndStartLineNumber = $( sudo cat /mnt/hdd/lnd/logs/${ network } /${ chain } net/lnd.log | grep -in "LTND: Active chain:" | tail -1 | cut -d ":" -f1)
2019-04-26 21:38:54 +02:00
# get logs of last LND start
2019-04-26 23:49:45 +02:00
lndLogsAfterStart = $( sudo tail --lines= +${ lndStartLineNumber } /mnt/hdd/lnd/logs/${ network } /${ chain } net/lnd.log)
2019-04-26 21:38:54 +02:00
# check RPC server ready (can take some time after wallet was unlocked)
2019-04-26 23:36:50 +02:00
lndRPCReady = $( echo " ${ lndLogsAfterStart } " | grep -c "RPCS: RPC server listening on" )
2019-04-26 21:38:54 +02:00
echo " lndRPCReady= ${ lndRPCReady } "
# check wallet if wallet was opened (after correct password)
2019-04-26 23:36:50 +02:00
lndWalletOpened = $( echo " ${ lndLogsAfterStart } " | grep -c "LNWL: Opened wallet" )
2019-04-26 21:38:54 +02:00
echo " walletOpened= ${ lndWalletOpened } "
# check wallet if wallet is ready (can take some time after wallet was opened)
2019-04-26 23:36:50 +02:00
lndWalletReady = $( echo " ${ lndLogsAfterStart } " | grep -c "LTND: LightningWallet opened" )
2019-04-26 21:38:54 +02:00
echo " walletReady= ${ lndWalletReady } "
### check errors
2019-04-24 23:28:45 +02:00
# scan error for walletLocked as common error
2019-04-25 00:02:19 +02:00
locked = $( echo ${ lndErrorFull } | grep -c 'Wallet is encrypted' )
2019-04-24 23:28:45 +02:00
if [ ${ locked } -gt 0 ] ; then
echo "walletLocked=1"
else
echo "walletLocked=0"
2019-04-25 02:20:50 +02:00
rpcNotWorking = $( echo ${ lndErrorFull } | grep -c 'connection refused' )
2019-04-25 01:55:11 +02:00
if [ ${ rpcNotWorking } -gt 0 ] ; then
2019-04-26 21:38:54 +02:00
# this can happen for a long time when LND is starting fresh sync
# on first startup - check if logs since start signaled RPC ready before
if [ ${ lndRPCReady } -eq 0 ] ; then
# nullify error - this is normal
lndErrorFull = ""
errorResolved = 1
# oputput basic data because no error
echo "# LND RPC is still warming up - no scan progress: prepare scan"
echo "scanTimestamp=-2"
echo "syncedToChain=0"
else
echo "# LND RPC was started - some other problem going on"
lndErrorShort = 'LND RPC not responding'
lndErrorFull = $( echo " LND RPC is not responding. LND may have problems starting up. Check logs, config files and systemd service. Org-Error: ${ lndErrorFull } " | tr -d "'" )
fi
2019-04-25 01:55:11 +02:00
fi
2019-04-26 21:38:54 +02:00
# if not known error and not resolved before - keep generic
if [ ${# lndErrorShort } -eq 0 ] && [ ${ errorResolved } -eq 0 ] ; then
2019-04-25 01:55:11 +02:00
lndErrorShort = 'Unkown Error - see logs'
lndErrorFull = $( echo ${ lndErrorFull } | tr -d "'" )
fi
# write to results
2019-04-26 21:38:54 +02:00
if [ ${# lndErrorFull } -gt 0 ] ; then
echo " lndErrorShort=' ${ lndErrorShort } ' "
echo " lndErrorFull=' ${ lndErrorFull } ' "
/home/admin/config.scripts/blitz.systemd.sh log lightning " ERROR: ${ lndErrorFull } "
fi
2019-04-25 01:55:11 +02:00
2019-04-24 23:28:45 +02:00
fi
2019-04-16 21:25:27 +01:00
else
2019-04-23 16:22:29 +02:00
# check if wallet is locked
locked = $( echo ${ lndinfo } | grep -c unlock)
if [ ${ locked } -gt 0 ] ; then
echo "walletLocked=1"
else
echo "walletLocked=0"
fi
2019-04-16 21:25:27 +01:00
# synced to chain
syncedToChain = $( echo ${ lndinfo } | jq -r '.synced_to_chain' | grep -c 'true' )
echo " syncedToChain= ${ syncedToChain } "
# lnd scan progress
2019-04-16 21:35:45 +01:00
scanTimestamp = $( echo ${ lndinfo } | jq -r '.best_header_timestamp' )
2019-04-17 00:06:11 +01:00
if [ ${# scanTimestamp } -gt 0 ] ; then
2019-04-24 23:40:27 +02:00
echo " scanTimestamp= ${ scanTimestamp } "
2019-04-17 00:06:11 +01:00
scanDate = $( date -d @${ scanTimestamp } )
echo " scanDate=' ${ scanDate } ' "
# calculate LND scan progress by seconds since Genesisblock
genesisTimestamp = 1230940800
nowTimestamp = $( date +%s)
totalSeconds = $( echo " ${ nowTimestamp } - ${ genesisTimestamp } " | bc)
scannedSeconds = $( echo " ${ scanTimestamp } - ${ genesisTimestamp } " | bc)
scanProgress = $( echo " scale=2; $scannedSeconds *100/ $totalSeconds " | bc)
echo " scanProgress= ${ scanProgress } "
2019-04-24 23:40:27 +02:00
else
echo "# was not able to parse 'best_header_timestamp' from: lncli getinfo"
echo "scanTimestamp=-1"
2019-04-17 00:06:11 +01:00
fi
2019-04-16 21:25:27 +01:00
fi
2019-04-26 21:38:54 +02:00
# output if lnd-RPC is ready
echo " lndRPCReady= ${ lndRPCReady } "
2019-04-16 21:25:27 +01:00
fi
# check if online if problem with other stuff
# info on scan run time
endTime = $( date +%s)
runTime = $( echo " ${ endTime } - ${ startTime } " | bc)
2019-04-17 00:12:43 +01:00
echo " scriptRuntime= ${ runTime } "