Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / currency / zcash / zcash_flypool_hashrate_ @ 33e95e6f

Historique | Voir | Annoter | Télécharger (2,65 ko)

1
#!/usr/bin/env python3
2

    
3
"""
4
=head1 NAME
5

    
6
zcash_flypool_hashrate_ - Munin plugin to monitor your zcash.flypool.org hashrate (H/s)
7

    
8
=head1 APPLICABLE SYSTEMS
9

    
10
All systems with "python3" and "munin"
11

    
12
=head1 CONFIGURATION
13

    
14
zcash_flypool_hashrate_<YOUR_ZCASH_ADDRESS>_<YOUR_WORKER_NAME>
15

    
16
=head1 SYNOPSIS
17

    
18
  ln -s /usr/share/munin/plugins/zcash_flypool_hashrate_ \
19
  /etc/munin/plugins/zcash_flypool_hashrate_t1gMVWjGhdjvb71UU11JDrFmiZhgUf4x5TH_mine
20

    
21
=head1 INTERPRETATION
22

    
23
This plugin shows the zcash.flypool.org mining pool hashrate (H/s) of a given Zcasg address and rig name.
24
Hashrate is queried via Flypool API L<https://zcash.flypool.org>.
25

    
26
=head1 VERSION
27

    
28
0.0.1
29

    
30
=head1 AUTHOR
31

    
32
L<Nils Knieling|https://github.com/Cyclenerd>
33

    
34
=head1 LICENSE
35

    
36
GPLv2
37

    
38
=head1 MAGIC MARKERS
39

    
40
 #%# family=manual
41

    
42
=cut
43
"""
44

    
45
import sys
46
import json
47
import codecs
48
from urllib.request import urlopen
49
from urllib.request import Request
50

    
51
command = ''
52
if len(sys.argv) > 1:
53
    command = sys.argv[1]
54

    
55
try:
56
    zcash_address, miner = sys.argv[0].split("_")[3:]
57
except ValueError:
58
    print("The filename of this plugin (or its symlink) should follow this pattern: "
59
          "'zcash_flypool_hashrate_<YOUR_ZCASH_ADDRESS>_<YOUR_WORKER_NAME>'", file=sys.stderr)
60
    sys.exit(9)
61

    
62
if command == 'config':
63
    print("graph_title Flypool {}".format(miner))
64
    print("graph_info zcash.flypool.org Mining Pool Hashrate for {}_{}".format(zcash_address, miner))
65
    print("graph_vlabel Flypool Hashrate")
66
    print("graph_category htc")
67
    print("flypool_hs_{}_{}.warning 200:".format(zcash_address, miner))
68
    print("flypool_hs_{}_{}.critical 100:".format(zcash_address, miner))
69
    print("flypool_hs_{}_{}.label H/s:".format(zcash_address, miner))
70
    sys.exit(0)
71

    
72

    
73
flypool_api_url = 'https://api-zcash.flypool.org/miner/' + zcash_address + '/worker/' + miner + '/currentStats'
74

    
75
mining_req = Request(flypool_api_url)
76
# User-Agent to bypass Cloudflare
77
mining_req.add_header('User-Agent', 'Flypool Munin Plugin/1.0')
78

    
79
try:
80
    mining_stats_raw = urlopen(mining_req, timeout=15)
81
except IOError as exc:
82
    print("Failed to request Flypool API: {}".format(exc), file=sys.stderr)
83
    sys.exit(9)
84

    
85
hash_rate = "U"
86

    
87
try:
88
    mining_stats = json.loads(mining_stats_raw.read().decode("utf-8"))
89
except ValueError:
90
    print("Failed to parse JSON responce.", file=sys.stderr)
91
else:
92
    try:
93
        worker = mining_stats['data']
94
    except (KeyError, TypeError):
95
        print("JSON result error!", file=sys.stderr)
96
    else:
97
        try:
98
            hash_rate = worker['currentHashrate']
99
        except (KeyError, TypeError):
100
            print("No current Hashrate!", file=sys.stderr)
101

    
102
print("flypool_hs_{}_{}.value {}".format(zcash_address, miner, hash_rate))