From 4d9a7e2cabc970d65ba12cf446236c6499ff77a0 Mon Sep 17 00:00:00 2001 From: Nils Date: Tue, 27 Mar 2018 11:36:14 +0200 Subject: [PATCH 1/9] Zcash Flypool Hashrate Munin plugin to monitor your zcash.flypool.org hashrate. --- .../currency/zcash/zcash_flypool_hashrate_ | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 plugins/currency/zcash/zcash_flypool_hashrate_ diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ new file mode 100755 index 00000000..d11fb7fb --- /dev/null +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -0,0 +1,113 @@ +#!/usr/bin/env python + +""" +=head1 NAME + +zcash_flypool_hashrate_ - Munin plugin to monitor your zcash.flypool.org hashrate (H/s) + +=head1 APPLICABLE SYSTEMS + +All systems with "python" and "munin" + +=head1 CONFIGURATION + +zcash_flypool_hashrate__ + +=head1 SYNOPSIS + + ln -s /usr/share/munin/plugins/zcash_flypool_hashrate_ \ + /etc/munin/plugins/zcash_flypool_hashrate_t1gMVWjGhdjvb71UU11JDrFmiZhgUf4x5TH_mine + +=head1 INTERPRETATION + +This plugin shows the zcash.flypool.org mining pool hashrate (H/s) of a given Zcasg address and rig name. +Hashrate is queried via Flypool API L. + +=head1 VERSION + +0.0.1 + +=head1 AUTHOR + +L + +=head1 LICENSE + +GPLv2 + +=head1 MAGIC MARKERS + + #%# family=manual + +=cut +""" + +from __future__ import print_function + +import sys +import json +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: + zcash_address, miner = sys.argv[0].split("_")[3:] +except ValueError: + print("The filename of this plugin (or its symlink) should follow this pattern: " + "'zcash_flypool_hashrate__'", file=sys.stderr) + sys.exit(9) + +if command == 'config': + print("graph_title Flypool {}".format(miner)) + print("graph_info zcash.flypool.org Mining Pool Hashrate for {}_{}".format(zcash_address, miner)) + print("graph_vlabel Flypool Hashrate") + print("graph_category other") + print("flypool_hs_{}_{}.warning 200:".format(zcash_address, miner)) + print("flypool_hs_{}_{}.critical 100:".format(zcash_address, miner)) + print("flypool_hs_{}_{}.label H/s:".format(zcash_address, miner)) + sys.exit(0) + + +flypool_api_url = 'https://api-zcash.flypool.org/miner/' + zcash_address + '/worker/' + miner + '/currentStats' + +mining_req = Request(flypool_api_url) +# User-Agent to bypass Cloudflare +mining_req.add_header('User-Agent', 'Flypool Munin Plugin/1.0') + +try: + mining_stats_raw = urlopen(mining_req, timeout=15) +except IOError as exc: + print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) + +reader = codecs.getreader("utf-8") + +try: + mining_stats = json.load(reader(mining_stats_raw)) +except ValueError: + print("Failed to parse JSON responce.", file=sys.stderr) + sys.exit(9) + +try: + worker = mining_stats['data'] +except: + print("JSON result error!", file=sys.stderr) + sys.exit(9) + +try: + hash_rate = worker['currentHashrate'] +except: + print("No current Hashrate!", file=sys.stderr) + sys.exit(9) + +print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) \ No newline at end of file From 21341b5a00facd4c5e4212c8e1050cc398b5a569 Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 29 Mar 2018 08:11:59 +0200 Subject: [PATCH 2/9] python3 only --- .../currency/zcash/zcash_flypool_hashrate_ | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index d11fb7fb..fbcd4c10 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ =head1 NAME @@ -7,7 +7,7 @@ zcash_flypool_hashrate_ - Munin plugin to monitor your zcash.flypool.org hashrat =head1 APPLICABLE SYSTEMS -All systems with "python" and "munin" +All systems with "python3" and "munin" =head1 CONFIGURATION @@ -42,20 +42,11 @@ GPLv2 =cut """ -from __future__ import print_function - import sys import json 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 +from urllib.request import urlopen +from urllib.request import Request command = '' if len(sys.argv) > 1: @@ -110,4 +101,4 @@ except: print("No current Hashrate!", file=sys.stderr) sys.exit(9) -print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) \ No newline at end of file +print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) From f27fc67ccf7e0ef3978e061bc7f3d4586b7919de Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 29 Mar 2018 08:37:53 +0200 Subject: [PATCH 3/9] reader removed --- plugins/currency/zcash/zcash_flypool_hashrate_ | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index fbcd4c10..06bb397d 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -81,10 +81,9 @@ try: except IOError as exc: print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) -reader = codecs.getreader("utf-8") try: - mining_stats = json.load(reader(mining_stats_raw)) + mining_stats = json.load(mining_stats_raw) except ValueError: print("Failed to parse JSON responce.", file=sys.stderr) sys.exit(9) From d809d138b1b31c8e85de4830b85788ded8b6e65a Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 29 Mar 2018 08:39:20 +0200 Subject: [PATCH 4/9] exit if api failed --- plugins/currency/zcash/zcash_flypool_hashrate_ | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index 06bb397d..390ec069 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -80,7 +80,7 @@ try: mining_stats_raw = urlopen(mining_req, timeout=15) except IOError as exc: print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) - + sys.exit(9) try: mining_stats = json.load(mining_stats_raw) From 727fc7180de95558f4a0f326ac4149e5e6676545 Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 29 Mar 2018 08:41:32 +0200 Subject: [PATCH 5/9] better except --- plugins/currency/zcash/zcash_flypool_hashrate_ | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index 390ec069..a9cba304 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -90,13 +90,13 @@ except ValueError: try: worker = mining_stats['data'] -except: +except (KeyError, TypeError): print("JSON result error!", file=sys.stderr) sys.exit(9) try: hash_rate = worker['currentHashrate'] -except: +except (KeyError, TypeError): print("No current Hashrate!", file=sys.stderr) sys.exit(9) From e2db693c0da7d55dcb8a6b3f7a6021293e6ae138 Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 29 Mar 2018 08:50:21 +0200 Subject: [PATCH 6/9] codecs.getreader ("utf-8") added again without python3.4 fails --- plugins/currency/zcash/zcash_flypool_hashrate_ | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index a9cba304..609c1aa9 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -82,8 +82,10 @@ except IOError as exc: print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) sys.exit(9) +reader = codecs.getreader("utf-8") + try: - mining_stats = json.load(mining_stats_raw) + mining_stats = json.load(reader(mining_stats_raw)) except ValueError: print("Failed to parse JSON responce.", file=sys.stderr) sys.exit(9) From 8a72386a3037681c11fe266fa5181d3a52e00dbe Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 30 Mar 2018 09:58:14 +0200 Subject: [PATCH 7/9] print magic value U if error --- .../currency/zcash/zcash_flypool_hashrate_ | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index 609c1aa9..4eec991d 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -82,24 +82,25 @@ except IOError as exc: print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) sys.exit(9) +# manage codec and error handling lookup process reader = codecs.getreader("utf-8") try: mining_stats = json.load(reader(mining_stats_raw)) except ValueError: print("Failed to parse JSON responce.", file=sys.stderr) - sys.exit(9) - -try: - worker = mining_stats['data'] -except (KeyError, TypeError): - print("JSON result error!", file=sys.stderr) - sys.exit(9) - -try: - hash_rate = worker['currentHashrate'] -except (KeyError, TypeError): - print("No current Hashrate!", file=sys.stderr) - sys.exit(9) - -print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) + print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) +else: + try: + worker = mining_stats['data'] + except (KeyError, TypeError): + print("JSON result error!", file=sys.stderr) + print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) + else: + try: + hash_rate = worker['currentHashrate'] + except (KeyError, TypeError): + print("No current Hashrate!", file=sys.stderr) + print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) + else: + print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) From 6ff34af50ebfa3ccc4a0cfa46f9a73cec363855b Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 30 Mar 2018 14:16:57 +0200 Subject: [PATCH 8/9] set U as default --- plugins/currency/zcash/zcash_flypool_hashrate_ | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index 4eec991d..079f7198 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -85,22 +85,21 @@ except IOError as exc: # manage codec and error handling lookup process reader = codecs.getreader("utf-8") +hash_rate = "U" + try: mining_stats = json.load(reader(mining_stats_raw)) except ValueError: print("Failed to parse JSON responce.", file=sys.stderr) - print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) else: try: worker = mining_stats['data'] except (KeyError, TypeError): print("JSON result error!", file=sys.stderr) - print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) else: try: hash_rate = worker['currentHashrate'] except (KeyError, TypeError): print("No current Hashrate!", file=sys.stderr) - print("flypool_hs_{}_{}.value U".format(zcash_address, miner)) - else: - print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) + +print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate)) From 6a0a0c8d58544ec2969282df4e31bde5f78ce137 Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 30 Mar 2018 20:56:12 +0200 Subject: [PATCH 9/9] use json.loadS --- plugins/currency/zcash/zcash_flypool_hashrate_ | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/plugins/currency/zcash/zcash_flypool_hashrate_ b/plugins/currency/zcash/zcash_flypool_hashrate_ index 079f7198..8db6effc 100755 --- a/plugins/currency/zcash/zcash_flypool_hashrate_ +++ b/plugins/currency/zcash/zcash_flypool_hashrate_ @@ -82,13 +82,10 @@ except IOError as exc: print("Failed to request Flypool API: {}".format(exc), file=sys.stderr) sys.exit(9) -# manage codec and error handling lookup process -reader = codecs.getreader("utf-8") - hash_rate = "U" try: - mining_stats = json.load(reader(mining_stats_raw)) + mining_stats = json.loads(mining_stats_raw.read().decode("utf-8")) except ValueError: print("Failed to parse JSON responce.", file=sys.stderr) else: