DataStage經過分析日誌獲取Job插入目標表的記錄數html
這只是一種不太好的方法,也許還有更好、更簡便的方法。這種方法要求每次運行Job以前刪除已有的日誌信息,不然沒法統計出正確的記錄數。固然,在Job跑完以後,能夠在shell備份本次Job運行的日誌到服務器磁盤。shell
登陸Datastage Administrator,選擇對應項目,項目屬性->記錄,勾選「自動清除做業日誌」,設置爲自動清理上次及之前的日誌。api
Figure 1 Administrator日誌清理設置bash
在shell裏用dsjob調起來Job,Job運行以後,將本次Job的日誌備份到磁盤。服務器
$DSHOME/bin/dsjob -logsum $projectName $jobName > $sysLogDir/$jobName.txtless
注意到往目標表裏寫記錄的時候會有關鍵字「Number of rows inserted:」或「Number of rows rejected:」,後面跟記錄的數目。考慮到可能有多個節點,所以能夠將各個節點的數據都加起來。ide
#inserted rowsui
insertedRows=`cat $sysLogDir/$jobName.txt | grep "Number of rows inserted:" | awk -F: '{print $3}' | sed 's/,/''/' | awk '{sum=sum+$1;} END {print sum}'`this
#rejected rowsspa
rejectedRows=`cat $sysLogDir/$jobName.txt | grep "Number of rows rejected:" | awk -F: '{print $3}' | sed 's/,/''/' | awk '{sum1=sum1+$1;} END {print sum1}'`
考慮到Job可能沒進數就終止了,須要在接下來作處理。例如爲空的話賦值0。
if [ ! -n "$insertedRows" ]; then
insertedRows=0
fi
監控可能還要些Job運行起止時間、結束狀態等等,能夠一併加上,而後寫入一個日誌文件。
其中起止時間能夠在Job運行先後記錄,Job的運行裝能夠用dsjob –run –status獲取。
jobsta=$($DSHOME/bin/dsjob -run -mode NORMAL $jobParameters -warn 0 -jobstatus $projectName $jobName 2>&1 | awk -F= '/^Status code/{print $2}')
Job執行完畢以後,將該Job的一些監控信息寫入日誌。
echo $projectName $jobName $jobsta `date +%Y-%m-%d" "%H:%M:%S` $startTime $insertedRows $rejectedRows >> $logdir/job_run_` date +%Y%m%d`.log
當全部的Job都執行完以後,能夠創建一個Job,將記錄在$logdir/job_run_` date +%Y%m%d`.log中的數據抽取到一個表,用於查看。
//日誌表
create table DSLog
(
id INTEGER NOT NULL GENERATED BY DEFAULT
AS IDENTITY (START WITH 1, INCREMENT BY 1) primary key ,
prjName varchar (20),
jobName varchar (50),
state varchar (20),
rDate date ,
startTime time,
endTime time,
insertedRows integer,
rejectedRows integer
)
//日誌狀態表
create table DSLogState
(
state varchar (20),
mark varchar (50 ),
des varchar (500)
)
https://www-304.ibm.com/support/docview.wss?uid=swg21469644
#!/bin/bash
########################################
#
# runJob.sh 2012-08-19
# run a job with parameters
#
#######################################
# if the number of input parameters is less than 2,then output the help document and exit
if [ $# -lt 2 ] ; then
cat << HELP
runJob --run a job UASGE: runJob projectName jobName jobParameters
EXAMPLE: runJob dsstage1 DD_Test '-param endDT=20120819'
HELP
exit 0
fi
projectName="$1"
jobName="$2"
jobParameters="$3"
#echo $projectName
#echo $jobName
echo $jobParameters
#exit 0
logdir=/DS/DSLogs #directory to store logs
workdate=`date +%Y%m%d`
sysLogDir=/DS/DSLogs/sysLogsBK/`date +%Y%m%d` #directory to back everyday datastage log.Datastage Administrator is setted to delete the logs before run a job.
#solve the problem of $DSHOME is null
source /mistel/IBM/InformationServer/Server/DSEngine/dsenv
#logdir processing.If log folder not exists,create folder.
if [ -d $logdir ]; then
echo "$logdir is exist,continue..."
else
echo "$logdir is not exist,creating $logdir..."
mkdir -p $logdir
fi
#create datastage logs backup direcotry
if [ ! -d $sysLogDir ]; then
mkdir -p $sysLogDir
fi
#job state processing.If job state is not finished ok,then reset the job
jobsta=$($DSHOME/bin/dsjob -jobinfo $projectName $jobName 2>&1 | awk -F: '/^Job Status/{print $2}')
echo 'last status: ' $jobsta
if [ "$jobsta" == " RUN FAILED (3)" -o "$jobsta" == " STOPPED (97)" ];then
echo "Reset before run job $jobname"
$DSHOME/bin/dsjob -run -mode RESET $projectName $jobName >>${logdir}/job_init_` date +%Y%m%d`.log
sleep 5
fi
#job start run time
startTime=`date +%H:%M:%S`
#run a job
jobsta=$($DSHOME/bin/dsjob -run -mode NORMAL $jobParameters -warn 0 -jobstatus $projectName $jobName 2>&1 | awk -F= '/^Status code/{print $2}')
#backup datastage logs
$DSHOME/bin/dsjob -logsum $projectName $jobName > $sysLogDir/$jobName.txt
#calculate the inserted rows and rejected rows from the back up log file
#inserted rows
insertedRows=`cat $sysLogDir/$jobName.txt | grep "Number of rows inserted:" | awk -F: '{print $3}' | sed 's/,/''/' | awk '{sum=sum+$1;} END {print sum}'`
#rejected rows
rejectedRows=`cat $sysLogDir/$jobName.txt | grep "Number of rows rejected:" | awk -F: '{print $3}' | sed 's/,/''/' | awk '{sum1=sum1+$1;} END {print sum1}'`
if [ ! -n "$insertedRows" ]; then
insertedRows=0
fi
if [ ! -n "$rejectedRows" ]; then
rejectedRows=0
fi
echo 'this run status code [1:Finished;2:Finished (see log);3:Aborted;97:Stopped] : ' $jobsta
#log
echo $projectName $jobName $jobsta `date +%Y-%m-%d" "%H:%M:%S` $startTime $insertedRows $rejectedRows >> $logdir/job_run_` date +%Y%m%d`.log