Skip to content

Instantly share code, notes, and snippets.

@mistakia
Last active March 6, 2018 23:48
Show Gist options
  • Save mistakia/0613390556649ac5e21f0ee5ed703bb7 to your computer and use it in GitHub Desktop.
Save mistakia/0613390556649ac5e21f0ee5ed703bb7 to your computer and use it in GitHub Desktop.
Python script to check Nanopool api (every 5 minutes) and restart machine when hashrate is 0.

Edit check.py and set your worker name, wallet address, email info, and google app password.

Make it executable

chmod +x check.py

Setup root's crontab (root needed to reboot)

sudo crontab -e

Set to run every 5 minutes

*/5 * * * * /home/user/check.py
#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
import os.path
import urllib2
from urllib2 import urlopen
import json
import os
email_address = '[email protected]'
gmail_user = '[email protected]'
gmail_password = 'xxx'
worker_name = 'xxx'
wallet_address = '0xxx'
nanopool_url = 'https://eth.nanopool.org/api/v1/hashrate/{}/{}'.format(wallet_address, worker_name)
def send_email(hashrate):
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
msg = MIMEText('Miner hashrate: {}MH/s'.format(hashrate))
msg['Subject'] = 'Restarted Miner: ' + worker_name
msg['From'] = gmail_user
msg['To'] = email_address
smtpserver.sendmail(gmail_user, [email_address], msg.as_string())
smtpserver.quit()
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' }
req = urllib2.Request(nanopool_url, None, headers)
html = urlopen(req).read()
json = json.loads(html)
hashrate = json['data']
if hashrate == 0:
send_email(json['data'])
os.system('/sbin/reboot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment