<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

VMware VDR email notifications script

Published on
10,383 Points
4,383 Views
Last Modified:
Approved
Managing some ESX (4.1) farms for some time now I was looking for a way to get notified of the backup results of my VDR devices (1.2). I came across an article in the VMware communities: VDR email which seemed to be what I was looking for. After some testing I got it to work but wasn't quite satisfied yet. I wanted a report that I could present or mail directly to the management if necessary.
So I started to play around to suite it to my needs. Though I'm not a scripting expert I know enough to be dangerous. But the result might not be a typical example of neat programming.

Anyway, I quite happy with the result and wanted to share this with you.

Here is the code:

#!/bin/bash
#
# Variables 
domain=mydomain.local
host=$HOSTNAME
smtphost=mymailhost.mydomain.local
mailto=myemail@mydomain.com
mailto2=mysecondemail@mydomain.com
#
cat /dev/null >> /root/oldfailure.out
cat /dev/null >> /root/oldlocks.out
cat /dev/null >> /root/olderror.out
cat /dev/null >> /root/oldsuccess.out
cat /dev/null >> /root/oldbackup.out
cat /dev/null >> /root/oldintegrity.out
cat /dev/null >> /root/oldrestorepoint.out
cat /dev/null >> /root/oldincomplete.out
cat /dev/null >> /root/temperror.out
cat /dev/null >> /root/temperror2.out
cat /dev/null >> /root/temperror3.out
cat /dev/null >> /root/temperror4.out
cat /dev/null >> /root/tempincomplete.out
#
fail=0
lock=0
err=0
succ=0
incomp=0
#
# First get the errors and make it a bit more readable
strings -e l /var/vmware/datarecovery/operations_log.utx | grep -i "error" | sed -e 's/\[[^]]*\]//g' > /root/temperror.out
#
# Now filter out the failures and make it human readable
cat /root/temperror.out | grep  "Fail" > /root/tempfailure.out
cat /root/tempfailure.out | awk -F"$" '{print $2 $3 $6}' > /root/newfailure.out
#
# Diff out the failures so only the other errors remain
diff /root/temperror.out /root/tempfailure.out > /root/temperror2.out
#
# See if there are any locks (lock=no backup!)
cat temperror2.out | grep "locked" > /root/templock.out
cat /root/templock.out | awk -F"$" '{print $2 $3 $4 $5}' | sed 's/?//g;s/"//g' > /root/newlocks.out
#
# And diff out the locks
diff /root/temperror2.out /root/templock.out > /root/temperror3.out
#
# Lines with the general notice "execution error" we can ignore, so diff them out
cat temperror3.out | grep -A1 "execution errors" > /root/tempexecerr.out
diff /root/temperror3.out /root/tempexecerr.out > /root/temperror4.out
#
# Now we get the remaining errors separated from the rubbish
cat temperror4.out | grep "< < <" > /root/temperror5.out
cat /root/temperror5.out | awk -F"$" '{print $2 $3 $4}' | awk -F"," '{print $1 $2}' | sed 's/?//g;s/"//g' > /root/newerror.out
#
#creating incomplete list
strings -e l /var/vmware/datarecovery/operations_log.utx | grep -i -B2 "incomplete" | sed -e 's/\[[^]]*\]//g' | awk -F"$" '{print $1 $2 $3 $4}' | awk -F"." '{print $1}' > /root/newincomplete.out
#
#creating backuplist
strings -e l /var/vmware/datarecovery/operations_log.utx | grep -i -A3 "back up" | awk -F"$" '{print $2 $3}' | sed -e '/\[[^]][0-9]*\]$/ {N; s/\[[^]][0-9]*\]\n//}' | sed -e 's/\[[^]]*\]//g' | sed 's/ "//' > /root/newbackup.out
diff /root/oldbackup.out /root/newbackup.out | sed '1d; s/^.\{2\}//g' > /root/diffbackup.out
mv /root/newbackup.out /root/oldbackup.out -f
succ=$(cat /root/diffbackup.out | grep -c "Completed")
#creating integrity check list
strings -e l /var/vmware/datarecovery/operations_log.utx | egrep  -A1 '.*Starting.*integrity.*check.*' | awk -F"$" '{print $2 $3 $4 $5}' | sed -e 's/\[[^]]*\]//g' > /root/newintegrity.out
diff /root/oldintegrity.out /root/newintegrity.out | sed '1d; s/^.\{2\}//g' > /root/diffintegrity.out
mv /root/newintegrity.out /root/oldintegrity.out
#creating restore point overview
strings -e l /var/vmware/datarecovery/operations_log.utx | grep -i "restore point" | sed -e 's/\[[^]]*\]//g' | sed 's/?//g;s/"//g' | awk -F"$" '{print $2 $3 $4 $5}' > /root/newrestorepoint.out
diff /root/oldrestorepoint.out /root/newrestorepoint.out | sed '1d; s/^.\{2\}//g' > /root/diffrestorepoint.out
mv /root/newrestorepoint.out /root/oldrestorepoint.out -f
##
# Compare new list of all failures to old list of all failures
if ! diff /root/oldfailure.out /root/newfailure.out
then
diff /root/oldfailure.out /root/newfailure.out | sed '1d; s/^.\{2\}//g' > /root/difffail.out
fail=$(cat /root/difffail.out | grep -c "Fail")
mv /root/newfailure.out /root/oldfailure.out -f
fi
#
# Compare new list of all locks to old list of all locks
if ! diff /root/oldlocks.out /root/newlocks.out
then
diff /root/oldlocks.out /root/newlocks.out | sed '1d; s/^.\{2\}//g; s/" //' > /root/difflocks.out
lock=$(cat /root/difflocks.out | grep -c "lock")
mv /root/newlocks.out /root/oldlocks.out -f
fi
#
# Compare new list of all errors to old list of all errors
if ! diff /root/olderror.out /root/newerror.out
then
diff /root/olderror.out /root/newerror.out | sed '1d; s/^.\{2\}//g; s/" //' > /root/differr.out
err=$(cat /root/differr.out | grep -c "error")
mv /root/newerror.out /root/olderror.out -f
fi
#
#Compare new list of all incompletes to old list of all incompletes
#
if ! diff /root/oldincomplete.out /root/newincomplete.out
then
diff /root/oldincomplete.out /root/newincomplete.out | sed '1d; s/^.\{2\}//g' > /root/diffincomplete.out
incomp=$(cat /root/diffincomplete.out | grep -c "incomplete")
mv /root/newincomplete.out /root/oldincomplete.out -f
fi
#if ! diff /root/oldsuccess.out /root/newsuccess.out
#then
#diff /root/oldsuccess.out /root/newsuccess.out  | grep ">"  > /root/diffsuccess.out
#succ=$(cat /root/diffsuccess.out | grep -c "success")
#mv /root/newsuccess.out /root/oldsuccess.out -f
#fi
# Send email with any errors found
(
sleep 1
echo "HELO $host.$domain"
sleep 1
echo "mail from: vdrreport@$domain"
sleep 1
echo "rcpt to:$mailto"
sleep 1
echo "rcpt to:$mailto2"
sleep 1
echo "data"
sleep 1
echo "subject: $host VDR-Report $(date +"%Y.%m.%d %H:%M") Jobs OK: $succ, Failures: $fail, Incomplete jobs: $incomp, Locks: $lock, Errors: $err"
sleep 1
echo "from: "vdrreport@$domain
sleep 1
echo "to:$mailto"
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "$host $(date +"%Y.%m.%d %H:%M") Jobs OK: $succ, Failures: $fail, Incomplete jobs: $incomp, Locks: $lock, Errors: $err #####"
echo ""
sleep 1
echo "#################################################################################################"
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### $fail Failures #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/difffail.out ]
    then
      cat /root/difffail.out
    else
      echo No list for failures found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### $incomp Incomplete jobs #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/diffincomplete.out ]
    then
      cat /root/diffincomplete.out
    else
      echo No list for incomplete jobs found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### $lock Locks #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/difflocks.out ]
    then
      cat /root/difflocks.out
    else
      echo No list for locks found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### $err Errors #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/differr.out ]
    then
      cat /root/differr.out
    else
      echo No list for errors found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### Backup overview ($succ successfull jobs) #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/diffbackup.out ]
    then
      cat /root/diffbackup.out
    else
      echo No list for backups found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "##### Integrity check overview #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/diffintegrity.out ]
    then
      cat /root/diffintegrity.out
    else
      echo No list for integrity checks found!
  fi
sleep 1
echo ""
sleep 1
echo ""
sleep 1
echo "####################################################################"
sleep 1
echo "#####  Restore point overview #####"
sleep 1
echo " "
sleep 1
  if [ -f /root/diffrestorepoint.out ]
    then
      cat /root/diffrestorepoint.out
    else
      echo No list for restore point found!
  fi
sleep 1
echo " "
sleep 1
echo "."
sleep 1
echo "QUIT"
) | telnet $smtphost.$domain 25
  
# Cleaning up
rm -f /root/diff*.out
rm -f /root/temp*.out

exit 0

Open in new window


Basically what it does is reading the VDR backup log (/var/vmware/datarecovery/operations_log.utx) and filtering out the errors, failures, locks and incompletes. These are written to separate log files and compared with the logs kept from the previous backup check (my backups run once a day so the script runs once a day as well after the backup). If the compare shows new items, these will be set aside to be mailed. Otherwise, just a notification of no error, lock, etc will be shown.

There are some variables used you need to adjust to match you environment:

domain=mydomain.local
host=$HOSTNAME
smtphost=mymailhost
mailto=myemail@mydomain.com
mailto2=mysecondemail@mydomain.com

domain: The domain for your mailhost. I'm using an internal mail server, hence the .local.
host: The hostname used in the VDR report. I'm using the local $HOSTNAME variable here.
smtphost: The hostname of the mailserver used to send the report.
mailto and mailto2: The recipients for the report. I use two (my own address and the address of itsupport).

Those should be all the changes you need in the script it self. On the VDR you'll need to install telnet to make the device able to send it.
You can do that from the console of the VDR by using: yum -y install telnet

After that you just need to copy over the script to the VDR and create a cronjob for it. I put the script in /var/vmware/datarecovery/ but because full pathnames are used you should be able to put it where ever you like.
Because I use root to run the script, all the output is written to the /root folder. If you want to use another user for the job you might want to consider changing those paths.

To add the script to the crontab use: crontab -e
This will open the vi editor showing the file that holds you cronjobs (if any). Now add the following line: 0 10 * * * /var/vmware/datarecovery/VDRreport.sh
This will run the script (I called it VDRreport.sh here) at 10 minutes past midnight, every day.

That should be it. You now should get email notifications from the VDR that looks something like:

VDR1 2012.07.05 10:00 Jobs OK: 5, Failures: 0, Incomplete jobs: 0, Locks: 0, Errors: 0 #####

#################################################################################################


####################################################################
##### 0 Failures #####

No list for failures found!


####################################################################
##### 0 Incomplete jobs #####

No list for incomplete jobs found!


####################################################################
##### 0 Locks #####

No list for locks found!


####################################################################
##### 0 Errors #####

No list for errors found!


####################################################################
##### Backup overview (5 successfull jobs) #####


7/4/2012 6:05:49 PM: Performing incremental back up of disk server10/server10-flat.vmdk
7/4/2012 6:17:22 PM: Performing incremental back up of disk server10/server10_1-flat.vmdk
7/4/2012 6:17:32 PM: Performing incremental back up of disk server10/server10_2-flat.vmdk
7/4/2012 6:29:38 PM: Execution completed successfully
7/4/2012 6:29:38 PM: Completed: 9 files, 90.1 GB

7/4/2012 6:05:18 PM: Performing incremental back up of disk server008-new/server008-new-flat.vmdk
7/4/2012 6:35:59 PM: Execution completed successfully
7/4/2012 6:35:59 PM: Completed: 5 files, 50.1 GB

7/4/2012 6:05:19 PM: Performing full back up of disk server020.mydomain.local/server020.mydomain.local-flat.vmdk
7/4/2012 6:52:41 PM: Execution completed successfully
7/4/2012 6:52:41 PM: Completed: 5 files, 12.7 GB

7/4/2012 6:02:45 PM: Performing incremental back up of disk vserver150/vserver150-000002-flat.vmdk
7/4/2012 6:32:57 PM: Performing incremental back up of disk vserver150/vserver150_1-000002-flat.vmdk
7/4/2012 7:07:29 PM: Execution completed successfully
7/4/2012 7:07:29 PM: Completed: 7 files, 250.1 GB

7/4/2012 6:05:19 PM: Performing incremental back up of disk VCS-w2k8R2-x64/VCS-w2k8R2-x64-flat.vmdk
7/4/2012 7:24:10 PM: Performing incremental back up of disk VCS-w2k8R2-x64/VCS-w2k8R2-x64_1-flat.vmdk
7/4/2012 7:26:22 PM: Execution completed successfully
7/4/2012 7:26:22 PM: Completed: 7 files, 100.1 GB


####################################################################
##### Integrity check overview #####



####################################################################
#####  Restore point overview #####

7/5/2012 7:00:24 AM: Deleted restore point 7/3/2012 6:00:04.000 PM for virtual machine vserver150

Open in new window


Like said before: I'm no scripting expert but this is working nicely for me. If you have any suggestions or improvements, do let me know and I will gladly try them and update the script.
0
Comment
Author:Ernie Beek
0 Comments

Featured Post

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Join & Write a Comment

Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…
Teach the user how to install and configure the vCenter Orchestrator virtual appliance Open vSphere Web Client: Deploy vCenter Orchestrator virtual appliance OVA file: Verify vCenter Orchestrator virtual appliance boots successfully: Connect to the …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month