I have a code which will give me the result of the diskspace which reached the threshold limit 70% and am redirecting it to report.txt . The code is given below.
df -g | awk '+$4 >= 70 {print}' >report.txt
Now i need your help , I need to use IF ELSE condition statment for getting the output as expected. If it reaches the threshold i should get the output which i will get for the above code, But if the server din face any threshold issues i should get the output as "No issues to report".
So In IF condition " Filesystems with 70% and more should get printed" in ELSE condition " If there is no threshold issues it should print like " NO ISSUES" ..
help needed immediately. Thanks for the help in advance
if [ ! -s report.txt ]
then
echo "No issues to report."
fi
Does that help or are you after combining it with the awk command. That would be best served by having a counter incremented with each line, then having an END section to the awk
what os and version are you using? i just tried df -g on centos, ubuntu and opensuse where i got the illegal option error on "-g" ... i tried on solaris 10 and df -g does not give me a percent result ...
i have no access to an aix box so i will take your word about the df -g output ... see below for modified code based on your requirements ...
df -g | awk '+$4 >= 70 {print}' > report.txt
if [ ! -s report.txt ]
then
echo "No issues to report."
else
echo "Filesystems at least 70% full"
cat report.txt
fi
your awk '+$4 >= 70 {print}' is actually looking for a value equal to or greater than 70 in column 4 of the df -g output as per your requirement ... not want the Filesystem line? just grep it out ...
df -g | awk '+$4 >= 70 {print}' | grep -v Filesystem > report.txt
if [ ! -s report.txt ]
then
echo "No issues to report."
else
echo "Filesystems at least 70% full"
cat report.txt
fi
The thing is there is no threshold limit crossed , so the output for that should be "NO issues to report" . Am i right? but i got the line "Filesystem GB blocks Free %Used Iused %Iused Mounted on" instead of "No issues to report".
first off, assistance you receive on this website is offered by unpaid volunteers on their own time -- that includes me -- so it would be good to maintain a professional tone ...
second, your issue is that your report is empty except for the Filesystem line and i have given you the fix for it which is to grep it out (see red font in prior post) ... if you run the df -g | awk '+$4 >= 70 {print}' by itself and no filesystem is over the threshold, you will only get the Filesystem line ... if you run the same command and a filesystem reaches the threshold, the Filesystem line will get printed with the actual filesystem info that hit the threshold ...
you need to reread the script as given and test as well as understand each line before you demand help
I apologize for the wrong tone, i din mean to express like that. I dont know whether you got me or not. AM sorry for the words used. And i will try using the grep code which you shared.
---------- Post updated at 11:57 PM ---------- Previous update was at 11:54 PM ----------
df -g | awk 'NR>1&&($4+0)>70' > report.txt
if [ -s report.txt ]
then
echo "Filesystems above 70% utilization"
cat report.txt
else
echo "No filesystems found above 70% utilization"
fi
This one worked Thanks for the code.. And I apologize all if i went wrong in my words.
Just adding my 2 cents here. Another way to get this done would be:
#!/bin/bash
# Do not show the titles of columns:
df -H | grep -vE '^Filesystem' | awk '{ print $5 " " $1 }' | while read output;
do
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 90 ]; then
# Here you can either display a message to the screen or log the results to a file
echo "Running out of space $partition ($usep%) on $(hostname) as on $(date +%Y%m%d)"
fi
done
If your question has been answered to your satisfaction, please mark this thread as solved for the future reference of other users. Thanks.
I have used the code given and edited some parts and implemented the code in my servers . The code implemented was given below:
#Report file format for all the servers to grep the threshold reached files ( Script name : Report.sh)
df -g | awk 'NR>1&&($4+0)>70' > report010.txt
if [ -s report151.txt ]
then
cat report010.txt >> report010.txt
else
echo "No file systems found above 70% utilization on the server `hostname`">>report010.txt
fi
#Concatenating all reports in one mail (Script name: Mail.sh)
echo > blank_line
echo �The list of File systems that reached threshold were given below� > mailbody.txt
echo �---------------------------------------------------------------------------------�>>mailbody.txt
cat report010.txt blank_line report011.txt >>mailbody.txt|mailx -s "Disk space Report-$(date +'%A %B %d, %Y')� �r �DSReport@office.com� hariharan.s@office.com
And i Got the output as below
From: DSReport@office.com [mailto:DSReport@office.com]
Sent: Thursday, August 08, 2013 6:33 PM
To: Sundaram, Hariharan
Subject: Disk space Report-Thursday August 08, 2013
The list of File systems that reached threshold were given below
--------------------------------------------------------------
Disk space report for vathpubs010
/dev/hd2 5.88 1.76 71% 61014 13% /usr
/dev/fnetwebc_lv 2.00 0.34 84% 3558 5% /opt/IBM/FileNet/WebClient
Disk space report for vathpubs011
/dev/fnetwebc_lv 2.00 0.34 84% 3558 5% /opt/IBM/FileNet/WebClient
No Issues to report for the server vathpubs012
No Issues to report for the server vathpubs013
No Issues to report for the server vathpubs014
Disk space report for vathpubs015
/dev/fnetwebc_lv 2.00 0.34 84% 3558 5% /opt/IBM/FileNet/WebClient
Disk space report for vathpubs016
/dev/fnetwebc_lv 2.00 0.34 84% 3558 5% /opt/IBM/FileNet/WebCli
No Issues to report for the server vathpubs017
No Issues to report for the server vathpubs018
I have achieved the goal , but there was some additional request requested by my leads, They want the output like the above but some little modifications required, thats is We got the output but They need the "Disk space report for" (with filesystem) sorted first and then the "no issues to report" these servers should come like " The list of servers which are not having issues are given below: " with the servernames below.
For example :
The list of servers thats not facing the space issue are:
vathpubs017
vathpubs018
So is there any possibilities to bring this output? Please help me .
Well done! But in your last request you are literally drowning in a glass of water, considering the long way you've already come up to this point with your script.
Your request can easily be implemented with something like this (pseudo code):
1) Log the list of servers with disk space issue to one file, say servers_with_issues.txt .
2) Log the list of servers without issues to a separate file, say servers_without_issues.txt .
In your script, and in the place that you want, first echo the legend "Servers with issues" and then cat the first file.
Then echo the legend "Servers without issues" and then cat the 2nd file.
The rest remains basically the same.
Good luck!
Yeah definitely i will use the ideas which you shared and i had an idea, but could you please let me know whether this will help for me..
In this below code :
df -g | awk 'NR>1&&($4+0)>70' > report010.txt
if [ -s report010.txt ]
then
cat report010.txt >> report010.txt
else
echo "`hostname`"> noissues.txt
fi
I have redirected the echo part to the same text file "report010.txt" instead am planning to redirect it to a different file named "noissues.txt" and followed by this below code
#Concatenating all reports in one mail (Script name: Mail.sh)
echo > blank_line
echo �The list of File systems that reached threshold were given below� > mailbody.txt
echo �---------------------------------------------------------------------------------�>>mailbody.txt
cat report010.txt blank_line report011.txt >>mailbody.txt
echo" The servers listed below were not facing not any issues" > noproblem.txt
cat noissues010.txt blank_line noissues011.txt >> noproblem.txt
cat mailbody.txt noproblem.txt |mailx -s "Disk space Report-$(date +'%A %B %d, %Y')� -r �DSReport@office.com� hariharan.s@office.com
How about this plan? Whether it will help me out to get the expected output
will echo the name of the host where you're running the script to the noissues.txt file, instead of saving the name of the server that is not experiencing disk space issues.
If I were you, I'd take note of all the suggestions that you've received in this thread and start from scratch. It should not be hard to do.