From 4f6a8dc32ccc4f9c8200fdd15d09c3567308d196 Mon Sep 17 00:00:00 2001 From: Marc Mance Date: Thu, 30 May 2024 16:27:52 -0400 Subject: [PATCH] added topstat.py --- collect.sh | 3 ++ topstat.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 topstat.py diff --git a/collect.sh b/collect.sh index 91c15ae..c0d8718 100755 --- a/collect.sh +++ b/collect.sh @@ -15,6 +15,9 @@ echo "Starting gstat Collection" timeout "$minutes"m gstat -C -s -d -o -p -I 60s > gstat.csv & echo "Starting iostat Collection" +echo "Starting zpool iostat Collection" +echo "Starting cpu Collection" + for (( i=1; i<=$iterations; i++ )); do # Execute the command python3 zpooliostat.py diff --git a/topstat.py b/topstat.py new file mode 100644 index 0000000..d5c64f3 --- /dev/null +++ b/topstat.py @@ -0,0 +1,112 @@ +import subprocess +import re +import csv +import os +import time + +# Define your shell command +command = ["top", "-n", "1"] + +headerRow = [ + "loadaverage_1", + "loadaverage_5", + "loadaverage_15", + "name", + "total_processes", + "running", + "sleeping", + "zombie", + "cpu_user", + "cpu_nice", + "cpu_system", + "cpu_interrupt", + "cpu_idle", + "mem_active", + "mem_inactive", + "mem_wired", + "mem_free", + "arc_total", + "arc_MFU", + "arc_MRU", + "arc_anon", + "arc_header", + "arc_other", + "swap_total", + "swap_free", +] + +filename = "topStat.csv" + +# Check if the file exists +if not os.path.exists(filename): + # Create the file in write mode ('w') if it doesn't exist + with open(filename, "w", newline="") as csvfile: + csv_writer = csv.writer(csvfile) + # Write the header row (optional) + csv_writer.writerow(headerRow) + + +def runtopstat(): + # Run the command and capture output + result = subprocess.run(command, capture_output=True, text=True) + + timestamp = time.time() + local_time = time.localtime(timestamp) + # Format the time components for a more readable output + timestamp = time.strftime("%Y-%m-%d %H:%M:%S", local_time) + + # Check the return code (0 for success) + if result.returncode == 0: + # Access the captured output as a string + output = result.stdout + + loadaverages = re.search("load averages.*", output).group() + loadaverages = re.sub("load averages.", "", loadaverages.strip()) + loadaverages = re.sub("up.*", "", loadaverages.strip()).split() + + loadaverage1 = loadaverages[0] + loadaverage5 = loadaverages[1] + loadaverage15 = loadaverages[2] + + processes = re.search("*.processes.*", output).group() + processes = re.sub("processes.", "", processes.strip()) + processes = re.sub("[a-z,]", "", processes.strip()).split() + + total_processes = processes[0] + running = processes[1] + sleeping = processes[2] + zombie = processes[3] + + cpu = re.search("^CPU.*", output).group() + cpu = re.sub("CPU.", "", cpu.strip()) + cpu = re.sub("[a-z,%]", "", cpu.strip()).split() + + cpu_user = cpu[0] + cpu_nice = cpu[1] + cpu_system = cpu[2] + cpu_interrupt = cpu[3] + cpu_idle = cpu[4] + + mem = re.search("^Mem.*", output).group() + mem = re.sub("Mem.", "", mem.strip()) + mem = re.sub("[Active|Inact|Wired|Free|,]", "", mem.strip()).split() + + breakpoint() + + if lineData: + with open(filename, "a", newline="") as csvfile: + csv_writer = csv.writer(csvfile) + lineData.insert(0, "cpu") + lineData.insert(0, timestamp) + csv_writer.writerow(lineData) + + else: + print(f"Error running command: {result.stderr}") + + +def main(): + runIostat() + + +if __name__ == "__main__": + main()