#!/bin/bash # https://www.majlovesreg.one/tag/code/ # Made for betanet, can be edited for other networks instances=$(pgrep -xc $(basename "$0")) [ "${instances}" ] && [ ${instances} -gt 1 ] && { echo "Multiple instances found, exiting..."; exit 0; } source $HOME/.profile export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion [ "$(pgrep -c near )" ] || { echo "No near binary running, exiting..."; exit 1; } ##### val_acct_id="$(cat ~/.near/betanet/validator_key.json | jq -r '.account_id')" val_pubkey="$(cat ~/.near/betanet/validator_key.json | jq -r '.public_key')" staking_pool_id="" wallet_acct_id="" chest_acct_id="" callparams='--nodeUrl http://127.0.0.1:3030 --helperUrl http://127.0.0.1:3030' subj="NEAR Alert on $(hostname)" recipients=",stefano@near.org" yocto=1000000000000000000000000 yoctoz=$(echo ${yocto} | tr -d '1') msg="alert(s) triggered on $(hostname) at $(date):\n\n" msg+=">>> Validator details\n" msg+="\n" msg+="account_id: ${val_acct_id}\n" msg+="validator public key: ${val_pubkey}\n" msg+="\n" msg+="staking_pool: @${staking_pool_id}\n" msg+="wallet_account: @${wallet_acct_id}\n" msg+="chest_account: @${chest_acct_id}\n" msg+="\n" alertlv=0 msg+="##### ALERTS #####\n" msg+="\n" ##### Check binaries ##### [ "$(which near | grep '\.nvm')" ] || { ((alertlv++)) msg+="* * * Unresolvable: near-cli could not be found * * *\n" msg+="\n" msg+="Check environment? Here is PATH: $(echo $PATH)\n" msg+="\n" } ##### Check if part of validators pool ##### if ! [ "$(near proposals ${callparams} | grep ${staking_pool_id})" ]; then ((alertlv++)) msg+="Staking pool ${staking_pool_id} not in the validators list! Will try to ping the contract...\n" near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} sleep 2 if [ "$(near proposals ${callparams} | grep ${staking_pool_id})" ]; then msg+="Success! The pool is now in the proposals list.\n" else msg+="Ping failed! The pool is still not in the proposals list. :-(\n" msg+="Check if NEAR network has issues? https://status.nearprotocol.com/\n" fi msg+="\n" fi # Not present in validator next if [ ! "$(near validators next ${callparams} | grep ${staking_pool_id})" ]; then near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} fi if [ "$(near validators next ${callparams} | grep ${staking_pool_id} | grep 'Kicked out')" -a ! "$(near proposals ${callparams} | grep ${staking_pool_id} | grep 'Accepted')" ]; then near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} fi ##### Stake amount ##### nowseatprice="$(near validators current ${callparams} | grep -o 'seat price: [0-9,]\+' | sed 's/seat price: //' | tr -d ',')" nextseatprice="$(near validators next ${callparams} | grep -o 'seat price: [0-9,]\+' | sed 's/seat price: //' | tr -d ',')" nextstake="$(( $(( nowseatprice < nextseatprice ? nowseatprice : nextseatprice )) * 2 - 2 ))" nowstake_yocto=$(near view ${staking_pool_id} get_account_staked_balance "{\"account_id\": \"${chest_acct_id}\"}" ${callparams} | grep -o '[0-9]\{24,\}') nowstake=${nowstake_yocto::-24} nowbalance_yocto=$(near view ${staking_pool_id} get_account_total_balance "{\"account_id\": \"${chest_acct_id}\"}" ${callparams} | grep -o '[0-9]\{24,\}') nowbalance=${nowbalance_yocto::-24} if (( nowbalance < nextseatprice )); then ((alertlv++)) msg+="* * * Not enough NEAR tokens for next validator seat * * *\n\n" msg+="Token details:\n" msg+="\n" msg+="Current seat price: $(numfmt --grouping ${nowseatprice})\n" msg+="Next seat price: $(numfmt --grouping ${nextseatprice})\n" msg+="\n" msg+="Current staked amount: $(numfmt --grouping ${nowstake})\n" msg+="Current balance total: $(numfmt --grouping ${nowbalance})\n" msg+="\n" msg+="Tokens needed for next epoch: $(numfmt --grouping $(( nextseatprice - nowbalance )))\n" msg+="\n" elif (( nowstake < nextstake )); then newstake="$(( nextstake < nowbalance ? nextstake : nowbalance - 2 ))" if (( newstake - nowstake > 0 )); then near call ${staking_pool_id} stake "{\"amount\": \"$(( newstake - nowstake ))${yoctoz}\"}" --accountId ${chest_acct_id} ${callparams} near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} fi elif (( nowstake > nextstake )); then newstake="$(( nextstake < nowbalance ? nextstake : nowbalance - 2 ))" if (( nowstake - newstake > 0 )); then near call ${staking_pool_id} unstake "{\"amount\": \"$(( nowstake - newstake ))${yoctoz}\"}" --accountId ${chest_acct_id} ${callparams} near call ${staking_pool_id} ping '{}' --accountId ${wallet_acct_id} ${callparams} fi fi ##### Disk free space ##### dsk="/dev/sda1" dpct=90 pctfree="$(df -h ${dsk} | grep ${dsk} | grep -o '[0-9]\+%' | tr -d '%')" if [ "${pctfree}" ] && (( ${pctfree} >= ${dpct} )); then ((alertlv++)) msg+="* * * Disk space alert * * *\n" msg+="\n" msg+="The server $(hostname) now uses up ${pctfree}% of diskspace on ${dsk}. Alert was set at ${dpct}%.\n" msg+="\n" msg+="$(df -h ${dsk} | head -n1)\n" msg+="$(df -h ${dsk} | grep ${dsk})\n" msg+="\n" fi ##### nearup logs ##### msg+="##### No other alerts #####\n" msg+="\n" msg+=">>> Latest logs\n" msg+="\n" msg+="$(nearup logs | tail -n10 | sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g')\n" ##### Plug ##### msg+="\n" msg+=">>> This a custom alert made using Bash script on a Linux server. If this was sent in error to your inbox, please contact https://github.com/majal. Have a good day!\n" msg+="\n" msg+="<<<<< $(date)\n" ##### Excute alerts ##### if (( alertlv > 0 )); then echo -e "${alertlv} ${msg}" | mail -s "${subj} (${alertlv})" "${recipients}" fi