Wednesday, March 8, 2017

recursive function shell script for File System Monitoring

/opt/newdir file system is 92% full and we want to send an email to root once it goes above 90%

[root@dns1 shellscripts]# df -h /opt/newdir/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/lvm--raid-myvol01
                       97M   84M  7.9M  92% /opt/newdir

[root@dns1 shellscripts]# df -h /opt/newdir/ | awk '{print $4}' | grep -v "Avail" | sed 's/%$//'


92

[root@dns1 shellscripts]# cat recursive_functions.sh

#!/bin/bash
# define first function
my_one() {
        monitor_new_dir=`df -h /opt/newdir/ | awk '{print $4}' | grep -v "Avail" | sed 's/%$//'`
        size=90
        if [ $size -lt $monitor_new_dir ]
        then
        echo "Filesystem is $monitor_new_dir% full. Please take action"| mail -s "Do the clean up now" root
        else
        echo "You are good"
        fi
        my_two
}
my_two() {
        echo This is second function
}

my_one

             
             

Here is the OutPut:

[root@dns1 shellscripts]# ./recursive_functions.sh
This is second function
[root@dns1 shellscripts]# mail
  600 root                  Sat Mar  4 11:04  21/803   "Do the clean up now"
>N601 root                  Sat Mar  4 11:05  21/787   "Do the clean up now"
& 601
Message 601:
From root@dns1.example.com  Sat Mar  4 11:05:02 2017
Return-Path:
From: root
Date: Sat, 04 Mar 2017 11:05:02 -0800
To: root@dns1.example.com
Subject: Do the clean up now
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
Status: R

Filesystem is 92% full. Please take action

No comments: