Reading a unix flat file and mailing the contents when PCTUSED is 90%

Hello,

I am trying to write a shell script that reads the unix flat file and email me those lines only when PCT_USED is greater than 50% and I got stuck. Can anyone please help me ASAP. Thanks in advance!

The flat file format looks like this:

HOSTNAME
FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON

and the following is the script that I started out before I got stuck:

#!/bin/ksh                                                                      
                                                             
cat unix flat file |while read record              
do                                                                              
num_col=`echo $record|wc -w`                                                  
if [ $num_col -eq 1 ];                                                        
then                                                                          
  HOSTNAME=${record}                                                          
echo $HOSTNAME                                                                  
else                                                                          
  if  [ -n $record ];                                                        
  then                                                                        
    echo $record|read FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON    
fi
fi
done      
The script works fine upto here and reads the values but then where I go from here, how do you give the condition: [if $PCTUSED -gt 50] then mailx $hostname, $filesystem,$allocated,$used,$free,$pctused,$mounton

Any help is appreciated!    
issa7860Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HamdyHassanCommented:
 if [ $i -gt 200 ]; then
   echo more than 200
  else
   echo $i
  fi


Here is example .............


1657 bin/ee> cat hh
100
200
300
400

1656 bin/ee>  h9 hh
100
100
200
200
300
more than 200
400
more than 200

1659 bin/ee> cat h9
#!/bin/ksh
export myfile=$1
for i in `cat $myfile`
do
  echo $i
  if [ $i -gt 200 ]; then
   echo more than 200
  else
   echo $i
  fi
done
0
yokelCommented:
if [ $PCTUSED -gt 50 ]
then
 echo "$hostname $filesystem,$allocated,$used,$free,$pctused,$mounton " | mailx -s "Filesystem Report"
fi
0
rob-gCommented:
Assuming that your datafile looks something like
host1
1 2 3 4 5 6
host2
1 2 3 4 51 6
host3
1 2 3 4 50 6
host4
1 2 3 4 52 6
host5
1 2 3 4 5 6


awk 'NF == 1 {host=$0} NF == 6 {if ($5 > 50) print host " : " $0}' data.dat | mailx -s "PCT_USED > 50%" issa7860@domain.com
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

yokelCommented:
Oh yeah sorry, forgot put the e-mail address you are sending to at the end of the mailx! So in my answer above the mailx command is..

 mailx -s "Filesystem Report" user@domain.com
0
issa7860Author Commented:
yokel,

Thanks for the help!

Where should I plug you script at the end of the DONE statement in my script. My script works fine, all I need is the last part. Please let me know.

Thanks.
0
issa7860Author Commented:
I get the following error when plug your script at the end of mine script:

ksh: 21%: 0403-053 Expression is not complete; more tokens expected.

What missing?
0
HamdyHassanCommented:
Hey
Why you didn't try my post?

 
if [ $PCTUSED  -gt 200 ]; then
echo "$hostname $filesystem,$allocated,$used,$free,$pctused,$mounton " | mailx -s "Filesystem Report"
fi
0
HamdyHassanCommented:
I mean 50 , plus you need email address like issa7860@host.com at mailx command



if [ $PCTUSED  -gt 50 ]; then
echo "$hostname $filesystem,$allocated,$used,$free,$pctused,$mounton " | mailx -s "Filesystem Report"  issa7860@host.com
fi
0
yokelCommented:
issa7860 your error is probably due to me missing off the e-mail address of the recipient. Do as HamdyHassan says and it should work.
0
issa7860Author Commented:
yokel,

well I plugged your script in my script in the following way:

echo $record|read FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON
else
if [ $PCTUSED  -gt 50 ]; then
echo "$hostname $filesystem,$allocated,$used,$free,$pctused,$mounton " | mailx -s "Filesystem Report"  issa7860@host.com
fi
fi
fi
done      

The script executed but no mail received. Can you please check that I put it in the right place.
0
issa7860Author Commented:
I plugged your script after the last line in my script which is a DONE statement and I still received the following error: ksh: [21%:  not found.

any idea?? am I putting in the right place??

The PCTUSED has the following values:
35%
41%
67%
.
.
.
21%

Can someone help me on this and I don't want to change my script.
0
yokelCommented:
To get rid of the ksh error try stripping the % from PCTUSED

ie.

TMP=`echo ${PCTUSED} | sed "s/\%//"`

Then in your if..then statement test for $TMP to be over 50 instead of $PCTUSED ie

if [ $TMP -gt 50 ]
then
echo .....
fi

As for the mail getting recieved can you normally send mail from the server to the recipients server?

0
issa7860Author Commented:
I got rid of % sign and still get the same error.

Any ideas??
0
yokelCommented:
There are spaces between "[" and $TMP and spaces between "50" and "]"
0
issa7860Author Commented:
The scripts is only taking the last value of 21 and ignoring other values and when I used the if statement for -eq 21 then it's working but for -gt 50 it's not working. Yokel, do you have any suggestion?
0
rob-gCommented:
issa7860,

Can I suggest that you post (cut & paste) your data file (or an excerpt) AND your shell script so far. Then it will be easier to iron out any formatting or script perculiarities without guessing about what you've done already.
0
issa7860Author Commented:
rob-g,

The following is the script I have so far:



#!/bin/ksh                                                                      
                                                         
cat unix flat file |while read record              
do                                                                              
num_col=`echo $record|wc -w`                                                  
if [ $num_col -eq 1 ];                                                        
then                                                                          
 HOSTNAME=${record}                                                          
echo $HOSTNAME                                                                  
else                                                                          
 if  [ -n $record ];                                                        
 then                                                                        
   echo $record|read FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON

TMP=`echo ${PCTUSED} | sed "s/\%//"`

   
fi
fi
done      


                                 
                                               
if [ $TMP -gt 50 ];  

                                                       
then                                                                            
echo "$HOSTNAME, $FILESYSTEM, $ALLOCATED, $USED, $FREE, $val%, $MOUNTON"|mailx -
s "Filesystem Report" \ name@server.com                                                
fi                                                            

The data file has the following format:


HOSTNAME
FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON

first line has a hostname and second line have the 6 fields and on with 24 lines. What I was looking for is when PCTUSED is > 50% then it should email to me the hostname plus the 6 fields only those lines where PCTUSED is >= 50%.

Any help is appreciated!


                                                                                           
0
HamdyHassanCommented:
It's recursion too deep problem, you need AWK for second test



here is the script - I tested and it's working


#!/bin/ksh                                                                      
cat unix_flat_file |while read record              
do                                                                              
   num_col=`echo $record|wc -w`                                                  
   if [ $num_col -eq 1 ]; then                                                                          
      HOSTNAME=${record}                                                          
      echo $HOSTNAME                                                                  
   else
     export F5=`echo $record | awk '{print $5}' |sed -e "s/\%//"`
     if [ ! "$F5" = "PCTUSED" ]; then
        if [ $F5 -gt 50 ] ; then
         echo $record | mailx -s "subject here" email@host.com
        fi
     fi
   fi
done
0
issa7860Author Commented:
rob-g,

The following is the script I have so far:



#!/bin/ksh                                                                      
                                                         
cat unix flat file |while read record              
do                                                                              
num_col=`echo $record|wc -w`                                                  
if [ $num_col -eq 1 ];                                                        
then                                                                          
 HOSTNAME=${record}                                                          
echo $HOSTNAME                                                                  
else                                                                          
 if  [ -n $record ];                                                        
 then                                                                        
   echo $record|read FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON

TMP=`echo ${PCTUSED} | sed "s/\%//"`

   
fi
fi
done      


                                 
                                               
if [ $TMP -gt 50 ];  

                                                       
then                                                                            
echo "$HOSTNAME, $FILESYSTEM, $ALLOCATED, $USED, $FREE, $val%, $MOUNTON"|mailx -
s "Filesystem Report" \ name@server.com                                                
fi                                                            

The data file has the following format:


HOSTNAME
FILESYSTEM ALLOCATED USED FREE PCTUSED MOUNTON

first line has a hostname and second line have the 6 fields and on with 24 lines. What I was looking for is when PCTUSED is > 50% then it should email to me the hostname plus the 6 fields only those lines where PCTUSED is >= 50%.

Any help is appreciated!


                                                                                           
0
issa7860Author Commented:
Ignore the last message!
0
issa7860Author Commented:
HamdyHassan,

It's working now! Thank you.

One more request from you is that when ever the value is -gt 50 then it's emailing me just that line. so, if there are 10 lines that is > 50 then I am getting 10 emails. Is it possible to combine all these lines into one email. Instead of 10 emails I will get only 1 email with all the lines in it. The data file is going to grow and with all those emails it will fill up my email memory and also it's irritating.

Can you help me with this? Thanks in advance!
0
HamdyHassanCommented:
No problem

#!/bin/ksh                                                                      
cat unix_flat_file |while read record              
do                                                                              
  num_col=`echo $record|wc -w`                                                  
  if [ $num_col -eq 1 ]; then                                                                          
     HOSTNAME=${record}                                                          
     echo $HOSTNAME                                                                  
  else
    export F5=`echo $record | awk '{print $5}' |sed -e "s/\%//"`
    if [ ! "$F5" = "PCTUSED" ]; then
       if [ $F5 -gt 50 ] ; then
        echo $record >> /tmp/all_log.txt
       fi
    fi
  fi
done

cat /tmp/all_log.txt | mailx -s "subject here" email@host.com

0
HamdyHassanCommented:
Also you need to add the following, so to start fresh
rm -f /tmp/all_log.txt
     

so the complete script will be

#!/bin/ksh                                                

rm -f /tmp/all_log.txt
                     
cat unix_flat_file |while read record              
do                                                                              
 num_col=`echo $record|wc -w`                                                  
 if [ $num_col -eq 1 ]; then                                                                          
    HOSTNAME=${record}                                                          
    echo $HOSTNAME                                                                  
 else
   export F5=`echo $record | awk '{print $5}' |sed -e "s/\%//"`
   if [ ! "$F5" = "PCTUSED" ]; then
      if [ $F5 -gt 50 ] ; then
       echo $record >> /tmp/all_log.txt
      fi
   fi
 fi
done


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
issa7860Author Commented:
Finally, I got it the way I wanted. Thanks to all and especially to HamdyHassan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.