2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/currency/ethereum/etherscan_balance_

118 lines
2.8 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
2017-06-28 20:49:32 +02:00
"""
=head1 NAME
etherscan_balance_ - Munin plugin to monitor your ethereum (ETH) balance
2017-06-28 20:55:55 +02:00
=head1 APPLICABLE SYSTEMS
All systems with "python" and "munin"
2017-06-28 20:49:32 +02:00
=head1 CONFIGURATION
etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>
=head1 SYNOPSIS
ln -s /usr/share/munin/plugins/etherscan_balance_ \
/etc/munin/plugins/etherscan_balance_0x3257bde8cf067ae6f1ddc0e4b140fe02e3c5e44f
2017-06-28 20:55:55 +02:00
=head1 INTERPRETATION
2017-06-28 20:49:32 +02:00
This plugin shows the ether balance (ETH) of a given ethereum address.
2017-06-28 20:49:32 +02:00
Account balance is queried via etherscan.io API L<https://etherscan.io/apis>.
=head1 VERSION
0.0.1
=head1 AUTHOR
L<Nils Knieling|https://github.com/Cyclenerd>
=head1 LICENSE
GPLv2
2017-06-28 20:55:55 +02:00
=head1 MAGIC MARKERS
#%# family=manual
2017-06-28 20:49:32 +02:00
=cut
"""
2017-06-27 21:53:16 +02:00
from __future__ import print_function
2017-06-27 21:21:41 +02:00
import sys
import json
2017-06-28 22:34:12 +02:00
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]
2017-06-28 20:33:58 +02:00
api_action, eth_address = sys.argv[0].split("_")[1:]
if not eth_address:
2017-06-27 21:53:16 +02:00
print("The filename of this plugin (or its symlink) should follow this pattern: "
2017-06-27 22:25:05 +02:00
"'etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>'", file=sys.stderr)
sys.exit(9)
"""
API result is in Wei. Convert Wei to Ether (ETH) via 'fieldname.cdef'
1 : Wei
10^12 : Szabo
10^15 : Finney
10^18 : Ether
233874700000000000000000 Wei = 233,874.7 Ether
"""
if command == 'config':
2017-07-01 17:41:06 +02:00
print("graph_title Ether {}".format(eth_address))
2017-06-27 21:53:16 +02:00
print("graph_info Ethereum Account Balance for Address {}".format(eth_address))
2017-06-28 22:34:12 +02:00
print("graph_vlabel Ethereum Balance")
print("graph_category other")
print("wei_balance_{0}.cdef wei_balance_{0},1000000000000000000,/".format(eth_address))
print("wei_balance_{}.label ETH".format(eth_address))
sys.exit(0)
ethercan_balance_api_url = ('https://api.etherscan.io/api'
'?module=account&action=balance&tag=latest&address=' + eth_address)
2017-06-28 22:34:12 +02:00
etherscan_req = Request(ethercan_balance_api_url)
2017-06-28 20:33:58 +02:00
# User-Agent to bypass Cloudflare
etherscan_req.add_header('User-Agent', 'Etherscan Munin Plugin/1.0')
try:
2017-06-28 22:34:12 +02:00
etherscan_balance_raw = urlopen(etherscan_req, timeout=15)
2017-06-27 21:53:16 +02:00
except IOError as exc:
print("Failed to request etherscan.io API: {}".format(exc), file=sys.stderr)
2017-06-30 08:49:26 +02:00
sys.exit(9)
2017-06-28 22:34:12 +02:00
reader = codecs.getreader("utf-8")
try:
2017-06-28 22:34:12 +02:00
etherscan_balance = json.load(reader(etherscan_balance_raw))
2017-06-27 21:53:16 +02:00
except ValueError:
2017-07-01 09:34:50 +02:00
# Error in decoding operation or in JSON parsing throw a ValueError
2017-06-30 08:47:45 +02:00
print("Failed to parse JSON response.", file=sys.stderr)
sys.exit(9)
try:
eth = int(etherscan_balance['result'])
except (KeyError, ValueError) as exc:
2017-06-28 22:34:12 +02:00
print("JSON result error!", file=sys.stderr)
sys.exit(9)
2017-06-27 21:53:16 +02:00
print("wei_balance_{}.value {}".format(eth_address, eth))