Skip to content

Instantly share code, notes, and snippets.

@wileyj
Last active February 2, 2022 19:29
Show Gist options
  • Save wileyj/881eda423366134bc25e3fb5ec39ccbb to your computer and use it in GitHub Desktop.
Save wileyj/881eda423366134bc25e3fb5ec39ccbb to your computer and use it in GitHub Desktop.
stacks-node info parser
#!/usr/bin/env python3
import aiohttp
import asyncio
import time
from time import sleep
import math
from prometheus_client.parser import text_string_to_metric_families
urls = [
'http://localhost:20443/v2/neighbors',
'http://localhost:20443/v2/info',
'http://localhost:9153/metrics'
]
filename = ("./output/requests.%s.out" % math.trunc(time.time()))
try:
file = open(filename,'w')
except FileNotFoundError:
print("Failed to open file: %s No such file" % (filename));
async def main():
V2_TIP_HEIGHT = 0
V2_NEIGHBORS_INBOUND = 0
V2_NEIGHBORS_OUTBOUND = 0
METRICS_TIP_HEIGHT = 0
METRICS_MINERS_TOTAL = 0
METRICS_MEMPOOL_OUTSTANDING_TXS = 0
METRICS_NEIGHBORS_INBOUND = 0
METRICS_NEIGHBORS_INBOUND = 0
async with aiohttp.ClientSession() as session:
try:
file = open(filename,'a')
except FileNotFoundError:
print("Failed to open file: %s No such file" % (filename));
for u in urls:
item_url = f'{u}'
try:
async with session.get(item_url) as resp:
if "20443" in u:
item = await resp.json()
if "info" in u:
V2_TIP_HEIGHT=item['stacks_tip_height']
if "neighbors" in u:
V2_NEIGHBORS_INBOUND=len(item["inbound"])
V2_NEIGHBORS_OUTBOUND=len(item["outbound"])
if "9153" in u:
item = await resp.text()
for family in text_string_to_metric_families(item):
for sample in family.samples:
name = sample[0]
labels = sample[1]
value = sample[2]
timestamp = sample[3]
if name == "stacks_node_stacks_tip_height":
METRICS_TIP_HEIGHT = value
if name == "stacks_node_active_miners_total":
METRICS_MINERS_TOTAL = value
if name == "stacks_node_mempool_outstanding_txs":
METRICS_MEMPOOL_OUTSTANDING_TXS = value
if name == "stacks_node_neighbors_inbound":
METRICS_NEIGHBORS_INBOUND = value
if name == "stacks_node_neighbors_outbound":
METRICS_NEIGHBORS_OUTBOUND = value
except Exception as e:
if "20443" in u:
V2_TIP_HEIGHT = None
V2_NEIGHBORS_INBOUND = None
V2_NEIGHBORS_OUTBOUND = None
if "9153" in u:
METRICS_TIP_HEIGHT = None
METRICS_MINERS_TOTAL = None
METRICS_MEMPOOL_OUTSTANDING_TXS = None
METRICS_NEIGHBORS_INBOUND = None
METRICS_NEIGHBORS_OUTBOUND = None
file.write("%s (tip_height: %s/%s) (neighbors in:out: [%s/%s][%s/%s]) (miners_total: %s) (mempool_txs: %s)\n"
% (
time.strftime("%D %H:%M:%S",
time.localtime(time.time())),
V2_TIP_HEIGHT,
METRICS_TIP_HEIGHT,
V2_NEIGHBORS_INBOUND,
V2_NEIGHBORS_OUTBOUND,
METRICS_NEIGHBORS_INBOUND,
METRICS_NEIGHBORS_OUTBOUND,
METRICS_MINERS_TOTAL,
METRICS_MEMPOOL_OUTSTANDING_TXS,
)
)
print("%s (tip_height: %s/%s) (neighbors in:out: [%s/%s][%s/%s]) (miners_total: %s) (mempool_txs: %s)"
% (
time.strftime("%D %H:%M:%S",
time.localtime(time.time())),
V2_TIP_HEIGHT,
METRICS_TIP_HEIGHT,
V2_NEIGHBORS_INBOUND,
V2_NEIGHBORS_OUTBOUND,
METRICS_NEIGHBORS_INBOUND,
METRICS_NEIGHBORS_OUTBOUND,
METRICS_MINERS_TOTAL,
METRICS_MEMPOOL_OUTSTANDING_TXS,
)
)
file.close()
while True:
asyncio.run(main())
sleep(30)
@wileyj
Copy link
Author

wileyj commented Jan 29, 2022

simple script to check data exposed by prometheus with what's reported over /v2/info in the stacks blockchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment