Monitor ethminer using Icinga2

Monitoring your mining rigs is very important - GPUs sometimes hang for no reason or power settings reset back to default.
The below script reads the JSON from ethminer and outputs it into a nagios-compatible format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import subprocess
from time import sleep
from argparse import ArgumentParser

result = []

parser = ArgumentParser(add_help=False)
parser.add_argument('-H', '--hostname', dest='hostname', metavar='ADDRESS', required=True, help="host name or IP address")
args = parser.parse_args()

def get_miner_info():

global version
global uptime
global hashrate
global hardware
global temperature_gpu
global power_usage
global fanspeed_gpu
global pool

cmd = '''echo '{"method": "miner_getstathr", "jsonrpc": "2.0", "id": 5 }' | timeout 2 nc ''' + str(args.hostname) + ' 8085'
s = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
rsp = s.communicate()[0]
data = json.loads(rsp)
version = data['result']['version']
uptime = data['result']['runtime']
hashrate = data['result']['ethhashrates']
power_usage = data['result']['powerusages']
temperature_gpu = data['result']['temperatures']
fanspeed_gpu = data['result']['fanpercentages']
pool = data['result']['pooladdrs']

def print_result():

problem = False

gpu = 0
for item in hashrate:
if int(item) < 20000:
problem = True
item = int(item) / 1000
tmp = "hashrate_gpu" + str(gpu) + "=" + str(item)
result.append(tmp)
gpu = gpu + 1

gpu = 0
for item in temperature_gpu:
if int(item) > 70:
problem = True
tmp = "temperature_gpu" + str(gpu) + "=" + str(item)
result.append(tmp)
gpu = gpu + 1

gpu = 0
for item in power_usage:
if int(item) > 120:
problem = True
tmp = "powerusage_gpu" + str(gpu) + "=" + str(item)
result.append(tmp)
gpu = gpu + 1

gpu = 0
for item in fanspeed_gpu:
tmp = "fanspeed_gpu" + str(gpu) + "=" + str(item)
result.append(tmp)
gpu = gpu + 1

item = pool
tmp = "pool=" + str(item)
result.append(tmp)

if problem == True:
print("Something is wrong with this rig |"),
print("version=" + str(version) + " uptime=" + str(uptime)),
for item in result:
print item,
sys.exit(1)
else:
print("This mining rig is operating in its specified parameters on " + str(pool) + " |"),
print("version=" + str(version) + " uptime=" + str(uptime)),
for item in result:
print item,
sys.exit(0)

get_miner_info()
print_result()