Hashrate monitoring with python, claymore and collectd


#1

Hi,

I wrote this little script that queries port 3333 of the Claymore mining software and gets the current hashrate. Very little work would be needed to export GPUs temperature and more informations on the mining status. It is written to work with collectd which in turn will push this data to an influxdb database, so it can be graphed by grafana.
Note that the ZenCash version of Claymore doesn’t seem to support JSON HTTP POST, so instead I extract the value from the plain HTML message… not clean but does the job.

#!/usr/bin/env python

import requests
import collectd
import re
import json

cfg = {
    'interval': '20',
    'url': 'http://localhost:3333'
}

def readconf(config):
    for node in config.children:
        for k in ['interval', 'url']:
            if node.key == k:
                cfg[k] = node.values[0]
                collectd.info('{0} set to: {1}'.format(k, cfg[k]))


def readvals(cons = False):

    if not cons:
        collectd.info('calling {0}'.format(cfg['url']))

    try:
        r = requests.get(cfg['url'])
        t = re.search('\{[^\}]+\}', r.text)
        j = json.loads(t.group(0))
    except ValueError, e:
        if not cons:
            collectd.info(str(e))
        else:
            print(str(e))
        return

    val = [
        {
            'k': 'reportedHashRate',
            'v': j['result'][2].split(';')[0],
            't': 'gauge' 
        },
    ]

    if cons:
        print(val)
        return

    for v in val:
        c = collectd.Values(type = v['t'])
        c.plugin = v['k']
        c.dispatch(values = [v['v']])


if __name__ == '__main__':
    readvals(True)
else:
    collectd.register_config(readconf)
    collectd.register_read(readvals, int(cfg['interval']))

collectd configuration goes like this:

    ModulePath "/home/imil/bin"
    Import "claymorestats"
    
        interval "20"
        url "http://127.0.0.1:3333/"
    

Enjoy


#2

Very cool! i’m not plugged into our mining ops, but image there are plenty of folks here who’ll find this useful. Thank you for sharing!


#3

My pleasure! here’s what it looks like after one day of graphing:


#4

awesome looks great!


#5

That’s ace, thanks for sharing!