nagios使用gmail发送邮件 取mysql数据库的字段并邮件通知

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:
sendEmail使用tls发送邮件
  开始使用nagios时对于perl不太了解,在搜索到sendEmail后,这款工具使用相同的简单
./sendEmail -f 发送邮件地址 -t 接收邮件地址 -s 发送邮件的SMTP服务地址 -u 标题  -xu 用户名 -xp 用户密码 发送内容;现在开始学习使用perl相关知识了,了解相关perl模块安装后开始着手使用gmail免费邮发送邮件,因为gmail免费邮都是使用ssl加密的,发送时需带-o tls=yes 选项都知道是是使用ssl了就选择yes,其它免费邮如tom,126的好像不行
./sendEmail -f mlnagios@gmail.com -t qwujhq@126.com -u "this is title" -m "this is my first to mail you "  -s smtp.gmail.com -xu mlnagios -xp 34 -o tls=yes
如果在此系统你未安装过perl模块,会报
Jul 17 01:25:35 localhost sendEmail[20965]: ERROR => No TLS support!  SendEmail can't load required libraries. (try installing Net::SSLeay and IO::Socket::SSL)
这个错误,下面来动手安装下perl相关模块吧,我这里讲是利用cpan下载相关模块然后把模块放到相关perl路径下安装,当然你也可以在第一次使用cpan时设定cpan的路径,我这人比较懒,一路回车过去了,因为我这里只是想要这两个包传到别的地方去安装。
[root@localhost Net-SSLeay-1.35]# perl -MCPAN -e shell
使用这个命令进入cpan
进入后使用h查看帮助
cpan> h
 
Display Information
 command  argument          description
 a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
 i        WORD or /REGEXP/  about anything of above
 r        NONE              reinstall recommendations
 ls       AUTHOR            about files in the author's directory
 
Download, Test, Make, Install...
 get                        download
 make                       make (implies get)
 test      MODULES,         make test (implies make)
 install   DISTS, BUNDLES   make install (implies test)
 clean                      make clean
 look                       open subshell in these dists' directories
 readme                     display these dists' README files
 
Other
 h,?           display this menu       ! perl-code   eval a perl command
 o conf [opt]  set and query options   q             quit the cpan shell
 reload cpan   load CPAN.pm again      reload index  load newer indices
 autobundle    Snapshot                force cmd     unconditionally do cmd
 
使用get下载相关模块
cpan> get  Net::SSLeay
cpan> get IO::Socket::SSL
下载完成后会提示你下载在哪个目录下
我这个是下好了再次使用下载命令时会提示你已经在下载目录下有了
cpan> get IO::Socket::SSL
CPAN: Storable loaded ok
Going to read /root/.cpan/Metadata
  Database was generated on Tue, 28 Jul 2009 00:27:00 GMT
Running get for module IO::Socket::SSL
CPAN: Digest::MD5 loaded ok
Checksum for /root/.cpan/sources/authors/id/S/SU/SULLR/IO-Socket-SSL-1.27.tar.gz ok
进入相关目录查看
[root@localhost sendEmail-v1.55]# cd /root/.cpan/sources/authors/id/S/SU/SULLR/
[root@localhost SULLR]# ls
CHECKSUMS  IO-Socket-SSL-1.27.tar.gz
好了,两包都下好了,把相关传你想在哪台机器上实现发mail功能
找到perl安装模块的路径
我这里是/usr/lib/perl5/5.8.5/
这些模块的安装就比较简单了,使用more查看安装帮助
[root@localhost Net-SSLeay-1.35]# more README
这是Net::SSLeay的后面perldoc应该可以明白吧
         ./Makefile.PL -t     # builds and tests it
         make install         # You probably have to su to root to do this
         perldoc Net::SSLeay  # optional, but highly recommended
         perldoc Net::SSLeay::Handle
 
这个安装IO::Socket::SSL
 
         perl Makefile.PL
         make
         make test
         make install
 
[root@localhost Net-SSLeay-1.35]# ./Makefile.PL -t
Cannot determine perl version info from lib/Net/SSLeay.pm
Cannot determine license info from lib/Net/SSLeay.pm
*** Found OpenSSL-0.9.7a installed in /usr
*** Be sure to use the same compiler and options to compile your OpenSSL, perl,
    and Net::SSLeay. Mixing and matching compilers is not supported.
Do you want to run external tests?
These tests *will* *fail* if you do not have network connectivity. [n] y
Writing Makefile for Net::SSLeay
 [root@localhost Net-SSLeay-1.35]# make install
进入放置sendEmail程序的目录
[root@localhost sendEmail-v1.55]# pwd
/root/sendEmail-v1.55
[root@localhost sendEmail-v1.55]# ./sendEmail -f mlnagios@gmail.com -t qwujhq@126.com -u "this is title" -m "this is my first to mail you "  -s smtp.gmail.com -xu mlnagios -xp 1234 -o tls=yes
Jul 21 13:17:04 localhost sendEmail[10764]: Email was sent successfully!
好了邮件发送成功
# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line     /usr/local/bin/sendEmail/sendEmail -f mlnagios@gmail.com -t CONTACTEMAIL  -s smtp.gmail.com -xu mlnagios -xp 34 -o tls=yes -u "****** Nagios ******" -m " ** NOTIFICATIONTYPE Service Alert: HOSTNAME/HOSTADDRESS  is HOSTSTATE at \n\nDate/Time: LONGDATETIME Additional Info:HOSTOUTPUT"
        }
 
# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line     /usr/local/bin/sendEmail/sendEmail -f mlnagios@gmail.com -t CONTACTEMAIL  -s smtp.gmail.com -xu mlnagios -xp 34 -o tls=yes  -u " ****** Nagios ****** " -m " ** NOTIFICATIONTYPE Service Alert: HOSTNAME/HOSTADDRESS SERVICEDESC is SERVICESTATE\n\nDate/Time: LONGDATETIME Additional Info:SERVICEOUTPUT CONTACTPAGER"
        }
 
数据库相关字段通知脚本,如下取得数据库相关脚本,然后发邮件通知相关联系人
#!/bin/bash
#get the database field
field=`mysql -uroot --password=xxxxxx <<QUERY_SQL
         select last from qeedoo.tbl_sch_account where email="test1";
         QUIT
         QUERY_SQL`
#define CONTACTEMAIL
for mail in "jairus@xxx.com qwujhq@xx.com heaventxxx@xxx.com"
do
/usr/local/bin/sendEmail/sendEmail -f mlnagios@gmail.com -t $mail  -s smtp.gmail.com -xu mlnagios -xp xxx -o tls=yes  -u " ****** THE NOTICE FOR MYSQL  ****** " -m "$field"
Done
最后自己定义下crontab 定时查询一下,如果需要更智能些自己调整,呵呵,sendEmail可以从这里下载http://caspian.dotconf.net/menu/Software/SendEmail/


本文转自 qwjhq 51CTO博客,原文链接:http://blog.51cto.com/bingdian/184372
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
344
分享
相关文章
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
获取数据库中字段的数据作为下拉框选项
获取数据库中字段的数据作为下拉框选项
81 5
MySQL的group by与count(), *字段使用问题
正确使用 `GROUP BY`和 `COUNT()`函数是进行数据聚合查询的基础。通过理解它们的用法和常见问题,可以有效避免查询错误和性能问题。无论是在单列分组、多列分组还是结合其他聚合函数的场景中,掌握这些技巧和注意事项都能大大提升数据查询和分析的效率。
488 0
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型 图像处理 光通信 分布式计算 算法语言 信息技术 计算机应用
104 8
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型
SpringBoot项目中mysql字段映射使用JSONObject和JSONArray类型
102 0
Discuz! X 数据库字典详解:DZ各数据表作用及字段含义
我们使用DISCUZ做网站时,有时需要对数据表进行操作,在操作数据表之前,需要对数据表进行了解。下面是DISCUZ 数据库各数据表作用及字段含义详解,方便新手更好的了解DISCUZ数据库。
119 4
MySQL新增字段/索引会不会锁表?
MySQL新增字段/索引会不会锁表?
552 0
MySQL 查询某个字段含有字母数字的值
MySQL 查询某个字段含有字母数字的值
141 0
MySQL 字符字段长度设置详解:语法、注意事项和示例
MySQL 字符字段长度设置详解:语法、注意事项和示例
542 0
MySQL技术指南:如何更改数据字段的前几位数字
MySQL技术指南:如何更改数据字段的前几位数字
119 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等