#! /usr/bin/env python

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

[wormhole_*]
env.usagedb /path/to/your/wormhole/server/usage.sqlite
"""

from __future__ import print_function
import os, sys, time, sqlite3

CONFIG = """\
graph_title Magic-Wormhole Transit Active Channels
graph_vlabel Channels
graph_category network
waiting.label Transit Waiting
waiting.draw LINE1
waiting.type GAUGE
connected.label Transit Connected
connected.draw LINE1
connected.type GAUGE
"""

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

dbfile = os.environ["usagedb"]
assert os.path.exists(dbfile)
db = sqlite3.connect(dbfile)

MINUTE = 60.0
updated,waiting,connected = db.execute("SELECT `updated`,`waiting`,`connected`"
                                       " FROM `current`").fetchone()
if time.time() > updated + 5*MINUTE:
    sys.exit(1) # expired

print("waiting.value", waiting)
print("connected.value", connected)
