Ansible playbook for collecting Memory, Disk & IOwait from inventory

Ansible playbook for collecting Memory, Disk & IOwait from inventory…

Requirements:-

  • Need to configure Ansible server
  • Need to create a local user account on all hosts
  • Need to establish ssh key less authentication from Ansible server local account to remote servers local acc

Below Script execution requirements:-

  • Need to create diskcheck.sh  script by copying below lines
  • Need to create a folder named “file”
  • Need to create a YAML file by coping below YAML file contents
  • Need to create an inventory file with list of hostnames

 

Once all are completed, you can execute the ansible playbook like below:-

$  ansible-playbook  -i <inventory_file> <filename.yml>

  1. create a bash script: (diskcheck.sh)

#!/bin/bash
rm -f /var/tmp/fsv112
rm -f /var/tmp/fsv11

# Mem utilization
os=`cat /etc/redhat-release |awk {‘print $7′}|cut -d’.’ -f1`
if [ $os = 7 ]; then
free|grep Mem|awk ‘{fre1=($2-$7)*100/($2)} END {print fre1}’|awk ‘{printf(“%.0f\n”, $1)}’>> /var/tmp/fsv11
else
free|grep Mem|awk ‘{fre1=($2-$4-$6-$7)*100/($2)} END {print fre1}’|awk ‘{printf(“%.0f\n”, $1)}’>> /var/tmp/fsv11
fi

#iowait checking
iostat | awk ‘FNR == 4 { print $4}’ >> /var/tmp/fsv11

#Disk checking
df -HlP| grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $6 }’ | sed “s/%//” | while read LINE;
do
fsv1=`echo $LINE | cut -d ‘ ‘ -f1`;
if [ $fsv1 -gt 80 ]; then
echo $LINE >> /var/tmp/fsv11; fi done
echo -n “`hostname`,`cat /var/tmp/fsv11 | tr ‘\n’ ‘,’`” >> /var/tmp/fsv112
sed -i -e ‘$a\’ /var/tmp/fsv112
rm -f /var/tmp/fsv11

======

Then  create a filename.yml

 


– hosts: all
gather_facts: no
ignore_errors: yes
tasks:
– name: Delete old files
run_once: yes
delegate_to: localhost
file: path=file/*.* state=absent
– name: Transfer the script and execute a script
script: diskcheck.sh
– name: Fetch source list from clients
fetch:
src: /var/tmp/fsv112
flat: yes
dest: “file/{{ inventory_hostname }}.sourcelist”
– name: Merge files
run_once: yes
delegate_to: localhost
shell: “cat file/*.sourcelist >> file/allnodes.xls”
– name: Sort The File
run_once: yes
delegate_to: localhost
shell: “sort -r -t, -k4 file/allnodes.xls >> file/allnodes_sort.xls”
– name: Adding Header and making exel
run_once: yes
delegate_to: localhost
shell: “echo ‘Hostname,Mem_used(%),IOwait(%),FS1(%),FS2(%),FS3(%),FS4(%)’ >> file/allnodes_output.xls; cat file/allnodes_sort.xls >> file/allnodes_output.xls; cat file/allnodes_output.xls | tr ‘,’ ‘\t’ >> file/allnodes_output_`date +%m-%d-%Y`.xls”
– name: Sending excel file to users
run_once: yes
delegate_to: localhost
shell: “echo ‘All Linux Servers Health report’ | mailx -s ‘Automated Servers Health Report’ -a file/allnodes_output_`date +%m-%d-%Y`.xls  techtvm@gmail.com”

 

 

 

Leave a comment