Created
June 3, 2011 20:15
-
-
Save kylegibson/1007083 to your computer and use it in GitHub Desktop.
Script to display/email btcguild bitcoin miner progress on current round, and detect any idle workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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