raspiblitz/home.admin/00infoBlitz.sh

212 lines
7.5 KiB
Bash
Raw Normal View History

2018-07-17 13:12:03 +02:00
#!/bin/sh
# RaspiBolt LND Mainnet: systemd unit for getpublicip.sh script
# /etc/systemd/system/20-raspibolt-welcome.sh
# make executable and copy script to /etc/update-motd.d/
2018-07-29 01:33:54 +02:00
# root must be able to execute network cli and lncli
2018-07-17 13:12:03 +02:00
# set colors
color_red='\033[0;31m'
color_green='\033[0;32m'
color_yellow='\033[0;33m'
color_gray='\033[0;37m'
2018-07-29 01:33:54 +02:00
# load network
network=`sudo cat /home/admin/.network`
2018-07-17 13:12:03 +02:00
# set datadir
2018-07-29 01:33:54 +02:00
bitcoin_dir="/home/bitcoin/.${network}"
2018-07-17 13:12:03 +02:00
lnd_dir="/home/bitcoin/.lnd"
# get uptime & load
load=$(w | grep "load average:" | cut -c11-)
# get CPU temp
cpu=$(cat /sys/class/thermal/thermal_zone0/temp)
temp=$((cpu/1000))
# get memory
ram_avail=$(free -m | grep Mem | awk '{ print $7 }')
ram=$(printf "%sM / %sM" "${ram_avail}" "$(free -m | grep Mem | awk '{ print $2 }')")
if [ ${ram_avail} -lt 100 ]; then
color_ram="${color_red}\e[7m"
else
color_ram=${color_green}
fi
# get storage
sd_free_ratio=$(printf "%d" "$(df -h | grep "/$" | awk '{ print $4/$2*100 }')") 2>/dev/null
sd=$(printf "%s (%s%%)" "$(df -h | grep '/$' | awk '{ print $4 }')" "${sd_free_ratio}")
if [ ${sd_free_ratio} -lt 10 ]; then
color_sd="${color_red}"
else
color_sd=${color_green}
fi
hdd_free_ratio=$(printf "%d" "$(df -h | grep '/mnt/hdd$' | awk '{ print $4/$2*100 }')") 2>/dev/null
hdd=$(printf "%s (%s%%)" "$(df -h | grep '/mnt/hdd$' | awk '{ print $4 }')" "${hdd_free_ratio}")
if [ ${hdd_free_ratio} -lt 10 ]; then
color_hdd="${color_red}\e[7m"
else
color_hdd=${color_green}
fi
# get network traffic
network_rx=$(ifconfig eth0 | grep 'RX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
network_tx=$(ifconfig eth0 | grep 'TX packets' | awk '{ print $6$7 }' | sed 's/[()]//g')
# Bitcoin blockchain
2018-07-29 01:33:54 +02:00
btc_path=$(command -v ${network}-cli)
2018-07-17 13:12:03 +02:00
if [ -n ${btc_path} ]; then
2018-07-29 01:33:54 +02:00
btc_title=$network
chain="$(${network}-cli -datadir=${bitcoin_dir} getblockchaininfo | jq -r '.chain')"
2018-07-17 13:12:03 +02:00
if [ -n $chain ]; then
btc_title="${btc_title} (${chain}net)"
# get sync status
2018-07-29 01:33:54 +02:00
block_chain="$(${network}-cli -datadir=${bitcoin_dir} getblockcount)"
block_verified="$(${network}-cli -datadir=${bitcoin_dir} getblockchaininfo | jq -r '.blocks')"
2018-07-17 13:12:03 +02:00
block_diff=$(expr ${block_chain} - ${block_verified})
2018-07-29 01:33:54 +02:00
progress="$(${network}-cli -datadir=${bitcoin_dir} getblockchaininfo | jq -r '.verificationprogress')"
2018-07-17 13:12:03 +02:00
sync_percentage=$(printf "%.2f%%" "$(echo $progress | awk '{print 100 * $1}')")
if [ ${block_diff} -eq 0 ]; then # fully synced
sync="OK"
sync_color="${color_green}"
sync_behind=" "
elif [ ${block_diff} -eq 1 ]; then # fully synced
sync="OK"
sync_color="${color_green}"
sync_behind="-1 block"
elif [ ${block_diff} -le 10 ]; then # <= 2 blocks behind
sync="catchup"
sync_color="${color_red}"
sync_behind="-${block_diff} blocks"
else
sync="progress"
sync_color="${color_red}"
sync_behind="${sync_percentage}"
fi
# get last known block
2018-07-29 01:33:54 +02:00
last_block="$(${network}-cli -datadir=${bitcoin_dir} getblockcount)"
2018-07-17 13:12:03 +02:00
if [ ! -z "${last_block}" ]; then
btc_line2="${btc_line2} ${color_gray}(block ${last_block})"
fi
# get mem pool transactions
2018-07-29 01:33:54 +02:00
mempool="$(${network}-cli -datadir=${bitcoin_dir} getmempoolinfo | jq -r '.size')"
2018-07-17 13:12:03 +02:00
else
btc_line2="${color_red}NOT RUNNING\t\t"
fi
fi
# get IP address & port
local_ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
public_ip=$(curl -s http://v4.ipv6-test.com/api/myip.php)
2018-07-29 01:33:54 +02:00
public_port=$(cat ${bitcoin_dir}/${network}.conf 2>/dev/null | grep port= | awk -F"=" '{print $2}')
2018-07-17 13:12:03 +02:00
if [ "${public_port}" = "" ]; then
if [ "${network}" = "litecoin" ]; then
if [ "${chain}" = "test" ]; then
public_port=19333
else
public_port=9333
fi
2018-07-17 13:12:03 +02:00
else
if [ "${chain}" = "test" ]; then
public_port=18333
else
public_port=8333
fi
2018-07-17 13:12:03 +02:00
fi
fi
2018-08-21 03:52:14 +02:00
# CHAIN NETWORK
2018-08-15 12:12:21 +02:00
public_addr="??"
torInfo=""
2018-08-22 12:32:10 +02:00
# Version
networkVersion=$(${network}-cli -datadir=${bitcoin_dir} -version | cut -d ' ' -f6)
2018-08-15 12:12:21 +02:00
# TOR or IP
onionAddress=$(${network}-cli -datadir=${bitcoin_dir} getnetworkinfo | grep '"address"' | cut -d '"' -f4)
if [ ${#onionAddress} -gt 0 ]; then
# TOR address
public_addr="${onionAddress}:${public_port}"
public=""
2018-07-17 13:12:03 +02:00
public_color="${color_green}"
2018-08-15 12:12:21 +02:00
torInfo="+ TOR"
2018-07-17 13:12:03 +02:00
else
2018-08-15 12:12:21 +02:00
# IP address
public_addr="${public_ip}:${public_port}"
public_check=$(timeout 2s nc -z ${public_ip} ${public_port}; echo $?)
if [ $public_check = "0" ]; then
public="Yes"
public_color="${color_green}"
else
public="Not reachable"
public_color="${color_red}"
fi
2018-07-17 13:12:03 +02:00
fi
2018-08-22 12:32:10 +02:00
#IP
2018-08-21 03:52:14 +02:00
# LIGHTNING NETWORK
2018-08-22 12:32:10 +02:00
2018-08-22 13:52:16 +02:00
ln_baseInfo="-"
ln_channelInfo="-"
ln_external=""
2018-08-21 03:52:14 +02:00
2018-08-22 12:54:11 +02:00
wallet_unlocked=$(sudo tail -n 1 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log | grep -c unlock)
if [ "$wallet_unlocked" -gt 0 ] ; then
2018-07-17 13:12:03 +02:00
alias_color="${color_red}"
ln_alias="Wallet Locked"
else
2018-08-22 13:52:16 +02:00
ln_getInfo=$(/usr/local/bin/lncli --macaroonpath=${lnd_dir}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert getinfo)
ln_external=$(echo "${ln_getInfo}" | grep "uris" -A 2 | tr -d '\n' | cut -d '"' -f4)
2018-07-17 13:12:03 +02:00
alias_color="${color_grey}"
2018-08-22 12:56:17 +02:00
ln_alias=$(echo "${ln_getInfo}" | grep "alias" | cut -d '"' -f4)
2018-08-22 13:52:16 +02:00
ln_sync=$(echo "${ln_getInfo}" | grep "synced_to_chain" | grep "true" -c)
if [ ${ln_sync} -eq 0 ]; then
2018-08-22 14:23:14 +02:00
item=$(sudo -u bitcoin tail -n 100 /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log | grep height | tail -n1 | awk '{print $9} {print $10} {print $11} {print $12}' | tr -dc '0-9')
total=$(sudo -u bitcoin ${network}-cli -datadir=/home/bitcoin/.${network} getblockchaininfo | jq -r '.blocks')
2018-08-22 13:52:16 +02:00
ln_baseInfo="${color_red} waiting for chain sync"
2018-08-22 14:23:14 +02:00
ln_channelInfo="${item}/${total}"
2018-08-22 13:52:16 +02:00
else
ln_walletbalance="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert walletbalance | jq -r '.confirmed_balance')" 2>/dev/null
ln_channelbalance="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert channelbalance | jq -r '.balance')" 2>/dev/null
ln_channels_online="$(echo "${ln_getInfo}" | jq -r '.num_active_channels')" 2>/dev/null
ln_channels_total="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert listchannels | jq '.[] | length')" 2>/dev/null
ln_baseInfo="${color_gray}wallet ${ln_walletbalance} sat"
ln_channelInfo="${ln_channels_online}/${ln_channels_total} Channels ${ln_channelbalance} sat"
fi
2018-08-22 12:32:10 +02:00
fi
2018-07-17 13:12:03 +02:00
2018-08-15 12:12:21 +02:00
sleep 5
printf "
2018-07-17 13:12:03 +02:00
${color_yellow}
${color_yellow}
${color_yellow}
${color_yellow} ${color_yellow}%s ${color_green} ${ln_alias}
2018-08-15 12:12:21 +02:00
${color_yellow} ${color_gray}${network} Fullnode + Lightning Network ${torInfo}
2018-08-22 13:01:11 +02:00
${color_yellow} ,/ ${color_yellow}%s
${color_yellow} ,'/ ${color_gray}%s, CPU %s°C
${color_yellow} ,' / ${color_gray}Free Mem ${color_ram}${ram} ${color_gray} Free HDD ${color_hdd}%s
${color_yellow} ,' /_____, ${color_gray}Local ${color_green}${local_ip}${color_gray}${network_rx}${network_tx}
${color_yellow} .'____ ,' ${color_gray}
${color_yellow} / ,' ${color_gray}${network} ${color_green}${networkVersion} ${chain}net ${color_gray}Sync ${sync_color}${sync} (%s)
${color_yellow} / ,' ${color_gray}Public ${public_color}${public_addr} ${public}
${color_yellow} /,' ${color_gray}
${color_yellow} /' ${color_gray}LND ${color_green}v0.4.2 ${ln_baseInfo}
${color_yellow} ${color_gray}${ln_channelInfo}
2018-08-22 14:26:09 +02:00
${color_yellow}
2018-08-21 03:52:14 +02:00
${color_yellow}${ln_external}
2018-07-17 13:12:03 +02:00
" \
2018-08-22 12:32:10 +02:00
"RaspiBlitz v0.8" \
2018-07-17 13:12:03 +02:00
"-------------------------------------------" \
"${load##up*, }" "${temp}" \
"${hdd}" "${sync_percentage}"