2017-06-27 19:06:23 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
#
|
|
|
|
# etherscan_balance_
|
|
|
|
#
|
|
|
|
# Munin plugin to monitor your ethereum (ETH) balance.
|
|
|
|
# Account balance is queried via etherscan.io API (https://etherscan.io/apis).
|
|
|
|
#
|
|
|
|
# Author: Nils Knieling - https://github.com/Cyclenerd
|
|
|
|
# Licence: GPLv2
|
|
|
|
#
|
|
|
|
# USAGE
|
|
|
|
# etherscan_balance_<YOUR_PUBLIC_ETHEREUM_ADDRESS>
|
|
|
|
#
|
|
|
|
# EXAMPLE
|
|
|
|
# ln -s /usr/share/munin/plugins/etherscan_balance_ /etc/munin/plugins/etherscan_balance_0x3257bde8cf067ae6f1ddc0e4b140fe02e3c5e44f
|
|
|
|
#
|
|
|
|
|
2017-06-27 21:53:16 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-06-27 21:21:41 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
import socket
|
|
|
|
import json
|
2017-06-27 19:06:23 +02:00
|
|
|
|
|
|
|
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)
|
2017-06-27 19:06:23 +02:00
|
|
|
sys.exit(9)
|
|
|
|
|
|
|
|
if command == 'config':
|
2017-06-27 22:25:05 +02:00
|
|
|
print("graph_title ETH {}".format(eth_address))
|
2017-06-27 21:53:16 +02:00
|
|
|
print("graph_title Ethereum Address {}".format(eth_address))
|
|
|
|
print("graph_info Ethereum Account Balance for Address {}".format(eth_address))
|
|
|
|
print("graph_title Ethereum Balance")
|
|
|
|
print("graph_title htc")
|
2017-06-28 20:33:58 +02:00
|
|
|
print("{}.label ETH".format(eth_address))
|
2017-06-27 19:06:23 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-06-27 21:53:16 +02:00
|
|
|
ethercan_balance_api_url = 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&address=' + eth_address
|
2017-06-27 19:06:23 +02:00
|
|
|
|
2017-06-27 21:53:16 +02:00
|
|
|
etherscan_req = urllib2.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')
|
2017-06-27 19:06:23 +02:00
|
|
|
|
|
|
|
try:
|
2017-06-27 21:53:16 +02:00
|
|
|
etherscan_balance_raw = urllib2.urlopen(etherscan_req, timeout=1.5 )
|
|
|
|
except IOError as exc:
|
|
|
|
print("Failed to request etherscan.io API: {}".format(exc), file=sys.stderr)
|
2017-06-27 19:06:23 +02:00
|
|
|
|
|
|
|
try:
|
2017-06-27 21:53:16 +02:00
|
|
|
etherscan_balance = json.load(etherscan_balance_raw)
|
|
|
|
except ValueError:
|
|
|
|
print("Failed to parse JSON responce.", file=sys.stderr);
|
2017-06-27 19:06:23 +02:00
|
|
|
sys.exit(9)
|
|
|
|
|
|
|
|
try:
|
2017-06-27 21:53:16 +02:00
|
|
|
float(etherscan_balance['result'])
|
2017-06-27 19:06:23 +02:00
|
|
|
except:
|
2017-06-28 20:33:58 +02:00
|
|
|
print("JSON result error!", file=sys.stderr);
|
2017-06-27 19:06:23 +02:00
|
|
|
sys.exit(9)
|
2017-06-27 21:53:16 +02:00
|
|
|
|
|
|
|
eth = float(etherscan_balance['result']) / 1000000000000000000
|
2017-06-28 20:33:58 +02:00
|
|
|
print("{}.value {:.2f}".format(eth_address, eth));
|