62 lines
1.5 KiB
Python
62 lines
1.5 KiB
Python
"""
|
|
Monitors disk usage and notifies on low
|
|
|
|
Need to pass the path argument as first, e.g.:
|
|
|
|
python disk_monitor.py /
|
|
"""
|
|
import argparse
|
|
import logging
|
|
import shutil
|
|
import time
|
|
|
|
|
|
import config
|
|
import lib
|
|
|
|
logging.basicConfig()
|
|
logger = logging.getLogger()
|
|
|
|
|
|
def setup_logging_level(debug=False):
|
|
log_level = logging.DEBUG if debug else logging.ERROR
|
|
logger.setLevel(log_level)
|
|
logger.debug("Debugging enabled")
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('path', help="Path to monitor")
|
|
parser.add_argument('--debug', dest='debug', action='store_true')
|
|
return parser.parse_args()
|
|
|
|
|
|
def get_free_disk_space_g(path):
|
|
free_disk_space = shutil.disk_usage(path).free
|
|
free_disk_space_g = round(free_disk_space / (1024 ** 3), 1)
|
|
return free_disk_space_g
|
|
|
|
|
|
def loop(args):
|
|
is_low_disk = False
|
|
while True:
|
|
free_disk_space_g = get_free_disk_space_g(args.path)
|
|
logging.debug(f"Free disk space: {free_disk_space_g} G")
|
|
if free_disk_space_g < config.MIN_FREE_DISK_SPACE_G:
|
|
if not is_low_disk:
|
|
message = f"Less than {config.MIN_FREE_DISK_SPACE_G}G of free disk space available on {args.path}."
|
|
logging.warning(message)
|
|
lib.send_notification("Disk Space Alert", message)
|
|
is_low_disk = True
|
|
time.sleep(config.TIME_SLEEP_DISK_S)
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
setup_logging_level(args.debug)
|
|
loop(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|