2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/currency/ethereum/ethermine_hashrate_

114 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
2017-06-28 20:49:32 +02:00
"""
=head1 NAME
ethermine_hashrate_ - Munin plugin to monitor your ethermine.org hashrate (MH/s)
2017-06-28 20:55:55 +02:00
=head1 APPLICABLE SYSTEMS
All systems with "python" and "munin"
2017-06-28 20:49:32 +02:00
=head1 CONFIGURATION
ethermine_hashrate_<YOUR_PUBLIC_ETHEREUM_ADDRESS>_<YOUR_RIG_NAME>
=head1 SYNOPSIS
ln -s /usr/share/munin/plugins/ethermine_hashrate_ \
/etc/munin/plugins/ethermine_hashrate_3257bde8cf067ae6f1ddc0e4b140fe02e3c5e44f_mine
2017-06-28 20:55:55 +02:00
=head1 INTERPRETATION
2017-06-28 20:49:32 +02:00
This plugin shows the ethermine.org mining pool hashrate (MH/s) of a given ethereum address and rig name.
Hashrate is queried via ethermine.org API L<https://ethermine.org>.
=head1 VERSION
0.0.1
=head1 AUTHOR
L<Nils Knieling|https://github.com/Cyclenerd>
=head1 LICENSE
GPLv2
2017-06-28 20:55:55 +02:00
=head1 MAGIC MARKERS
#%# family=manual
2017-06-28 20:49:32 +02:00
=cut
"""
2017-06-27 21:35:36 +02:00
from __future__ import print_function
2017-06-27 21:21:41 +02:00
import sys
import json
2017-06-28 22:34:12 +02:00
import codecs
try:
# python3
from urllib.request import urlopen
from urllib.request import Request
except ImportError:
# python2
from urllib2 import urlopen
from urllib2 import Request
command = ''
if len(sys.argv) > 1:
command = sys.argv[1]
try:
2017-06-28 20:33:58 +02:00
eth_address, miner = sys.argv[0].split("_")[2:]
2017-06-27 21:35:36 +02:00
except ValueError:
print("The filename of this plugin (or its symlink) should follow this pattern: "
2017-06-27 22:25:05 +02:00
"'ethermine_hashrate_<YOUR_PUBLIC_ETHEREUM_ADDRESS>_<YOUR_RIG_NAME>'", file=sys.stderr)
2017-06-27 21:53:50 +02:00
sys.exit(9)
if command == 'config':
2017-06-28 22:34:12 +02:00
print("graph_title Ethermine {}".format(miner))
print("graph_info ethermine.org Mining Pool Hashrate for {}_{}".format(eth_address, miner))
2017-06-27 21:35:36 +02:00
print("graph_vlabel Ethermine Hashrate")
2017-06-30 08:55:51 +02:00
print("graph_category other")
2017-06-28 22:34:12 +02:00
print("{}_{}.warning 20:".format(eth_address, miner))
print("{}_{}.critical 10:".format(eth_address, miner))
print("{}_{}.label MH/s:".format(eth_address, miner))
sys.exit(0)
2017-06-27 21:53:50 +02:00
ethermine_api_url = 'https://ethermine.org/api/miner_new/' + eth_address
2017-06-28 22:34:12 +02:00
mining_req = Request(ethermine_api_url)
2017-06-28 20:33:58 +02:00
# User-Agent to bypass Cloudflare
mining_req.add_header('User-Agent', 'Ethermine Munin Plugin/1.0')
try:
2017-06-28 22:34:12 +02:00
mining_stats_raw = urlopen(mining_req, timeout=15)
2017-06-27 21:35:36 +02:00
except IOError as exc:
print("Failed to request ethermine.org API: {}".format(exc), file=sys.stderr)
2017-06-30 08:49:26 +02:00
sys.exit(9)
2017-06-28 22:34:12 +02:00
reader = codecs.getreader("utf-8")
try:
2017-06-28 22:34:12 +02:00
mining_stats = json.load(reader(mining_stats_raw))
2017-06-27 21:35:36 +02:00
except ValueError:
2017-07-01 09:34:50 +02:00
# Error in decoding operation or in JSON parsing throw a ValueError
2017-06-30 08:47:45 +02:00
print("Failed to parse JSON response.", file=sys.stderr)
sys.exit(9)
2017-06-28 20:33:58 +02:00
try:
workers = mining_stats['workers']
except:
2017-06-28 22:34:12 +02:00
print("JSON result error!", file=sys.stderr)
2017-06-28 20:33:58 +02:00
sys.exit(9)
# ethermine.org sometimes has caching errors. You can see data from other miner. Always check your rig name.
for worker in workers:
2017-06-27 21:53:50 +02:00
if workers[worker]['worker'] == miner:
hash_rate = workers[worker]['hashrate']
hash_rate = hash_rate.replace(" MH/s", "")
2017-06-28 22:34:12 +02:00
print("{}_{}.value {}".format(eth_address, miner, hash_rate))