Skip to content

Instantly share code, notes, and snippets.

@Janaka-Steph
Forked from Stadicus/20-thundroid-welcome
Last active October 21, 2018 15:56
Show Gist options
  • Save Janaka-Steph/de83259627cf21eebbca3684bc8befb4 to your computer and use it in GitHub Desktop.
Save Janaka-Steph/de83259627cf21eebbca3684bc8befb4 to your computer and use it in GitHub Desktop.
Thundroid: System overview MotD
#!/bin/bash
# by Stadicus & JanakaSteph
# copy script to /etc/update-motd.d/ and make it executable (chmod +x)
# /etc/update-motd.d/20-thundroid-welcome
# root must be able to execute bitcoin-cli and lncli
# Set colors
###
color_red='\033[0;31m'
color_green='\033[0;32m'
color_yellow='\033[0;33m'
color_gray='\033[0;37m'
# Set datadir
###
bitcoin_dir="/home/bitcoin/.bitcoin"
lnd_dir="/home/bitcoin/.lnd"
lnd_dir2="/home/bitcoin/.lnd/data/chain/bitcoin/testnet"
# Get uptime, num of connected users, load average (1min, 5min, 15min)
###
# https://en.wikipedia.org/wiki/Load_(computing)
load=$(w | grep "load average:" | cut -c11-)
# Get memory (avail / total)
###
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
###
# To printf floating point numbers use %f specifier
# %.2f to limit decimal precision
sd_free_ratio=$(printf "%.2f" "$(df -h | grep "/$" | awk '{ print $4/$2*100 }')") 2>null
sd=$(printf "%s (%s%%)" "$(df -h | grep '/$' | awk '{ print $4 }')" "${sd_free_ratio}")
# bash does not natively handle floating point numbers
# use bc - An arbitrary precision calculator language
if (( $(echo "$sd_free_ratio < 10" | bc) )); then
color_sd="${color_red}"
else
color_sd=${color_green}
fi
hdd_free_ratio=$(printf "%.2f" "$(df -h | grep '/mnt/hdd$' | awk '{ print $4/$2*100 }')") 2>null
hdd=$(printf "%s (%s%%)" "$(df -h | grep '/mnt/hdd$' | awk '{ print $4 }')" "${hdd_free_ratio}")
if (( $(echo "$hdd_free_ratio < 10" | bc) )); 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
###
btc_path=$(command -v bitcoin-cli)
if [ -n "$btc_path" ]; then
btc_title="฿itcoin"
chain="$(bitcoin-cli -datadir=/home/bitcoin/.bitcoin getblockchaininfo | jq -r '.chain')"
if [ -n "$chain" ]; then
btc_title="${btc_title} (${chain}net)"
# get sync status
block_chain="$(bitcoin-cli -datadir=${bitcoin_dir} getblockcount)" # Number of blocks in the longest blockchain
block_verified="$(bitcoin-cli -datadir=${bitcoin_dir} getblockchaininfo | jq -r '.blocks')" # Current number of blocks processed in the server
block_diff=$(expr ${block_chain} - ${block_verified})
progress="$(bitcoin-cli -datadir=${bitcoin_dir} getblockchaininfo | jq -r '.verificationprogress')"
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="${block_chain} blocks"
elif [ ${block_diff} -eq 1 ]; then # fully synced
sync="OK"
sync_color="${color_green}"
sync_behind="1 block behind"
elif [ ${block_diff} -le 10 ]; then # <= 2 blocks behind
sync="Catching up"
sync_color="${color_red}"
sync_behind="${block_diff} blocks"
else
sync="In progress"
sync_color="${color_red}"
sync_behind="${sync_percentage}"
fi
# get mempool transactions
mempool="$(bitcoin-cli -datadir=${bitcoin_dir} getmempoolinfo | jq -r '.size')"
else
sync="NOT RUNNING"
sync_color="${color_red}"
fi
fi
# Get public IP address & port
###
#public_ip=$(curl -s ipinfo.io/ip)
# No-IP domain name
public_ip="thundroid1.ddns.net"
# Get public_port in config if available
###
public_port=$(cat ${bitcoin_dir}/bitcoin.conf 2>null | grep port= | awk -F"=" '{print $2}')
# if port not in config and node is running, apply default port
if [ -z "$public_port" ] && [ -n "$chain" ]; then
if [ "$chain" = "test" ]; then
public_port=18333
else
public_port=8333
fi
# check node availability
# https://github.com/Stadicus/guides/issues/81
# nc -z Specifies that nc (netcat) should just scan for listening daemons, without sending any data to them
public_check=$(nc -z -w 2 ${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
fi
public_addr="${public_ip}:${public_port}"
# LND
ln_alias="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir2}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert getinfo | jq -r '.alias')" 2>null
ln_channels_active="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir2}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert getinfo | jq -r '.num_active_channels')"
ln_channels_total="$(/usr/local/bin/lncli --macaroonpath=${lnd_dir2}/readonly.macaroon --tlscertpath=${lnd_dir}/tls.cert listchannels | jq '.[] | length')"
printf "
${color_yellow} ___________
${color_yellow} / ./ %s: ${color_gray}Bitcoin Core & Lightning
${color_yellow} / ./ %s
${color_yellow} / ./ ${color_gray}%s
${color_yellow} / ./___
${color_yellow} /____ ./ %-24s %-30s %-20s
${color_yellow} / ./ ${color_gray}Memory ${color_ram}%-16s${color_gray}Sync ${sync_color}%-20s${color_gray} %s
${color_yellow} / ./ ${color_gray}SD ${color_sd}%-16s${color_gray} %-20s %s / %s Channels online
${color_yellow} / ./ ${color_gray}HDD ${color_hdd}%-16s${color_gray}Public ${public_color}%-16s
${color_yellow} / ./ ${color_gray}Bandwith ↑ %-12s %s
${color_yellow} / / ${color_gray} ↓ %-12s Mempool ${mempool} tx
${color_yellow} /. %s
%s %s
" \
"Thundroid" \
"-----------------------------------------------------------------------" \
"${load}" \
"Ressources free" "${btc_title}" "Lightning (LND)" \
"${ram}" "${sync}" "${ln_alias}" \
"${sd}" "${sync_behind}" "${ln_channels_active}" "${ln_channels_total}" \
"${hdd}" "${public}" \
"${network_tx}" "${public_addr}" \
"${network_rx}" \
"-----------------------------------------------------------------------"
echo "$(tput -T xterm sgr0)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment