Postgresql 使用 Pl/python实现邮件监控

目的:本项功能的目的是为了将数据库中某项内容监控的结果邮件出来提示给相关人员。

     实现方式:将数据库监控结果存储到监控记录表当中,使用PL/python函数将记录表中的数据传入附件并将附件使用MUTT邮件发送给相关人员进行通知。

   

      环境准备:

Linux:  Release:  5.8  版本

Postgreqsl9.5.1 版本

Python版本:2.7.12

Mutt 版本号:1.4.2.2i

 

 

  一、安装postgresql

安装过程(略)

下载地址:https://www.postgresql.org/download/

注意:在编译./configure的时候记得加上 --with-python 这样在拓展python的时候 直接拓展就OK

 

 

二、安装python 2.7.12

在官网上下载了python2.7.12

地址:https://www.python.org/getit/

将安装包上传到服务器中并解压 解压后会生成目录Python-2.7.12

进入目录编译安装

#./comfigure --prefix=/opt/python2.7.12 --enable-shared   #注意这个参数一定要带

#make

#make install

---完成。

 

三、安装MUTT邮件发送工具

这里解释下为什么选择MUTT而不是linuxmail或者xmail---因为在刚开始使用xmailfoxmail发送附件的时候 foxmail中接收不到附件 并且正文内容乱码,所以选用了更为简便的mutt 来进行发送邮件。

下载地址:http://www.mutt.org/download.html

安装mutt

 

#yum -y install mutt

 

四、postgresql端用创建pl/python拓展

 

#su - postgres

#psql

#create extension plpythonu;

 

五、编写调用操作系统mutt邮件命令的函数

 5.1首先是将监控记录表中信息通过copy命令传入到文件中 send_mail_info 表为监控记录表,自行创建即可。

CREATE OR REPLACE FUNCTION public.fc_copy_date(v text)

  RETURNS text AS

$BODY$

    try:

        plpy.execute("copy send_mail_info to '/app/pgdata/pg951date/view_test.xls'")

        plpy.execute("select fc_file_icon('%s')" % v)

    except plpy.SPIError:

        return "something went wrong"

    else:

        return "ok"

$BODY$

  LANGUAGE plpythonu VOLATILE

  COST 100;

ALTER FUNCTION public.fc_copy_date(text)

  OWNER TO postgres;

5.2其中fc_file_icon函数是用来转换文件编码的,因为从数据库中copy出来的文件是utf-8的邮件出来后是乱码,所以转换成了gb2312的格式。

CREATE OR REPLACE FUNCTION public.fc_file_icon(v text)

  RETURNS text AS

$BODY$

import commands

strs = args[0]  #接收函数传入命令

cmds ='iconv -f utf-8 -t gb2312 view_test.xls > %s' % v

(status, output) = commands.getstatusoutput(cmds)

return output

$BODY$

  LANGUAGE plpythonu VOLATILE

  COST 100;

ALTER FUNCTION public.fc_file_icon(text)

  OWNER TO postgres;

 

   5.3这是由postgresql调用操作系统命令mutt的一个函数,

 

CREATE OR REPLACE FUNCTION public.fc_send_mail(text)

  RETURNS text AS

$BODY$

import commands

 

strs = args[0]  #接收函数传入命令

emadr ='xxxxx@xxx.com '

cmd1 = 'echo  "正文:xxxxx" | mutt -a  ' + strs  -----strs为传入的文件名

emailx = ' -s "主题:xxx" '+ emadr

cmds = cmd1 + emailx

(status, output) = commands.getstatusoutput(cmds)

return output

$BODY$

  LANGUAGE plpythonu VOLATILE

  COST 100;

ALTER FUNCTION public.fc_send_mail(text)

  OWNER TO postgres;

 

5.4最后一个外层的函数调用这两个函数

 

-- Function: public.fc_sendmail_main(text)

 

-- DROP FUNCTION public.fc_sendmail_main(text);

 

CREATE OR REPLACE FUNCTION public.fc_sendmail_main(v text)

  RETURNS text AS

$BODY$

    try:

        plpy.execute("select fc_copy_date('%s')" % v)

        plpy.execute("select fc_send_mail('%s')" % v)

    except plpy.SPIError:

        return "something went wrong"

    else:

        return "OK"

$BODY$

  LANGUAGE plpythonu VOLATILE

  COST 100;

ALTER FUNCTION public.fc_sendmail_main(text)

  OWNER TO postgres;

 

5.5当需要发邮件的时候只需要执行这条命令即可

Select    fc_sendmail_main(‘xxxx.sss’) ;   ----- xxxx.sss代表 文件名.文件类型   

 

 

六、如若需要定时发送的话 可以使用postgresql的定时任务工具:pgAgent  

Pgagent的安装及使用(略):可参考 我的上一篇文章:Pgagent的安装及使用

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值