Skip to content

Instantly share code, notes, and snippets.

@gaia
Last active September 15, 2019 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaia/bbc84914867b63907b729a0eafdeaf77 to your computer and use it in GitHub Desktop.
Save gaia/bbc84914867b63907b729a0eafdeaf77 to your computer and use it in GitHub Desktop.
Harmony (ONE): your node versus your the other nodes in your shard's average, in %, with alerts.
#!/usr/bin/env bash
# This script will not alert in the first few minutes (seconds_between_alerts)
# the first time it is run (e.g., shard$1-lastalert.txt does not exist)
# Also, it waits for $seconds_wait seconds before running for the first time (in case it is being run by cron)
# Usage: ./average.sh 3 one1xxxxxxxxxxxxxxxxxxxxxxxxxxxx
# That's the shard number followed by your address in that shard
# Optionally, if you just want the stats shown, use
# ./average.sh 3 one1xxxxxxxxxxxxxxxxxxxxxxxxxxxx status
# If your shell does recognize $EPOCHSECONDS (only available in Bash 5.0 or later)
# you can comment the next line out
EPOCHSECONDS=$(date +%s)
shard="$1"
address="$2"
if [[ -z $shard || -z $address ]]; then
echo "You did not supply a shard and/or an address."
exit 0
fi
BASEDIR="/home/ubuntu/scripts/harmony" # Do not use a trailing slash
alert_email="email@example.com"
seconds_wait="120" # number of seconds to wait after host booted to start checking
seconds_between_alerts="1200" # script will wait X seconds between re-sending an alert
uptime=$(cat /proc/uptime | sed 's/\..*$//' | tr -d '[:blank:]')
if [[ $uptime -lt $seconds_wait ]]; then
echo >&2 "Exiting, host uptime is less than $seconds_wait seconds"
exit 0
fi
if [ -f $BASEDIR/shard$1-lastalert.txt ]; then
lastalert=$(< $BASEDIR/shard$1-lastalert.txt)
else
echo $EPOCHSECONDS >| $BASEDIR/shard$1-lastalert.txt
lastalert=$(< $BASEDIR/shard$1-lastalert.txt)
fi
onehour=$(curl -s https://harmony.one/1h | tr -d '[:blank:]')
current=$(echo "$onehour" | grep "$address" | sed 's/.*://g')
if [ $current = "0" ]; then
echo "You earned 0 ONE in the last hour. Stopping."
exit 1
fi
average=$(echo "$onehour" | sed -n '/EARNING/,/NOTEARNING/p' | sed -n "/Shard$1/,/^$/p" | tail +3 | head -n-1 | sed 's/.*://g' | awk -v N=1 '{ sum += $N } END { if (NR > 0) print sum / NR }')
difference=$(python3 -c "print(round ((($current - $average) / $average) * 100, 4))")
if [[ $3 == "status" ]]; then
echo "Average: $average"
echo "Current: $current"
echo "Difference: $difference %"
exit 0
fi
average=$(python3 -c "print ($average*0.9)") # allow 90% of average before warning. This avoids too many warnings when you are below but close to the average.
if python3 -c "(isinstance($average, float))"; then
if python3 -c "exit(1 if $current >= $average else 0)"; then
let timesincelastalert=($EPOCHSECONDS - $lastalert)
echo "Amount of ONE earned on the 1h is less than the ONE other nodes' average."
if [ "$timesincelastalert" -gt "$seconds_between_alerts" ]; then
echo "Alerting at $(date)"
echo "Current: $current" | mail -s "ONE shard$1 below average $average. $difference %" "$alert_email"
echo $EPOCHSECONDS >| $BASEDIR/shard$1-lastalert.txt
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment