#!/bin/bash
cd $(dirname $(readlink -f $0))
MAILTO=$1
. in_risk_vm_machines.env
>$OUTPUT_FILE
. govc.config
if [[ -z $1 ]]; then
echo "Hiba: nem adtal meg cimzettet! Helyes hasznalat:"
echo "$0 email@cimem.hu[,email2@cimem.hu]"
echo
exit 1
fi
#Kivetel lista felolvasasa
[[ ! -e $EXCEPTION_FILE ]] && touch $EXCEPTION_FILE
while read SOR; do
EXCEPTIONS="$(echo $EXCEPTIONS | sed 's/^\s*//') $(echo $SOR | awk '{print $1}')"
done <<< "`grep -v '^#' $EXCEPTION_FILE`"
datediff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 ))
}
for VCENTER in $VCENTERS; do
echo "<caption><b><u><i>${VCENTER#https://}</i></u></b></caption>" >> $OUTPUT_FILE
printf "%-30s %-60s %-40s %-5s %-15s\n" "VM_NAME","RESOURCE_POOL","UUID","PW_ST","LAST_BACKUP" >>$OUTPUT_FILE
export GOVC_URL="$VCENTER"
./govc find . -type p | sort | while read RESPOOL; do
./govc find "$RESPOOL" -type m | sed 's/^.*\///g' | sort | while read VM_NAME; do
POWER_STATE=$(./govc vm.info "$VM_NAME" | awk -F':' '/Power state/{print $2}' | sed 's/ *powered//')
UUID=$(./govc vm.info "$VM_NAME" | awk -F':' '/UUID/{print $2}' | sed 's/ *//')
LAST_BACKUP=$(dsmadmc -id=$TSMUSER -pa=$TSMPASS -se=$TSMSERVER -dataonly=y "select BACKUP_DATE from backups where node_name='$TSMNODE' and hl_name like '\\$VM_NAME\\%' order by BACKUP_DATE desc FETCH FIRST 1 ROWS ONLY" | grep -v 'AN[RS]' | sed 's/^ //' | sed 's/\.000000$//')
[[ -z $LAST_BACKUP ]] && LAST_BACKUP='NINCS MENTVE!'
IN_RISK=""
[[ $LAST_BACKUP =~ ^[2][0] && $(datediff "$(date '+%m/%d/%Y %H:%M:%S' )" "$LAST_BACKUP") -gt $IN_RISK_DAYS ]] && { LAST_BACKUP="$LAST_BACKUP ($IN_RISK_DAYS napnál régebbi!)"; IN_RISK='!!!'; }
[[ ! "${EXCEPTIONS^^}" = *"${UUID^^}"* && -n $IN_RISK || $LAST_BACKUP =~ ^[^2][^0] ]] && printf "%-30s %-60s %-40s %-5s %-15s\n" "$VM_NAME","$RESPOOL","$UUID","($POWER_STATE)","$LAST_BACKUP" | sed 's/ *$//' >> $OUTPUT_FILE
done
done
echo -e '<br>\n' >> $OUTPUT_FILE
done
echo '<html><head><style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 4px;
}
</style></head><body>' > $OUTPUT_FILE.html;
for VCENTER in $VCENTERS; do
VCENTER=$(echo ${VCENTER#https://} | sed 's/\/.*$//')
echo '<h2 style="text-align: center">Régen mentett VMware hosztok</h2>' >> $OUTPUT_FILE.html
ROWNUM=1
echo '<table>' >> $OUTPUT_FILE.html
while read INPUT ; do
[[ "$(echo $INPUT | grep -c '(Off)')" -eq 1 ]] && continue;
# if [[ $(($ROWNUM % 2)) -eq 0 ]]; then ROWNUM=$(($ROWNUM+1)); fi
if [[ $(echo $INPUT | grep -c '<caption>') -gt 0 ]]; then
echo "$INPUT" >> $OUTPUT_FILE.html;
elif [[ $(echo $INPUT | grep -c 'NINCS MENTVE') -gt 0 ]]; then
echo "<tr style=\"background-color: #ffa8a8\"><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html
else
if [[ $(echo $INPUT | grep -c 'VM_NAME') -gt 0 ]]; then
[[ $(($ROWNUM % 2)) -eq 0 ]] && echo "<tr style=\"background-color: #f2f2f2; font-weight: bold;\"><th>${INPUT//,/</th><th>}</th></th>" >> $OUTPUT_FILE.html || echo "<tr><th>${INPUT//,/</th><th>}</th></th>" >> $OUTPUT_FILE.html
else
[[ $(($ROWNUM % 2)) -eq 0 ]] && echo "<tr style=\"background-color: #f2f2f2\"><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html || echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html
fi
fi
ROWNUM=$(($ROWNUM+1))
done <<< "`awk 'BEGIN{IGNORECASE=1;RS=ORS="\n\n";FS=OFS="\n"}/'${VCENTER}'/' $OUTPUT_FILE`"
echo "</table><br>" >> $OUTPUT_FILE.html
echo '<h2 style="text-align: center">Kikapcsolt VMware hosztok</h2>' >> $OUTPUT_FILE.html
ROWNUM=1
echo '<table>' >> $OUTPUT_FILE.html
while read INPUT ; do
[[ "$(echo $INPUT | grep -c '(Off)')" -eq 0 ]] && continue;
if [[ $(echo $INPUT | grep -c '<caption>') -gt 0 ]]; then
echo "$INPUT" >> $OUTPUT_FILE.html;
elif [[ $(echo $INPUT | grep -c 'NINCS MENTVE') -gt 0 ]]; then
echo "<tr style=\"background-color: #ffa8a8\"><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html
else
if [[ $(echo $INPUT | grep -c 'VM_NAME') -gt 0 ]]; then
[[ $(($ROWNUM % 2)) -eq 0 ]] && echo "<tr style=\"background-color: #f2f2f2; font-weight: bold;\"><th>${INPUT//,/</th><th>}</th></th>" >> $OUTPUT_FILE.html || echo "<tr><th>${INPUT//,/</th><th>}</th></th>" >> $OUTPUT_FILE.html
else
[[ $(($ROWNUM % 2)) -eq 0 ]] && echo "<tr style=\"background-color: #f2f2f2\"><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html || echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" >> $OUTPUT_FILE.html
fi
fi
ROWNUM=$(($ROWNUM+1))
done <<< "`awk 'BEGIN{IGNORECASE=1;RS=ORS="\n\n";FS=OFS="\n"}/'${VCENTER}'/' $OUTPUT_FILE`"
echo "</table>" >> $OUTPUT_FILE.html
done
echo "<br><b>Kivetelek:</b> $EXCEPTIONS<br>" >> $OUTPUT_FILE.html
echo "</body></html>" >> $OUTPUT_FILE.html
echo -e "To: $MAILTO\nMIME-Version: 1.0\nContent-Type: text/html\nContent-Disposition: inline\nSubject: TSM VM kockazat riport\n$(cat $OUTPUT_FILE.html)" > $OUTPUT_FILE.html
cat $OUTPUT_FILE.html | sendmail -B 8BITMIME -t -F $EMAIL_FROM