Projet

Général

Profil

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

root / plugins / currency / ethereum / etherscan_balance_ @ f5715e5e

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

1 6db40b77 Nils
#!/usr/bin/env python
2
3 f117dddc Nils
"""
4
=head1 NAME
5
6
etherscan_balance_ - Munin plugin to monitor your ethereum (ETH) balance
7
8 91ca1385 Nils
=head1 APPLICABLE SYSTEMS
9
10
All systems with "python" and "munin"
11
12 f117dddc Nils
=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 91ca1385 Nils
=head1 INTERPRETATION
22 f117dddc Nils
23 3298c34c Nils
This plugin shows the ether balance (ETH) of a given ethereum address.
24 f117dddc Nils
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 91ca1385 Nils
=head1 MAGIC MARKERS
39
40
 #%# family=manual
41
42 f117dddc Nils
=cut
43
"""
44 6db40b77 Nils
45 60c8aba7 Nils
from __future__ import print_function
46
47 b10d8677 Nils
import sys
48
import json
49 6373ccf7 Nils
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 6db40b77 Nils
60
command = ''
61
if len(sys.argv) > 1:
62
    command = sys.argv[1]
63
64 4f148679 Nils
65
api_action, eth_address = sys.argv[0].split("_")[1:]
66
67
if not eth_address:
68 60c8aba7 Nils
    print("The filename of this plugin (or its symlink) should follow this pattern: "
69 497e66dd Nils
          "'etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>'", file=sys.stderr)
70 6db40b77 Nils
    sys.exit(9)
71
72 3298c34c Nils
"""
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 6db40b77 Nils
if command == 'config':
81 f5715e5e Nils
    print("graph_title Ether {}".format(eth_address))
82 60c8aba7 Nils
    print("graph_info Ethereum Account Balance for Address {}".format(eth_address))
83 6373ccf7 Nils
    print("graph_vlabel Ethereum Balance")
84 311888d3 Nils
    print("graph_category other")
85 3298c34c Nils
    print("wei_balance_{0}.cdef wei_balance_{0},1000000000000000000,/".format(eth_address))
86
    print("wei_balance_{}.label ETH".format(eth_address))
87 6db40b77 Nils
    sys.exit(0)
88
89 60c8aba7 Nils
ethercan_balance_api_url = 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&address=' + eth_address
90 6db40b77 Nils
91 6373ccf7 Nils
etherscan_req = Request(ethercan_balance_api_url)
92 4f148679 Nils
# User-Agent to bypass Cloudflare
93
etherscan_req.add_header('User-Agent', 'Etherscan Munin Plugin/1.0')
94 6db40b77 Nils
95
try:
96 6373ccf7 Nils
    etherscan_balance_raw = urlopen(etherscan_req, timeout=15)
97 60c8aba7 Nils
except IOError as exc:
98
    print("Failed to request etherscan.io API: {}".format(exc), file=sys.stderr)
99 25db9a77 Nils
    sys.exit(9)
100 6db40b77 Nils
101 6373ccf7 Nils
reader = codecs.getreader("utf-8")
102
103 6db40b77 Nils
try:
104 6373ccf7 Nils
    etherscan_balance = json.load(reader(etherscan_balance_raw))
105 60c8aba7 Nils
except ValueError:
106 17a2e236 Nils
    # Error in decoding operation or in JSON parsing throw a ValueError
107 2820f32d Nils
    print("Failed to parse JSON response.", file=sys.stderr)
108 6db40b77 Nils
    sys.exit(9)
109
110
try:
111 3298c34c Nils
    eth = int(etherscan_balance['result'])
112 6db40b77 Nils
except:
113 6373ccf7 Nils
    print("JSON result error!", file=sys.stderr)
114 6db40b77 Nils
    sys.exit(9)
115 60c8aba7 Nils
116 3298c34c Nils
print("wei_balance_{}.value {}".format(eth_address, eth));