• Uncategorized

About linux : check-the-filesystem-percentage-and-run-script-with-case-commands

Question Detail

i am new to bash scripting, trying to run the below script where i should check the system percentage and run the another script using case command

#!/bin/bash
clear
echo "will able to see the percentage of hard disk"
for line in $(</home/AAA/BBB/CCCC/pre-prod1.txt)
do
ssh $line 'hostname -s; df -P |grep '/XXX/CCCC' | awk '"'"'{print $5}'"'"
echo '--------------------------------------------------------------------------------------------'
usage = $(< 'hostname -s; df -P |grep '/opt/splunk' | awk '"'"'{print $5}'"'" )
#if [$? -gt 65]
#   then
#        '/home/ BB/DD /remove_old_data.sh'
#
#fi
done

case $usage in
     50% - 2880 minutes) echo "when the hard disk is above 50%"
         /home/BB/DD/remove_old_data.sh 2880
          ;;
     50-75% - 1440 minutes) echo "when the hard disk space is above 75%"
         /home/ BB/DD /remove_old_data.sh 1440
         ;;
     75-90% - 720 minutes) echo "when the hard disk space is above 90%"
        /home/ BB/DD /remove_old_data.sh 720
        ;;
     >90% - 360 minutes) echo "when the hard disk space above 91%"
        /home/ BB/DD /remove_old_data.sh 360
       ;;
esac

i am getting the below error :

./checksize1.sh: line 9: hostname -s; df -P |grep /opt/splunk | awk '{print $5}': No such file or directory
./checksize1.sh: line 9: usage: command not found
./checksize1.sh: line 19: syntax error near unexpected token `-'
./checksize1.sh: line 19: `     50% - 2880 minutes) echo "when the hard disk is above 50%"'

Question Answer

I have made some modifications in your script and it should fulfill your requirement:

#!/bin/bash
clear
echo "will able to see the percentage of hard disk"
for line in $(cat /home/AAA/BBB/CCCC/pre-prod1.txt)
do 
usage=$(ssh $line "df -P |grep '/opt/splunk'" | awk '{print $5}' | cut -d'%' -f1)
echo '-------------------------------------------------------------------------'
#if [$? -gt 65]
#   then
#        '/home/ BB/DD /remove_old_data.sh'
#
#fi
echo "Machine name: $( echo $line | cut -d'@' -f2 )"
    case $usage in
         50 ) echo "when the hard disk is above 50%"
            /home/BB/DD/remove_old_data.sh 2880
            ;;
         [5-6][0-9]|7[0-5] ) echo "when the hard disk space is above 75%"
             /home/BB/DD/remove_old_data.sh 1440
            ;;
         7[6-9]|8[0-9]|90 ) echo "when the hard disk space is above 90%"
            /home/BB/DD/remove_old_data.sh 720
            ;;
         9[1-9]|100 ) echo "when the hard disk space above 91%"
            /home/BB/DD/remove_old_data.sh 360
            ;;
          * ) echo "Disk space is less than 50%"
            ;;
    esac
done

The file /home/AAA/BBB/CCCC/pre-prod1.txt should contain the hostnames of the server like below:

[email protected]
[email protected]
[email protected]r3

Please check this and let me know if your requirement is something else.

NOTE: I have updated the script. Previously, I had given partition as /dev/sda1, however, I believe you are trying to get the disk usage of the partition /opt/splunk. I have made changes in my script and this should work fine. In case, you are trying to get disk usage of any other partition on the servers, please update in the below line in script:

usage=$(ssh $line "df -P |grep '/opt/splunk'" | awk '{print $5}' | cut -d'%' -f1)

I have tested the script now on test servers and found the below results.

[12:41]✓ -> bash test.sh 
will able to see the percentage of hard disk
-------------------------------------------------------------------------
Server: server1 
82
when the hard disk space is above 90%
-------------------------------------------------------------------------
Server: server2
87
when the hard disk space is above 90%
-------------------------------------------------------------------------
Server: server3
50
when the hard disk is above 50%

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.