Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Last active October 16, 2024 23:08
Show Gist options
  • Save mahemoff/3c498b762af2c320835913093a19e966 to your computer and use it in GitHub Desktop.
Save mahemoff/3c498b762af2c320835913093a19e966 to your computer and use it in GitHub Desktop.
Show disk space and warn about disk full in Ansible
* Shows a message while asserting like:
ok: [host] => {
"msg": "disk usage 4.2B of total 20.0GB (21.0%) (should be within limit 90.0%)"
}
* Note this only looks at first mount point on current node
* Fails if disk is near-full
* Last step pushes to a push-based monitoring service, which will alert us if it doesn't get there after some time
* Need to setup a variable `disk_limit`, which is the max acceptable usage ratio, e.g. set it to 0.8 if you want to keep disks within 80% of max size
- set_fact:
mount: "{{ ansible_mounts | first }}"
- set_fact: disk_usage="{{ mount.size_total - mount.size_available }}"
- set_fact: disk_usage_ratio="{{ (disk_usage|float) / mount.size_total }}"
- set_fact: disk_usage_s="{{ (disk_usage|float / 1000000000) | round(1, 'common') }}GB"
- set_fact: disk_total_s="{{ (mount.size_total / 1000000000) | round(1, 'common') }}GB"
- set_fact: disk_usage_ratio_s="{{ (100 * (disk_usage_ratio|float)) | round(1, 'common') }}%"
- set_fact: disk_limit_ratio_s="{{ (100 * disk_limit|float) |round }}%"
- debug:
msg: "disk usage {{ disk_usage_s }} of total {{ disk_total_s }} ({{ disk_usage_ratio_s }}) (should be within limit {{ disk_limit_ratio_s }})"
- name:
assert:
that: ( (disk_usage|float)/mount.size_total ) < disk_limit|float
msg: "Disk usage {{ disk_usage_ratio_s }} exceeds {{ disk_limit_ratio_s }}"
any_errors_fatal: true
- name: indicate disks okay to statuscake
local_action: command /usr/bin/wget 'https://push.statuscake.com/?PK=abcdefghi&TestID=123456&time=0'
run_once: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment