#!/bin/ksh93 # $Id: bpstart_notify 630 2014-07-10 21:22:01Z elkner $ # # Copyright (c) 2014 Jens Elkner. All rights reserved. # License: CDDL 1.0 CFG='/etc/netbackup/bp.conf' # Where to syslog single stream backup completions. Usually news isn't used # anymore. FACILITY='news.info' NB_HOME='/usr/openv/netbackup' SORT='/usr/bin/sort' MAILX='/usr/bin/mailx' WC='/usr/bin/wc' LOGGER='/usr/bin/logger' function getInfo { # We need the Job ID to distinguish this session from others. Unfortunately # this ancient stuff doesn't provide this info and thus we need to find it # out from the commandline parameters used, when bpbkar got started: typeset ARGS=( ${ pargs -l ${PPID}; } ) typeset -n JOBID=$1 integer I JOBID=-1 for (( I=0; I < ${#ARGS[@]}; I++ )); do [[ ${ARGS[I]} == '-jobgrpid' ]] && JOBID=${ARGS[I+1]} done } function anaLyse { typeset -n MSG=$1 SUBJECT=$2 integer DT H M C=0 T_START T_END typeset D_START D_END TXT='' T ${SORT} -k4,4n -k2,2n -k1,1r "$3" | while read LINE; do S=( ${LINE} ) read LINE E=( ${LINE} ) (( C++ )) (( DT=${E[1]} - ${S[1]} )) [[ -z ${D_START} ]] && D_START=${S[2]} && T_START=${S[1]} D_END=${E[2]} && T_END=${E[1]} (( H=DT/3600 )) (( DT-=(H * 3600) )) (( M=DT/60 )) (( DT-=(M * 60) )) T=${ printf "%02d:%02d:%02d" $H $M $DT ; } TXT+="Stream ${S[3]} Time: ${S[2]} - ${E[2]} Duration: $T\n" done (( DT=T_END - T_START )) (( H=DT/3600 )) (( DT-=(H * 3600) )) (( M=DT/60 )) (( DT-=(M * 60) )) T=${ printf "%02d:%02d:%02d" $H $M $DT ; } SUBJECT="backup ${ uname -n; } complete ${D_START}-${D_END}" MSG="Backup of $C streams from ${D_START%:*} 'til ${D_END%:*} completed.\nDuration total: $T\n\n${TXT}" } function checkPost { typeset CFILE="/tmp/${2}.cnt" DFILE="$1" KEY X FS TAIL typeset -l FS # need to determine the real number of streams to find out, whether all # streams have been processed, i.e. backup is finished - the damn ancient # netbackup doesn't provide this info ... integer COUNT=0 N if [[ ! -e ${CFILE} ]]; then ${NB_HOME}/bin/bpmount -i 2>&1 | while read KEY X FS TAIL; do # only ^MNT is interesting [[ ${KEY} != 'MNT' ]] && continue # for those no stream are generated [[ ${FS} =~ ^(proc|ctfs|mntfs|objfs|tmp|fd)$ ]] && continue (( COUNT++ )) done print "${COUNT}" >${CFILE} else COUNT=$(<${CFILE}) fi # if last, send mail FS=( ${ ${WC} -l "${DFILE}" ; } ) N=${FS[0]} if (( N == (COUNT * 2) )); then X='' while read TAIL ; do [[ ${TAIL:0:9} == 'LOCALMAIL' ]] || continue X="${TAIL#*=}" [[ -n ${X## } ]] && break || X='' done < "${CFG}" if [[ -n $X ]]; then anaLyse TAIL KEY "${DFILE}" print "${TAIL}" | ${MAILX} -s "${KEY}" ${X} fi fi rm -f "${DFILE}" "${CFILE}" } function doMain { # v-- redundant, but avoids need for add. tools typeset JID DATE=${ date '+%s %H:%M:%S'; } INFO DATA DFILE getInfo JID DFILE="/tmp/${JID}.lst" INFO="JOB=${JID} ${STREAM_NUMBER}/${STREAM_COUNT} ${TYPE} ${POLICY}" DATA="${DATE} ${STREAM_NUMBER} ${JID} ${STREAM_COUNT} ${STREAM_PID} ${TYPE} ${POLICY}" if (( IS_START )); then ${LOGGER} -i -p ${FACILITY} "backup START ${INFO}" print "S ${DATA}" >>"${DFILE}" else ${LOGGER} -i -p ${FACILITY} "backup END ${INFO} FIN=${FINISHED}" print "E ${DATA} ${FINISHED}" >>"${DFILE}" checkPost "${DFILE}" ${JID} fi } integer IS_START=0 [[ ${.sh.file##*/} == 'bpstart_notify' ]] && IS_START=1 CLIENT="$1" POLICY="$2" SCHEDULE="$3" TYPE="$4" # FULL | INCR | CINC | UBAK | UARC doMain >/dev/null 2>&1 # make sure we don't hijack the stream exit 0 # http://www.symantec.com/business/support/index?page=content&id=TECH61147