Created
October 24, 2016 13:04
-
-
Save roelandp/e2e4837241b7c51edc1b8a964f656dd9 to your computer and use it in GitHub Desktop.
Failover script (original by jesta) modified to use telegram to notify you.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
from piston.steem import Steem | |
from pprint import pprint | |
from steemtools.experimental import Transactions | |
# Steem connection and witness information | |
node = os.environ['steem_node'] | |
witness = os.environ['steem_account'] | |
wif = os.environ['steem_wif'] | |
telegram_token = os.environ['feed_telegram_token'] | |
telegram_id = os.environ['feed_telegram_id'] | |
# Get your telegram id with the bot: MyTelegramID_bot | |
# setup a telegram bot (for the token) with bot: BotFather | |
def telegram(method, params=None): | |
url = "https://api.telegram.org/bot"+telegram_token+"/" | |
params = params | |
r = requests.get(url+method, params = params).json() | |
return r | |
# Establish the connection to steem | |
steem = Steem(node=node, keys=[wif]) | |
# How many misses before we trigger the update, 0 for debugging (always trigger) | |
threshold = int(os.environ['threshold']) | |
# How often should we check for misses? (in seconds) | |
check_rate = int(os.environ['check_rate']) | |
# The signing key to swap to when the threshold is met | |
backup_key = os.environ['steem_backup'] | |
# Properties to set on the witness update | |
props = { | |
"account_creation_fee": os.environ['steem_account_creation_fee'], | |
"maximum_block_size": int(os.environ['steem_maximum_block_size']), | |
"sbd_interest_rate": int(os.environ['steem_sbd_interest_rate']), | |
} | |
witness_url = os.environ['steem_witness_url'] | |
# Check how many blocks a witness has missed | |
def check_witness(): | |
status = steem.rpc.get_witness_by_account(witness) | |
missed = status['total_missed'] | |
pprint("Missed = " + str(missed) + " | Failover if >= " + str(threshold) + " | Next Key: " + backup_key) | |
if missed >= threshold: | |
update_witness(witness, witness_url, backup_key, props) | |
# Update the witness to the new signing key | |
def update_witness(account, url, signing_key, props): | |
# Create the witness_update transaction and broadcast | |
t = Transactions(steem=steem) | |
tx = t.witness_update(witness, backup_key, witness_url, props, wif, sim_mode=False) | |
# Send SMS Notification of Failure | |
custom_keyboard = [["shit hit fan!"]] | |
reply_markup = json.dumps({"keyboard":custom_keyboard, "resize_keyboard": True}) | |
conf_msg = ("ALERT! ALERT! SWITCHED FROM WITNESS TO BACKUPNODE! GA DIE SHIT FIXEN") | |
payload = {"chat_id":telegram_id, "text":conf_msg, "reply_markup":reply_markup} | |
m = telegram("sendMessage", payload) | |
# Kill the script | |
raise SystemExit | |
# Main Loop | |
if __name__ == '__main__': | |
while True: | |
check_witness() | |
sys.stdout.flush() | |
time.sleep(check_rate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment