added cpu collection

This commit is contained in:
Marc Mance
2024-05-30 13:50:39 -04:00
parent 7b2e365e5e
commit 76b7f807e9
3 changed files with 72 additions and 4 deletions

View File

@@ -5,8 +5,8 @@ minutes=$1
echo "" echo ""
greeting="Running perfcollect for $minutes minutes" greeting="Running perfcollect for $minutes minutes"
echo "$greeting" echo "$greeting"
echo "Will auto upload to iXsystem when completed." echo "Will auto upload to ftp.ixsystem.com when completed."
echo "Airgapped Systems will need to exfiltrate /root/*.csv" echo "Airgapped Systems will need to exfiltrate *.csv"
echo "" echo ""
iterations=$1 iterations=$1
@@ -17,7 +17,9 @@ timeout "$minutes"m gstat -C -s -d -o -p -I 60s > gstat.csv &
echo "Starting iostat Collection" echo "Starting iostat Collection"
for (( i=1; i<=$iterations; i++ )); do for (( i=1; i<=$iterations; i++ )); do
# Execute the command # Execute the command
python3 ixiostat.py python3 zpooliostat.py
python3 iostat.py
echo "minute: $i" echo "minute: $i"
# Wait for 60 seconds before the next iteration # Wait for 60 seconds before the next iteration
sleep 60 sleep 60

66
iostat.py Normal file
View File

@@ -0,0 +1,66 @@
import subprocess
import re
import csv
import os
import time
# Define your shell command
command = ["iostat", "-n", "0"]
headerRow = [
"timestamp",
"name",
"tty_in",
"tty_out",
"cpu_user",
"cpu_user_niced",
"cpu_system",
"cpu_interrupt",
"cpu_idle",
]
filename = "cpuStat.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 runIostat():
# 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
byline = re.split("\n", output)
data = byline[2]
lineData = data.split()
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()

View File

@@ -8,7 +8,7 @@ import time
command = ["zpool", "iostat", "-Tu", "-l", "-p", "-v", "-y", "1", "1"] command = ["zpool", "iostat", "-Tu", "-l", "-p", "-v", "-y", "1", "1"]
headerRow = [ headerRow = [
"time", "timestamp",
"name", "name",
"capacity_alloc", "capacity_alloc",
"capacity_free", "capacity_free",