Skip to content

Instantly share code, notes, and snippets.

@r2DoesInc
Forked from jaydlawrence/check_hashrate_reboot.py
Last active February 27, 2018 23:29
Show Gist options
  • Save r2DoesInc/ac3a75cf22ab7318481ae4c4bbc3d46f to your computer and use it in GitHub Desktop.
Save r2DoesInc/ac3a75cf22ab7318481ae4c4bbc3d46f to your computer and use it in GitHub Desktop.
Script to check the hashrate of ethereum worker on ethOs and reboot if the hashrate is below the minimum for 2 runs of this script. This script is for any amount of gpus, or rather it will only reboot if the overall hashrate fallen below the specified hashrate. It also uses the pushover service to push notifications for when the script triggers …
import subprocess
import os
import re
import sys
import argparse
import httplib, urllib
import time
"""
# place this file at /home/ethos/check_hash_reboot.py
# The entries at the bottom of this comment block go into the crontab for the ethos user
"""
# parse the passed arguments
parser = argparse.ArgumentParser()
parser.add_argument(
'-f',
'--checkfilepath',
dest='check_file_path',
help="path to store temporary file at if reboot criteria is met, but we need to wait until next time this script runs, check if that file exists, criteria are till met and then reboot",
default="/tmp/reboot_conditions_met_last_time.txt"
)
args = parser.parse_args()
# call the update command from EthOs, which outputs current status, including hashrate
update_data = subprocess.check_output(['/opt/ethos/bin/update'])
hash = None
miner_id = ''
minimumHash = 160
# loop through the output of the update command, to parse the hashrate
for line in update_data.splitlines():
if 'hash: ' in line:
hash_line = line
hash_list = re.findall(r'\d+\.\d+', hash_line)
# if we don't get a 2 decimal number, then it is probably crashed
if len(hash_list) > 0:
hash = float(hash_list[0])
# store the hostname to add to the push notification
elif 'hostname: ' in line:
hostname_list = re.findall(r'\w+', line)
if len(hostname_list) > 1:
miner_id = hostname_list[1]
# debugging output
print time.ctime()
print hash
# start doing stuff if the hash is non-existant of less than 10
if not hash or hash < minimumHash:
print "hash is less than {}".format(minimumHash)
#criteria are met
#check if file exists, meaning that conditions were met last time
if os.path.isfile(args.check_file_path):
print 'file here'
os.remove(args.check_file_path)
# reboot the system
os.system("/opt/ethos/bin/r")
else:
print "making file {}".format(args.check_file_path)
# create a file so that next time we know if has been at state for a while
os.system('touch {}'.format(args.check_file_path))
else:
print "Hash is good"
# if the checkfile exists, remove it because the conditions are no longer met
if os.path.isfile(args.check_file_path):
os.remove(args.check_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment