Disk space script

i have 3 servers and i am checking for the disk space of a specific mount-point, should not be more than 85 %

considering example as below

server1 mountpoint_1 has 70% diskutilization
server2 mountpoint_1 has 80% diskutilization
server3 mountpoint_1 has 7% diskutilization

now when it check for server3 it says 7%: integer expression expected

How to handle this ,Please advise

#!/bin/bash
set -x
servers="server1 server2 server3"
for i in $servers;
do
FS=`ssh user1@$i df -kh . | awk '{print $5}' | tail -1`
echo $FS="${FS:0:3}"
 FS="${FS:0:2}"
        if [ $FS -lt 85% ];then
                echo INFO : FS Is under threshold on server $i 
                ExitProcess 1
        fi
done

You are using -eq which works purely on numeric data. Remove the % with tr :

df -kh | awk '{print $5}' | tail -1 | tr -d '%'

This is somewhat clunky but you can now see the source of your problem.

Could I suggest also adding the -P flag to the df to ensure that the output is a single line per filesystem. You will need to check that it is supported on the remote OS (which you don't tell us)

There can be a risk that having a long device name or a long mount-point name may cause the output to split over two lines and your processing will have unpredictable results.

I hope that this helps,
Robin

Yes, -P ensures that long lines are not split.
And -h is "human readable", here you need the opposite.

You can strip a trailing % also in awk (with the sub function), or with a shell variable modifier.

` ` can cause a head ache with quoting (in addition to ssh). Get used to $( ) !

FS=$(ssh -nx "user1@$i" "df -kP ." | awk 'NR>1 {print $5}')
FS=${FS%\%}

The % character after the % modifier must be quoted to not conflict with the %% modifier.

1 Like

Thanks all , this worked for me
:b:

Could someone explain line by line of how this program works starting with set -x

#!/bin/bash
set -x
servers="server1 server2 server3"
for i in $servers;
do
FS=`ssh user1@$i df -kh . | awk '{print $5}' | tail -1`
echo $FS="${FS:0:3}"
 FS="${FS:0:2}"
        if [ $FS -lt 85% ];then
                echo INFO : FS Is under threshold on server $i 
                ExitProcess 1
        fi
done

Hello zbest1966,

I know it's not your thread, but it is a valid and related question. It would probably be better to explore the finished script though because there are a number of suggestions to improve things, however I will have a go exploring what you requested:-

  1. #!/bin/bash
    Signifies this as a bash script. You can actually call all sort of things here, even something like ls if you want, but then the rest of the code might be meaningless. More useful options call perl, awk or similar scripting style languages.
  2. set -x
    This turns on tracing of the process. Each command is interpreted and then displayed on stderr (usually the screen or captured to a file by running with myscript.sh 2> myscript.trace ) so everything after this gives a visible trace of processing.
  3. servers="server1 server2 server3"
    This defines a string. It happens to be a space separated list used in the next step.
  4. for i in $servers;
    This starts a loop and on each pass, the variable i is assigned the next value from the string $servers because it was used unquoted and the list is space separated.
  5. do
    After setting up the condition for the loop, this statement is the start of the loop.
  6. FS=`ssh user1@$i df -kh . | awk '{print $5}' | tail -1`
    Not great because it uses backticks ` which are deprecated and wold be better replaced by $( ^ )
    This assigned the value of the processes called to the variable FS. The call runs df -kh on the server for this loop as the user1 account. I presume that this is set up for password-less authentication. The returned output is passed into the awk which gives the fifth column only. This list is then passed into the tail which gets just the last line, so the percentage used of the last filesystem mounted.
    A few issues here:-
    [list]
  7. Using both the k & h flags for df seems pointless. Only one (usually the last mentioned) is used.
  8. The output from df for very long device names or filesystem mount-points might get split over multiple lines so grabbing the fifth column may produce unpredictable results. Better to add the -P flag to ensure it is consistent, as I commented before.
    [/list]
  9. echo $FS="${FS:0:3}"
    This line re-assigns the value for FS from the previous statements by getting the first three characters (start at position zero/skip zero and get a length of 3)
    I'm not sure what/why this is here though given the next statement.
  10. FS="${FS:0:2}"
    This line re-assigns the value for FS from the previous statement by getting the first two characters (start at position zero/skip zero and get a length of 3)
    This will normally just trim off the % however 100% from df will become a value of ten. Values from df of under 10% will still have the % in the string.
  11. if [ $FS -lt 85% ];then
    An odd test that will fail (with better suggestions in the comments from other contributors) looking for the last-mounted filesystem being under 85%. Not sure why you would want to flag this, but I'm sure there are reasons.
  12. echo INFO : FS Is under threshold on server $i
    If we pass the test above, display the message including the server that has an issue (but oddly not the filesystem)
  13. ExitProcess 1
    Call some external code called ExitProcess with an argument of 1. We have no way to know what this really does.
  14. fi
    End the test condition section for the last-mounted filesystem being less than '85%'
  15. done
    End of the loop, so if there are more values to process with in the loop definition, we assign the next to i and go round again.

I hope that this helps, but really you should try to work out the finished script that is working.

Robin