raspiblitz/home.admin/config.scripts/blitz.statusscan.sh

306 lines
10 KiB
Bash
Raw Normal View History

2019-04-16 21:25:27 +01:00
#!/bin/bash
source /home/admin/raspiblitz.info
2021-05-18 18:48:51 -05:00
source /mnt/hdd/raspiblitz.conf 2>/dev/null
2019-04-16 21:25:27 +01:00
# LNTYPE is lnd | cln
if [ $# -gt 0 ];then
LNTYPE=$1
else
LNTYPE=lnd
fi
source <(/home/admin/config.scripts/network.aliases.sh getvars $LNTYPE ${chain}net)
2019-04-16 21:25:27 +01:00
# 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)
# localIP
localip=$(hostname -I | awk '{print $1}')
2019-04-16 21:25:27 +01:00
echo "localIP='${localip}'"
2019-05-08 14:34:33 +01:00
# temp - no measurement in a VM
2019-05-08 13:48:35 +01:00
tempC=0
2019-05-08 14:22:15 +01:00
if [ -d "/sys/class/thermal/thermal_zone0/" ]; then
2019-05-08 13:48:35 +01:00
tempC=$(echo "scale=1; $(cat /sys/class/thermal/thermal_zone0/temp)/1000" | bc)
2019-05-08 14:22:15 +01:00
echo "tempCelsius='${tempC}'"
2019-05-08 13:48:35 +01:00
fi
2019-04-16 22:00:49 +01:00
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-08-05 23:15:12 +02:00
# get UPS info (if configured)
2019-08-05 23:21:55 +02:00
/home/admin/config.scripts/blitz.ups.sh status
2019-08-05 23:15:12 +02:00
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
$bitcoincli_alias getblockchaininfo 1>/var/cache/raspiblitz/.bitcoind.out 2>/var/cache/raspiblitz/.bitcoind.error
2019-04-16 21:25:27 +01:00
# check if error on request
blockchaininfo=$(cat /var/cache/raspiblitz/.bitcoind.out 2>/dev/null)
bitcoinError=$(cat /var/cache/raspiblitz/.bitcoind.error 2>/dev/null)
#rm /var/cache/raspiblitz/.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
2021-05-25 19:22:09 -05:00
###################################
# Get data from blockchain network
###################################
source <(sudo -u bitcoin /home/admin/config.scripts/network.monitor.sh peer-status)
echo "blockchainPeers=${peers}"
2019-04-16 21:25:27 +01:00
##############################
# 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
2019-05-09 20:06:43 +02:00
lowDiskSpace=$(sudo tail -n 100 /mnt/hdd/${network}${pathAdd}/debug.log 2>/dev/null | grep -c "Error: Disk space is low!")
2019-04-17 18:34:20 +01:00
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-05-09 20:06:43 +02:00
bitcoinErrorFull=$(sudo tail -n 100 /mnt/hdd/${network}${pathAdd}/debug.log 2>/dev/null | 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 ${netprefix}lnd.service 2>/dev/null | grep -c running)
2019-04-16 21:25:27 +01:00
echo "lndActive=${lndRunning}"
if [ ${lndRunning} -eq 1 ]; then
# get LND info
2019-04-26 21:38:54 +02:00
lndRPCReady=1
lndinfo=$($lncli_alias getinfo 2>/var/cache/raspiblitz/.lnd.error)
2019-04-16 21:25:27 +01:00
# check if error on request
lndErrorFull=$(cat /var/cache/raspiblitz/.lnd.error 2>/dev/null)
2019-04-24 23:28:45 +02:00
lndErrorShort=''
#rm /var/cache/raspiblitz/.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-05-09 20:06:43 +02:00
lndStartLineNumber=$(sudo cat /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log 2>/dev/null | 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-05-09 20:06:43 +02:00
lndLogsAfterStart=$(sudo tail --lines=+${lndStartLineNumber} /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log 2>/dev/null)
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
2021-01-13 00:38:10 +01:00
# number of lnd peers
lndPeers=$(echo ${lndinfo} | jq -r '.num_peers')
echo "lndPeers=${lndPeers}"
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-07-30 04:59:57 +02:00
nowTimestamp=$(date +%s)
2020-03-04 01:33:36 +01:00
if [ ${#scanTimestamp} -gt 0 ] && [ ${scanTimestamp} -gt ${nowTimestamp} ]; then
2019-07-30 04:59:57 +02:00
scanTimestamp=${nowTimestamp}
fi
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-07-30 04:59:57 +02:00
scanDate=$(date -d @${scanTimestamp} 2>/dev/null)
2019-04-17 00:06:11 +01:00
echo "scanDate='${scanDate}'"
2019-07-30 04:59:57 +02:00
2019-04-17 00:06:11 +01:00
# calculate LND scan progress by seconds since Genesisblock
genesisTimestamp=1230940800
2019-07-30 04:59:57 +02:00
2019-04-17 00:06:11 +01:00
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
2021-07-16 12:19:14 +02:00
# is CLN running
2021-07-24 23:59:16 +02:00
clnRunning=$(systemctl status ${netprefix}lightningd.service 2>/dev/null | grep -c running)
2021-07-16 12:19:14 +02:00
echo "clnActive=${clnRunning}"
if [ ${clnRunning} -eq 1 ]; then
clnInfo=$($lightningcli_alias getinfo)
2021-07-25 00:39:24 +02:00
clnBlockHeight=$(echo "${clnInfo}" | jq -r '.blockheight' | tr -cd '[[:digit:]]')
2021-07-25 00:55:02 +02:00
scanProgress=$(echo "scale=2; $clnBlockHeight*100/$total" | bc)
echo "scanProgress=${scanProgress}"
2021-07-25 00:47:50 +02:00
clnBlockHeightPlusOne=$(expr $clnBlockHeight + 1)
2021-07-25 00:47:11 +02:00
if [ "${total}" == "${clnBlockHeight}" ] || [ "${total}" == "${clnBlockHeightPlusOne}" ]; then
echo "syncedToChain=1"
else
echo "syncedToChain=0"
fi
2021-07-16 12:19:14 +02:00
fi
# touchscreen statistics
if [ "${touchscreen}" == "1" ]; then
echo "blitzTUIActive=1"
if [ ${#blitzTUIRestarts} -gt 0 ]; then
echo "blitzTUIRestarts=${blitzTUIRestarts}"
else
echo "blitzTUIRestarts=0"
fi
else
echo "blitzTUIActive=0"
echo "blitzTUIRestarts=0"
fi
2021-05-05 01:01:11 +02:00
# check if runnig in vagrant
vagrant=$(df | grep -c "/vagrant")
echo "vagrant=${vagrant}"
2019-04-16 21:25:27 +01:00
# check if online if problem with other stuff
# info on scan run time
endTime=$(date +%s)
runTime=$(echo "${endTime}-${startTime}" | bc)
2020-07-22 16:56:10 +02:00
echo "scriptRuntime=${runTime}"