目的:主要作用于nagios监控系统邮件报警功能,也可以稍作修改使用于其他地方;
使用方法:
1
2
|
perl notify_service_by_mail.pl -t connact -s subject -m message
perl notify_host_by_mail.pl -t connact -s subject -m message
|
服务报警代码:
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/usr/bin/env perl
#notify_service_by_mail.pl
use
strict;
use
warnings;
use
Mail::Sender;
my
(
$CONTACTEMAIL
,
$Subject
,
$Message
,
$Monitor
,
$NOTIFICATIONTYPE
,
$SERVICEDESC
,
$HOSTALIAS
,
$HOSTADDRESS
,
$SERVICESTATE
,
$LONGDATETIME
,
$SERVICEOUTPUT
);
my
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
) = (
localtime
)[0..5];
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
) = (
sprintf
(
"%02d"
,
$sec
),
sprintf
(
"%02d"
,
$min
),
sprintf
(
"%02d"
,
$hour
),
sprintf
(
"%02d"
,
$mday
),
sprintf
(
"%02d"
,
$mon
+ 1),
$year
+ 1900
);
my
$date
=
"$year-$mon-$mday $hour:$min:$sec"
;
#Sender
my
$send_user
=
'xxx@xx.com'
;
#passwd for Sender
my
$send_user_pw
=
'xxx'
;
#smtp address
my
$smtp_server
=
'smtp.xxx'
;
while
(
defined
(
my
$arg
=
shift
)){
if
(
$arg
eq
'-t'
){
$CONTACTEMAIL
=
shift
;
next
;
}
elsif
(
$arg
eq
'-s'
){
$Subject
=
shift
;
next
;
}
elsif
(
$arg
eq
'-m'
){
$Message
=
shift
;
}
else
{
next
;
}
if
(
defined
$Message
){
(
$Monitor
,
$NOTIFICATIONTYPE
,
$SERVICEDESC
,
$HOSTALIAS
,
$HOSTADDRESS
,
$SERVICESTATE
,
$LONGDATETIME
,
$SERVICEOUTPUT
)=
split
(/\|/,
$Message
);
print
"$Monitor\n$NOTIFICATIONTYPE\n$SERVICEDESC\n$HOSTALIAS\n$HOSTADDRESS\n$SERVICESTATE\n$LONGDATETIME\n$SERVICEOUTPUT"
;
sendTestMail();
}
}
sub
sendTestMail {
my
$sender
= new Mail::Sender{
ctype =>
'text/plain; charset=utf-8'
,
encoding =>
'utf-8'
,
};
#die "Error in mailing : $Mail::Sender::Error\n" unless ref $sender;
if
(
$sender
->MailMsg({
smtp =>
$smtp_server
,
from =>
$send_user
,
to =>
$CONTACTEMAIL
,
subject =>
$Subject
,
msg =>
"$Monitor\n$NOTIFICATIONTYPE\n$SERVICEDESC\n$HOSTALIAS\n$HOSTADDRESS\n$SERVICESTATE\n$LONGDATETIME\n$SERVICEOUTPUT"
,
auth =>
'LOGIN'
,
authid =>
$send_user
,
authpwd =>
$send_user_pw
,
charset
=>
'utf-8'
}) < 0) {
warn
"$Mail::Sender::Error\n"
;
return
1;
}
else
{
$sender
->Close();
#print "Test mail Send to $recv_user Mail sent OK.\n";
#print "$send_user to $CONTACTEMAIL smtp Success Subject : $Subject\n";
return
0;
}
}
sub
Usage {
print
"perl notify_service_by_mail.pl -t connact -s subject -m message"
;
exit
2;
}
|
主机报警代码:
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#!/usr/bin/env perl
#notify_host_by_mail.pl
use
strict;
use
warnings;
use
Mail::Sender;
my
(
$CONTACTEMAIL
,
$Subject
,
$Message
,
$Monitor
,
$NOTIFICATIONTYPE
,
$HOSTNAME
,
$HOSTALIAS
,
$HOSTADDRESS
,
$HOSTSTATE
,
$LONGDATETIME
,
$HOSTOUTPUT
);
my
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
) = (
localtime
)[0..5];
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
) = (
sprintf
(
"%02d"
,
$sec
),
sprintf
(
"%02d"
,
$min
),
sprintf
(
"%02d"
,
$hour
),
sprintf
(
"%02d"
,
$mday
),
sprintf
(
"%02d"
,
$mon
+ 1),
$year
+ 1900
);
my
$date
=
"$year-$mon-$mday $hour:$min:$sec"
;
#Sender
my
$send_user
=
'xxx@xx.com'
;
#passwd for Sender
my
$send_user_pw
=
'xxx'
;
#smtp address
my
$smtp_server
=
'smtp.xxx'
;
while
(
defined
(
my
$arg
=
shift
)){
if
(
$arg
eq
'-t'
){
$CONTACTEMAIL
=
shift
;
next
;
}
elsif
(
$arg
eq
'-s'
){
$Subject
=
shift
;
next
;
}
elsif
(
$arg
eq
'-m'
){
$Message
=
shift
;
}
else
{
next
;
}
if
(
defined
$Message
){
(
$Monitor
,
$NOTIFICATIONTYPE
,
$HOSTNAME
,
$HOSTALIAS
,
$HOSTADDRESS
,
$HOSTSTATE
,
$LONGDATETIME
,
$HOSTOUTPUT
)=
split
(/\|/,
$Message
);
print
"$Monitor\n$NOTIFICATIONTYPE\n$HOSTNAME\n$HOSTALIAS\n$HOSTADDRESS\n$HOSTSTATE\n$LONGDATETIME\n$HOSTOUTPUT "
;
sendTestMail();
}
}
sub
sendTestMail {
my
$sender
= new Mail::Sender{
ctype =>
'text/plain; charset=utf-8'
,
encoding =>
'utf-8'
,
};
#die "Error in mailing : $Mail::Sender::Error\n" unless ref $sender;
if
(
$sender
->MailMsg({
smtp =>
$smtp_server
,
from =>
$send_user
,
to =>
$CONTACTEMAIL
,
subject =>
$Subject
,
msg =>
"$Monitor\n$NOTIFICATIONTYPE\n$HOSTNAME\n$HOSTALIAS\n$HOSTADDRESS\n$HOSTSTATE\n$LONGDATETIME\n$HOSTOUTPUT"
,
auth =>
'LOGIN'
,
authid =>
$send_user
,
authpwd =>
$send_user_pw
,
charset
=>
'utf-8'
}) < 0) {
warn
"$Mail::Sender::Error\n"
;
return
1;
}
else
{
$sender
->Close();
#print "Test mail Send to $recv_user Mail sent OK.\n";
#print "$send_user to $CONTACTEMAIL smtp Success Subject : $Subject\n";
return
0;
}
}
sub
Usage {
print
"perl notify_service_by_mail.pl -t connact -s subject -m message"
;
exit
2;
}
|
centreon 页面通知命令侧配置:
服务
1
2
3
4
5
6
|
/usr/local/nagios/libexec/notify_service_by_mail
.pl -t $CONTACTEMAIL$ -s
"** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is
$SERVICESTATE$ **
" -m "
*****Monitor Notification*****|Notifycation Type:
$NOTIFICATIONTYPE$ |Service: $SERVICEDESC$ |Host: $HOSTALIAS$ |Address:
$HOSTADDRESS$ |State: $SERVICESTATE$ |Date
/Time
: $LONGDATETIME$
|Additional Info: $SERVICEOUTPUT$"
|
主机
1
2
3
4
5
6
|
/usr/local/nagios/libexec/notify_host_by_mail
.pl -t $CONTACTEMAIL$ -s
"** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
-m
"*****Monitor notification***** |Notification Type: $NOTIFICATIONTYPE$
|HOSTNAME: $HOSTNAME$ |HOSTALIAS: $HOSTALIAS$|Address: $HOSTADDRESS$
|State: $HOSTSTATE$ |
/Date/Time
: $LONGDATETIME$ |Additional Info:
$HOSTOUTPUT$ "
|
报警结果:
本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1665760