Ansible Disk Usage and Disk Space Full Alert generation Script.

9,306 views
Skip to first unread message

Kiran Shrimali

unread,
Mar 7, 2018, 11:43:31 PM3/7/18
to Ansible Project
Hi,

I am tryig to write an Ansible script which will Generat Alert on Disk Usage. Once the Disk Usage goes above the Threshold, alert will be generated. 

Below is start up code.

---
 - hosts: "{{ hostGroup }}"
   user: gamesroot
   become: true
   become_user: root
   gather_facts: no
   tasks:
   - shell: df -h "{{ item }}" | tail -n 1 | awk {'print $5 '}  | sed 's/%//g'
     with_items:
      - /
      - /home
      - /backup
     register: test

   - debug:
       msg: "{{ test.results|map(attribute='stdout')|list }}"
   - name: Size is big
     shell: echo "tobig size "
     when: test.results > 20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Here Problem isi  not able to Register Value and Compare it with Threshold. 

PLEASE help me for the SAME.

Thomas Hikade

unread,
Mar 9, 2018, 8:15:56 AM3/9/18
to Ansible Project
Hi Kiran

A few problems: 
First. you register the results (plural) of your with_items loop in the first shell command. That means, test.results is actually a list of result objects!
Thus you cannot simply compare the value "20" with test.results. Instead, you need to loop over test.results again and do the comparison for each result in turn.
Second, printing out anything using shell cmd is not really working, because you wont actually see stdout! You would need to register the cmd output again and then print that using a debug statement in a loop again.


eg like this:
  - name: Size is big
    debug:
      msg:  "too big size: {{ item}} "
    when: item > "20"
    with_items: "{{ test.results|map(attribute='stdout')|list}}"

Note: I am extracting stdout attribute like you did, as Ansible will verbosely print out the entire results items structure in debug statement loops, which makes it hard to read.

regards
Thomas

Thomas Hikade

unread,
Mar 9, 2018, 8:22:58 AM3/9/18
to Ansible Project
In addition you might like to print out the name of your FS which is too big. For that you need the names again in the last debug loop.
You could fix that like this, using advanced looping statement "with_together" (Ansible Loops)

  vars:
    filesystems:
      - /
      - /tmp

    size_too_big: "20"

  tasks:

...

  - name: Size is big v2
    debug:
      msg:  "{{ item.1 }} is too big! size={{ item.0 }} "
    when: item.0 > size_too_big
    with_together:
       - "{{ test.results|map(attribute='stdout')|list }}"
       - "{{ filesystems }}"

This will produce the following output:

TASK [Size is big v2] **********************************************************
ok: [demobox] => (item=[u'37', u'/']) => {
    "item": [
        "37",
        "/"
    ],
    "msg": "/ is too big! size=37 "
}
ok: [demobox] => (item=[u'37', u'/tmp']) => {
    "item": [
        "37",
        "/tmp"
    ],
    "msg": "/tmp is too big! size=37 "
}


Kiran Shrimali

unread,
Mar 12, 2018, 2:45:33 AM3/12/18
to Ansible Project
---
 - hosts: test-servers
   user: gamesroot
   become: true
   become_user: root
   gather_facts: yes 
   vars:
       mountpoint: "{{m}}"

   tasks:
   - shell: df -h "{{mountpoint}}" | tail -n 1 | awk {'print $5 '}  | sed 's/%//g'
     register: test

   - shell: rm -rf /tmp/disk.txt; touch /tmp/disk.txt
     delegate_to: localhost
 
   - shell: echo "{{ inventory_hostname }}"
     register: op

   - debug:
       msg:
         - "{{test.stdout}}"
         - "{{ op.stdout }}"

   - name: add lines
     lineinfile: 
       dest: /tmp/disk.txt 
       line: "{{ m }} HAS REACHED {{ test.stdout }} % ON {{ op.stdout }}, KINDLY CHECK !!!"
       state: present
     delegate_to: 127.0.0.1 
     register: msgtxt
     with_items:
       - "{{ test.stdout }}"
       - "{{ op.stdout }}"
     when: test.stdout|int > 5 
   - local_action: copy content= "{{ msgtxt.results }}" dest=/tmp/disk2.txt 


I have create this , everything is working fine.. Now i am giving mountpoint at the run time. 
Now if i want to define more then one 1 mountpoint, how can it will compete the full loop for one mount point and next it will jump on second mountpoint. 

e.g. vars:
       mountpoint: 
           - /
           - /home
           - /backup
           - /dev
Message has been deleted

Varun Chopra

unread,
Mar 12, 2018, 4:20:23 AM3/12/18
to Ansible Project
You can use it as a role and loop over it:

- name: Disk role.
      include_role:
        name: disk_role
      vars:
        mount_point: "{{ item }}"
      with_items:
        - "/dev"
        - "/backup"

Varun Chopra

unread,
Mar 12, 2018, 4:21:16 AM3/12/18
to Ansible Project
Don't mind the messed up indent...

Jean-Yves LENHOF

unread,
Mar 12, 2018, 4:57:05 AM3/12/18
to ansible...@googlegroups.com



Le 08/03/2018 à 05:43, Kiran Shrimali a écrit :
Hi,

I am tryig to write an Ansible script which will Generat Alert on Disk Usage. Once the Disk Usage goes above the Threshold, alert will be generated. 

Below is start up code.

---
 - hosts: "{{ hostGroup }}"
   user: gamesroot
   become: true
   become_user: root
   gather_facts: no
   tasks:
   - shell: df -h "{{ item }}" | tail -n 1 | awk {'print $5 '}  | sed 's/%//g'
     with_items:
      - /
      - /home
      - /backup
     register: test

   - debug:
       msg: "{{ test.results|map(attribute='stdout')|list }}"
   - name: Size is big
     shell: echo "tobig size "
     when: test.results > 20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Why not using ansible_mounts fact instead of running df ? Shell task should be avoided each time it is possible !
It already has information about FS

Regards,

JYL

Varun Chopra

unread,
Mar 12, 2018, 5:22:32 AM3/12/18
to Ansible Project
This thread is filled with red flags, and im not even talking about the fact that he's using ansible for monitoring....
Reply all
Reply to author
Forward
0 new messages