Email Trap Alert

  • Updated

This is a basic example which shows:

  • How to access parameters passed through from Trap Alerting.
  • How to access the message of the trap and put into the body of the email.
  • How to create and send an email to a single address.
  • How to create and send an email to a profile.

NOTE: This example script:

  • Does not implement any alert bundling.
  • Will generate an email message for every alert passed in from Status Alerting.
sub alert_mail_trap
{

# Fill in the email address or profile you want the trap sent to
my $mail_to = ‘example@email.com'; # e.g. jbloggs@example.com
my $profile = ''; # e.g. NetEng

# Alert Arguments
my ($arg_ref) = @_;
my $tt = $arg_ref->{tt};
my $time_str = strftime ("%H:%M", localtime ($arg_ref->{tt}));
my $device = $arg_ref->{device};
my $ipaddr = $arg_ref->{ipaddr};
my $trap_oid = $arg_ref->{trap_oid};

# Access the array reference stored under the key 'oids'. Contains message of trap
my @oids = $arg_ref->{oids}->@*;

# e-mail body and subject
my @body;
my $subject;

# Put the header into the subject field.
$subject = sprintf ("%s %s %s %s", $time_str, $device, $ipaddr, $trap_oid);

# Header also put into body as first line
push @body, sprintf ("%s %s %s %s", $time_str, $device, $ipaddr, $trap_oid);

# Add message lines to the body of email
foreach my $msgLine (@oids) {
push @body, sprintf ("%s", $msgLine);
}

# Send to a single email address
if ($mail_to ne "") {
mail ({ subject => $subject, to => $mail_to, body => \@body });
}

# Send to all email addresses in a profile

if ($profile ne "") {
for my $addr (config_get_emails ($profile)) {
mail ({ subject => $subject, to => $addr, body => \@body });
}
}
return;
}

Was this article helpful?

/

Comments

0 comments

Please sign in to leave a comment.