Skip to content

Instantly share code, notes, and snippets.

@canit00
Forked from alexanderadam/Ansible Disk Check
Created November 10, 2018 02:52
Show Gist options
  • Select an option

  • Save canit00/6aeabfb56f06c10843ed76eecec8d75f to your computer and use it in GitHub Desktop.

Select an option

Save canit00/6aeabfb56f06c10843ed76eecec8d75f to your computer and use it in GitHub Desktop.

Revisions

  1. @mahemoff mahemoff revised this gist Sep 15, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Ansible Disk Check
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,5 @@ ok: [host] => {

    * 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
    * 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
  2. @mahemoff mahemoff revised this gist Sep 15, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions Ansible Disk Check
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    * Shows a message like: "120.7GB of 329.7GB (57.8%)"
    * Shows a message while asserting like:

    ok: [host] => {
    "msg": "disk usage 4.2B of total 20.0GB (21.0%) (should exceed limit 90.0%)"
    }

    * Note this only looks at first mount point on current node
    * Fails if disk is near-full
    * Only looks at first mount point on current node.
    * Last step pushes to a push-based monitoring service, which will alert us if it doesn't get there after some time
  3. @mahemoff mahemoff revised this gist Sep 15, 2018. 1 changed file with 23 additions and 14 deletions.
    37 changes: 23 additions & 14 deletions check.yml
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,23 @@
    - name: show disk space
    debug: msg="{{ ((mount.size_total - mount.size_available) / 1000000000) | round(1,'common') }}GB of {{ (mount.size_total / 1000000000)|round(
    1, 'common') }}GB ({{ (100 * ( (mount.size_total - mount.size_available) / mount.size_total)) | round(1, 'common')}}%)"
    vars:
    mount: "{{ ansible_mounts | first }}"
    tags: disk

    - name: ensure at least 20% free
    assert:
    that: mount.size_available > mount.size_total|float * 0.2
    msg: Disk space is near full!
    vars:
    mount: "{{ ansible_mounts | first }}"
    tags: disk
    - 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 }}"
    tags: disk
    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
  4. @mahemoff mahemoff revised this gist Sep 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check.yml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    - name: show disk space
    debug: msg="{{ ((mount.size_total - mount.size_available) / 1000000000) | round(1,'common') }}GB of {{ (mount.size_total / 1000000000)|round(
    1, 'common') }}GB ({{ (100 * ( (mount.size_total - mount.size_available) / mount.size_available)) | round(1, 'common')}}%)"
    1, 'common') }}GB ({{ (100 * ( (mount.size_total - mount.size_available) / mount.size_total)) | round(1, 'common')}}%)"
    vars:
    mount: "{{ ansible_mounts | first }}"
    tags: disk
  5. @mahemoff mahemoff renamed this gist Sep 15, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @mahemoff mahemoff renamed this gist Sep 15, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @mahemoff mahemoff revised this gist Sep 15, 2018. 2 changed files with 5 additions and 1 deletion.
    3 changes: 3 additions & 0 deletions README>md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    * Shows a message like: "120.7GB of 329.7GB (57.8%)"
    * Fails if disk is near-full
    * Only looks at first mount point on current node.
    3 changes: 2 additions & 1 deletion check.yml
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@
    that: mount.size_available > mount.size_total|float * 0.2
    msg: Disk space is near full!
    vars:
    mount: "{{ ansible_mounts | first }}"
    mount: "{{ ansible_mounts | first }}"
    tags: disk
  8. @mahemoff mahemoff renamed this gist Sep 15, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @mahemoff mahemoff created this gist Sep 15, 2018.
    13 changes: 13 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    - name: show disk space
    debug: msg="{{ ((mount.size_total - mount.size_available) / 1000000000) | round(1,'common') }}GB of {{ (mount.size_total / 1000000000)|round(
    1, 'common') }}GB ({{ (100 * ( (mount.size_total - mount.size_available) / mount.size_available)) | round(1, 'common')}}%)"
    vars:
    mount: "{{ ansible_mounts | first }}"
    tags: disk

    - name: ensure at least 20% free
    assert:
    that: mount.size_available > mount.size_total|float * 0.2
    msg: Disk space is near full!
    vars:
    mount: "{{ ansible_mounts | first }}"