linux中smokeping警示配置教程詳解

來源:互聯網
上載者:User


smokeping 預設用sendmail 發郵件警示,也可以直接調用外部程式進行警示。smokeping的alert設定有點複雜,但是卻很好用,設定很靈活,考慮得很周全。

一、配置郵件支援

預設情況下sendmail 是不通過163、qq之類的郵件伺服器發送的,不過我們略微修改下配置即可以支援。首先需要安裝 Authen::SASL 模組(auth 需要用的),然後修改 smokeping/lib/Smokeping.pm 。

如下:

#頭上加
use Authen::SASL;
#定位到sendmail函數,預設新版本sendmail函數的定義的已經是下面這樣了,如果不是改成下面這樣
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*/, $to));
        $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,$to);
        print M $body;
        close M;
    } else {
        warn "ERROR: not sending mail to $to, as all methodes failed\n";
    }
}
#找到  '_vars =>' ,把 mailusr mailpwd  加進去。不然不能啟動哦!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 ) ],
配置完成後修改設定檔/opt/smokeping/etc/config ,增加郵件伺服器的配置:

mailhost = smtp.361way.com
mailusr = monitor@361way.com
mailpwd = xxxxxxxxx

二、Alerts部分配置

1、自訂alerts警示策略

如下定義了幾種警示策略,bigloss 、someloss等

*** 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,>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
以上幾種警示,這裡選取三種說明如下:

someloss: 如果在12次檢查中出現了3次丟包的情況(不論丟多少個包),就進行alert;
rttbad:
如果連續出現兩次50毫秒以上的延時,就進行alert;
rrtdetect:
之前5次檢查延時都少於10毫秒,前6次檢查延時都少於100毫秒,第7次開始連續3次檢查延時都大於100毫秒的話,就進行alert。

2、策略應用

在target裡面加上相應的策略配置即可,如下:

++ 361way
menu = 361way_host
title =361way.com
host = www.361way.com
alerts = someloss,hostdown
主機一旦出現警示,就會通過郵件發送到我們事先配置的郵箱中,類似下圖:

smokeping-alert

上圖中的郵件警示內容也可以進行自訂格式輸出和修改,這個還是修改Smokeping.pm檔案,修改其中如下部分即可:

                    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匹配

pattern匹配是編寫alert規則中很最要的一部分,官方對這部分有說明,不過我查到了一個香港同胞總結的要好一些,這裡摘錄下,不做翻譯了。

smokeping-pattern

三、特殊調用

1、外部程式調用

調用外部警示程式,如IM、簡訊等的樣本如下:

*** Alerts ***
to = |/usr/local/smokeping/bin/alert.sh
from = joe@somehost
"to" 選項,預設是要填入一個email地址的,但是只要在"="後面加上"|",後面再跟你自訂的指令碼的路徑,就可以調用自己的指令碼進行alert了。指令碼會讀入5或者6個參數:name-of-alert, target, loss-pattern, rtt-pattern, hostname,[raise]。自己選擇使用哪些參數alert即可。

這裡從老外網站上找到有一個樣本如下:

config 配置

to = |/etc/smokeping/config.d/trace_alert.sh 2> /tmp/trace.log
指令碼內容:

########################################################
# 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、特定主機發送到特定使用者

to 表示接受所有警示的郵箱,如果需要在特定的節點警示發送到特定的郵箱
則在該節點上增加alertee = testmonitor@139.com即可。具體可以參看/opt/smokeping/lib/Smokeping.pm源碼,如下部分:

foreach my $addr (map {$_ ? (split /\s*,\s*/,$_) : ()} $cfg->{Alerts}{to},$tree->{alertee},$alert->{to}){

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.