By default, smokeping sends an email alert using sendmail. You can also directly call an external program to send an alert. The alert settings of smokeping are a bit complex, but easy to use. The settings are flexible and well-considered.
1. Configure email support
By default, sendmail is not sent through email servers such as 163 and qq, but we can support it by slightly modifying the configuration. First install the Authen: SASL module (required for auth), and then modify smokeping/lib/Smokeping. pm.
As follows:
# Add above
Use Authen: SASL;
# Locate the sendmail function. The default version of the sendmail function is defined as follows.
Sub sendmail ($ ){
My $ from = shift;
My $ to = shift;
$ To = $1 if $ to = ~ /<(. *?)> /;
My $ body = shift;
If ($ cfg-> {General} {mailhost} and
My $ smtp = Net: SMTP-> new ([split/\ s *, \ s */, $ cfg-> {General} {mailhost}], timeout => 5 )){
$ Smtp-> auth (split (/\ s *, \ s */, $ cfg-> {General} {mailusr}), split (/\ s *, \ s */, $ cfg-> {General} {mailpwd }));
$ Smtp-> mail ($ from );
$ Smtp-> to (split (/\ s *, \ s */, $ ));
$ Smtp-> data ();
$ Smtp-> datasend ($ body );
$ Smtp-> dataend ();
$ Smtp-> quit;
} Elsif ($ cfg-> {General} {sendmail} or-x "/usr/lib/sendmail "){
Open (M, "|-") | exec ($ cfg-> {General} {sendmail} | "/usr/lib/sendmail "), "-f", $ from, $ );
Print M $ body;
Close M;
} Else {
Warn "ERROR: not sending mail to $ to, as all methodes failed \ n ";
}
}
# Locate '_ vars =>' and add mailusr mailpwd. Otherwise, it cannot be started! General configuration values valid for the whole SmokePing setup.
DOC
_ Vars =>
[Qw (owner imgcache imgurl datadir dyndir pagedir piddir sendmail offset
Smokemail cgiurl mailhost mailusr mailpwd snpphost contact display_name
Syslogfacility syslogpriority concurrentprobes changeprocessnames tmail
Changecgiprogramname linkstyle precreateperms)],
After the configuration is complete, modify the configuration file/opt/smokeping/etc/config to add the mail server configuration:
Mailhost = smtp.361way.com
Mailusr = monitor@361way.com
Mailpwd = xxxxxxxxx
II. Alerts configuration
1. Customize alerts alarm policies
Several alarm policies are defined as follows: bigloss, someloss, etc.
* ** Alerts ***
To = admin@361way.com
From = monitor@361way.com
+ Bigloss
Type = loss
# In percent
Pattern = 0%, = 0%, = 0%, = 0%,> 0%,> 0%,> 0%
Comment = suddenly there is packet loss
+ Someloss
Type = loss
# In percent
Pattern => 0%, * 12 *,> 0%, * 12 *,> 0%
Comment = loss 3 times in a row
+ Startloss
Type = loss
# In percent
Pattern = S,> 0%,> 0%,> 0%
Comment = loss at startup
+ Rttdetect
Type = rtt
# In milli seconds
Pattern = <10, <10, <10, <10, <10, <100,> 100,> 100
Comment = routing messed up again?
+ Hostdown
Type = loss
# In percent
Pattern = 0%, = 0%, = 0%, = U
Comment = no reply
+ Lossdetect
Type = loss
# In percent
Pattern = 0%, = 0%, = 0%, = 0%,> 20%,> 20%,> 20%
Comment = suddenly there is packet loss
The three types of alarms are described as follows:
Someloss: if three packet loss occurs during 12 checks (no matter how many packets are lost), perform alert;
Rttbad:
If two consecutive latencies of more than 50 milliseconds occur, perform alert;
Rrtdetect:
The latency of the previous five checks is less than 10 milliseconds, and the latency of the first six checks is less than 100 milliseconds. If the latency of the first three consecutive checks is greater than 7th milliseconds, alert is performed.
2. Policy application
Add the corresponding policy configuration in target, as shown below:
++ 361way
Menu = 361way_host
Title = 361way.com
Host = www.361way.com
Alerts = someloss, hostdown
Once an alarm occurs on the host, it is sent to the email address we configured in advance, similar to the following figure:
Smokeping-alert
The email alert content in the preceding figure can also be output and modified in a custom format. Modify the Smokeping. pm file as follows:
My $ default_mail = <DOC;
Subject: [SmokeAlert] <# ALERT ###>< # WHAT ##> on <# LINE ##>
<## STAMP ##>
Alert "<# ALERT ##>" <# WHAT ##> for <# URL ##>
Pattern
-------
<## PAT ##>
Data (old --> now)
------------------
<## LOSS ##>
<## RTT ##>
Comment
-------
<## COMMENT ##>
DOC
3. pattern matching
Pattern matching is the most important part in the preparation of alert rules. The official explanation is provided for this part. However, I found that the summary of a Hong Kong compatriot is better.
Smokeping-pattern
III. Special calls
1. External program call
An example of calling an external alarm program, such as IM and SMS is as follows:
* ** Alerts ***
To = |/usr/local/smokeping/bin/alert. sh
From = joe @ somehost
"To" option. By default, you need to enter an email address, but you only need to add "|" after "=", followed by the path of your custom script, you can call your own script to perform alert. The script reads five or six parameters: name-of-alert, target, loss-pattern, rtt-pattern, hostname, [raise]. Select the alert parameter.
Here, we can find an example on a foreigner site:
Config configuration
To = |/etc/smokeping/config. d/trace_alert.sh 2>/tmp/trace. log
Script content:
######################################## ################
# Script to email a mtr report on alert from Smokeping #
######################################## ################
Alertname = $1
Target = $2
Losspattern = $3
Rtt = $4
Hostname = $5
Email = "monitoring@email.com"
Smokename = "BR-NYC -"
If ["$ losspattern" = "loss: 0%"];
Then
Subject = "Clear-$ {smokename}-Alert: $ target host :$ {hostname }"
Else
Subject = "$ {smokename} Alert: $ {target}-$ {hostname }"
Fi
Echo "MTR Report for hostname :$ {hostname}">/tmp/mtr.txt
Echo "">/tmp/mtr.txt
Echo "sudo mtr-n-report $ {hostname }"
Sudo/usr/sbin/mtr-n-report $ {hostname}>/tmp/mtr.txt
Echo "">/tmp/mtr.txt
Echo "Name of Alert:" $ alertname>/tmp/mtr.txt
Echo "Target:" $ target>/tmp/mtr.txt
Echo "Loss Pattern:" $ losspattern>/tmp/mtr.txt
Echo "RTT Pattern:" $ rtt>/tmp/mtr.txt
Echo "Hostname:" $ hostname>/tmp/mtr.txt
Echo "">/tmp/mtr.txt
Echo "Full mtr command is: sudo/usr/sbin/mtr-n-report $ {hostname}">/tmp/mtr.txt
Echo "subject:" $ subject
If [-s/tmp/mtr.txt] then
Mailx-s "$ {subject}" $ email
Fi
2. A specific host is sent to a specific user
To indicates the email address that accepts all alerts. If you want to send alerts to a specific email address at a specific node
Then add alertee = testmonitor@139.com on the node. For details, see The/opt/smokeping/lib/Smokeping. pm source code, as shown in the following section:
Foreach my $ addr (map {$ _? (Split/\ s *, \ s */, $ _): ()} $ cfg-> {Alerts} {to}, $ tree-> {alertee }, $ alert-> {}){