root / plugins / currency / ethereum / etherscan_balance_ @ 2820f32d
Historique | Voir | Annoter | Télécharger (2,45 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 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 |
if command == 'config': |
| 73 |
print("graph_title ETH {}".format(eth_address))
|
| 74 |
print("graph_info Ethereum Account Balance for Address {}".format(eth_address))
|
| 75 |
print("graph_vlabel Ethereum Balance")
|
| 76 |
print("graph_category htc")
|
| 77 |
print("{}.label ETH".format(eth_address))
|
| 78 |
sys.exit(0) |
| 79 |
|
| 80 |
ethercan_balance_api_url = 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&address=' + eth_address |
| 81 |
|
| 82 |
etherscan_req = Request(ethercan_balance_api_url) |
| 83 |
# User-Agent to bypass Cloudflare |
| 84 |
etherscan_req.add_header('User-Agent', 'Etherscan Munin Plugin/1.0')
|
| 85 |
|
| 86 |
try: |
| 87 |
etherscan_balance_raw = urlopen(etherscan_req, timeout=15) |
| 88 |
except IOError as exc: |
| 89 |
print("Failed to request etherscan.io API: {}".format(exc), file=sys.stderr)
|
| 90 |
|
| 91 |
reader = codecs.getreader("utf-8")
|
| 92 |
|
| 93 |
try: |
| 94 |
etherscan_balance = json.load(reader(etherscan_balance_raw)) |
| 95 |
except ValueError: |
| 96 |
print("Failed to parse JSON response.", file=sys.stderr)
|
| 97 |
sys.exit(9) |
| 98 |
|
| 99 |
try: |
| 100 |
float(etherscan_balance['result']) |
| 101 |
except: |
| 102 |
print("JSON result error!", file=sys.stderr)
|
| 103 |
sys.exit(9) |
| 104 |
|
| 105 |
eth = float(etherscan_balance['result']) / 1000000000000000000 |
| 106 |
print("{}.value {:.2f}".format(eth_address, eth));
|
