Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to display/email btcguild bitcoin miner progress on current round, and detect any idle workers
#!/usr/bin/python
# https://gist.github.com/gists/1007083
# Donate: 1BdWJEUCkm6on231iX2ThXZHXZDfQny69R
from __future__ import division
import json
import urllib2
from smtplib import SMTP
from email.mime.text import MIMEText
from cStringIO import StringIO
from pprint import pprint
API_URL = "http://www.btcguild.com/api.php?api_key=%s"
ALERT_EMAIL = "REPLACE"
IDLE_THRESHOLD = 600 # seconds
EMAIL_INFO = {
"host" : "smtp.gmail.com",
"port" : 587,
"user" : "REPLACE",
"passwd" : "REPLACE"
}
def send_email(email):
msg = MIMEText(email["body"])
msg["Subject"], msg["From"], msg["To"] = email["subject"], email["from"], email["to"]
server = SMTP(EMAIL_INFO["host"], EMAIL_INFO["port"])
server.ehlo()
server.starttls()
server.ehlo()
server.login(EMAIL_INFO["user"], EMAIL_INFO["passwd"])
server.sendmail(email["from"], [email["to"]], msg.as_string())
server.close()
def main(args):
if len(args) < 2:
print "usage: %s api_key" % args[0]
return
data = urllib2.urlopen(API_URL % args[1]).read()
if data[0] != '{':
print "ERROR", data
return
stdout = sys.stdout
sys.stdout = StringIO()
p = json.loads(data)
pool = p["pool"]
total = { "hash_rate" : 0, "round_shares" : 0 }
idles = []
for w in p["workers"].values():
last = map(int, w["last_share"].split(":"))
idle = last[0] * 3600 + last[1] * 60 + last[2]
total["hash_rate"] += w["hash_rate"]
total["round_shares"] += w["round_shares"]
percent_shares = 100*w["round_shares"]/pool["round_shares"]
print w["worker_name"], w["hash_rate"], "%.4f%%"%percent_shares, idle, "seconds"
if idle > IDLE_THRESHOLD:
idles.append((w["worker_name"], idle))
percent_shares = 100*total["round_shares"]/pool["round_shares"]
print "total", total["hash_rate"], "%.4f%%"%percent_shares
if idles:
pprint(idles)
output = sys.stdout.getvalue()
sys.stdout = stdout
print output
#send_email({
# "to" : ALERT_EMAIL,
# "from" : EMAIL_INFO["user"],
# "subject" : "btcguild report",
# "body" : output,
#})
if __name__ == '__main__':
import sys
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment