Smokeping 发送告警邮件

smokeping 默认用sendmail发邮件,无法满足要求,改了一下源码,这样可以使用自定义方式发送邮件(如用QQ的smtp server来发告警邮件)。

首先需要安装 Authen::SASL 模块(auth 需要用的) 我用CPAN装的,不细说了

修改 smokeping/lib/Smokeping.pm

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#头上加
use Authen::SASL;
#定位到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 ) ],

然后修改配置文件
/etc/config

1
2
3
4
5
6
7
mailhost = smtp.qq.com
mailusr = noreply@qq.com
mailpwd = xxxxxxxxx

发表评论