|
一、为什么要自定义邮件脚本报警?
灵活,方便。可以自定义过滤信息。
下面是使用不同方式的邮件报警,一个是利用sendEmail程序来发送报警邮件,第二个是利用python脚本来发送邮件。
二、sendEmail的部署步骤
1.官方介绍:http://caspian.dotconf.net/menu/Software/SendEmail/
2.先下载安装包到本地,解压。
[root@zabbix-6 ~]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@zabbix-6 ~]# tar zxf sendEmail-v1.56.tar.gz
[root@zabbix-6 ~]# cd sendEmail-v1.56
[root@zabbix-6 sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/
[root@zabbix-6 sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail
[root@zabbix-6 sendEmail-v1.56]# file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable
3.看下sendemail命令的帮助信息
[root@zabbix-6 sendEmail-v1.56]# /usr/local/bin/sendEmail
sendEmail-1.56 by Brandon Zehm
Synopsis: sendEmail -f ADDRESS [options]
Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE
Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail> Optional:
-a FILE [FILE ...] file attachment(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication
Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls= -o fqdn=FQDN
Help:
--help the helpful overview you're reading now
--help addressing explain addressing and>
--help message explain message body input and> --help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more
4.安装下依赖
[root@zabbix-6 sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
5.简单的报警脚本mail
新建[root@zabbix]#mkdir /usr/lib/zabbix/alertscripts/
[root@zabbix-6 sendEmail-v1.56]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix-6 alertscripts]# cat mail.sh#!
#!/bin/bash
SMTP_server='smtp.163.com' # SMTP服务器
username='zabbix@163.com' # 用户名
password='zabbix' # 密码(163客户端授权密码)
from_email_address='zabbix@163.com' # 发件人Email地址
to_email_address="$1" # 收件人Email地址,zabbix传入的第一个参数
message_subject_utf8="$2" # 邮件标题,zabbix传入的第二个参数
message_body_utf8="$3" # 邮件内容,zabbix传入的第三个参数
# 转换邮件标题为GB2312,解决邮件标题含有中文,收到邮件显示乱码的问题。
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 |
|
|