Disk space script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Disk space script
# 1  
Old 04-05-2019
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
Code:
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

Code:
#!/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

Moderator's Comments:
Mod Comment Please do wrap your samples of input and expected output into CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 04-05-2019 at 12:07 PM..
# 2  
Old 04-05-2019
You are using -eq which works purely on numeric data. Remove the % with tr:
Code:
df -kh | awk '{print $5}' | tail -1 | tr -d '%'

This is somewhat clunky but you can now see the source of your problem.
# 3  
Old 04-05-2019
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
# 4  
Old 04-06-2019
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 $( )!
Code:
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.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 04-08-2019
Thanks all , this worked for me
Smilie
# 6  
Old 04-23-2019
Could someone explain line by line of how this program works starting with set -x
Code:
#!/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

Moderator's Comments:
Mod Comment
Please wrap all code, data, input & output/errors in CODE tags.
It makes them far easier to read and respects spaces for indenting for fixed-width data.

Last edited by rbatte1; 04-24-2019 at 05:36 AM..
# 7  
Old 04-24-2019
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:-
    • Using both the k & h flags for df seems pointless. Only one (usually the last mentioned) is used.
    • 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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)
  11. ExitProcess 1
    Call some external code called ExitProcess with an argument of 1. We have no way to know what this really does.
  12. fi
    End the test condition section for the last-mounted filesystem being less than '85%'
  13. 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I need help!! disk free space script

i want to write a shell script,when disk uses is 90% then automatically send a email to distribution list (group member)...... (1 Reply)
Discussion started by: sonu pandey
1 Replies

2. Shell Programming and Scripting

Disk Space Script to direct output

Hi, I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file; #!/bin/bash CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g') THRESHOLD=30 if ; then echo "Remaining free space is low" > output.txt else... (10 Replies)
Discussion started by: SSKAAB
10 Replies

3. Solaris

Disk space being used up while running a script

We have a script which when run consumes the space of the disk from where it is being run. we have to kill this script every time to release space. why do this happen ? any work around please we are using solaris 10 P.S. : a part of the code will make some connection to the DB (1 Reply)
Discussion started by: chidori
1 Replies

4. Shell Programming and Scripting

Disk Space Monitoring Script - OLD and NEW

It's the old thread "Disk Space Monitoring Script", modified for UNIX This is the new code: df -k | awk ' { if ( int($4) > 90) {subject = $1 " More than 90% disk usage. Used: " $4 email = "email@test.com" print subject cmd = "mailx -s \"" subject "\" " email cmd | getline... (4 Replies)
Discussion started by: dungureanu
4 Replies

5. Shell Programming and Scripting

disk space utilization script

Hi, I wrote the following script for monitoring disk space and inform the concerned team accordingly. But script gives me below error syntax error at line 70 : `<' unmatched #!/bin/ksh . /home/scr/.profile . /home/scr/.infa_env # Get the list of Integration Services ... (6 Replies)
Discussion started by: svajhala
6 Replies

6. Shell Programming and Scripting

Script for Disk space

:( Hi All, i have 4 linux server for which i want set up script to monitor the disk space ... here my problem is i want the output like graph... also it should reflect in monitor ...as non stop process.. can any one suggest me any way where i can implement the script? ... (3 Replies)
Discussion started by: Shahul
3 Replies

7. Shell Programming and Scripting

Please help - disk space check script

I have a disk space check script that uses an exceptions file, the only issue with the script is that it does not work with values higher than the FSMAX=85 value. I have a file system that is at 92% and it doesn't change, so I would like to add it to the exceptions file. The exceptions file format... (0 Replies)
Discussion started by: maddhadder71
0 Replies

8. Shell Programming and Scripting

Disk Space Monitoring Script

#!/bin/bash # Disk Space Monitoring for more than 95 % # and Sending Alerts by Mail if ; then `df -k |awk '$5 > 95 {print $1 " ----------- " $5}' |mailx -s "More than 95% disk usage in DEV" email@test.com'; else exit 0 fi I get the... (8 Replies)
Discussion started by: sriram003
8 Replies

9. Shell Programming and Scripting

Disk space script

Hi all, Can any one help me in making a disk space script in solaris 8/9 for instance i only want to get those partitions whose diskspace has exceed 70%. Any volunteer? Cheers! BR/asad (8 Replies)
Discussion started by: asadlone
8 Replies

10. Shell Programming and Scripting

Frustrating Disk space script

This my frustrating disk space script that is supposed to send me a email whenever the disk space reaches 90% but this has some problem that just would not work ..can anyone please tell me when im going wrong #!/bin/ksh sendemail=-1 space=`df -bhk /users/siebelserver |awk '{print$5}'` echo... (4 Replies)
Discussion started by: vivsiv
4 Replies
Login or Register to Ask a Question