diff --git a/src/hetrixtools_freebsd/cli.py b/src/hetrixtools_freebsd/cli.py index a98a526..26d8cf3 100644 --- a/src/hetrixtools_freebsd/cli.py +++ b/src/hetrixtools_freebsd/cli.py @@ -39,20 +39,20 @@ class Update: self.cpucores = "" # getSysCtlInfo self.cputhreads = "" # getSysCtlInfo self.cpuspeed = "" # getSysCtlInfo - self.cpu = "" - self.wa = "" - self.st = "" - self.us = "" - self.sy = "" + self.cpu = "" # getVMStatInfo + self.wa = "" # getVMStatInfo + self.st = "" # getVMStatInfo + self.us = "" # getVMStatInfo + self.sy = "" # getVMStatInfo self.load1 = "" # getSysCtlInfo self.load5 = "" # getSysCtlInfo self.load15 = "" # getSysCtlInfo self.ramsize = "" # getSysCtlInfo self.ram = "" self.ramswapsize = "" # getSysCtlInfo - self.ramswap = "" - self.rambuff = "" - self.ramcache = "" + self.ramswap = "" # getSwapInfo + self.rambuff = "" # getSwapInfo + self.ramcache = "" # getSwapInfo self.disks = "" self.inodes = "" self.iops = "" @@ -90,21 +90,18 @@ class Update: # procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- # r b swpd free buff cache si so bi bo in cs us sy id wa st # 1 0 725024 184732 98836 5720268 0 0 41 653 1963 3761 3 4 88 5 1 + # # FreeBSD # procs memory page disks faults cpu # r b w avm fre flt re pi po fr sr ad0 cd0 in sy cs us sy id # 1 0 0 496M 1.4G 124 0 0 0 179 1 0 0 81 232 152 0 0 100 vminfo = json.loads( self.callCommand(command=['vmstat', '3', '2', '--libxo', 'json']) ) - cpustats = vminfo.get('cpu-statistics', None) + memstats = vminfo.get('memory', None) faults = vminfo.get('fault-rates', None) procs = vminfo.get('processes', None) if( procs is not None ): self.wa = procs.get('waiting', 0) - # Time stealing hard-coded to 0. I had to find a blog post from 2010 to even explain - # what it is. And it's not defined on FreeBSD anyways. - # http://web.archive.org/web/20120215064222/http://adrianotto.com/2010/02/time-stolen-from-a-virtual-machine/ - self.st = 0 # Need my own way to get similar stats for FreeBSD: # kern.cp_time returns 5 values for the the system that are the total amount # of time since boot spent in user, nice, system, interrupt and idle @@ -113,16 +110,39 @@ class Update: t0 = self.callCommand(command=['sysctl', '-n', 'kern.cp_time']).split(" ") time.sleep(2.0) t1 = self.callCommand(command=['sysctl', '-n', 'kern.cp_time']).split(" ") - user = t1[0] - t0[0] - nice = t1[1] - t0[1] - system = t1[2] - t0[2] - interrupt = t1[3] - t0[3] - idle = t1[4] - t0[4] + user = int(t1[0]) - int(t0[0]) + nice = int(t1[1]) - int(t0[1]) + system = int(t1[2]) - int(t0[2]) + interrupt = int(t1[3]) - int(t0[3]) + idle = int(t1[4]) - int(t0[4]) total = user + nice + system + interrupt + idle # cpu "busy" == everything that wasn't "idle" - self.cpu = 1 - (idle/total) - self.us = user/total - self.system = system/total + self.cpu = str(1 - (idle/total)) + self.us = str(user/total) + self.sy = str(system/total) + self.wa = str(interrupt/total) + + if( memstats is not None ): + # appears to be % free RAM + self.ram = int(memstats.get('free', 0)) / int(self.ramsize) + + # Time stealing hard-coded to 0. I had to find a blog post from 2010 to even explain + # what it is. And it's not defined on FreeBSD anyways. + # http://web.archive.org/web/20120215064222/http://adrianotto.com/2010/02/time-stolen-from-a-virtual-machine/ + self.st = 0 + + def getSwapInfo(self): + """ Eventually will call swapinfo(8) and so on for some of this. + ramswap is percent swap used. + """ + # get last line, split it on spaces + # Total 2097152 259452 1837700 12% + swapinfo = self.callCommand(['/usr/sbin/swapinfo']).splitlines()[-1:].split(" ") + self.ramswap = str(int(swapinfo[2]) / int(swapinfo[1])) + # these don't seem to exist on FreeBSD in the same way + self.rambuff = str(0) + self.ramcache = str(0) + def getSysCtlInfo(self): """ Parses all the things that can be gotten from sysctl and sets all the values in the data structure. Other values can come from other places. @@ -208,6 +228,8 @@ def go(): if( update.sysctl is not None ): # _logger.debug(f"{update.sysctl}") update.getSysCtlInfo() + update.getVMStatInfo() + update.getSwapInfo() else: _logger.fatal("Failed to get output from sysctl") _logger.info(f"Done fetching results")