mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +00:00
Four probes returned the number of failures as the process exit code. An exit status is 8 bits wide, so exactly 256 or 512 failures arrive as 0 and the probe reports success. sys-service installs these as Type=oneshot units (roles/sys-service/vars/main.yml:28) and sys-service-terminator starts them with until: result is not failed, so that exit code is the whole health verdict. The count is not lost: each of the four prints it to stdout a line earlier. Only the exit status stops carrying it, because it cannot. This makes no CI job green and is not meant to. For any count other than 256 or 512 nothing changes; at those two it turns a false success into an honest failure. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
57 lines
1.4 KiB
Python
Executable File
57 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import argparse
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def get_disk_usage_percentages():
|
|
"""
|
|
Returns a list of filesystem usage percentages as integers.
|
|
Equivalent to: df --output=pcent | sed 1d | tr -d '%'
|
|
"""
|
|
result = subprocess.run(
|
|
["df", "--output=pcent"], capture_output=True, text=True, check=True
|
|
)
|
|
|
|
lines = result.stdout.strip().split("\n")[1:]
|
|
percentages = []
|
|
|
|
for line in lines:
|
|
value = line.strip().replace("%", "")
|
|
if value.isdigit():
|
|
percentages.append(int(value))
|
|
|
|
return percentages
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Check disk usage and report if any filesystem exceeds the given threshold."
|
|
)
|
|
|
|
parser.add_argument(
|
|
"minimum_percent_cleanup_disk_space",
|
|
type=int,
|
|
help="Minimum free disk space percentage threshold that triggers a warning.",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
threshold = args.minimum_percent_cleanup_disk_space
|
|
|
|
print("Checking disk space usage...")
|
|
subprocess.run(["df"], check=False)
|
|
|
|
errors = 0
|
|
percentages = get_disk_usage_percentages()
|
|
|
|
for usage in percentages:
|
|
if usage > threshold:
|
|
print(f"WARNING: {usage}% exceeds the limit of {threshold}%.")
|
|
errors += 1
|
|
|
|
sys.exit(1 if errors else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|