monitor: use human readable outputs

And some drive-by cleanups
This commit is contained in:
darosior
2019-09-04 21:17:05 +02:00
committed by Christian Decker
parent 3532288db1
commit 2448e26618

View File

@@ -1,17 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
This is a quick hack and adapted plugin from the summary.py plugin (orinigally written by Rusty Russell
This is a quick hack and adopted plugin from the summary.py plugin (orinigally written by Rusty Russell This one is adapted by Rene Pickhardt and aims to help you identify inactive channels quickly
This one is adapted by Rene Pickhardt and aimed to help you identify inactive channels quickly
""" """
from lightning import Plugin, Millisatoshi from lightning import Plugin, Millisatoshi
import lightning import lightning
import json import json
plugin = Plugin(autopatch=True) plugin = Plugin()
# __version__ was introduced in 0.0.7.1, with utf8 passthrough support. # __version__ was introduced in 0.0.7.1, with utf8 passthrough support.
try: try:
@@ -23,15 +20,15 @@ except Exception:
@plugin.method("monitor") @plugin.method("monitor")
def monitor(plugin): def monitor(plugin):
"""Monitors channels of this node.""" """Monitors channels of this node."""
reply = {} reply = {}
reply['num_connected'] = 0 reply['num_connected'] = 0
reply['num_channels'] = 0 reply['num_channels'] = 0
reply['format-hint'] = 'simple'
peers = plugin.rpc.listpeers() peers = plugin.rpc.listpeers()
info = plugin.rpc.getinfo() info = plugin.rpc.getinfo()
nid = info["id"] nid = info["id"]
chans={} chans = {}
states={} states = {}
for p in peers['peers']: for p in peers['peers']:
for c in p['channels']: for c in p['channels']:
if p['connected']: if p['connected']:
@@ -39,35 +36,29 @@ def monitor(plugin):
reply['num_channels'] += 1 reply['num_channels'] += 1
state = c['state'] state = c['state']
if state in states: if state in states:
states[state]+=1 states[state] += 1
else: else:
states[state]=1 states[state] = 1
connected = "connected" connected = 'connected' if p['connected'] else 'disconnected'
if p['connected'] != True:
connected = "disconnected"
funding = c['funding_allocation_msat'] funding = c['funding_allocation_msat']
our_funding = funding[nid] our_funding = funding[nid]
fees = "our fees" fees = "our fees"
if int(our_funding) == 0: if int(our_funding) == 0:
fees = "their fees" fees = "their fees"
total = float(c['msatoshi_total']) total = float(c['msatoshi_total'])
ours = float(c['our_channel_reserve_satoshis']) + float(c['spendable_msatoshi']) ours = float(c['our_channel_reserve_satoshis']) + float(c['spendable_msatoshi'])
our_fraction = "{:4.2f}% owned by us".format(ours*100/total) our_fraction = '{:4.2f}% owned by us'.format(ours * 100 / total)
tmp = "\t".join([p['id'], connected, fees, our_fraction, c['short_channel_id']]) tmp = "\t".join([p['id'], connected, fees, our_fraction,
c['short_channel_id'] if 'short_channel_id' in c
else 'unknown scid'])
if state in chans: if state in chans:
chans[state].append(tmp) chans[state].append(tmp)
else: else:
chans[state] = [tmp] chans[state] = [tmp]
reply['states'] = []
serialized_states = []
for key, value in states.items(): for key, value in states.items():
serialized_states.append(key + ": " + str(value)) reply['states'].append(key + ": " + str(value))
reply['states']=serialized_states reply['channels'] = json.dumps(chans)
reply['channels'] = chans#json.dumps(chans)
reply = json.dumps(reply, indent=4)
return reply return reply