Projet

Général

Profil

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

root / plugins / currency / ethereum / etherscan_balance_ @ 3298c34c

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

1
#!/usr/bin/env python
2

    
3
"""
4
=head1 NAME
5

    
6
etherscan_balance_ - Munin plugin to monitor your ethereum (ETH) balance
7

    
8
=head1 APPLICABLE SYSTEMS
9

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

    
12
=head1 CONFIGURATION
13

    
14
etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>
15

    
16
=head1 SYNOPSIS
17

    
18
  ln -s /usr/share/munin/plugins/etherscan_balance_ \
19
  /etc/munin/plugins/etherscan_balance_0x3257bde8cf067ae6f1ddc0e4b140fe02e3c5e44f
20

    
21
=head1 INTERPRETATION
22

    
23
This plugin shows the ether balance (ETH) of a given ethereum address.
24
Account balance is queried via etherscan.io API L<https://etherscan.io/apis>.
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
from __future__ import print_function
46

    
47
import sys
48
import json
49
import codecs
50

    
51
try:
52
    # python3
53
    from urllib.request import urlopen
54
    from urllib.request import Request
55
except ImportError:
56
    # python2
57
    from urllib2 import urlopen
58
    from urllib2 import Request
59

    
60
command = ''
61
if len(sys.argv) > 1:
62
    command = sys.argv[1]
63

    
64

    
65
api_action, eth_address = sys.argv[0].split("_")[1:]
66

    
67
if not eth_address:
68
    print("The filename of this plugin (or its symlink) should follow this pattern: "
69
          "'etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>'", file=sys.stderr)
70
    sys.exit(9)
71

    
72
"""
73
API result is in Wei. Convert Wei to Ether (ETH) via 'fieldname.cdef'
74
1     : Wei
75
10^12 : Szabo
76
10^15 : Finney
77
10^18 : Ether
78
233874700000000000000000 Wei = 233,874.7 Ether
79
"""
80
if command == 'config':
81
    print("graph_title ETH {}".format(eth_address))
82
    print("graph_info Ethereum Account Balance for Address {}".format(eth_address))
83
    print("graph_vlabel Ethereum Balance")
84
    print("graph_category other")
85
    print("wei_balance_{0}.cdef wei_balance_{0},1000000000000000000,/".format(eth_address))
86
    print("wei_balance_{}.label ETH".format(eth_address))
87
    sys.exit(0)
88

    
89
ethercan_balance_api_url = 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&address=' + eth_address
90

    
91
etherscan_req = Request(ethercan_balance_api_url)
92
# User-Agent to bypass Cloudflare
93
etherscan_req.add_header('User-Agent', 'Etherscan Munin Plugin/1.0')
94

    
95
try:
96
    etherscan_balance_raw = urlopen(etherscan_req, timeout=15)
97
except IOError as exc:
98
    print("Failed to request etherscan.io API: {}".format(exc), file=sys.stderr)
99
    sys.exit(9)
100

    
101
reader = codecs.getreader("utf-8")
102

    
103
try:
104
    etherscan_balance = json.load(reader(etherscan_balance_raw))
105
except ValueError:
106
    # Error in decoding operation or in JSON parsing throw a ValueError
107
    print("Failed to parse JSON response.", file=sys.stderr)
108
    sys.exit(9)
109

    
110
try:
111
    eth = int(etherscan_balance['result'])
112
except:
113
    print("JSON result error!", file=sys.stderr)
114
    sys.exit(9)
115

    
116
print("wei_balance_{}.value {}".format(eth_address, eth));