first incarnation
This commit is contained in:
62
ifstat.py
Normal file
62
ifstat.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import subprocess
|
||||||
|
import re
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Define your shell command
|
||||||
|
command = ["ifstat", "-znq", "-b", "1", "1"]
|
||||||
|
|
||||||
|
headerRow = [
|
||||||
|
"timestamp",
|
||||||
|
"name",
|
||||||
|
"Kbps_in",
|
||||||
|
"Kbps_out",
|
||||||
|
]
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
bylines = output.split("\n")
|
||||||
|
interfaces = bylines[0].split()
|
||||||
|
stats = bylines[2].split()
|
||||||
|
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
if lineData:
|
||||||
|
with open(filename, "a", newline="") as csvfile:
|
||||||
|
csv_writer = csv.writer(csvfile)
|
||||||
|
csv_writer.writerow(lineData)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"Error running command: {result.stderr}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
runifstat()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user