Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Email Syslog Alert

This is a basic example which shows:

  • How to access parameters passed through from Syslog Alerting.
  • How to retrieve sysContact and sysLocation of a device from ADB.
  • 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 Syslog Alerting.
  • The priority and facility parameters are only available in v20.1 or later.
sub alert_mail_syslog
{
   my ($arg_ref) = @_;
   my $mail_to = ''; # e.g. [email protected]
   my $profile = ''; # e.g. NetEng

   # Parameters from alert
   my $tt       = $arg_ref->{tt};
   my $device   = $arg_ref->{device};
   my $ipaddr   = $arg_ref->{ipaddr};
   my $priority = $arg_ref->{priority};
   my $facility = $arg_ref->{facility};
   my $msg      = $arg_ref->{msg};
   my $time_str = strftime ("%H:%M", localtime ($arg_ref->{tt}));

   # Device parameters from database
   my $contact  = adb_result (sprintf ("get %s sys SNMPv2-MIB.sysContact", $device));
   my $location = adb_result (sprintf ("get %s sys SNMPv2-MIB.sysLocation", $device));

   my $subject = sprintf ("Syslog: %s %s %s",
                          $priority,
                          $device,
                          $ipaddr);
   my @body;

   push @body, join (" ",
                     $time_str,
                     $device,
                     $ipaddr,
                     #$contact  || "",
                     #$location || "",
                     $msg
                    );

   # 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 });
      }
   }
}