Files
statscollect/ixiostat.py
2024-05-29 18:31:15 -04:00

118 lines
3.2 KiB
Python

import subprocess
import re
import csv
import os
from datetime import datetime
def convert_to_machine_readable(date_string):
# Define the format string considering the time zone information (EDT)
format_string = "%a %b %d %H:%M:%S %p %Z %Y"
try:
# Parse the date string using the format string
datetime_obj = datetime.strptime(date_string, format_string)
return datetime_obj
except ValueError:
print(f"given: {date_string}")
print(
f"Error: Invalid date format. Please provide a string in the format '{format_string}'."
)
return None
# Define your shell command
command = ["zpool", "iostat", "-Td", "-l", "-p", "-v", "-y", "1", "1"]
headerRow = [
"time",
"capacity_alloc",
"capacity_free",
"ops_read",
"ops_write",
"bandwidth_read",
"bandwidth_write",
"total_wait_read",
"total_wait_write",
"disk_wait_read",
"disk_wait_write",
"syncq_wait_read",
"syncq_wait_write",
"asyncq_wait_read",
"asyncq_wait_write",
"scrub_wait",
"trim_wait",
]
def runZpoolIostat():
# Run the command and capture output
result = subprocess.run(command, capture_output=True, text=True)
# Check the return code (0 for success)
if result.returncode == 0:
# Access the captured output as a string
output = result.stdout
# split output by pools
pools = re.split(r"^-", output, flags=re.MULTILINE)
i = 0
for pool in pools:
if i == 0:
# get timestamp
timestamp = pool.splitlines()[0].strip()
if timestamp:
timestamp = str(convert_to_machine_readable(timestamp))
else:
# remove ------ lines
pool = re.sub("^---.*", "", pool).strip()
# turn - values into 0
pool = re.sub(" -", "0", pool)
y = 0
poolbyline = re.split("\n", pool)
for line in poolbyline:
lineData = line.split()
if not lineData:
break
if y == 0:
poolname = lineData[0]
filename = poolname + "-zio.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)
# Now open the file in append mode ('a')
with open(filename, "a", newline="") as csvfile:
csv_writer = csv.writer(csvfile)
lineData.insert(0, timestamp)
csv_writer.writerow(lineData)
y += 1
i += 1
else:
print(f"Error running command: {result.stderr}")
def main():
runZpoolIostat()
if __name__ == "__main__":
main()