From 0bdc036f01220c28a33472fa115dbd79f9c4f052 Mon Sep 17 00:00:00 2001 From: Marc Mance Date: Thu, 6 Jun 2024 14:50:24 -0400 Subject: [PATCH] moved file check into statcollect function only if results are true --- ifstat.py | 16 ++++++++-------- iostat.py | 16 ++++++++-------- topstat.py | 16 ++++++++-------- zpooliostat.py | 16 ++++++++-------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/ifstat.py b/ifstat.py index be9b199..0d39a62 100644 --- a/ifstat.py +++ b/ifstat.py @@ -16,14 +16,6 @@ headerRow = [ filename = "ifStat.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 runifstat(): # Run the command and capture output @@ -36,6 +28,14 @@ def runifstat(): # Check the return code (0 for success) if result.returncode == 0: + # 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) + # Access the captured output as a string output = result.stdout diff --git a/iostat.py b/iostat.py index 98b596a..ea252ba 100644 --- a/iostat.py +++ b/iostat.py @@ -21,14 +21,6 @@ headerRow = [ 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 @@ -41,6 +33,14 @@ def runIostat(): # Check the return code (0 for success) if result.returncode == 0: + # 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) + # Access the captured output as a string output = result.stdout byline = re.split("\n", output) diff --git a/topstat.py b/topstat.py index 8d8f68f..bc41c24 100644 --- a/topstat.py +++ b/topstat.py @@ -38,14 +38,6 @@ headerRow = [ 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 convert_to_megabytes(data_size): """ @@ -89,6 +81,14 @@ def runtopstat(): # Check the return code (0 for success) if result.returncode == 0: + # 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) + # Access the captured output as a string output = result.stdout diff --git a/zpooliostat.py b/zpooliostat.py index 9c98cef..8d93143 100644 --- a/zpooliostat.py +++ b/zpooliostat.py @@ -36,6 +36,14 @@ def runZpoolIostat(): # Check the return code (0 for success) if result.returncode == 0: + # 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) + # Access the captured output as a string output = result.stdout @@ -73,14 +81,6 @@ def runZpoolIostat(): 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)