blob: a2903ec2aa643e50684d89d6b36b71df06a62a17 [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# !/usr/bin/env python3
Wei-Yu Chen49950b92021-11-08 19:19:18 +08002
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08003# SPDX-FileCopyrightText: 2020 The Magma Authors.
4# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
5#
6# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08007
Wei-Yu Chen49950b92021-11-08 19:19:18 +08008import textwrap
9
10
11class ActiveState:
12 ACTIVE = 'active'
13 RELOADING = 'reloading'
14 INACTIVE = 'inactive'
15 FAILED = 'failed'
16 ACTIVATING = 'activating'
17 DEACTIVATING = 'deactivating'
18
19 dbus2state = {
20 b'active': ACTIVE,
21 b'reloading': RELOADING,
22 b'inactive': INACTIVE,
23 b'failed': FAILED,
24 b'activating': ACTIVATING,
25 b'deactivating': DEACTIVATING,
26 }
27
28 state2status = {
29 ACTIVE: u'\u2714',
30 RELOADING: u'\u27A4',
31 INACTIVE: u'\u2717',
32 FAILED: u'\u2717',
33 ACTIVATING: u'\u27A4',
34 DEACTIVATING: u'\u27A4',
35 }
36
37
38class Errors:
39 def __init__(self, log_level, error_count):
40 self.log_level = log_level
41 self.error_count = error_count
42
43 def __str__(self):
44 return '{}: {}'.format(self.log_level, self.error_count)
45
46
47class RestartFrequency:
48 def __init__(self, count, time_interval):
49 self.count = count
50 self.time_interval = time_interval
51
52 def __str__(self):
53 return 'Restarted {} times {}'.format(
54 self.count,
55 self.time_interval,
56 )
57
58
59class HealthStatus:
60 DOWN = 'Down'
61 UP = 'Up'
62 UNKNOWN = 'Unknown'
63
64
65class Version:
66 def __init__(self, version_code, last_update_time):
67 self.version_code = version_code
68 self.last_update_time = last_update_time
69
70 def __str__(self):
71 return '{}, last updated: {}'.format(
72 self.version_code,
73 self.last_update_time,
74 )
75
76
77class ServiceHealth:
78 def __init__(
79 self, service_name, active_state, sub_state,
80 time_running, errors,
81 ):
82 self.service_name = service_name
83 self.active_state = active_state
84 self.sub_state = sub_state
85 self.time_running = time_running
86 self.errors = errors
87
88 def __str__(self):
89 return '{} {:20} {:10} {:15} {:10} {:>10} {:>10}'.format(
90 ActiveState.state2status.get(self.active_state, '-'),
91 self.service_name,
92 self.active_state,
93 self.sub_state,
94 self.time_running,
95 self.errors.log_level,
96 self.errors.error_count,
97 )
98
99
100class HealthSummary:
101 def __init__(
102 self, version, platform,
103 services_health,
104 internet_health, dns_health,
105 unexpected_restarts,
106 ):
107 self.version = version
108 self.platform = platform
109 self.services_health = services_health
110 self.internet_health = internet_health
111 self.dns_health = dns_health
112 self.unexpected_restarts = unexpected_restarts
113
114 def __str__(self):
115 any_restarts = any([
116 restarts.count
117 for restarts in self.unexpected_restarts.values()
118 ])
119 return textwrap.dedent("""
120 Running on {}
121 Version: {}:
122 {:20} {:10} {:15} {:10} {:>10} {:>10}
123 {}
124
125 Internet health: {}
126 DNS health: {}
127
128 Restart summary:
129 {}
130 """).format(
131 self.version, self.platform,
132 'Service', 'Status', 'SubState', 'Running for', 'Log level',
133 'Errors since last restart',
134 '\n'.join([str(h) for h in self.services_health]),
135 self.internet_health, self.dns_health,
136 '\n'.join([
137 '{:20} {}'.format(name, restarts)
138 for name, restarts
139 in self.unexpected_restarts.items()
140 ])
141 if any_restarts
142 else "No restarts since the gateway started",
143 )