Thursday 19 July 2012

Automatic CA eHealth Script to Delete Elements with old Data

I wrote this ksh script this week so we can have a scheduled job to purge elements that are no generating any database data for CA eHealth 6.3

For our site I found the nhListElements -nodbdata didn't actually return a list of all the elements we wanted to delete that were not polling properly.

We added this to crontab for ehealth user to automate management. It makes a day of month file listing all the deleted elements that day.


# PurgeOldElements
# 1 6 * * * /appl/ehealth/telecom/PurgeOldElements

# ./"PurgeOldElements"
# nhElementStatus -noDbDataFor 129600 >/var/tmp/ehealth.report.nodbdatafor.129600.20.txt
# NUMBER OF MATCHING ELEMENTS=
# 153
# nhDeleteElements -inFile /var/tmp/ehealth.elements.nodbdatafor.129600.20.txt
# ./"PurgeOldElements"
# nhElementStatus -noDbDataFor 129600 >/var/tmp/ehealth.report.nodbdatafor.129600.20.txt
# NUMBER OF MATCHING ELEMENTS=
# 0
# no elements to delete

# delete elements with no dbdata for 90 days (60*24*90)=129600
# get a list in a file
daynumber=`date +"%d"`
threshold=129600
report=/var/tmp/ehealth.report.nodbdatafor.$threshold.$daynumber.txt
elementlist=/var/tmp/ehealth.elements.nodbdatafor.$threshold.$daynumber.txt
rm $report $elementlist $report.2

echo "nhElementStatus -noDbDataFor $threshold >$report"
nhElementStatus -noDbDataFor $threshold >$report

# strip off headers
tail +5 $report>$report.2

echo "NUMBER OF MATCHING ELEMENTS="
grep -c "Was not polled in $threshold" $report.2
if [ $? -ne 0 ]; then
echo "no elements to delete"
else
# get the first field (element name
i=0
while read line
do
echo $line| awk  '{print $1}' >>$elementlist
done <"$report.2"

cd $NH_HOME
echo "nhDeleteElements -inFile $elementlist"
nhDeleteElements -inFile $elementlist

fi