From 2034477270d88e5f9b5ac89cef736a5c900531fb Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sun, 8 Oct 2023 15:25:26 -0400 Subject: [PATCH] final netif with base64 --- src/hetrixtools_freebsd/cli.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/hetrixtools_freebsd/cli.py b/src/hetrixtools_freebsd/cli.py index db79986..62aa4ce 100644 --- a/src/hetrixtools_freebsd/cli.py +++ b/src/hetrixtools_freebsd/cli.py @@ -149,10 +149,10 @@ class Update: def getNetInfo(self): """ Take 2 samples, about 2 seconds apart. Report the difference.""" - t0 = json.loads( self.callCommand(command=['netstat', '-f', 'inet', '-b', + t0 = json.loads( self.callCommand(command=['/usr/bin/netstat', '-f', 'inet', '-b', '-i', '--libxo', 'json']) ) time.sleep(2.0) - t1 = json.loads( self.callCommand(command=['netstat', '-f', 'inet', '-b', + t1 = json.loads( self.callCommand(command=['/usr/bin/netstat', '-f', 'inet', '-b', '-i', '--libxo', 'json']) ) t0list = {ifname['name']:(ifname['sent-bytes'], ifname['received-bytes']) for ifname in t0 ['statistics']['interface']} @@ -167,23 +167,26 @@ class Update: self.nics = base64.b64encode("".join(result).encode()).decode('utf-8') # Now get the IPv4 and IPv6 addresses for each interface - ifconfigtxt = self.callCommand(command=['netstat', '-a']) + ifconfigtxt = self.callCommand(command=['/sbin/ifconfig', '-a']) ifconfig = jc.parse(parser_mod_name="ifconfig", data=ifconfigtxt) # XXX only returns first IP address assigned to an interface. Interfaces cnan have more # than one IP # Deliberately excludes lo0 interface ipv4 = {ifname['name']:ifname['ipv4'][0]['address'] for ifname in ifconfig if ifname['name'] != 'lo0' } - self.ipv4 = ','.join(f"{key}:{value}" for key, value in ipv4.items()) - + ipv4s = ';'.join(f"{key},{value}" for key, value in ipv4.items()) + self.logger.debug(f"ipv4 = {ipv4s}") + self.ipv4 = base64.b64encode(ipv4s.encode()).decode('utf-8') # XXX Same thing: only catches first ipv6 addr, which is probably worse, since # most ipv6 interfaces have several IP addresses. ipv6 = {ifname['name']:ifname['ipv6'][0]['address'] for ifname in ifconfig if ifname['name'] != 'lo0' } - self.ipv6 = ','.join(f"{key}:{value}" for key, value in ipv6.items()) + ipv6s = ';'.join(f"{key},{value}" for key, value in ipv6.items()) + self.logger.debug(f"ipv6 = {ipv6s}") + self.ipv6 = base64.b64encode(ipv6s.encode()).decode('utf-8') # Get open connections - conntext = self.callCommand(['sockstat', '-c', '-n', '-P', 'tcp']) + conntext = self.callCommand(['/usr/bin/sockstat', '-c', '-n', '-P', 'tcp']) # At first glance it might seem like a bug. len() returns the number of lines # counting from 0, so if there are 4 lines it reports 3. But one of the lines # is a header line, not a connection. So we WANT to return 1 less than the actual @@ -277,6 +280,7 @@ def go(): update.getSysCtlInfo() update.getVMStatInfo() update.getSwapInfo() + update.getNetInfo() else: _logger.fatal("Failed to get output from sysctl") _logger.info(f"Done fetching results")