Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script to send smtp messages from NNM

Perl Scripts

Perl script to send smtp messages from NNM

by  vlan52  Posted    (Edited  )
Afetr adding you command for automatic action in $OV_CONF/trustedCmds.conf and creating ALLOW_ALL file in the same directory, use the following script developed by Nigel Bowden or nbowden (there is some aditional info at the bottom regarding NNM's variables, etc...) if you wish, of course:
vlan52
[sunshine]

# Perl script to send email alerts from HP OV events
#
# The event must pass the following parameters :
#
# 1. The node name/ip address (mandatory)
# 2. The alert message (e.g. node down) (mandatory)
# 3. The time of the alert in Unix time format (mandatory)
#
# These 3 fields may optionally be followed by any number of email addresses alert to be sent to.
#
# To generate documentation for this script, use the command :
#
# pod2html --infile=email_alert.pl --outfile=email_alert.html
#
use Net::SMTP;
use strict;

my $DISABLE = '0'; # set this to 1 to disable this script (for maintenance) and 0 to enable.

exit if ($DISABLE);

# Configure the mail server IP/name here
my $MAILHOST = '216.82.106.14';

# Configure the email address of the receivers here.
# format for multiple entries : my @EMAIL_TO = ( 'what.ever@fgfsdg.com', 'fsdfd.rfsrfs@dfsfs.com', 'nnm@nocview.gfgdsg.com');
my @EMAIL_TO = ( 'helpdesk@my_company.con');

# Configure a name to appear in the 'From' field of the email msg here
my $FROM = 'nnm@sdfgsdgs.com';

###################################################
# Don't change anything below here !!!
###################################################

my ($node, $alert, $time, @email_addresses) = @ARGV;

# check that we have the miminum fields which we require
if (! ($node && $alert && $time) ) {
die "You must supply a value for the node, alert message and time !\nNode = $node\nMessage = $alert\nTime = $time\n\n";
}

$time = localtime($time); # convert to readable format

my $msg = ("$time : $node : $alert\n");

# connect to the smtp server
my $smtp = Net::SMTP->new($MAILHOST, Timeout => 10) or die "Unable to connect to mail host : $MAILHOST - $!\n";

# add email addresses configured in @EMAIL_TO to any email addresses
# passed in args from command line
@email_addresses = (@email_addresses, @EMAIL_TO);

foreach my $address (@email_addresses) {

$smtp->mail($address);
$smtp->to($address);
$smtp->data();
$smtp->datasend("From: $FROM\n");
$smtp->datasend("To: $address\n");
$smtp->datasend("Subject: $alert : $node\n");
$smtp->datasend("\n$msg \n\n");
$smtp->datasend;
$smtp->dataend();
}
$smtp->quit;

####################################################################
# End of script, beginning of documentation
####################################################################


=head1 HP OV Email Alert Script

This script provides a very simple means of creating email alerts from events generated by
HP OV.

By calling the script (and supplying accompanying parameters) from the "Command for Automatic Action" field
of an event, a notification e-mail will be sent each time that particular event occurs.

=head1 SYNOPSIS

perl email_alert.pl <node_name> <event msg> <timestamp> [email_address email_address ... ]

e.g. perl email_alert.pl $2 "Node Down" $5 helpdesk@my_company.com
perl email_alert.pl $2 "Node Down" $5 helpdesk@my_company.com fred@my_company.com
perl email_alert.pl $2 "Interface Down : $7" $5


=head1 Pre-requisites

=over

=item 1. Working version of NNM 6.x

=item 2. Installed perl distribution which includes the Net::SMTP module (e.g. http://www.activestate.com)

=item 3. An smtp server to send the e-mails

=back


=head1 Configuration

Before the script can be run from HP OV events, a number of parameters need to be configured within
the email_alert.pl script.

You will need to edit the script file directly. Use a text editor which does not insert formatting
characters (e.g. Notepad, UltraEdit (http://www.ultraedit.com) ).

Set values for the following variables :

=over

=item 1. $MAILHOST - the ip address or hostname of the smtp server

=item 2. @EMAIL_TO - the e-mail address(es) which will receive the e-mail (note these will be used in addition
to any e-mail addresses passed in via the command line). Leave this variable blank if you do not want a static
e-mail address used, but wish to pass in the e-mail address(es) via the command line. (i.e. @EMAIL_TO = ';)

=back


=head1 Typical Usage

The script must be passed 3 fields which must be :

=over

=item 1. The node name of the node causing the event

=item 2. The event message itself

=item 3. The (Unix) timestamp that the event occurred at

=back

Optionally, any number of email addresses can also be passed after the 3 fields described above. If email
addresses are passed with the event (i.e. via the command line), then they will be appended to the
@EMAIL_TO address(es) (set in the script) and the alert message will be sent to all of the addresses.

Here are some typical examples (note that the variables $2, $5 etc. are obtained from the event description
within the event configuration utility on HP OV) :

Node Down Event : perl c:/scripts/email_alert $2 "Node Down" $5
Node Up Event : perl c:/scripts/email_alert $2 "Node Up" $5
Interface Down Event : perl c:/scripts/email_alert $2 "Interface Down : $7" $5 helpdesk@my_company.com support@my_company.com
Interface Down Up : perl c:/scripts/email_alert $2 "Interface Down : $7" $5


=head1 Troubleshooting

A number of issues may be experienced due to mis-configuration. Here are a few tips to help
diagnose problems :

=head2 Testing if emails are being sent correctly

To test emails are being sent correctly by the script, it is best to run the script from the
command line initially and see if e-mails are received by the recipient.

Remember that (at least) three parameters must be passed to the script.

Here is an example of how you might test the script to see if e-mails are being sent
correctly. Open up a command prompt window and enter the following (you will need to change the paths
to the files to suit your own environment) :

e.g. c:\perl\bin\perl.exe c:\scripts\email_alert Test_Node "Test Message" "1028713184" me@my_company.com

If you do not receive an e-mail, check that you have the recipient (@EMAIL_TO) set correctly (or
you are passing an email address as a 4th parameter) and the smtp server ($MAILHOST) is set correctly
in the script.

=head2 Checking if the script is running correctly from HP OV

If you have tested whether e-mail is being correctly sent by the script, but you still do not get
emails when events occur, you will need to look in an HP OV log to see what is happening.

The log file to check is $HPOV_ROOT/log/ovactiond.log (where $HPOV_ROOT is the location where is
HP OV is installed)

If you check the last few entries in the log file, you shoild see if there are any errors being caused
by the script.

=head1 FAQ

Q1. Can the generated email be sent to multiple e-mail addresses ?
A1. Yes, you can pass a number of e-mail addresses with the event parameters and/or hardcode a number of
email addresses in the @EMAIL_TO variable in the script.

Q2. Can I use the perl distribution which is supplied with HP OV to run this script ?
A1. No, as it does not contain the Net::SMTP module. Get the perl distribution from http://www.activestate.com to be safe.

Q3. Which platforms has this been tried on ?
A3. HP OV NNM 6.2/Win2K

Q4. Does this script work on Solaris ?
A4. Probably...let me know if you try it.

Q5. Why is NNM complaining about "Trusted commands" when an event occurs ?
A5. See the document "ovactiond.pdf" which is supplied with on your NNM CD and the ovactiond reference/man page

Q6. Where can I get the latest version of this script ?
A6. Try here : http://www.nbnetworks.co.uk

=head1 Author

Nigel Bowden - nigel.bowden@snsl.co.uk

=head1 Disclaimer & Copyright

You are welcome to use this script as you wish, but it is entirely at your own risk.

I take no repsonsibility for any damage or loss which you may incurr whilst using it.

Use this at your own risk. I make no representations or committments about its accuracy,
quality or that it is fit for purpose.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

A little background on variables, characters, etc... for NNM events:

The Event Log Message, Pop-up Window Message, and Command for Automatic Action fields in the Modify Events and Copy Events dialog boxes use special $ variables to present data that was received with the event. You may enter several special characters to give you control of the formatted output. The format can include the following standard C printf formats, which will be converted to their ASCII equivalent.
All nonprintable characters are converted to their octal (\ooo) equivalent for display in the event browser, or when passed to the operator initiated (manual) actions. The two exception are that a tab is displayed as \t in the Alarm Browser and as spaces in pop-up messages. A new line is displayed as \n in the Alarm Browser and as a new line in pop-up messages.

All nonprintable characters are passed unconverted to automatic actions executed by ovactiond.

Special Characters:

a Alert (bell) character
b Backspace
f Form feed
n Newline
r Carriage return
t Horizontal tab
v Vertical tab
\ Backslash (Use double backslashes to separate elements in a pathname.)
ooo Octal number, first character must be 0 or 1 and second and third numbers must be 0-7
xhh Hex number, both hh characters must be 0-9a-fA-F

Sequential Attribute Variables:

The following $ variables are used to access the sequential attributes that were received with the event. Each event has (possibly none) attributes associated with it. They are accessed using the $n notation, where n is the positional attribute, with 1 being the first attribute. The printing format is based on the ASN.1 type of the attribute These attributes are equivalent to the variable bindings (varBinds) in an SNMP trap.

$arg
$# Print the number of attributes in the event.
$* Print all the attributes as seq name (type): value strings, where seq is the attribute sequence number.
$n Print the nth attribute as a value string. Must be in the range of 1 to 99.
$-n Print the nth attribute as a seq name (type): value string. Must be in the range of 1 to 99.
$+n Print the nth attribute as a name: value string. Must be in the range of 1 to 99.

$>n Print all attributes greater than n as value strings, useful for printing a variable number of arguments. $>0 is equivalent to $* without sequence numbers, names, or types.
$>-n Print all attributes greater than n as seq name (type): value strings.
$>+n Print all variables greater than n as name: value strings.

Special Information Variables:

You can also include information from the incoming event by using the $arg format specification. The following $ variables are valid regardless of the type of event (SNMPv1, SNMPv2C, CMIP, GENERIC):

$arg
$a Print the source of the event as an IP address.
$x Print the date the event was received using the local date representation.
$X Print the time the event was received using the local time representation.
$@ Print the time the event was received as a number of seconds since the Epoch (Jan 1, 1970) using the time_t representation.
$O Print the name (object identifier) of the received event.
$o Print the name (object identifier) of the received event as a string of numbers.

$V Print the event type, based on the transport the event was received. Currently supported types are SNMPv1, SNMPv2C, CMIP, and GENERIC.
$r Print the implied "source" of the event in textual form. This may not be the "true source" of the event if the "true source" is proxying for another source, such as when a monitoring application running locally is reporting information about a remote node.
$R Print the "true source" of the event in textual form. This value is inferred by means of the transport mechanism which delivered the event. If the event was forwarded, this will display the address of the remote event framework (that is, remote pmd's machine).

$c Print the category the event belongs in.
$s Print the severity of the event.
$N Print the name (textual alias) of the event format specification used to format the event, as defined in trapd.conf.
$F Print the textual name of the remote event framework's machine (that is, remote pmd's machine) if this event was forwarded, else local machine's name.
$U Print the Universally Unique Identifier (UUID) of the current event as a string of numbers.
$$ Print the $ character.

SNMP-specific Variables:

The following variables are meaningful for events created from SNMPv1 or SNMPv2 traps/informs.

$arg
$C Print the trap community string. Set to public for non-SNMPv1 events.
$E Print the trap enterprise as a text string if possible, otherwise as in the $e arg below. This option tries to use the enterprise name as formatted in trapd.conf, as opposed to $O which formats using the MIB definitions, which is typically a longer string. This number is implied by the event object identifier for non-SNMPv1 events.
$e Print the trap enterprise as an Object ID string of numbers. This number is implied by the event object identifier for non-SNMPv1 events.

$A Print the trap agent address as defined in the trap PDU.

NOTE This may be different from the agent that actually sent the event. If the name server knows about this node, the node name will be printed, otherwise the address will be printed.

$G Print the trap's generic-trap number. This number is implied by the event object identifier for non-SNMPv1 events.
$S Print the trap's specific-trap number. This number is implied by the event object identifier for non-SNMPv1 events.
$T Print the trap's sysUpTime time-stamp. This is the remote machine's time in hundredths of a second between the last reinitialization of the device and the generation of the trap, it is NOT the time the event was received (see $x, $X, and $@) instead. For non-SNMPv1 events this value is 0.

Obsoleted/Reserved Variables:

The following variables are obsolete or reserved. Their use may cause unpredictable results, and should not be used:

$arg
$L Obsolete, do not use.
$I Do not use.
$I Do not use.
$t Do not use.
$= Do not use.

HP OpenView Network Node Manager Release B.06.20 Copyright (c) 1990-2001 Hewlett-Packard Company. All rights reserved.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top