diff --git a/topstat.py b/topstat.py index f63bcfe..cf99913 100644 --- a/topstat.py +++ b/topstat.py @@ -46,6 +46,35 @@ if not os.path.exists(filename): csv_writer.writerow(headerRow) +def convert_to_megabytes(data_size): + """ + Converts a string representing data size (number + unit) to size in megabytes. + + Args: + data_size (str): String representing data size (e.g., "10G", "2M"). + + Returns: + int: Size of data in megabytes, or None if the format is invalid. + """ + try: + size, unit = data_size.upper().split() + size = float(size) + except (ValueError, AttributeError): + return None + + unit_map = { + "K": 1 / (1024**1), + "M": 1, + "G": 1024, + "T": 1024**2, + } + + if unit not in unit_map: + return None + + return int(size * unit_map[unit]) + + def runtopstat(): # Run the command and capture output result = subprocess.run(command, capture_output=True, text=True) @@ -62,7 +91,7 @@ def runtopstat(): loadaverages = re.search("^last.*", output, re.MULTILINE).group() loadaverages = re.sub("up.*", "", loadaverages.strip()) - loadaverages = re.sub("[a-z:;]", "", loadaverages.strip()).split() + loadaverages = re.sub("[a-z:;,]", "", loadaverages.strip()).split() loadaverage1 = loadaverages[0] loadaverage5 = loadaverages[1] @@ -90,6 +119,11 @@ def runtopstat(): mem = re.sub("Mem.", "", mem.strip()) mem = re.sub("[Active|Inact|Wired|Free|,]", "", mem.strip()).split() + meminmeg = [] + + for x in mem: + meminmeg.append(convert_to_megabytes(x)) + breakpoint() if lineData: