From 33b6ba594175869bdf96db38d8157c4f6fd5920a Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Thu, 5 Oct 2023 09:21:06 -0400 Subject: [PATCH] added ram, swap, and cpu --- src/hetrixtools_freebsd/cli.py | 46 +++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/hetrixtools_freebsd/cli.py b/src/hetrixtools_freebsd/cli.py index 17023a9..a98a526 100644 --- a/src/hetrixtools_freebsd/cli.py +++ b/src/hetrixtools_freebsd/cli.py @@ -7,6 +7,7 @@ To test, run ``pip install -e .`` which will install the command ``hetrixtools`` import argparse import logging +import time import sys import subprocess import base64 @@ -48,7 +49,7 @@ class Update: self.load15 = "" # getSysCtlInfo self.ramsize = "" # getSysCtlInfo self.ram = "" - self.ramswapsize = "" + self.ramswapsize = "" # getSysCtlInfo self.ramswap = "" self.rambuff = "" self.ramcache = "" @@ -83,6 +84,45 @@ class Update: result = subprocess.run(command, stdout=subprocess.PIPE) return result.stdout.decode(encoding="utf-8") + def getVMStatInfo(self): + # Linux: + # vmstat 3 2 + # 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) + 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 + # We take to samples with a 2-second sleep in the middle, subtract t0 from t1, + # and calculate the percentage. + 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] + 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 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. @@ -106,8 +146,8 @@ class Update: (self.load1, self.load5, self.load15) = self.sysctl.get('vm.loadavg').split(" ")[1:4] # Memory and Swap - self.ramsize = int(self.sysctl.get('hw.physmem', 0)) / 1024 - self.ramswapsize = int(self.sysctl.get('vm.swap_total', 0)) / 1024 + self.ramsize = int(int(self.sysctl.get('hw.physmem', 0)) / 1024) + self.ramswapsize = int(int(self.sysctl.get('vm.swap_total', 0)) / 1024) def toJsonString(self): """Return the object as a JSON string."""