Skip to content

Commit d19a178

Browse files
tridgeclaude
andcommitted
keydb.py: add 'stats' command to show live connection state
Reads connections.tdb (sibling of keys.tdb) via conntdb_lib and prints one line per active connection, joined with the entry's name from keys.tdb. Convenient for quick on-server checks without loading the web UI. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 88179ae commit d19a178

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

keydb.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import sys
88

9+
import conntdb_lib
910
import keydb_lib
1011
from keydb_lib import CLIError, FLAG_NAMES
1112

@@ -23,7 +24,8 @@ def main():
2324
choices=['list', 'convert', 'add', 'remove',
2425
'setname', 'setpass', 'setport1',
2526
'initialise', 'resettimestamp',
26-
'setflag', 'clearflag', 'flags'],
27+
'setflag', 'clearflag', 'flags',
28+
'stats'],
2729
help="action to perform")
2830
parser.add_argument("args", default=[], nargs=argparse.REMAINDER)
2931
args = parser.parse_args()
@@ -101,6 +103,26 @@ def main():
101103
on = ke.flag_names()
102104
print("flags=0x%x %s" % (ke.flags, ','.join(on) if on else '(none)'))
103105

106+
elif args.action == "stats":
107+
# Live-connection stats from connections.tdb (sibling of
108+
# keys.tdb), joined with each entry's name from this DB.
109+
entries = {ke.port2: ke for ke in keydb_lib.list_entries(db)}
110+
conn_path = conntdb_lib.conn_path_for(args.keydb)
111+
active = conntdb_lib.list_active(conn_path)
112+
if not active:
113+
print("(no active connections)")
114+
else:
115+
for c in active:
116+
ke = entries.get(c.port2)
117+
if ke is not None:
118+
label = "%d/%d '%s'" % (ke.port1, ke.port2, ke.name)
119+
else:
120+
label = "?/%d" % c.port2
121+
side = 'user' if c.is_user else 'eng#%d' % c.conn_index
122+
print("%s %s %s peer=%s uptime=%ds rx=%u tx=%u"
123+
% (label, side, c.transport_name, c.peer,
124+
c.uptime_s(), c.rx_msgs, c.tx_msgs))
125+
104126
else:
105127
raise CLIError("Unknown action: %s" % args.action)
106128

0 commit comments

Comments
 (0)