Email Threshold Alert
This is a basic example which shows:
- How to access parameters passed through from Threshold Alerting.
- How to retrieve the IP address of a device from ADB (AKIPS database).
- How to apply formatting to interface thresholds.
- 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 Threshold Alerting.
sub alert_mail_threshold
{
my ($arg_ref) = @_;
# Modify the following parameters
my $mail_to = ''; # e.g. [email protected]
my $profile = ''; # e.g. NetEng
# Get the IP address of the device which triggered the alert
my $ipaddr = adb_result (sprintf ("get %s sys SNMP.ipaddr", $arg_ref->{device}));
my $time_str = strftime ("%H:%M", localtime ($arg_ref->{tt}));
my $child = "";
my $thr = $arg_ref->{thr};
my $val = $arg_ref->{val};
my $subject;
my @body;
my $msg;
# Parse the MIB and object which triggered the alert
my ($mib, $obj) = split (/\./m, $arg_ref->{attr}, 2);
# Apply formatting to interface thresholds
if ($mib eq "IF-MIB") {
if (defined $obj && $obj =~ /if.*BitRate/m) {
$thr = int_ifspeed ($thr);
$val = int_ifspeed ($val);
}
elsif (defined $obj && $obj =~ /if.*Util/m) {
$thr = int_util ($thr);
$val = int_util ($val);
}
else {
$thr = int_metric ($thr);
$val = int_metric ($val);
}
trim $val;
$child = $arg_ref->{child}; # interface name
}
# Set the email subject
$subject = sprintf ("%s: %s %s %s %s %s %s %s %s",
$arg_ref->{alias} || "",
$arg_ref->{device},
$child,
$arg_ref->{descr},
$arg_ref->{lastn},
$arg_ref->{stat}, # avg or total
$val, # calculated value
$arg_ref->{state}, # above or below
$thr); # threshold value
# Set the email body
$msg = sprintf ("%s %s %s %s %s %s %s %s %s %s %s",
$time_str,
$arg_ref->{alias} || "",
$arg_ref->{device},
$ipaddr,
$child,
$arg_ref->{descr},
$arg_ref->{lastn},
$arg_ref->{stat}, # avg or total
$val, # calculated value
$arg_ref->{state}, # above or below
$thr); # threshold value
trim $subject;
trim $msg;
push @body, $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 });
}
}
}