diff --git a/README.md b/README.md index 10f7986..d905618 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Community curated plugins for c-lightning. | [donations][donations] | A simple donations page to accept donations from the web | | [graphql][graphql] | Exposes the c-lightning API over [graphql][graphql-spec] | | [lightning-qt][lightning-qt] | A bitcoin-qt-like GUI for lightningd | +| [monitor][monitor] | helps you analyze the health of your peers and channels | | [persistent-channels][pers-chans] | Maintains a number of channels to peers | | [probe][probe] | Regularly probes the network for stability | | [prometheus][prometheus] | Lightning node exporter for the prometheus timeseries server | @@ -105,3 +106,4 @@ your environment. [lightning-qt]: https://github.com/darosior/pylightning-qt [cpp-api]: https://github.com/darosior/lightningcpp [js-api]: https://github.com/darosior/clightningjs +[monitor]: https://github.com/renepickhardt/plugins/tree/master/monitor diff --git a/monitor/README.md b/monitor/README.md new file mode 100644 index 0000000..6ab65fe --- /dev/null +++ b/monitor/README.md @@ -0,0 +1,44 @@ +# Monitor plugin + +Monitors the health of your peers and helps you to decide if you might want to close a channel + +## Installation + +For general plugin installation instructions see the repos main +[README.md](https://github.com/lightningd/plugins/blob/master/README.md#Installation) + +## Example Usage + +Unfortunately the python plugin framework doesn't pretty-print, nor does +lightning-cli, so best viewed with -H (sorry for all the slasshes in the output. nothing I can do about it at this point): + +``` +lightning-cli -H monitor + +{ + \"num_connected\": 26, + \"num_channels\": 37, + \"states\": [ + \"CHANNELD_NORMAL: 37\" + ], + \"channels\": { + \"CHANNELD_NORMAL\": [ + \"024a8228d764091fce2ed67e1a7404f83e38ea3c7cb42030a2789e73cf3b341365\ connected\ their fees\ xx.xx% owned by us\ 537914x2372x0\", + \"032e04b67641c00444af1d83145c0b63bac8316a6afb8fec0f87938295ed8bb129\ disconnected\ their fees\ xx.xx% owned by us\ 539125x1288x0\", + \"0279c22ed7a068d10dc1a38ae66d2d6461e269226c60258c021b1ddcdfe4b00bc4\ connected\ our fees\ xx.xx% owned by us\ 539467x852x0\", + ... + \"0227d5b940cba21be92244953475ccdd3cefbed8f397be03e3155a5f41f304fc93\ connected\ their fees\ xx.xx% owned by us\ 581418x2157x0\" + ] + } +} +``` + +As you can see you will see a list of channels to which you are connected or disconnected. How much percent of the funds is owned by you and who has to pay the fees in case of force channel closing. + + +Or if you just want to see the channels which are currently disconnected from peers + +``` +lightning-cli -H monitor | grep "disconnected" +``` + diff --git a/monitor/monitor.py b/monitor/monitor.py new file mode 100755 index 0000000..eb9dbe0 --- /dev/null +++ b/monitor/monitor.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +""" + +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 aimed to help you identify inactive channels quickly + +""" + +from lightning import Plugin, Millisatoshi +import lightning +import json + +plugin = Plugin(autopatch=True) + +# __version__ was introduced in 0.0.7.1, with utf8 passthrough support. +try: + if version.parse(lightning.__version__) >= version.parse("0.0.7.1"): + have_utf8 = True +except Exception: + pass + +@plugin.method("monitor") +def monitor(plugin): + """Monitors channels of this node.""" + + reply = {} + reply['num_connected'] = 0 + reply['num_channels'] = 0 + peers = plugin.rpc.listpeers() + info = plugin.rpc.getinfo() + nid = info["id"] + chans={} + states={} + for p in peers['peers']: + for c in p['channels']: + if p['connected']: + reply['num_connected'] += 1 + reply['num_channels'] += 1 + state = c['state'] + if state in states: + states[state]+=1 + else: + states[state]=1 + connected = "connected" + if p['connected'] != True: + connected = "disconnected" + funding = c['funding_allocation_msat'] + our_funding = funding[nid] + fees = "our fees" + if int(our_funding) == 0: + fees = "their fees" + + + total = float(c['msatoshi_total']) + ours = float(c['our_channel_reserve_satoshis']) + float(c['spendable_msatoshi']) + our_fraction = "{:4.2f}% owned by us".format(ours*100/total) + tmp = "\t".join([p['id'], connected, fees, our_fraction, c['short_channel_id']]) + + if state in chans: + chans[state].append(tmp) + else: + chans[state] = [tmp] + + serialized_states = [] + for key, value in states.items(): + serialized_states.append(key + ": " + str(value)) + reply['states']=serialized_states + reply['channels'] = chans#json.dumps(chans) + reply = json.dumps(reply, indent=4) + return reply + + +@plugin.init() +def init(options, configuration, plugin): + plugin.log("Plugin monitor.py initialized") + +plugin.run() diff --git a/monitor/requirements.txt b/monitor/requirements.txt new file mode 100644 index 0000000..81d8712 --- /dev/null +++ b/monitor/requirements.txt @@ -0,0 +1,2 @@ +pylightning>=0.0.6 +