Skip to content

Instantly share code, notes, and snippets.

@dreness dreness/
Last active Dec 21, 2015

What would you like to do?
Send email notifications of shares and blocks.
# -*- coding: utf-8 -*-
import time, os
import re
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
from datetime import datetime, timedelta
# Watch the bitcoin log, send notifications of shares and blocks, with
# current payout
# The bitcoin log to watch for interesting events
BTLOG = "/home/towelie/.bitcoin/p2pool/data/bitcoin/log"
# Log file to update with status and sent message notifications
ACTIVITY_LOG = "/home/towelie/watcher.log"
# The address to notify. If SMS, consider setting TERSE_OUTPUT to True below.
# Set a custom 'full name' email field. Doesn't seem to work with SMS.
# This is used as the argument for sendmail's -F option
# In terse mode, we use no message subject and a shorter body
# Max number of messages we'll send within 1 minute. If we go over, we assume
# something went crazy and we exit.
# Enable or disable test mode. In test mode, we'll process the log
# starting from the beginning, and send notifications until we hit the
# sanity check threshold, or until we reach the end of the file. This tests
# both delivery and possibly the sanity check.
# Customize the message prefix. Emojo might not work with SMS.
#MESSAGE_PREFIX = "btc-watcher: GOT"
# The regular expression pattern for parsing the current payout
PAYOUT_RE = '.* Current payout: (\S+) BTC'
# Store the current payout rate here (it's a float)
PAYOUT = 0.0
# Full status here
# Keep track of times we've sent email
SENT = []
# open the log file, get its size
file = open(BTLOG, 'r')
st_results = os.stat(BTLOG)
st_size = st_results[6]
# If we're not in testmode, seek to the end of the file
if not TEST_MODE:
# Also open our activity log for writing
activity = open(ACTIVITY_LOG,'a')
# The adjusted payout doesn't arrive in the log until after SHARE / BLOCKs
# are logged. Every time we get a share / block, we'll wait until the next
# payout rate arrives before sending the message.
pendingMessage = ''
### Functions
# Sanity check; have we sent more than MAX_PER_MINUTE in the last minute?
def overloaded():
global SENT
inRange = 0
now =
d = timedelta(minutes=1)
for x in SENT:
if x > now - d:
inRange += 1
if inRange > MAX_PER_MINUTE:
return True
return False
# Compose and send a message
def sendMessage(line, PAYOUT, STATUS):
global SENT, pendingMessage, MESSAGE_PREFIX
global TERSE_OUTPUT, EMADDRESS, activity
# Compose the message. Emoji doesn't seem to work via (some?) SMS
# Add the type of event and maybe some details
if ('SHARE' in line):
subject += "SHARE! [%.4f BTC]" % (PAYOUT,)
if ('BLOCK' in line):
subject += "BLOCK! [%.4f BTC]" % (PAYOUT,)
msg = MIMEText("%s\n%s" % (line,STATUS))
msg["Subject"] = subject
msg = MIMEText("%s\n" % (subject,))
msg["To"] = EMADDRESS
aLog("%s sending: %s to address %s\n" %
if overloaded(): # sanity check
print "exiting due to insane message rate!"
aLog("%s bailed due to insane message rate!\n" % (stamp,))
# shell out to sendmail, passing the message on stdin
cmd = ['/usr/sbin/sendmail', '-F', EMAIL_FROM, '-t']
aLog("%s\n" % (' '.join(cmd)))
p = Popen(cmd, stdin=PIPE)
if p.returncode: # non-zero return code indicates error
aLog("sendmail exited abnormally, bailing! code %r" % (p.returncode))
# Turn off the pendingMessage indicator
pendingMessage = ''
# Store a current timestamp, for rate-limiting sanity checks
# Truncate the list of timestamps so it doens't grow without bound
# Remove from the front of the list, since new ones appear at the end
if len(SENT) > 50:
SENT = SENT[20:]
def aLog(s):
global activity
### Main
# Loop forever
while 1:
# get the current offset in the file (i.e. current location)
where = file.tell()
# get a line
line = file.readline()
# Make a timestamp for our activity log
stamp = time.strftime("%m-%d-%Y %H:%M:%S", time.localtime())
# if we got nothing, sleep for a bit, reset position, exit the loop early
if (not line):
aLog("%s Current payout: %f\n" % (stamp,PAYOUT,))
# We got a line. Process it.
# Get rid of the time / date stamp (first two fields)
line = ' '.join(line.split()[2:])
# Keep track of current status and payout info
if ('Current' in line):
m =, line)
if (m):
PAYOUT = float(
STATUS = line
if (pendingMessage != ''):
sendMessage(pendingMessage, PAYOUT, STATUS)
# Something cool happened!
if ('GOT' in line):
pendingMessage = line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.