Skip to content

Commit 99dd268

Browse files
committed
cli-pretty: add pretty printing of system services
Signed-off-by: Richard Alpe <[email protected]>
1 parent d457d43 commit 99dd268

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/statd/python/cli_pretty/cli_pretty.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ class PadNtpSource:
172172
poll = 14
173173

174174

175+
class PadService:
176+
name = 16
177+
status = 8
178+
pid = 8
179+
description = 40
180+
181+
175182
class PadWifiScan:
176183
ssid = 40
177184
encryption = 30
@@ -1507,6 +1514,42 @@ def show_software(json, name):
15071514
slot.print()
15081515

15091516

1517+
def show_services(json):
1518+
if not json.get("ietf-system:system-state", "infix-system:services"):
1519+
print("Error, cannot find infix-system:services")
1520+
sys.exit(1)
1521+
1522+
services_data = get_json_data({}, json, 'ietf-system:system-state', 'infix-system:services')
1523+
services = services_data.get("service", [])
1524+
1525+
hdr = (f"{'NAME':<{PadService.name}}"
1526+
f"{'STATUS':<{PadService.status}}"
1527+
f"{'PID':>{PadService.pid -1}}"
1528+
f" {'DESCRIPTION'}")
1529+
print(Decore.invert(hdr))
1530+
1531+
for svc in services:
1532+
name = svc.get('name', '')
1533+
status = svc.get('status', '')
1534+
pid = svc.get('pid', 0)
1535+
description = svc.get('description', '')
1536+
1537+
if status in ('running', 'active', 'done'):
1538+
status_str = Decore.green(status)
1539+
elif status in ('crashed', 'failed', 'halted', 'missing', 'dead', 'conflict'):
1540+
status_str = Decore.red(status)
1541+
else:
1542+
status_str = Decore.yellow(status)
1543+
1544+
pid_str = str(pid) if pid > 0 else '-'
1545+
1546+
row = f"{name:<{PadService.name}}"
1547+
row += f"{status_str:<{PadService.status + 9}}"
1548+
row += f"{pid_str:>{PadService.pid}}"
1549+
row += f" {description}"
1550+
print(row)
1551+
1552+
15101553
def show_hardware(json):
15111554
if not json.get("ietf-hardware:hardware"):
15121555
print("Error, top level \"ietf-hardware:component\" missing")
@@ -2496,6 +2539,8 @@ def main():
24962539
subparsers.add_parser('show-routing-table', help='Show the routing table') \
24972540
.add_argument('-i', '--ip', required=True, help='IPv4 or IPv6 address')
24982541

2542+
subparsers.add_parser('show-services', help='Show system services')
2543+
24992544
subparsers.add_parser('show-software', help='Show software versions') \
25002545
.add_argument('-n', '--name', help='Slotname')
25012546

@@ -2528,6 +2573,8 @@ def main():
25282573
show_routing_table(json_data, args.ip)
25292574
elif args.command == "show-software":
25302575
show_software(json_data, args.name)
2576+
elif args.command == "show-services":
2577+
show_services(json_data)
25312578

25322579
else:
25332580
print(f"Error, unknown command '{args.command}'")

0 commit comments

Comments
 (0)