diff --git a/collect.py b/collect.py index a895d8b..7cdc14c 100644 --- a/collect.py +++ b/collect.py @@ -82,6 +82,94 @@ def scaleIostat_cpu(): print(f"Error running command: {collect}") +def convert_to_megabytes(data_size): + try: + unit = data_size[-1].upper() + size = data_size[:-1] + 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 scaleMemstat(): + command = ["top", "-n", "1"] + + filename = "memStat.csv" + collect = runCollect(command) + + if collect: + mem = re.search("Mem :.*", collect).group() + mem = re.sub("^.*Mem..", "", mem.strip()) + mem = re.sub("[total|free|used|buff.cache|,]", "", mem.strip()).split() + + meminmeg = [] + + for x in mem: + meminmeg.append(convert_to_megabytes(f"{int(x)}M")) + + mem = meminmeg + + mem_wired = mem[0] + mem_free = mem[1] + mem_active = mem[2] + mem_inactive = mem[3] + + lineData = [mem_active, mem_inactive, mem_wired, mem_free] + + if lineData: + with open(filename, "a", newline="") as csvfile: + csv_writer = csv.writer(csvfile) + lineData.insert(0, "mem") + lineData.insert(0, getTimestamp()) + csv_writer.writerow(lineData) + else: + print(f"Error running command: {collect}") + + +def coreMemstat(): + command = ["top", "-n", "1"] + + filename = "memStat.csv" + collect = runCollect(command) + + if collect: + mem = re.search("Mem:.*", collect).group() + 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)) + + mem = meminmeg + + mem_active = mem[0] + mem_inactive = mem[1] + mem_wired = mem[2] + mem_free = mem[3] + + lineData = [mem_active, mem_inactive, mem_wired, mem_free] + + if lineData: + with open(filename, "a", newline="") as csvfile: + csv_writer = csv.writer(csvfile) + lineData.insert(0, "mem") + lineData.insert(0, getTimestamp()) + csv_writer.writerow(lineData) + else: + print(f"Error running command: {collect}") + + def ifstat(): command = ["ifstat", "-znq", "1", "1"] @@ -99,8 +187,8 @@ def ifstat(): with open(filename, "a", newline="") as csvfile: csv_writer = csv.writer(csvfile) csv_writer.writerow(lineData) - else: - print(f"Error running command: {collect}") + else: + print(f"Error running command: {collect}") def coreCPUstat(): @@ -193,17 +281,18 @@ def collect_data(minutes): if is_debian(): scaleIostat_cpu() scaleIostat_disk() + scaleMemstat() if is_freebsd(): coreCPUstat() + coreMemstat() if i == minutes: + print("") break time.sleep(60) - print("") - # kill gstat if freebsd if is_freebsd(): process.kill()