diskmon 0.1.0__104dbeeaa-py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diskmon/disk_monitor.py +86 -0
- diskmon/fs_types.so +0 -0
- diskmon-0.1.0.dist-info/METADATA +4 -0
- diskmon-0.1.0.dist-info/RECORD +5 -0
- diskmon-0.1.0.dist-info/WHEEL +4 -0
diskmon/disk_monitor.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
import logging
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
# Thresholds for disk usage percentage
|
|
6
|
+
DISK_WARN_THRESHOLD = 80 # log a warning when disk is this full
|
|
7
|
+
DISK_ABORT_THRESHOLD = 95 # log a critical error and exit when disk is this full
|
|
8
|
+
|
|
9
|
+
# Path to the log file
|
|
10
|
+
LOG_FILE = "/var/log/freespace.log"
|
|
11
|
+
|
|
12
|
+
# Disk path to monitor
|
|
13
|
+
DISK_PATH = "/"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_logger() -> logging.Logger:
|
|
17
|
+
"""Configure and return a logger that writes to both a file and stdout."""
|
|
18
|
+
logger = logging.getLogger("disk_monitor")
|
|
19
|
+
logger.setLevel(logging.DEBUG)
|
|
20
|
+
|
|
21
|
+
fmt = logging.Formatter(
|
|
22
|
+
fmt="%(asctime)s [%(levelname)s] %(message)s",
|
|
23
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
# File handler — requires write permission to LOG_FILE
|
|
27
|
+
try:
|
|
28
|
+
fh = logging.FileHandler(LOG_FILE)
|
|
29
|
+
fh.setFormatter(fmt)
|
|
30
|
+
logger.addHandler(fh)
|
|
31
|
+
except PermissionError:
|
|
32
|
+
print(f"[WARN] No permission to write to {LOG_FILE}. Logging to stdout only.")
|
|
33
|
+
|
|
34
|
+
# Console handler — always active
|
|
35
|
+
ch = logging.StreamHandler()
|
|
36
|
+
ch.setFormatter(fmt)
|
|
37
|
+
logger.addHandler(ch)
|
|
38
|
+
|
|
39
|
+
return logger
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def check_disk_space(path: str, logger: logging.Logger) -> bool:
|
|
43
|
+
"""
|
|
44
|
+
Check disk usage at the given path and log the result.
|
|
45
|
+
|
|
46
|
+
Returns True if usage is below DISK_ABORT_THRESHOLD, False otherwise.
|
|
47
|
+
"""
|
|
48
|
+
usage = shutil.disk_usage(path)
|
|
49
|
+
|
|
50
|
+
used_pct = usage.used / usage.total * 100
|
|
51
|
+
free_gb = usage.free / (1024 ** 3)
|
|
52
|
+
total_gb = usage.total / (1024 ** 3)
|
|
53
|
+
|
|
54
|
+
# Human-readable summary included in every log record
|
|
55
|
+
summary = (
|
|
56
|
+
f"disk={path!r} "
|
|
57
|
+
f"used={used_pct:.1f}% "
|
|
58
|
+
f"free={free_gb:.2f}GB / {total_gb:.2f}GB"
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
if used_pct >= DISK_ABORT_THRESHOLD:
|
|
62
|
+
# Disk is critically full — caller should stop any heavy operation
|
|
63
|
+
logger.critical(f"Disk critically full. {summary}")
|
|
64
|
+
return False
|
|
65
|
+
|
|
66
|
+
if used_pct >= DISK_WARN_THRESHOLD:
|
|
67
|
+
# Disk is getting full — warn so the operator can act
|
|
68
|
+
logger.warning(f"Low disk space. {summary}")
|
|
69
|
+
else:
|
|
70
|
+
# Plenty of space available — routine info record
|
|
71
|
+
logger.info(f"Disk space OK. {summary}")
|
|
72
|
+
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if __name__ == "__main__":
|
|
77
|
+
logger = get_logger()
|
|
78
|
+
|
|
79
|
+
logger.info("=" * 60)
|
|
80
|
+
logger.info(f"Disk monitor started at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
81
|
+
|
|
82
|
+
ok = check_disk_space(path=DISK_PATH, logger=logger)
|
|
83
|
+
|
|
84
|
+
if not ok:
|
|
85
|
+
# Non-zero exit code signals failure to any calling process or CI system
|
|
86
|
+
raise SystemExit(1)
|
diskmon/fs_types.so
ADDED
|
Binary file
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
diskmon/disk_monitor.py,sha256=cA4TH836Dz-sfRQRYeDIY4utm-qpdO5Rpb6Sto-MjtI,2534
|
|
2
|
+
diskmon-0.1.0.dist-info/METADATA,sha256=_qgFztpZ-QUyxhy_WYjQj2mQ07k2m0TAEKkxWYQQJxs,78
|
|
3
|
+
diskmon-0.1.0.dist-info/WHEEL,sha256=ecvr-UMg8NATu7SzWPtikHyoPU6iJZ_q1ow9yAlbDzQ,89
|
|
4
|
+
diskmon/fs_types.so,sha256=y_qv5YCh1hjvx2Ce55Yi7SqN5v2W3vi-5ExgCtRRSck,2568
|
|
5
|
+
diskmon-0.1.0.dist-info/RECORD,,
|