cancel
Showing results for 
Search instead for 
Did you mean: 

Email notifications without BLAT

vikram_mehta08
Level 3

Hello,

 

I am trying to retreive email notifications from NetBackup 7.5. My supervisor does not want me to download BLAT so I wrote a VBScript that uses CDO to send emails. I wrote a line in nbmail.cmd that runs the script with parameters:

cscript mailtest.vbs %1 %2 %3

The parameters I used are what was defined in nbmail.cmd. I have tested running just the nbmail.cmd through the server and it works, I have gotten emails from there, but when I try to have NetBackup send it, I get nothing. I have configured the Universal Settings in the clients to include my email and have tried both "Server sends mail" and "Client sends mail". I ran a backup and even saw it complete, but I got no email.

Note: The scripts that I configured are all on the master server, but I added them to the client servers as well to test out.

Any help would be much appreciated.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

sdo
Moderator
Moderator
Partner    VIP    Certified

However, sometimes it is useful to capture the output, and errors if any, of commands embedded within scripts that NetBackup calls.  So, I usually like to start Windows DOS batch scripts with this at the very start:

@echo off

setlocal enabledelayedexpansion

set z_path=%~dp0

set z_name=%~n0

set z_file_log=!z_path!!z_name!.log

...which means:

1) @echo off - the leading @ stops the next command from being echo'd, and echo off disables echoing for the rest of the script

2) setlocal enabledelayedexpansion - means that variables can be expanded within loops using the exclamation (or shriek) characters, i.e. these ! !, because if you try to use a % % around variable names inside code inside a loop then they pre-expanded before the loop actually starts, whereas using ! ! means that variables get expanded at the time the line of code being intrepreted actually executes.

3) set z_path=%~dp0 - means, store in the variable named z_path the value of parameter 0 (i.e. the full file specification of the script being executed), but extract just the drive letter and folder path (dp = drive+path) segment from the full path of the file name of the script being executed.

4) set z_name=%~n0 - means, store in the variable named z_name the value of paramter 0 (i.e. the full path specification of the script being executed), but just extract the name part/segment of the full script file name.

5) set z_file_log=!z_path!!z_name!.log - means store in a variable named z_file_log, the contents of the variables z_path and z_name and add the characters .log to the end of those values - so, what this means is that the variable z_file_log now holds a full drive letter + folder path + filename + file extenstion of '.log', which is great because it means that now we can use that full "log file" specification file name to capture output from other commands - which also means that the log file for any script that you use this code in also lives in the same folder as the script that NetBackup calls.

.

Now that we have a decent log file name, this means that instead of binning (to nul, to the bit-bucket) any output or errors from a call to 'cscript', that instead now we can capture the output, so in your nbmail.cmd script you could now do something like:

cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1

...which means:

- cscript - means run the VBscript interpretation engine named cscript.exe

- mailer.vbs - means tells the 'cscript' engine to open and begin reading and to start executing the contents of mailer.vbs

- %1 %2 %3 - means pass the three DOS parameters (that mail.cmd received from NetBackup) to the command line so the cscript will make them available to mailer.vbs

- >>"!z_file_log!" - means >> = append, and append to a file whose name is inside z_file_log, i.e. the use of surrounding ! ! characters tell DOS to expand the contents of the variable named z_file_log, and the surrounding quotes " " are required because the full drive+folder+name+.log inside z_file_log may contain spaces, and so using " " tells DOS to keep it all together

- 2>&1 - means do the same for stderr as you do for stdout, and so any DOS or cscript errors also get appended to the contents of the log file.

- and note how we didn't need a leading '@' on this command, because we already disabled echoing above with the '@echo off' command.

.

And I like to capture the exit status of the cscript command, so that I can writethe status to the log file, so that we can see why our cscript command, or the execution of mailer.vbs, may have failed, e.g.:

set z_sts=!errorlevel!

echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1

exit /b !z_sts!

...which means:

- set z_sts=!errorlevel! - means store in teh variable named z_sts the volume of the variable errorlevel, see how the use of !errorlevel! tells DOS to expand errorlevel in to the contents of errorlevel.

- and then the echo sends a string of words and the contents (because we're using ! ! again) of z_sts to stdout, but our use of >>"!z_file_log!" means append stdout to the end of the file whose name is expanded out of the contents of variable z_file_log, and finnaly our old friend 2>&1 means do the same with errors which be generated by the echo command.

- exit /b !z_sts! - means exit teh script, and /b means just exit this level instance ie. don't exit the whole chain of calling/called DOS scripts, and !z_sts! tells the exit command to pass back the contents of the variable z_sts, i.e. to pass backup a final exit code back to the NetBackup process which called nbmail.cmd in the first place.

.

So, maybe your nbmail.cmd could look someting like:

@echo off

setlocal enabledelayedexpansion

set z_path=%~dp0

set z_name=%~n0

set z_file_log=!z_path!!z_name!.log

cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1

set z_sts=!errorlevel!

echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1

exit /b !z_sts!

...which will now run silently, and pass back a proper exit code, and capture all output and all errors in a file named "nbmail.log", as a companion file in the same folder that "nbmail.cmd" lives in.

HTH.

View solution in original post

12 REPLIES 12

sdo
Moderator
Moderator
Partner    VIP    Certified

As it stands, it is highly likely that your call to cscript is generating output to stdout (or possibly even stderr too).  Any commands inside any nbmail.cmd or mail_dr_info.cmd or any *_notify.cmd/bat type script always need to run silently - i.e. no output to stdout or stderr.  Make your cscript run silently and it might work.

sdo
Moderator
Moderator
Partner    VIP    Certified

Hi Vikram - all ok?  Need any help with making mail.cmd, and its contents, run silently?

If you are able to take a copy of your mailtest.vbs - and edit out any email and server and usernames and password (if any) - and replace these items with dummy text - and post/share a copy of this edited file, then I can think of at least one other person on here, right now, who might benefit you sharing this script.

vikram_mehta08
Level 3

HI sdo,

Thank you. My mailtest.vbs is:

option explicit

option explicit
    Dim objMail
    Dim objArgs

    Set objMail = CreateObject("CDO.Message")
    Set objArgs = wscript.Arguments

    objMail.From = "mailtest@domain.com"

    objMail.To = objArgs(0)

    objMail.Subject = objArgs(1)

    objMail.Testbody = objArgs(2)

    objMail.Configuration.Fields,Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
    objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="mailserver.domain.com"
    objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 

    objMail.Send

 

I am running this script through nbmail.cmd with the given command above but I also added running it silently like previous commentator mentioned, but no email still: cscript //B //nologo mailtest.vbs %1 %2 %3

sdo
Moderator
Moderator
Partner    VIP    Certified

A good construct to add, in scripts that NetBackup calls, is this before all DOS commands:

@

...e.g.

@command blah blah blah

....which stops the command from being 'echo'd to stdout (if an '@cho off' is not already active).

.

And a construct which can be added to the end of DOS commands, so that all output hits the bit-bucket, is:

>nul 2>&1

...where >nul means send stdout to null device (aka bit-bucket), and 2>&1 means do the same with stderr, e.g:

@command blah blah blah >nul 2>&1

HTH.

sdo
Moderator
Moderator
Partner    VIP    Certified

However, sometimes it is useful to capture the output, and errors if any, of commands embedded within scripts that NetBackup calls.  So, I usually like to start Windows DOS batch scripts with this at the very start:

@echo off

setlocal enabledelayedexpansion

set z_path=%~dp0

set z_name=%~n0

set z_file_log=!z_path!!z_name!.log

...which means:

1) @echo off - the leading @ stops the next command from being echo'd, and echo off disables echoing for the rest of the script

2) setlocal enabledelayedexpansion - means that variables can be expanded within loops using the exclamation (or shriek) characters, i.e. these ! !, because if you try to use a % % around variable names inside code inside a loop then they pre-expanded before the loop actually starts, whereas using ! ! means that variables get expanded at the time the line of code being intrepreted actually executes.

3) set z_path=%~dp0 - means, store in the variable named z_path the value of parameter 0 (i.e. the full file specification of the script being executed), but extract just the drive letter and folder path (dp = drive+path) segment from the full path of the file name of the script being executed.

4) set z_name=%~n0 - means, store in the variable named z_name the value of paramter 0 (i.e. the full path specification of the script being executed), but just extract the name part/segment of the full script file name.

5) set z_file_log=!z_path!!z_name!.log - means store in a variable named z_file_log, the contents of the variables z_path and z_name and add the characters .log to the end of those values - so, what this means is that the variable z_file_log now holds a full drive letter + folder path + filename + file extenstion of '.log', which is great because it means that now we can use that full "log file" specification file name to capture output from other commands - which also means that the log file for any script that you use this code in also lives in the same folder as the script that NetBackup calls.

.

Now that we have a decent log file name, this means that instead of binning (to nul, to the bit-bucket) any output or errors from a call to 'cscript', that instead now we can capture the output, so in your nbmail.cmd script you could now do something like:

cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1

...which means:

- cscript - means run the VBscript interpretation engine named cscript.exe

- mailer.vbs - means tells the 'cscript' engine to open and begin reading and to start executing the contents of mailer.vbs

- %1 %2 %3 - means pass the three DOS parameters (that mail.cmd received from NetBackup) to the command line so the cscript will make them available to mailer.vbs

- >>"!z_file_log!" - means >> = append, and append to a file whose name is inside z_file_log, i.e. the use of surrounding ! ! characters tell DOS to expand the contents of the variable named z_file_log, and the surrounding quotes " " are required because the full drive+folder+name+.log inside z_file_log may contain spaces, and so using " " tells DOS to keep it all together

- 2>&1 - means do the same for stderr as you do for stdout, and so any DOS or cscript errors also get appended to the contents of the log file.

- and note how we didn't need a leading '@' on this command, because we already disabled echoing above with the '@echo off' command.

.

And I like to capture the exit status of the cscript command, so that I can writethe status to the log file, so that we can see why our cscript command, or the execution of mailer.vbs, may have failed, e.g.:

set z_sts=!errorlevel!

echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1

exit /b !z_sts!

...which means:

- set z_sts=!errorlevel! - means store in teh variable named z_sts the volume of the variable errorlevel, see how the use of !errorlevel! tells DOS to expand errorlevel in to the contents of errorlevel.

- and then the echo sends a string of words and the contents (because we're using ! ! again) of z_sts to stdout, but our use of >>"!z_file_log!" means append stdout to the end of the file whose name is expanded out of the contents of variable z_file_log, and finnaly our old friend 2>&1 means do the same with errors which be generated by the echo command.

- exit /b !z_sts! - means exit teh script, and /b means just exit this level instance ie. don't exit the whole chain of calling/called DOS scripts, and !z_sts! tells the exit command to pass back the contents of the variable z_sts, i.e. to pass backup a final exit code back to the NetBackup process which called nbmail.cmd in the first place.

.

So, maybe your nbmail.cmd could look someting like:

@echo off

setlocal enabledelayedexpansion

set z_path=%~dp0

set z_name=%~n0

set z_file_log=!z_path!!z_name!.log

cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1

set z_sts=!errorlevel!

echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1

exit /b !z_sts!

...which will now run silently, and pass back a proper exit code, and capture all output and all errors in a file named "nbmail.log", as a companion file in the same folder that "nbmail.cmd" lives in.

HTH.

vikram_mehta08
Level 3

Thank you! The log helped me out. The problem was that I didn't save my .vbs file in C:\Windows\System32 folder. I thought it would be called from the same directory where the nbmail.cmd file was. I am getting emails now, but I do not know what they mean at this point. The subject is "Backup on <server> - 0". The body shows a .tmp file saved in the C:\Windows\TEMP. How do I know from the email if a backup is successful or if it failed? 

sdo
Moderator
Moderator
Partner    VIP    Certified

Glad it helped.  The trailing zero '0' of the subject is the backup status - and status zero = success (no files were blocked/locked).

vikram_mehta08
Level 3

Gotcha. I am writing a script in the batch file to better format with successes anf failures. I am unfamiliar with batch syntax, but this is what I have so far:

 

@FOR /F "tokens=1-5" %%i IN ("%2") DO SET SUBJECT="%%1 failed %%j %%k %%l %%m"
@IF "%m"=="0" (
SET SUBJECT="%%i %%j %%k Success %%l %%m"
) ELSE IF "%m"=="1" (
SET SUBJECT="%%i %%j %%k Partial %%l %%m"
) ELSE (
SET SUBJECT="%%i %%j %%k Failed %%l %%m"
)

cscript mailtest.vbs %1 "%SUBJECT%" %3

Would you know if this is correct syntax? I am not sure about when to use %% vs %.

sdo
Moderator
Moderator
Partner    VIP    Certified

Let's go back a half step.

1) Are you wanting to send an email for all jobs?

2) Or only for backup jobs?

.

I ask because, depending upon your answers, I think you may be better off using a 'backup_exit_notify' script - because such a script would receive more parameters, i.e. policy name, client name, schedule name, job PID, job status, and a few more if I remember - and so could afford you more flexibility, and not having to assume that the structure of the email 'subject' parameter is always consistent.

The other thing to remember is that multiple instance of any of these scripts could be running at one time, and so any use of log files, or temporary files within any of these scripts has be coded to in effect handle multi-threading - or more correctly, multi-processing.

And, I get the idea of sending emails for failed backups, but I never really had a need to send emails for successful backups.  And other things that you can do with backup_exit_notify is put logic inside to ignore certain policy names (or ignore based on fragments of policy names), and ignore certain clients (e.g. test clients).  E.g. you could ignore all policy names that do not contain "_PROD_" at a certain position in the policy name, and so not bother sending emails for failed _TEST_ or _PREPROD_ or _TRAINING_ environment backups.

Let me know if you want to explore your options around 'backup_exit_notify'.

vikram_mehta08
Level 3

I am trying to receive emails for failed and successful jobs. From most guides I read, they said you can only receive emails from either successful jobs, or all jobs. If there is a way I can get only failed jobs emails, and be able to filter out those emails for certain policies, that would make this so much better. 

sdo
Moderator
Moderator
Partner    VIP    Certified

I think 'backup_exit_notify' is the way to go.

Start a new post and I'll pick it up.  :)

vikram_mehta08
Level 3

Thank you so much! https://www-secure.symantec.com/connect/forums/backup-exit-notify-script