def get_magma_services_status(): """ Get health for all the running services """ # DBus Unit objects: https://www.freedesktop.org/wiki/Software/systemd/dbus/ chan = ServiceRegistry.get_rpc_channel('magmad', ServiceRegistry.LOCAL) client = MagmadStub(chan) configs = client.GetConfigs(common_pb2.Void()) services_health_summary = [] for service_name in configs.configs_by_key: unit = Unit('magma@{}.service'.format(service_name), _autoload=True) active_state = ActiveState.dbus2state[unit.Unit.ActiveState] sub_state = str(unit.Unit.SubState, 'utf-8') if active_state == ActiveState.ACTIVE: pid = unit.Service.MainPID process = subprocess.Popen( 'ps -o etime= -p {}'.format(pid).split(), stdout=subprocess.PIPE) time_running, error = process.communicate() else: time_running = b'00' services_health_summary.append( ServiceHealth(service_name=service_name, active_state=active_state, sub_state=sub_state, time_running=str(time_running, 'utf-8').strip())) return services_health_summary
def get_gateway_service_mconfigs(services: List[str]) -> Dict[str, Any]: """ Get the managed configurations of some gateway services. Args: services: List of service names to fetch configs for Returns: service mconfigs keyed by name """ ret = {} magmad_stub = MagmadStub(get_rpc_channel('magmad')) stub_response = magmad_stub.GetConfigs(Void()) for srv in services: ret[srv] = unpack_mconfig_any(stub_response.configs_by_key[srv]) return ret