shell example.
#!/bin/sh
#=============================================================================
#Author:GarethLiao
#Desc:runMonitorInquireIncrease.sh
#Run:shrunMonitorInquireIncrease.shInquiry
#=============================================================================
exportSNARK_HOME=$HOME/abc
curParam=$1;
curTable='';
oldFileName=$HOME/inquire_increase_old.txt;
newFileName=$HOME/inquire_increase_new.txt;
lastTimeFile=$HOME/inquire_time.txt;
exportSQL_SCRIPT_HOME=$SNARK_HOME/src/sql;
CTRL_FILE=$SNARK_HOME/data/import_cache/.ImportCtrl;
echo"---startofrunMonitorInquireIncrease.sh---";
#RCP='garethliao@**.com';
EMAIL_FROM='pscadmin@**.com';
userpwd=$2;
sid=$3;
#userpwd='test/test';
#sid='test';
nowTime=$(date+'%Y-%m-%d%H:%M:%S');
today=$(date+%Y-%m-%d);
yesterday=$(date-dyesterday+%Y-%m-%d);
beforeYesterday=$(date-d'2daysago'+%Y-%m-%d);
lastRunTime='';
todayTotal=0;
yesterdayTotal=0;
beforeYesterdayTotal=0;
newtodayTotal=0;
newyesterdayTotal=0;
newbeforeYesterdayTotal=0;
todayAdd=0;
yesterdayAdd=0;
beforeyesterdayAdd=0;
totalAdd=0;
functionsendEmail
{
todayAdd=`expr$newtodayTotal-$todayTotal`;
if[$todayAdd-lt0]
thentodayAdd=0;
fi;
yesterdayAdd=`expr$newyesterdayTotal-$yesterdayTotal`;
if[$yesterdayAdd-lt0]
thenyesterdayAdd=0;
fi;
beforeyesterdayAdd=`expr$newbeforeYesterdayTotal-$beforeYesterdayTotal`;
if[$beforeyesterdayAdd-lt0]
thenbeforeyesterdayAdd=0;
fi;
totalAdd=`expr$todayAdd+$yesterdayAdd`;
totalAdd=`expr$totalAdd+$beforeyesterdayAdd`;
if[$totalAdd-gt0]
then
send_TMP=".tmp";
if[-f$zipfile]
then
echo"Preparetosend....to$RCP";
cat>>$send_TMP<<EOT
Subject:MonitorReportof
Content-Type:text/plain
From:$EMAIL_FROM
To:$RCP
DearAll,
Thefollowingistheresultaboutmonitordata(lastrunningis$lastRunTime).
$todaylasttimeis$todayTotalcurrentis$newtodayTotalincreasednumberis$todayAdd
$yesterdaylasttimeis$yesterdayTotalcurrentis$newyesterdayTotalincreasednumberis$yesterdayAdd
$beforeYesterdaylasttimeis$beforeYesterdayTotalcurrentis$newbeforeYesterdayTotalincreasednumberis$beforeyesterdayAdd
Regards,
Admin
EOT
uuencode$zipfile$zipfile>>$send_TMP;
cat$send_TMP|/usr/sbin/sendmail-f$EMAIL_FROM$RCP;
rm$send_TMP;
fi;
fi;
if["$totalAdd"="0"]
then
send_TMP=".tmp";
if[-f$zipfile]
then
echo"Preparetosend....to$RCP";
cat>>$send_TMP<<EOT
Subject:Nodataadded,MonitorReportofthePSEinquireincreasefor$curParam
Content-Type:text/plain
From:$EMAIL_FROM
To:$RCP
DearAll,
Nodatasaddedbynow.
Thefollowingistheresultaboutmonitordata(lastrunningis$lastRunTime).
Regards,
Admin
EOT
uuencode$zipfile$zipfile>>$send_TMP;
cat$send_TMP|/usr/sbin/sendmail-f$EMAIL_FROM$RCP;
rm$send_TMP;
fi;
fi;
}
functionsendAlertEmail
{
send_TMP=".tmp";
if[-f$zipfile]
then
echo"Preparetosend....to$RCP";
cat>>$send_TMP<<EOT
Subject:AlertEmailforprocessdead
Content-Type:text/plain
From:$EMAIL_FROM
To:$RCP
DearAll,
Theimportprocessisdead,itwaskillednow.
Itisfromthemonitorofinquiry.
Regards,
PSCAdmin
EOT
uuencode$zipfile$zipfile>>$send_TMP;
cat$send_TMP|/usr/sbin/sendmail-f$EMAIL_FROM$RCP;
rm$send_TMP;
fi
}
functiontableName
{
if["$curParam"="Inquiry"]
then
curTable='export_inquiry_increase.sql';
oldFileName=$SNARK_HOME/i_inquire_increase_old.txt;
newFileName=$SNARK_HOME/i_inquire_increase_new.txt;
elif["$curParam"="InquireAll"]
then
curTable='export_inquireall_increase.sql';
oldFileName=$SNARK_HOME/ia_inquire_increase_old.txt;
newFileName=$SNARK_HOME/ia_inquire_increase_new.txt;
elif["$curParam"="AggInquiry"]
then
curTable='export_agginquiry_increase.sql';
oldFileName=$SNARK_HOME/ai_inquire_increase_old.txt;
newFileName=$SNARK_HOME/ai_inquire_increase_new.txt;
elif["$curParam"="AggInquireAll"]
then
curTable='export_agginquireall_increase.sql';
oldFileName=$SNARK_HOME/aia_inquire_increase_old.txt;
newFileName=$SNARK_HOME/aia_inquire_increase_new.txt;
fi;
}
functionexportData
{
echo"Exportingdatatotextfilefromdatabase...";
echo"----Currenttable:"$curTable;
#sqlplus-s$userpwd@$sid@$SQL_SCRIPT_HOME/$curTable>>$newFileName;
if["$curParam"="Inquiry"]
then
sqlplus-s$userpwd@$sid@$SQL_SCRIPT_HOME/export_inquire_increase.sql>>$newFileName;
elif["$curParam"="InquireAll"]
then
sqlplus-s$userpwd@$sid@$SQL_SCRIPT_HOME/export_inquireall_increase.sql>>$newFileName;
elif["$curParam"="AggInquiry"]
then
sqlplus-suserpwd@$sid@$SQL_SCRIPT_HOME/export_agginquiry_increase.sql>>$newFileName;
elif["$curParam"="AggInquireAll"]
then
sqlplus-s$userpwd@$sid@$SQL_SCRIPT_HOME/export_agginquireall_increase.sql>>$newFileName;
fi;
echo">>>>>ENDexport";
}
functionreadTime
{
if[-f$lastTimeFile]
then
whilereadline
do
lastRunTime=$line;
done<$lastTimeFile;
fi;
}
functionwriteTime
{
echo$nowTime>$lastTimeFile;
}
functionreadFiles
{
if[-f$oldFileName]
then
whilereadline
do
sendDate=`echo$line|awk'{print$1}'`;
total=`echo$line|awk'{print$2}'`;
echo"oldfile$sendDate---todayis$today";
if["$sendDate"="$today"]
then
todayTotal=`expr$total+0`;
echo"1111$todayTotal";
elif["$sendDate"="$yesterday"]
then
yesterdayTotal=`expr$total+0`;
echo"222$yesterdayTotal";
elif["$sendDate"="$beforeYesterday"]
then
beforeYesterdayTotal=`expr$total+0`;
echo"3333$beforeYesterdayTotal";
fi;
done<$oldFileName;
fi;
whilereadnewline
do
newsendDate=`echo$newline|awk'{print$1}'`;
newtotal=`echo$newline|awk'{print$2}'`;
if["$newsendDate"="$today"]
then
newtodayTotal=`expr$newtotal+0`;
elif["$newsendDate"="$yesterday"]
then
newyesterdayTotal=`expr$newtotal+0`;
elif["$newsendDate"="$beforeYesterday"]
then
newbeforeYesterdayTotal=`expr$newtotal+0`;
fi;
done<$newFileName;
rm-rf$oldFileName;
mv$newFileName$oldFileName;
}
functioncheckAndKill
{
if[-f$CTRL_FILE]
then
modify_time=`ls-l$CTRL_FILE|awk'{print$(NF-1)}'`;
curtime=`date+"%H:%M"`;
sys_time=`date-d"$curtime"+%s`;
file_time=`date-d"$modify_time"+%s`;
interval=`expr$sys_time-$file_time`;
ta=`expr$newtodayTotal-$todayTotal`;
ya=`expr$newyesterdayTotal-$yesterdayTotal`;
ba=`expr$newbeforeYesterdayTotal-$beforeYesterdayTotal`;
aa=`expr$ta+$ya+$ba`;
if[$interval>=14400]
then
if[$aa<=0]
then
PID=`psgaux|grepdailyImportInquiry.sh|grep-vgrep|awk'{print$2}'`;
kill-9$PID;
sendAlertEmail;
fi
fi
fi
}
functionmain
{
#tableName;
readTime;
writeTime;
exportData;
readFiles;
#checkAndKill;
sendEmail;
}
main;
echo"---endofrunMonitorInquireIncrease.sh---";