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)