Created
July 25, 2021 10:14
-
-
Save danielbene/57f1fdf6d5d49b1c44ae85cf98d6250b to your computer and use it in GitHub Desktop.
Reporting NAS drive free space after server startup via discord messages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# automate reporting after homlab starts with below cronjob: | |
# @reboot sleep 60 && python3 <path_to_script> | |
import logging | |
import requests | |
import shutil | |
from datetime import datetime | |
target = '<mount_path>' | |
ts_format = '%Y.%m.%d %H:%M:%S' | |
logging.basicConfig(filename='<log_file_path>', | |
filemode='a', | |
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', | |
datefmt=ts_format, | |
level=logging.INFO) # logging.DEBUG for verbosity | |
url = '<discord_channel_webhook_url>' | |
# KB:2**10, MB:2**20, GB:2**30 etc (1024=2**10) | |
def get_free_space(): | |
free = shutil.disk_usage(target).free | |
return str(free // (2**20)) + 'MB' | |
def get_ts(): | |
now = datetime.now() | |
return now.strftime(ts_format) | |
available = get_free_space() | |
data = { | |
'content': f'{get_ts()} - Available space on device: {available}' | |
} | |
try: | |
logging.info('request start') | |
result = requests.post(url, json=data) | |
result.raise_for_status() | |
except Exception as err: | |
logging.error(f'request error: {err}') | |
else: | |
logging.info(f'request success: {result.status_code}, value: {available}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment