From 9634bedb9b42bd5fecfad715b831878db484ea73 Mon Sep 17 00:00:00 2001 From: Wesley Ray Date: Mon, 18 Mar 2024 18:15:46 -0400 Subject: [PATCH] v1.0 it works. Gonna add it to crontab and see if anything blows up --- snapraid_smart_logging.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 snapraid_smart_logging.py diff --git a/snapraid_smart_logging.py b/snapraid_smart_logging.py new file mode 100644 index 0000000..e6ea0ec --- /dev/null +++ b/snapraid_smart_logging.py @@ -0,0 +1,48 @@ +# Poopballs + +import json, re, requests, subprocess + +WEBHOOK_URL = 'https://script.google.com/macros/s/AKfycbwgDA3qkwtRSR437gv0yGwvaIL19H3WcvtzX0eXV0D_MfOQtqrpy9-uJ1a6uF3gUIaxKw/exec' +PATTERN = r'\s+(?P\d+)\s+(?P\d+)\s+(?P\d+)\s+(?P\d+)%\s+(?P\d+.\d+)\s+(?P[A-Z\d]+)\s+(?P[a-z/]+)\s+(?P[a-z\d-]+)' +FIELD_NAMES = ['temp', 'powerOnDays', 'errCount', 'fp', 'capacity', 'serial', 'device', 'snapraidID'] +results = [] + + +def run_snapraid_smart(): + try: + # Run the command "snapraid smart" using subprocess.run() + completed_process = subprocess.run(["snapraid", "smart"], capture_output=True, text=True, check=True) + + # Check if the command was successful + if completed_process.returncode == 0: + return completed_process.stdout.strip()#.split('\n')[5:] + else: + return "Error executing SnapRAID SMART command." + except subprocess.CalledProcessError as e: + return "Error executing SnapRAID SMART command: " + str(e) + +# Call the function to run the SnapRAID SMART command and get the output +snapraid_smart_output = run_snapraid_smart() +matches = re.finditer(PATTERN, snapraid_smart_output) + +for match in matches: + row = { + FIELD_NAMES[0]: match.group(FIELD_NAMES[0]), + FIELD_NAMES[1]: match.group(FIELD_NAMES[1]), + FIELD_NAMES[2]: match.group(FIELD_NAMES[2]), + FIELD_NAMES[3]: match.group(FIELD_NAMES[3]), + FIELD_NAMES[4]: match.group(FIELD_NAMES[4]), + FIELD_NAMES[5]: match.group(FIELD_NAMES[5]), + FIELD_NAMES[6]: match.group(FIELD_NAMES[6]), + FIELD_NAMES[7]: match.group(FIELD_NAMES[7]) + } + results.append(row) + +json_payload = json.dumps(results) +response = requests.post(WEBHOOK_URL, json=json_payload) + +# Check if the request was successful (status code 200) +if response.status_code == 200: + print("POST request successful.", response.text) +else: + print("POST request failed:", response.status_code)