Author Topic: Checking for an empty file.  (Read 4277 times)

jo19021

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Checking for an empty file.
« on: January 19, 2011, 09:05:30 AM »
I have the following script running to look for '88' (load failures) and if the file is created, it should send an email to notify the team of a failure.  I created a public query called loadcheck in OD that looks for 88's and time is between t-1 and t. 

#! /usr/bin/ksh
yy=`date +%Y`
jd=`date +%j`
#jd=$1
pre="$yy$jd"
arsdoc query -h ARCHIVE -u admin -p ondemand -f "System Log" -o ${pre}loadcheck.txt -q loadcheck

if [ `ls -l /home/ccsuser/${pre}loadcheck.txt | awk '{print$5}' ` -eq 0 ]
then
     mail -s "All Loads ran fine" <email address>  < /dev/null
else
         /opt/SAscripts/bin/uuencode /home/ccsuser/${pre}loadcheck.txt ${pre}loadcheck.txt | mailx -s "A load job failed on PG & Oil Prod" <email address>
fi

Note- I removed the email addresses and replaced with <email address>

If there are no matches (no failures), the file is not created, and I get the following message -
/home/ccsuser/2011019loadcheck.txt not found
./loadcheck.sh[8]: test: argument expected
uuencode: cannot read /home/ccsuser/2011019loadcheck.txt
Null message body; hope that's ok

I am getting the load job failed portion of the "if" statement in email.  Is there another test I can do that will work for this script? 

Alessandro Perucchi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1002
    • View Profile
Re: Checking for an empty file.
« Reply #1 on: January 19, 2011, 11:35:31 AM »
Hello jo19021,

Well to avoid such script errors it would be nice to rewrite your test in another way :-)

#! /usr/bin/ksh
#yy=`date +%Y`
#jd=`date +%j`
#jd=$1
pre=$(date +%Y%j)

arsdoc query -h ARCHIVE -u admin -p ondemand -f "System Log" -o ${pre}loadcheck.txt -q loadcheck

if [[ ! -s  /home/ccsuser/${pre}loadcheck.txt ]]
then
     mail -s "All Loads ran fine" <email address>  < /dev/null
else
         /opt/SAscripts/bin/uuencode /home/ccsuser/${pre}loadcheck.txt ${pre}loadcheck.txt | mailx -s "A load job failed on PG & Oil Prod" <email address>
fi


So in that way, you will check that the file is empty or if it doesn't exist. In addition you will not receive all these awful error messages you shown us!!!

So another question, why don't you use the arslog script to check if you have a wrongly archived file?
I mean the file which is located in ondemand/bin/arslog ??

Cheers,
Alessandro
Alessandro Perucchi

#Install #Migrations #Conversion #Educate #Repair #Upgrade #Migrate #Enhance #Optimize #AIX #Linux #Multiplatforms #DB2 #Windows #Oracle #TSM #Tivoli #Performance #Audits #Customizing #Availability #HA #DR #JavaApi #ContentNavigator #ICN #WEBi #ODWEK #Services #PDF #AFP #XML

jo19021

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Checking for an empty file.
« Reply #2 on: January 19, 2011, 12:58:45 PM »
Thanks for your help.  I am a mainframer, so i am just dipping into the pool of unix.  This particular server runs OD, but i did not want to sign on daily to see if the loads worked or not.  If you have a better way, I would be curious as to how you do it. 

Alessandro Perucchi

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1002
    • View Profile
Re: Checking for an empty file.
« Reply #3 on: January 20, 2011, 02:17:38 AM »
Hello jo19021,

as your box is in Unix, you have 1000 ways to do the same thing :-D All depending on how you want to do it done!

Your solution is good. For me it's not so good, because you need to hardcode the password of CMOD in your script.

My suggestion was simply you could use the fact that each time CMOD wants to log something in the "System Log", it calls the "arslog" command.
This arslog is a simple shell script which can help the admin to fetch the message before it sends it to "System Log". And do something useful for you.

For example:
Code: [Select]
#!/bin/ksh
# $1 - OnDemand Instance Name
# $2 - Time Stamp
# $3 - Log Identifier
# $4 - Userid
# $5 - Account
# $6 - Severity
# $7 - Message Number
# $8 - Message Text

if [[ $7 -eq 88 ]]; then
  mail -s "CMOD: Error while archiving ($1)" <some e-mail> << EOF
$@
EOF
fi

It means you will receive one e-mail per error... which could be not what you want.

But it could be usefull to gather all messages somewhere, and then sends it to you. In that way, you don't need to write the password in your script!

For example:

Code: [Select]
#!/bin/ksh
# $1 - OnDemand Instance Name
# $2 - Time Stamp
# $3 - Log Identifier
# $4 - Userid
# $5 - Account
# $6 - Severity
# $7 - Message Number
# $8 - Message Text

if [[ $7 -eq 88 ]]; then
  print "Instance: $1\tTimestamp: $2\tLog ID:  $3\tUser ID: $4\tAccount: $5\tMessage Text: $8" >> /home/ccsuser/$(date +%Y%j)loadcheck.txt
fi

And you have another one which will get the file and send to you, which will put in the crontab, or any scheduler of your choice.

Code: [Select]
#!/bin/ksh

# Depending on the UNIX search the correct PATH for arsdate
if [[ -f /usr/lpp/ars/bin/arsdate ]]; then
  # AIX Path
  ARSDATE=/usr/lpp/ars/bin/arsdate
else
  # All other Unix (Linux, Sun, HP, ...)
  ARSDATE=/opt/ondemand/bin/arsdate
fi

LOGDATE=$1

if [[ -z "${LOGDATE}" ]]; then
  # If no date given, I will take the date of yesterday
  LOGDATE=$($ARSDATE -g -d -1 -f %Y%j | cut -f2)
fi

LOGFILE=/home/ccsuser/${LOGDATE}loadcheck.txt

if [[ ! -s  ${LOGFILE} ]]
then
     mail -s "All Loads ran fine" <email address>  < /dev/null
else
         /opt/SAscripts/bin/uuencode ${LOGFILE} ${LOGFILE##*/} | mailx -s "A load job failed on PG & Oil Prod" <email address>
fi

Of course, you need to put the code in your crontab for the day after (meaning today you receive the errors from yesterday, etc...) in this example.

As you can see no password, nowhere... so it means you don't have to remember to change the password in the script. In addition, no one can guess what is the admin password for example!!

I don't know if it can help you, or give you some ideas.
This is something I would do.


Or you could also do, like I do for lots of customer, create your own archiving scripts (which use arsload at the end), and get the errors directly by checking if arsload was correctly executed, if not... then write the error somewhere.


But again, your solution is good, but it is not how I would have done it ;D And it is ok like that too !!

Cheers,
Alessandro
Alessandro Perucchi

#Install #Migrations #Conversion #Educate #Repair #Upgrade #Migrate #Enhance #Optimize #AIX #Linux #Multiplatforms #DB2 #Windows #Oracle #TSM #Tivoli #Performance #Audits #Customizing #Availability #HA #DR #JavaApi #ContentNavigator #ICN #WEBi #ODWEK #Services #PDF #AFP #XML

Justin Derrick

  • IBM Content Manager OnDemand Consultant
  • Administrator
  • Hero Member
  • *****
  • Posts: 2230
  • CMOD Guru for hire...
    • View Profile
    • Tenacious Consulting
Re: Checking for an empty file.
« Reply #4 on: January 20, 2011, 07:28:21 AM »
As always, another great response from Alessandro.  I agree with everything he's written!  :)

Using arslog is the way to go when it comes to real-time monitoring.

-JD.
IBM CMOD Professional Services: http://TenaciousConsulting.com
Call:  +1-866-533-7742  or  eMail:  jd@justinderrick.com
IBM CMOD Wiki:  https://CMOD.wiki/
FREE IBM CMOD Education & Webinars:  https://CMOD.Training/

Interests: #AIX #Linux #Multiplatforms #DB2 #TSM #SP #Performance #Security #Audits #Customizing #Availability #HA #DR