#! /usr/bin/env python

"""
Use the following in /etc/munin/plugin-conf.d/wormhole :

[wormhole_*]
env.serverdir /path/to/your/wormhole/server
"""

import os, sys, time, json

CONFIG = """\
graph_title Magic-Wormhole Transit Usage (all time)
graph_vlabel Bytes Since DB Creation
graph_category network
bytes.label Transit Bytes
bytes.draw LINE1
bytes.type GAUGE
"""

if len(sys.argv) > 1 and sys.argv[1] == "config":
    print CONFIG.rstrip()
    sys.exit(0)

serverdir = os.environ["serverdir"]
fn = os.path.join(serverdir, "stats.json")
with open(fn) as f:
    data = json.load(f)
if time.time() > data["valid_until"]:
    sys.exit(1) # expired

t = data["transit"]["all_time"]
print "bytes.value", t["bytes"]