43 lines
1.3 KiB
Python
Executable file
43 lines
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
# nonstdlib requirements: apprise
|
|
# to be invoked via btrfs-scrub@.(timer+service)
|
|
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import apprise
|
|
|
|
MATRIX_USER = os.environ['MATRIX_USER']
|
|
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
|
|
MATRIX_ROOM = os.environ['MATRIX_ROOM']
|
|
|
|
MAIL_USER = os.environ['MAIL_USER']
|
|
MAIL_PW = os.environ['MAIL_PASSWORD']
|
|
MAIL_TO = os.environ['MAIL_TO']
|
|
|
|
|
|
def report_results(res: subprocess.CompletedProcess) -> None:
|
|
print(res.stdout)
|
|
print(res.stderr, file=sys.stderr)
|
|
apobj = apprise.Apprise()
|
|
with apprise.LogCapture(level=apprise.logging.INFO) as output:
|
|
apobj.add(f'matrixs://{MATRIX_USER}@nitro.chat/{MATRIX_ROOM}?pass={MATRIX_PASSWORD}')
|
|
# can't specify user & pw at the start
|
|
# https://github.com/caronc/apprise/wiki/Troubleshooting#special-characters-and-url-conflicts
|
|
apobj.add(
|
|
f'mailtos://@matous.dev:465?user={MAIL_USER}&stmp=mx1.matous.dev&from=Apprise Monitor <{MAIL_USER}>&pass={MAIL_PW}&to={MAIL_TO}&mode=ssl'
|
|
)
|
|
|
|
title = f'{platform.node()} BTRFS scrub finished: {res.returncode}'
|
|
apobj.notify(title=title, body=f'Summary:\n{res.stdout}\n\n{res.stderr}')
|
|
print(output.getvalue())
|
|
|
|
|
|
path = Path(sys.argv[1])
|
|
cmd = ['/bin/btrfs', 'scrub', 'start', '-d', '-B', path]
|
|
res = subprocess.run(cmd, text=True, capture_output=True, check=False)
|
|
report_results(res)
|