def _system_status(self): cpu = psutil.cpu_times() mem = psutil.virtual_memory() swap = psutil.swap_memory() def partition_gen(): for partition in psutil.disk_partitions(): usage = psutil.disk_usage(partition.mountpoint) yield DiskPartition( device=partition.device, mount_point=partition.mountpoint, total=usage.total, used=usage.used, free=usage.free, ) return SystemStatus( time=int(time.time()), uptime_secs=int(time.time() - self._boot_time), cpu_user=int(cpu.user * 1000), # convert second to millisecond cpu_system=int(cpu.system * 1000), cpu_idle=int(cpu.idle * 1000), mem_total=mem.total, mem_available=mem.available, mem_used=mem.used, mem_free=mem.free, swap_total=swap.total, swap_used=swap.used, swap_free=swap.free, disk_partitions=[partition for partition in partition_gen()], )
async def _checkin(self, service_statusmeta): """ if previous checkin is successful, create a new channel (to make sure the channel does't become stale). Otherwise, keep the existing channel. """ if self._checkin_client is None: chan = ServiceRegistry.get_rpc_channel('checkind', ServiceRegistry.CLOUD) self._checkin_client = CheckindStub(chan) mconfig = self._service.mconfig cpu = psutil.cpu_times() mem = psutil.virtual_memory() try: gw_ip = get_ip_from_if('tun0') # look for tun0 interface except ValueError: gw_ip = 'N/A' request = CheckinRequest( gateway_id=snowflake.snowflake(), magma_pkg_version=self._service.version, system_status=SystemStatus( cpu_user=int(cpu.user * 1000), # convert second to millisecond cpu_system=int(cpu.system * 1000), cpu_idle=int(cpu.idle * 1000), mem_total=mem.total, mem_available=mem.available, mem_used=mem.used, mem_free=mem.free, uptime_secs=int(time.time() - self._boot_time), ), vpn_ip=gw_ip, kernel_version=self._kernel_version, kernel_versions_installed=self._kernel_versions_installed, ) for statusmeta in service_statusmeta.values(): request.status.meta.update(statusmeta) try: await grpc_async_wrapper( self._checkin_client.Checkin.future( request, mconfig.checkin_timeout, ), self._loop) self._checkin_done() except grpc.RpcError as err: self._checkin_error(err)
def create_checkin_request(): """Create request object to send with Checkin""" cpu = psutil.cpu_times() mem = psutil.virtual_memory() request = CheckinRequest( gateway_id=snowflake.snowflake(), magma_pkg_version='fake_version', system_status=SystemStatus( cpu_user=int(cpu.user * 1000), # convert second to millisecond cpu_system=int(cpu.system * 1000), cpu_idle=int(cpu.idle * 1000), mem_total=mem.total, mem_available=mem.available, mem_used=mem.used, mem_free=mem.free, ), ) return request