wrapped collect in try catch for keyboard interupt

This commit is contained in:
Marc Mance
2024-08-01 12:04:28 -04:00
parent d15ea132ac
commit 5b4be35dba

View File

@@ -259,12 +259,6 @@ def zpoolIostat():
def collect_data(minutes): def collect_data(minutes):
if is_freebsd():
gstat_command = ["gstat", "-C", "-s", "-d", "-o", "-p", "-I", "5s"]
with open("gstat.csv", "a") as output_file:
# Create a Popen object with stdout redirected to the file
process = subprocess.Popen(gstat_command, stdout=output_file)
for i in range(minutes): for i in range(minutes):
if i == 0: if i == 0:
print("Minute:", end="", flush=True) print("Minute:", end="", flush=True)
@@ -290,10 +284,6 @@ def collect_data(minutes):
time.sleep(45) time.sleep(45)
# kill gstat if freebsd
if is_freebsd():
process.kill()
def run_debug(): def run_debug():
print("Taking new debug.") print("Taking new debug.")
@@ -380,6 +370,7 @@ def main():
minutes = int(input("Enter the duration in minutes: ")) minutes = int(input("Enter the duration in minutes: "))
minutesToWait = int(input("Enter the delay before capture in minutes: ")) minutesToWait = int(input("Enter the delay before capture in minutes: "))
try:
if not minutesToWait: if not minutesToWait:
minutesToWait = 0 minutesToWait = 0
@@ -393,8 +384,38 @@ def main():
print("Starting Collection") print("Starting Collection")
# Collect data # Collect data
if is_freebsd():
timeout = f"{minutes}m"
gstat_command = [
"timeout",
timeout,
"gstat",
"-C",
"-s",
"-d",
"-o",
"-p",
"-I",
"5s",
]
with open("gstat.csv", "a") as output_file:
# Create a Popen object with stdout redirected to the file
process = subprocess.Popen(gstat_command, stdout=output_file)
collect_data(minutes) collect_data(minutes)
# kill gstat if freebsd
if is_freebsd():
process.kill()
except KeyboardInterrupt:
print("Caught Ctrl-C, cancelling collection...")
# kill gstat if freebsd
if is_freebsd():
process.kill()
exit()
# Copy data files to /var/log (replace with appropriate copying function) # Copy data files to /var/log (replace with appropriate copying function)
collect_csv() collect_csv()