From 8edaa7fee41f9d5597a656123a481f2483906053 Mon Sep 17 00:00:00 2001 From: Tobi Oetiker Date: Wed, 30 Aug 2006 23:12:31 +0000 Subject: added alert mailtemplates and new probe config ProbeUnit --- lib/Smokeping.pm | 138 +++++++++++++++++++++++++++--------- lib/Smokeping/probes/FTPtransfer.pm | 12 ++-- lib/Smokeping/probes/base.pm | 4 ++ 3 files changed, 116 insertions(+), 38 deletions(-) (limited to 'lib') diff --git a/lib/Smokeping.pm b/lib/Smokeping.pm index f0fb982..417f10e 100644 --- a/lib/Smokeping.pm +++ b/lib/Smokeping.pm @@ -548,16 +548,18 @@ sub target_menu($$$;$){ }; - -sub fill_template ($$){ +sub fill_template ($$;$){ my $template = shift; my $subst = shift; - my $line = $/; - undef $/; - open I, $template or return "ERROR: Reading page template $template: $!"; - my $data = ; - close I; - $/ = $line; + my $data = shift; + if ($template){ + my $line = $/; + undef $/; + open I, $template or return undef; + $data = ; + close I; + $/ = $line; + } foreach my $tag (keys %{$subst}) { $data =~ s/<##${tag}##>/$subst->{$tag}/g; } @@ -603,6 +605,7 @@ sub get_overview ($$$$){ my $max = $cfg->{Presentation}{overview}{max_rtt} || "100000"; my $medc = $cfg->{Presentation}{overview}{median_color} || "ff0000"; my $probe = $probes->{$tree->{$prop}{probe}}; + my $ProbeUnit = $probe->ProbeUnit(); my $pings = $probe->_pings($tree->{$prop}); my ($graphret,$xs,$ys) = RRDs::graph ($cfg->{General}{imgcache}.$dir."/${prop}_mini.png", @@ -611,7 +614,7 @@ sub get_overview ($$$$){ '--title',$tree->{$prop}{title}, '--height',$cfg->{Presentation}{overview}{height}, '--width',$cfg->{Presentation}{overview}{width}, - '--vertical-label',"Seconds", + '--vertical-label', $ProbeUnit, '--imgformat','PNG', '--alt-autoscale-max', '--alt-y-grid', @@ -734,6 +737,7 @@ sub get_detail ($$$$){ my $probe = $cfg->{__probes}{$tree->{probe}}; my $ProbeDesc = $probe->ProbeDesc(); + my $ProbeUnit = $probe->ProbeUnit(); my $step = $probe->step(); my $pings = $probe->_pings($tree); my $page; @@ -966,7 +970,7 @@ sub get_detail ($$$$){ '--rigid','--upper-limit', $max->{$start}, @log, '--lower-limit',(@log ? ($max->{$start} > 0.01) ? '0.001' : '0.0001' : '0'), - '--vertical-label',"Seconds", + '--vertical-label',$ProbeUnit, '--imgformat','PNG', '--color', 'SHADEA#ffffff', '--color', 'SHADEB#ffffff', @@ -1022,7 +1026,7 @@ sub get_detail ($$$$){ $page .= "
"; $page .= ( $ERROR || qq{' - . qq{}."" ); + . qq{}."" ); #" $page .= ""; } @@ -1047,7 +1051,7 @@ sub display_webpage($$){ my $readversion = "?"; $VERSION =~ /(\d+)\.(\d{3})(\d{3})/ and $readversion = sprintf("%d.%d.%d",$1,$2,$3); - print fill_template + my $page = fill_template ($cfg->{Presentation}{template}, { menu => target_menu($cfg->{Targets}, @@ -1070,6 +1074,8 @@ sub display_webpage($$){ smokelogo => '', } ); + print $page || "ERROR: Reading page template".$cfg->{Presentation}{template}.""; + } # fetch all data. @@ -1218,33 +1224,47 @@ SNPPALERT } }; if (@to){ - my $rfc2822stamp = strftime("%a, %e %b %Y %H:%M:%S %z", @stamp); - my $to = join ",",@to; - sendmail $cfg->{Alerts}{from},$to, <{Alerts}{from} -Date: $rfc2822stamp -Subject: [SmokeAlert] $_ $what on $line + my $default_mail = < <##WHAT##> on <##LINE##> -$stamp +<##STAMP##> -Alert "$_" $what for $urlline +Alert "<##ALERT##>" <##WHAT##> for <##URL##> Pattern ------- -$cfg->{Alerts}{$_}{pattern} +<##PAT##> Data (old --> now) ------------------ -$loss -$rtt +<##LOSS##> +<##RTT##> Comment ------- -$cfg->{Alerts}{$_}{comment} - - - +<##COMMENT##> + +DOC + + my $mail = fill_template($cfg->{Alerts}{$_}{mailtemplate}, + { + ALERT => $_, + WHAT => $what, + LINE => $line, + URL => $urlline, + STAMP => $stamp, + PAT => $cfg->{Alerts}{$_}{pattern}, + LOSS => $loss, + RTT => $rtt, + COMMENT => $cfg->{Alerts}{$_}{comment} + },$default_mail) || "Subject: smokeping failed to open mailtemplate '$cfg->{Alerts}{$_}{mailtemplate}'\n\nsee subject\n"; + my $rfc2822stamp = strftime("%a, %e %b %Y %H:%M:%S %z", @stamp); + my $to = join ",",@to; + sendmail $cfg->{Alerts}{from},$to, <{Alerts}{from} +Date: $rfc2822stamp +$mail ALERT } } else { @@ -1721,7 +1741,7 @@ DOC Instead of using sendmail, you can specify the name of an smtp server and use perl's Net::SMTP module to send mail (for alerts and DYNAMIC client script). Several comma separated mailhosts can be specified. SmokePing will -try one after the other is one does not anwer answer for 5 seconds. +try one after the other if one does not answer for 5 seconds. DOC _sub => sub { require Net::SMTP ||return "ERROR: loading Net::SMTP"; return undef; } }, @@ -2367,7 +2387,7 @@ A complete example DOC _sections => [ '/[^\s,]+/' ], - _vars => [ qw(to from edgetrigger) ], + _vars => [ qw(to from edgetrigger mailtemplate) ], _mandatory => [ qw(to from)], to => { _doc => <"this must either be 'yes' or 'no'", _default => 'no', }, + mailtemplate => { + _doc => <##>IB<##E> type +strings will get replaced in the template before it is sent out. the +following keywords are supported: + + <##ALERT##> - target name + <##WHAT##> - status (is active, was raised, was celared) + <##LINE##> - path in the config tree + <##URL##> - webpage for graph + <##STAMP##> - date and time + <##PAT##> - pattern that matched the alert + <##LOSS##> - loss history + <##RTT##> - rtt history + <##COMMENT##> - comment + + +DOC + + _sub => sub { + open (my $tmpl, $_[0]) or + return "mailtemplate '$_[0]' not readable"; + my $subj; + while (<$tmpl>){ + $subj =1 if /^Subject: /; + next if /^\S+: /; + last if /^$/; + return "mailtemplate '$_[0]' should start with mail header lines"; + } + return "mailtemplate '$_[0]' has no Subject: line" unless $subj; + return undef; + }, + }, '/[^\s,]+/' => { - _vars => [ qw(type pattern comment to edgetrigger) ], - _inherited => [ qw(edgetrigger) ], + _vars => [ qw(type pattern comment to edgetrigger mailtemplate) ], + _inherited => [ qw(edgetrigger mailtemplate) ], _mandatory => [ qw(type pattern comment) ], to => { _doc => 'Similar to the "to" parameter on the top-level except that it will only be used IN ADDITION to the value of the toplevel parameter. Same rules apply.', _re => '(\|.+|.+@\S+|snpp:)', @@ -2433,7 +2488,22 @@ DOC _re_error =>"this must either be 'yes' or 'no'", _default => 'no', }, - }, + mailtemplate => { + _sub => sub { + open (my $tmpl, $_[0]) or + return "mailtemplate '$_[0]' not readable"; + my $subj; + while (<$tmpl>){ + $subj =1 if /^Subject: /; + next if /^\S+: /; + last if /^$/; + return "mailtemplate '$_[0]' should start with mail header lines"; + } + return "mailtemplate '$_[0]' has no Subject: line" unless $subj; + return undef; + }, + }, + }, }, Targets => {_doc => < '', }); - print PAGEFILE $page; + print PAGEFILE $page || "ERROR: Reading page template ".$cfg->{Presentation}{template}.""; close PAGEFILE; foreach my $key (keys %$tree) { diff --git a/lib/Smokeping/probes/FTPtransfer.pm b/lib/Smokeping/probes/FTPtransfer.pm index 707129c..c4c39e8 100644 --- a/lib/Smokeping/probes/FTPtransfer.pm +++ b/lib/Smokeping/probes/FTPtransfer.pm @@ -53,7 +53,7 @@ Many variables can be specified either in the probe or in the target definition, the target-specific variable will override the prove-specific variable. DOC authors => <<'DOC', -Tobias Oetiker +Tobias Oetiker sponsored by Virtela DOC bugs => <quit; $elapsed = ( $end - $start ); $ok or next; - my $speed = $elapsed / $size; + my $speed = $size / $elapsed; $self->do_debug("$host: ftp $mode $speed Bytes/s"); push @rates, $speed; } diff --git a/lib/Smokeping/probes/base.pm b/lib/Smokeping/probes/base.pm index 4a83b50..e33416f 100644 --- a/lib/Smokeping/probes/base.pm +++ b/lib/Smokeping/probes/base.pm @@ -90,6 +90,10 @@ sub ProbeDesc ($) { return "Probe which does not overrivd the ProbeDesc methode"; } +sub ProbeUnit ($) { + return "Seconds"; +} + sub target2dynfile ($$) { # the targets are stored in the $self->{targets} # hash as filenames pointing to the RRD files -- cgit v1.2.3-24-g4f1b