mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-19 22:24:19 +01:00
With c-lightning v0.7.0, there seems to be no `short_channel_id`
key present in the input sent to the `prometheus.py` script when
it is is called via the `plugin` config value for c-lightning
when a channel is opened.
Without this change, the script throws `500 Internal Server` error
when doing `$ lightning-cli fundchannel <id> <sats>`.
A sample JSON messages passed to the plugin is:
```
{
'state': 'CHANNELD_AWAITING_LOCKIN',
'scratch_txid': '120ad265b66aa0c4bbb5ff50a23ca85463337fb8b58b395f3811918610686a734',
'owner': 'lightning_channeld',
'channel_id': 'eda784df73aa105d728a0d9fee6ff450eeeac78aa57d3218f7b0dd89b2bd20f1',
'funding_txid': 'f120bdb2eec4b0f718327dc58cc7eaee5dd46fa19f0d8a725d10ff73df84a7ed',
'private': False,
'funding_allocation_msat': {
'03584abd04d69168600ff0ff5d11723d324f2deadbeefcbb84804580e344af235e': 0,
'03a9c46ddacd373f78b6679f9dec6adb10b3ddeadbeef2a685e09b2ce6101bc18c': 100000000
},
'funding_msat': {
'03584abd04d69168600340ad5d11723d324f2deadbeefcbb84804580e344af235e': '0msat',
'03a9c46ddacd373f78b6679f9dec6adb10b3ddeadbeef2a685e09b2ce6101bc18c': '100000000msat'
},
'msatoshi_to_us': 100000000,
'to_us_msat': '100000000msat',
'msatoshi_to_us_min': 100000000,
'min_to_us_msat': '100000000msat',
'msatoshi_to_us_max': 100000000,
'max_to_us_msat': '100000000msat',
'msatoshi_total': 100000000,
'total_msat': '100000000msat',
'dust_limit_satoshis': 546,
'dust_limit_msat': '546000msat',
'max_htlc_value_in_flight_msat': 18446744073709551615,
'max_total_htlc_in_msat': '18446744073709551615msat',
'their_channel_reserve_satoshis': 1000,
'their_reserve_msat': 1000000msat,
'our_channel_reserve_satoshis': 1000,
'our_reserve_msat': '1000000msat',
'spendable_msatoshi': 99000000,
'spendable_msat': '99000000msat',
'htlc_minimum_msat': 0,
'minimum_htlc_in_msat': '0msat',
'their_to_self_delay': 144,
'our_to_self_delay': 144,
'max_accepted_htlcs': 483,
'status': ['CHANNELD_AWAITING_LOCKIN:Reconnected, and reestablished.', 'CHANNELD_AWAITING_LOCKIN:Funding needs more confirmations.'],
'in_payments_offered': 0,
'in_msatoshi_offered': 0,
'in_offered_msat': '0msat',
'in_payments_fulfilled': 0,
'in_msatoshi_fulfilled': 0,
'in_fulfilled_msat': '0msat',
'out_payments_offered': 0,
'out_msatoshi_offered': 0,
'out_offered_msat': '0msat',
'out_payments_fulfilled': 0,
'out_msatoshi_fulfilled': 0,
'out_fulfilled_msat': '0msat',
'htlcs': []
}
```
141 lines
4.3 KiB
Python
Executable File
141 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from lightning import Plugin
|
|
from prometheus_client import start_http_server, CollectorRegistry
|
|
from prometheus_client.core import InfoMetricFamily, GaugeMetricFamily
|
|
from sys import exit
|
|
|
|
plugin = Plugin()
|
|
|
|
|
|
class BaseLnCollector(object):
|
|
def __init__(self, rpc, registry):
|
|
self.rpc = rpc
|
|
self.registry = registry
|
|
|
|
|
|
class NodeCollector(BaseLnCollector):
|
|
def collect(self):
|
|
info = self.rpc.getinfo()
|
|
info_labels = {k: v for k, v in info.items() if isinstance(v, str)}
|
|
node_info_fam = InfoMetricFamily(
|
|
'lightning_node',
|
|
'Static node information',
|
|
labels=info_labels.keys(),
|
|
)
|
|
node_info_fam.add_metric(info_labels, info_labels)
|
|
yield node_info_fam
|
|
|
|
|
|
class FundsCollector(BaseLnCollector):
|
|
def collect(self):
|
|
funds = self.rpc.listfunds()
|
|
print(funds['outputs'])
|
|
output_funds = sum(
|
|
[o['amount_msat'].to_satoshi() for o in funds['outputs']]
|
|
)
|
|
channel_funds = sum(
|
|
[c['our_amount_msat'].to_satoshi() for c in funds['channels']]
|
|
)
|
|
total = output_funds + channel_funds
|
|
|
|
yield GaugeMetricFamily(
|
|
'lightning_funds_total',
|
|
"Total satoshis we own on this node.",
|
|
value=total,
|
|
)
|
|
yield GaugeMetricFamily(
|
|
'lightning_funds_output',
|
|
"On-chain satoshis at our disposal",
|
|
value=output_funds,
|
|
)
|
|
yield GaugeMetricFamily(
|
|
'lightning_funds_channel',
|
|
"Satoshis in channels.",
|
|
value=channel_funds,
|
|
)
|
|
|
|
|
|
class PeerCollector(BaseLnCollector):
|
|
def collect(self):
|
|
peers = self.rpc.listpeers()['peers']
|
|
|
|
connected = GaugeMetricFamily(
|
|
'lightning_peer_connected',
|
|
'Is the peer currently connected?',
|
|
labels=['id'],
|
|
)
|
|
count = GaugeMetricFamily(
|
|
'lightning_peer_channels',
|
|
"The number of channels with the peer",
|
|
labels=['id'],
|
|
)
|
|
|
|
for p in peers:
|
|
labels = [p['id']]
|
|
count.add_metric(labels, len(p['channels']))
|
|
connected.add_metric(labels, int(p['connected']))
|
|
|
|
return [count, connected]
|
|
|
|
|
|
class ChannelsCollector(BaseLnCollector):
|
|
def collect(self):
|
|
balance_gauge = GaugeMetricFamily(
|
|
'lightning_channel_balance',
|
|
'How many funds are at our disposal?',
|
|
labels=['id', 'scid'],
|
|
)
|
|
spendable_gauge = GaugeMetricFamily(
|
|
'lightning_channel_spendable',
|
|
'How much can we currently send over this channel?',
|
|
labels=['id', 'scid']
|
|
)
|
|
total_gauge = GaugeMetricFamily(
|
|
'lightning_channel_capacity',
|
|
'How many funds are in this channel in total?',
|
|
labels=['id', 'scid'],
|
|
)
|
|
htlc_gauge = GaugeMetricFamily(
|
|
'lightning_channel_htlcs',
|
|
'How many HTLCs are currently active on this channel?',
|
|
labels=['id', 'scid'],
|
|
)
|
|
|
|
peers = self.rpc.listpeers()['peers']
|
|
for p in peers:
|
|
for c in p['channels']:
|
|
labels = [p['id'], c['channel_id']]
|
|
balance_gauge.add_metric(labels, c['to_us_msat'].to_satoshi())
|
|
spendable_gauge.add_metric(labels,
|
|
c['spendable_msat'].to_satoshi())
|
|
total_gauge.add_metric(labels, c['total_msat'].to_satoshi())
|
|
htlc_gauge.add_metric(labels, len(c['htlcs']))
|
|
|
|
return [htlc_gauge, total_gauge, spendable_gauge, balance_gauge]
|
|
|
|
|
|
@plugin.init()
|
|
def init(options, configuration, plugin):
|
|
s = options['prometheus-listen'].rpartition(':')
|
|
if len(s) != 3 or s[1] != ':':
|
|
print("Could not parse prometheus-listen address")
|
|
exit(1)
|
|
ip, port = s[0], int(s[2])
|
|
|
|
registry = CollectorRegistry()
|
|
start_http_server(addr=ip, port=port, registry=registry)
|
|
registry.register(NodeCollector(plugin.rpc, registry))
|
|
registry.register(FundsCollector(plugin.rpc, registry))
|
|
registry.register(PeerCollector(plugin.rpc, registry))
|
|
registry.register(ChannelsCollector(plugin.rpc, registry))
|
|
|
|
|
|
plugin.add_option(
|
|
'prometheus-listen',
|
|
'0.0.0.0:9900',
|
|
'Address and port to bind to'
|
|
)
|
|
|
|
|
|
plugin.run()
|