?
Solved

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

Posted on 2003-03-13
24
Medium Priority
?
420 Views
Last Modified: 2012-08-13
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!    
0
Comment
Question by:issa7860
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 5
  • +1
24 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8128290
 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
 
LVL 3

Expert Comment

by:yokel
ID: 8128453
if [ $PCTUSED -gt 50 ]
then
 echo "$hostname $filesystem,$allocated,$used,$free,$pctused,$mounton " | mailx -s "Filesystem Report"
fi
0
 
LVL 1

Expert Comment

by:rob-g
ID: 8128506
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:yokel
ID: 8128537
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
 

Author Comment

by:issa7860
ID: 8128931
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
 

Author Comment

by:issa7860
ID: 8129006
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8129530
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8129554
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
 
LVL 3

Expert Comment

by:yokel
ID: 8129598
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
 

Author Comment

by:issa7860
ID: 8130036
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
 

Author Comment

by:issa7860
ID: 8132615
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
 
LVL 3

Expert Comment

by:yokel
ID: 8134923
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
 

Author Comment

by:issa7860
ID: 8140007
I got rid of % sign and still get the same error.

Any ideas??
0
 
LVL 3

Expert Comment

by:yokel
ID: 8150432
There are spaces between "[" and $TMP and spaces between "50" and "]"
0
 

Author Comment

by:issa7860
ID: 8159417
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
 
LVL 1

Expert Comment

by:rob-g
ID: 8160282
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
 

Author Comment

by:issa7860
ID: 8162045
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8162609
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
 

Author Comment

by:issa7860
ID: 8166368
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
 

Author Comment

by:issa7860
ID: 8166380
Ignore the last message!
0
 

Author Comment

by:issa7860
ID: 8166692
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8166832
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
 
LVL 9

Accepted Solution

by:
HamdyHassan earned 160 total points
ID: 8166856
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
 

Author Comment

by:issa7860
ID: 8168636
Finally, I got it the way I wanted. Thanks to all and especially to HamdyHassan.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question