Retrieving execution logs of BladeLogic Jobs

Version 1
    Share:|

    Some background

    There have been a few threads running on Communities about the way to retrieve Job logs of various types of BladeLogic Jobs. Whilst there might exist some direct commands for most Job types, the result can be a little disappointing for composite Job types like BatchJobs or DeployJobs (which we will see is in fact a kind of BatchJob) because the simple commands will only retrieve the header message of the JobRun but not the sub-sections. (essentially the child Job logs in fact)

     

    Because I had to retrieve logs for pretty much all main types of Jobs in BladeLogic for my portal project (see this other document for details) I pushed further the method Bill ROBINSON already described in several threads of Communities. I've added some readability by prefixing the log records coming from child Jobs or "sub-Jobs", which will allow easy filtering.

     

    Assumptions

    Because the idea here is to understand how we can extract the logs for a particular Job, we will assume that the DB Key of the job is known and will be input.

    Also because this is mainly for use in environments where we will programmatically trigger the execution of the Job and subsequently retrieve the logs upon completion, I'm assuming that every Job only runs once and therefore the JobRun is always unique and the last one. Is this cheating? Not at all, that's just smart design.

     

     

    JOB_DBKEY=
    JOB_LOG_FILE=/tmp/job_log.csv
    
    blcli_execute Job findByDBKey $JOB_DBKEY >/dev/null 2>&1
    blcli_execute Job getType >/dev/null 2>&1
    blcli_storeenv JOB_TYPE
    
    case $JOB_TYPE in
    
        30)     # DeployJob
                PHASE_LIST="Simulate Stage Commit"
       
                blcli_execute Utility exportDeployResults $JOB_DBKEY $JOBRUN_KEY "$JOB_LOG_FILE" >/dev/null 2>&1
                blcli_execute JobRun findByJobRunKey $JOBRUN_KEY
                blcli_execute JobRun getJobRunId && blcli_storeenv PARENT_JOBRUN_ID
                blcli_execute BatchJobRun findMemberHeadersByJobKeyAndJobRunId $JOB_DBKEY $PARENT_JOBRUN_ID
                blcli_execute SJobHeader getDBKey
                blcli_execute Utility setTargetObject
                blcli_execute Utility listPrint && blcli_storeenv CHILD_JOB_DBKEY_LIST
               
                for CHILD_JOB_DBKEY in $CHILD_JOB_DBKEY_LIST
                do
                    blcli_execute JobRun findLastRunKeyByJobKey $CHILD_JOB_DBKEY >/dev/null 2>&1 &&    blcli_storeenv CHILD_JOBRUN_KEY
                    blcli_execute Utility exportDeployResults $CHILD_JOB_DBKEY $CHILD_JOBRUN_KEY "$JOB_LOG_FILE.tmp"
                    PHASE=$(echo $PHASE_LIST |awk '{ print $1 }')
                    PHASE_LIST=$(echo $PHASE_LIST |sed -e "s/$PHASE//")
                    cat "$JOB_LOG_FILE.tmp" |sed "s/^/$PHASE /g" >> "$JOB_LOG_FILE"
                done
                rm "$JOB_LOG_FILE.tmp"
                ;;
        31)     # AuditJob
                blcli_execute Utility exportAuditResults $JOB_DBKEY $JOBRUN_KEY "$JOB_LOG_FILE" "" >/dev/null 2>&1
                ;;
        32)     # SnapshotJob
                blcli_execute Utility simpleExportSnapshotResults $JOB_DBKEY $JOBRUN_KEY "$JOB_LOG_FILE" "" >/dev/null 2>&1
                ;;
        40)     # FileDeployJob
                blcli_execute Utility exportDeployResults $JOB_DBKEY $JOBRUN_KEY "/tmp/job_log.csv" >/dev/null 2>&1
                ;;
        111)    # NSHScriptJob
                blcli_execute Utility exportNSHScriptResults $JOBRUN_KEY "$JOB_LOG_FILE" >/dev/null 2>&1
                ;;
        200)    # BatchJob
                   
                SEQUENCE=1
                   
                blcli_execute Utility exportBatchResults $JOBRUN_KEY "$JOB_LOG_FILE" >/dev/null 2>&1
               
                blcli_execute JobRun findByJobRunKey $JOBRUN_KEY
                blcli_execute JobRun getJobRunId && blcli_storeenv PARENT_JOBRUN_ID
                blcli_execute BatchJobRun findMemberHeadersByJobKeyAndJobRunId $JOB_DBKEY $PARENT_JOBRUN_ID
                blcli_execute SJobHeader getDBKey
                blcli_execute Utility setTargetObject
                blcli_execute Utility listPrint && blcli_storeenv CHILD_JOB_DBKEY_LIST
                for CHILD_JOB_DBKEY in $CHILD_JOB_DBKEY_LIST
                do
                    blcli_execute JobRun findLastRunKeyByJobKey $CHILD_JOB_DBKEY >/dev/null 2>&1 &&    blcli_storeenv CHILD_JOBRUN_KEY
                    blcli_execute JobRun getType && blcli_storeenv CHILD_JOB_TYPE_ID
                   
                    case $CHILD_JOB_TYPE_ID in
                       
                        385)    # DeployJobRun
                                PHASE_LIST="Simulate Stage Commit"
                                               
                                blcli_execute Utility exportDeployResults $CHILD_JOB_DBKEY $CHILD_JOBRUN_KEY "$JOB_LOG_FILE.tmp" >/dev/null 2>&1
                                blcli_execute JobRun findByJobRunKey $CHILD_JOBRUN_KEY
                                blcli_execute JobRun getJobRunId && blcli_storeenv DEPLOY_JOBRUN_ID
                                blcli_execute BatchJobRun findMemberHeadersByJobKeyAndJobRunId $CHILD_JOB_DBKEY $DEPLOY_JOBRUN_ID
                                blcli_execute SJobHeader getDBKey
                                blcli_execute Utility setTargetObject
                                blcli_execute Utility listPrint && blcli_storeenv PHASE_JOB_DBKEY_LIST
                                for    PHASE_JOB_DBKEY in $PHASE_JOB_DBKEY_LIST
                                do
                                    blcli_execute JobRun findLastRunKeyByJobKey $PHASE_JOB_DBKEY >/dev/null 2>&1 &&    blcli_storeenv PHASE_JOBRUN_KEY
                                    blcli_execute Utility exportDeployResults $PHASE_JOB_DBKEY $PHASE_JOBRUN_KEY "$JOB_LOG_FILE.phase"
                                    PHASE=$(echo $PHASE_LIST |awk '{ print $1 }')
                                    PHASE_LIST=$(echo $PHASE_LIST |sed -e "s/$PHASE//")
                                    cat "$JOB_LOG_FILE.phase" |sed "s/^/$PHASE /g" >> "$JOB_LOG_FILE.tmp"
                                done
                                rm "$JOB_LOG_FILE.phase"
                                ;;
                        386)    # UpdateServerPropertiesJobRun
                                ;;
                        391)    # AuditJobRun
                                blcli_execute Utility exportAuditResults $CHILD_JOB_DBKEY $CHILD_JOBRUN_KEY "$JOB_LOG_FILE.tmp" ""
                                ;;
                        392)    # SnapshotJobRun
                                blcli_execute Utility simpleExportSnapshotResults $CHILD_JOB_DBKEY $CHILD_JOBRUN_KEY "$JOB_LOG_FILE.tmp" ""
                                ;;
                        393)    # NSHScriptJobRun
                                blcli_execute Utility exportNSHScriptResults $CHILD_JOBRUN_KEY "$JOB_LOG_FILE.tmp"
                                ;;
                        395)    # FileDeployJobRun
                                ;;
                        398)    # DicoveryJobRun
                                ;;
                        399)    # ACLPushJobRun
                                ;;
                        5108)    # ComplianceJobRun
                                blcli_execute Utility exportComplianceResults $CHILD_JOB_DBKEY -1 "$JOB_LOG_FILE.tmp" null null null CSV 'simple'
                                ;;
                    esac
                   
                    cat "$JOB_LOG_FILE.tmp" |sed "s/^/Member $SEQUENCE /g" >> "$JOB_LOG_FILE"
                    SEQUENCE=$(echo "$SEQUENCE + 1" |bc)
                done
                rm "$JOB_LOG_FILE.tmp"           
                ;;
        405)    # ComponentDiscoveryJob
                ;;
        1009)   # ACLPushJob
                ;;
        1017)   # UpdateServerPropertiesJob
                ;;
        5106)   # ComplianceJob
                blcli_execute Utility exportComplianceResults $JOB_DBKEY -1 "$JOB_LOG_FILE" null null null CSV 'simple' >/dev/null 2>&1
                ;;
    esac