Script
Script
sh
#!/bin/sh
source /opt/apigee/custom_lib/dec.sh
logDate=$(date +%Y-%m-%d)
#logDate="2019-07-05"
timeStart=$(date +"${logDate}T%H:%M:%S.%N")
username=apigee
server1=a0110papirmp01
server2=a0110papirmp02
password=$( depcrypt )
proxy=$1
logLines=$2
# MessageLogging path
LOG_HOME="/opt/apigee/var/log/edge-message-processor/messagelogging/digi/
production/${proxy}/*/ML-TransactionLogging/*"
# Email subject
#subject="ALERT: API proxy [${proxy}] errors on ${logDate}"
#subject="TEST: API proxy [${proxy}] errors on ${logDate}"
subject="INFO: API proxy [${proxy}] errors on ${logDate}"
debug=false
#==================================================================================
=====#
#==================================================================================
=====#
# DO NO CHANGE ANYTHING BELOW THIS POINT
#==================================================================================
=====#
#==================================================================================
=====#
echo "timeStart=$timeStart"
echo "timeStamp=$timeStamp"
echo "timeLabel=$timeLabel"
echo "timeNano=$timeNano"
echo "timeSeconds=$timeSeconds"
logTrimmed=false
declare -a fileTrimmed=()
tempDir=${tempDir}/${timeLabel}.${timeNano}
grepLines=$(( $logLines - 1 ))
logTECHNICAL=${proxy}_TECHNICAL.log
logSTART=${proxy}_START.log
logCOUNT=${proxy}_COUNT.log
LOG_LOCAL1=$tempDir/${proxy}/rmp01
LOG_LOCAL2=$tempDir/${proxy}/rmp02
LOG_TEMP=$tempDir/${proxy}
set -x
# Create the temporarily directory, if it's not there
rm -fr $tempDir
mkdir -p $tempDir
mkdir -p $logDir
mkdir -p $LOG_LOCAL1
mkdir -p $LOG_LOCAL2
set +x
echo "${timeStamp}: [00:00:00] ${proxy} - START" >>
$logDir/ProxyErrorCheck_$timeMonth.log
echo "###########################################"
echo "## START"
echo "###########################################"
echo "## Date: $timeStart"
echo "## Proxy: $proxy"
echo "## Log: $LOG_HOME"
echo "###########################################"
###################################################################################
######
echo "###########################################"
echo "## RUNNING: Completed RMP01 transfer ($(date +"%d-%m-%Y, %r")) [$(( $(date +
%s) - $timeSeconds ))s]"
echo "###########################################"
echo "LOG_SERVER1=${LOG_SERVER1}"
echo "LOG_SERVER2=${LOG_SERVER2}"
echo "LOG_LOCAL1=${LOG_LOCAL1}"
echo "LOG_LOCAL2=${LOG_LOCAL2}"
echo "###########################################"
echo "## RUNNING: Completed RMP02 transfer ($(date +"%d-%m-%Y, %r")) [$(( $(date +
%s) - $timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
echo "todayOK=$todayOK"
echo "todayBUSINESS=$todayBUSINESS"
echo "todayTECHNICAL=$todayTECHNICAL"
echo "todayTOTAL=$todayTOTAL"
echo "###########################################"
echo "## RUNNING: Finish today counts ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) -
$timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
technicalString="${technicalString}START\|${timeHour}"
if [ $h -lt $technicalHours ]; then
technicalString="${technicalString}|"
fi
done
echo "technicalString=$technicalString"
echo "###########################################"
echo "## RUNNING: Finish compiling TECHNICAL ($technicalHours hour(s)) ($(date
+"%d-%m-%Y, %r")) [$(( $(date +%s) - $timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
ls -l $tempDir/$logTECHNICAL
echo "###########################################"
echo "## RUNNING: Completed TECHNICAL log ($(date +"%d-%m-%Y, %r")) [$(( $(date +
%s) - $timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
# Get only the last x lines, we do not want the log to be too huge!
maxLines=$(( $maxError * $logLines ))
# Debug
echo "startError=$startError"
echo "logLines=$logLines"
echo "maxError=$maxError"
echo "maxLines=$maxLines"
echo "###########################################"
echo "## RUNNING: Completed trimming $tempDir/$logTECHNICAL ($(date +"%d-%m-%Y,
%r")) [$(( $(date +%s) - $timeSeconds ))s]"
echo "###########################################"
fi
###################################################################################
######
ls -l $tempDir/$logSTART
echo "###########################################"
echo "## RUNNING: Completed START log ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) -
$timeSeconds ))s]"
echo "###########################################"
#Last 4 = {message.status.code}|{errorCode}|{errorMessage}|{logType}
statusCode=$(echo "$temp" | rev | cut -d '|' -f 4 | rev)
errorCode=$(echo "$temp" | rev | cut -d '|' -f 3 | rev)
errorMessage=$(echo "$temp" | rev | cut -d '|' -f 2 | rev)
logType=$(echo "$temp" | rev | cut -d '|' -f 1 | rev)
echo "###########################################"
echo "## RUNNING: Completed temporarily LINE log ($(date +"%d-%m-%Y, %r")) [$(( $
(date +%s) - $timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
echo "###########################################"
echo "## RUNNING: Completed temporarily SORT log ($(date +"%d-%m-%Y, %r")) [$(( $
(date +%s) - $timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
echo "###########################################"
echo "## RUNNING: Completed COUNT log ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) -
$timeSeconds ))s]"
echo "###########################################"
###################################################################################
######
html="<html>
<head>
<style>
.tab, .tab th, .tab td {
border: 1px solid black;
padding: 5px;
}
th {
background-color: yellow;
}
.nok {
background-color: orange;
color: white;
}
.h {
background-color: yellow;
font-weight: bold;
}
.l {
background-color: whitesmoke;
}
</style>
</head>
<body>${trimmedNotice}
<p style=margin:0px;padding:0px;>Proxy [<b>$proxy</b>] TECHNICAL errors for the
past <b>${technicalHours}+ hours</b>:</p>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td><b>From</b></td><td><b>:</b> $(date --date="-$technicalHours hours
$timeStart" +"%d-%m-%Y, %I:00:00 %p")</td></tr>
<tr><td><b>To</b></td><td><b>:</b> $(date --date="$timeStart" +"%d-%m-%Y,
%r")</td></tr>
</table>
<br>
<table class=tab>
<tr>
<th>Method</th>
<th>API</th>
<th>Status Code</th>
<th>Error Code</th>
<th>Error Message</th>
<th>Count</th>
<th>Last Occurrence</th>
</tr>"
lastIsAlert=0
lastLatest=0
lastDisplay=0
errorTotal=0
while read line
do
errorCount=$(echo $line | cut -d " " -f 1)
errorTotal=$(($errorTotal+$errorCount))
countLength=${#errorCount}
errorLine=${line:($countLength + 1)}
# Debug
echo "errorCount=$errorCount"
echo "errorTotal=$errorTotal"
echo "errorLine=$errorLine"
#errorLine={request.verb}|{proxy.basepath}|{proxy.pathsuffix}|
{message.status.code}|{errorCode}|{errorMessage}|{logType}
requestVerb=$(echo "$errorLine" | cut -d '|' -f 1)
basePath=$(echo "$errorLine" | cut -d '|' -f 2)
pathSuffix=$(echo "$errorLine" | cut -d '|' -f 3)
statusCode=$(echo "$errorLine" | cut -d '|' -f 4)
errorCode=$(echo "$errorLine" | cut -d '|' -f 5)
errorMessage=$(echo "$errorLine" | cut -d '|' -f 6)
logType=$(echo "$errorLine" | cut -d '|' -f 7)
echo "errorMessage=$errorMessage"
echo "errorMessageEsc=$errorMessageEsc"
set -x
lastLine=$(grep -P "^(?=.*START\|)(?=.*\|${basePath}\|)(?=.*\|${pathSuffix}\|)(?
=.*\|${errorCode}\|)(?=.*\|${errorMessageEsc}\|)" $tempDir/$logSTART | tail -1)
lastTime=$(echo "$lastLine" | cut -d '|' -f 2)
set +x
# Debug
echo "lastLine=$lastLine"
echo "lastTime=$lastTime"
# Debug
echo "lastNow=$lastNow"
echo "lastOccur=$lastOccur"
echo "lastGap=$lastGap"
echo "lastIsActive=$lastIsActive"
echo "lastLatest=$lastLatest"
echo "lastDisplay=$lastDisplay"
# For attachments
attachmentList="${attachmentList}${requestVerb}|${basePath}|${pathSuffix}|$
{errorCode}\n"
html="${html}<tr>
<td>${requestVerb}</td>
<td>${basePath}${pathSuffix}</td>
<td>${statusCode}</td>
<td>${errorCode}</td>
<td>${errorMessage}</td>
<td align=right>$(printf "%'d" ${errorCount})</td>
<td$([ $lastIsActive -eq 1 ] && (echo " class=nok"))>${lastTime}</td>
</tr>"
# Debug
echo "----------"
done < $tempDir/$logCOUNT
# Debug
echo "attachmentList=$attachmentList"
echo "###########################################"
echo "## RUNNING: Completed content manipulation ($(date +"%d-%m-%Y, %r")) [$(( $
(date +%s) - $timeSeconds ))s]"
echo "###########################################"
# Debug
echo "requestVerb=$requestVerb"
echo "basePath=$basePath"
echo "pathSuffix=$pathSuffix"
echo "errorCode=$errorCode"
filename=$tempDir/${proxy}${requestPath}_${requestVerb}_${errorCode}_$
{timeDate}.log
set -x
grep -P -A $grepLines --no-group-separator "^(?=.*START\|)(?=.*\|${basePath}\|)(?
=.*\|${pathSuffix}\|)(?=.*\|${errorCode}\|)" $tempDir/$logTECHNICAL | sed
"s/|END/&\n----/" > $filename
set +x
# Get only the last x lines, we do not want the log to be too huge!
maxLines=$(( $maxAttachment * $(($logLines+1)) ))
# Debug
echo "startError=$startError"
echo "logLines=$logLines"
echo "maxAttachment=$maxAttachment"
echo "maxLines=$maxLines"
mv $filename $filename.full
mv $filename.trimmed $filename
echo "###########################################"
echo "## RUNNING: Completed trimming $filename ($(date +"%d-%m-%Y, %r")) [$(( $
(date +%s) - $timeSeconds ))s]"
echo "###########################################"
fi
# If filesize bigger than x MB, then zip it
filesize=$(stat --printf="%s" "$filename")
# Debug
echo "filename=$filename"
echo "filesize=$filesize"
echo "fileTrimmed[*]=${fileTrimmed[*]}"
# Debug
echo "attachment=$attachment"
echo "###########################################"
echo "## RUNNING: Completed attachments ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s)
- $timeSeconds ))s]"
echo "###########################################"
set -x
#todayPctOK=$(echo "scale=4;(((${todayOK}/${todayTOTAL})*100))" | bc)
#todayPctBUSINESS=$(echo "scale=4;(((${todayBUSINESS}/${todayTOTAL})*100))" | bc)
#todayPctTECHNICAL=$(echo "scale=4;(((${todayTECHNICAL}/${todayTOTAL})*100))" | bc)
todayPctOK=$(awk -v v1="$todayOK" -v v2="$todayTOTAL" 'BEGIN{printf "%.2f",
v1/v2*100}')
todayPctBUSINESS=$(awk -v v1="$todayBUSINESS" -v v2="$todayTOTAL" 'BEGIN{printf
"%.2f", v1/v2*100}')
todayPctTECHNICAL=$(awk -v v1="$todayTECHNICAL" -v v2="$todayTOTAL" 'BEGIN{printf
"%.2f", v1/v2*100}')
set +x
html="${html}</table>${lastHTML}
<br><hr><br>
<table class=tab>
<tr>
<th></th>
<th colspan=2>Today, so far...<br>($(date --date="$timeStart" +"%d-%m-%Y"))</th>
</tr>
<tr>
<td class=h>OK Transactions</td>
<td align=right>$(printf "%'d" $todayOK)</td>
<td align=right>${todayPctOK}%</td>
</tr>
<tr>
<td class=h>BUSINESS Errors</td>
<td align=right>$(printf "%'d" $todayBUSINESS)</td>
<td align=right>${todayPctBUSINESS}%</td>
</tr>
<tr>
<td class=h>TECHNICAL Errors</td>
<td align=right><font color=red>$(printf "%'d" $todayTECHNICAL)</font></td>
<td align=right>${todayPctTECHNICAL}%</td>
</tr>
<tr>
<td class=h>TOTAL Transactions</td>
<td align=right>$(printf "%'d" $todayTOTAL)</td>
<td align=right>-</td>
</tr>
</table>
${trimmedHTML}
${attachmentHTML}
<p>Time taken to generate this report: $(date -d@$(($(date +%s)-$timeSeconds)) -u +
%H:%M:%S)<br>
Script location: $0</p>
</body>"
contentType="set content_type=text/html"
set -x
if [ "$email_send" = true ]; then
if [[ ! -z $attachment ]]; then
echo -e $html | mutt -e "${contentType}" $attachment -s "${subject}" --
$email_to
else
echo -e $html | mutt -e "${contentType}" -s "${subject}" -- $email_to
fi
echo "###########################################"
echo "## Email sent ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) -
$timeSeconds ))s]"
echo "###########################################"
else
echo "###########################################"
echo "## Email not sent ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) - $timeSeconds
))s]"
echo "###########################################"
fi
set +x
# Debug
if [ "$debug" = false ]; then
# Cleanup!
set -x
rm -fr $tempDir
set +x
else
ls -l $tempDir
fi
echo "###########################################"
echo "## END ($(date +"%d-%m-%Y, %r")) [$(( $(date +%s) - $timeSeconds ))s]"
echo "###########################################"