unchartedworks
1/24/2018 - 8:50 AM

Reboot ethOS when any miner's hash rate is too low.

Reboot ethOS when any miner's hash rate is too low.

#!/usr/bin/env python3
import os

# 1. Save this file in /home/ethos/monitor.py on your ethOS
# 2. Change permission to make it executable
  # chmod 0755 /home/ethos/monitor.py
# 3. Run crontab
# # crontab -e
# 4. Schedule a cron task to execute every 5 minutes
"""
## runs this script every 5 minutes
 */5 * * * * /home/ethos/monitor.py
"""

def main():
    def should_reboot():
        def count_low_hashrate_miners():
            def miner_hashrates():
                def read_lastline(filename):
                    with open(filename) as f:
                        xs = f.readlines()
                        return xs[-1]

                def read_numbers(s):
                    xs = s.strip().split(' ')
                    return [float(x) for x in xs]

                filename = "/var/run/ethos/miner_hashes.file"
                return read_numbers(read_lastline(filename))

            def is_low_hashrate(x):
                low_hashrate_threshold = 10.0
                return x < low_hashrate_threshold

            def filter_list(f, xs):
                return list(filter(f, xs))

            return filter_list(is_low_hashrate, miner_hashrates())

        def has_bad_miners(hs):
            return len(hs) > 0

        return has_bad_miners(count_low_hashrate_miners())

    def reboot():
        print("rebooting...")
        os.system("/opt/ethos/bin/r")

    if should_reboot():
        reboot()

main()