如何监控PostgreSQL存储过程/函数代码运行?本文介绍用python+微信/邮件的方式进行报警、监控。
首先要有一张表、用于存放PostgreSQL存储过程/函数代码运行异常的信息。
处理原则:若出现异常;把“发生时间+所在的程序+原因”通过微信/邮件发给对应人员。当然发送一次即可;起到通知的效果。
一、媒介
通过什么方式进行发送内容;下面介绍微信/邮件两种方式
1、python发送微信
py_wechar.py的内容
企业微信号;大家可以到企业微信上配置
#!/usr/bin/python3
#coding=utf-8
import json
import time
import urllib.request as urllib2
options = {
'WeiXin': {
'corp_id': '*', #微信企业号ID
'agent_id': '*', #微信企业号应用ID
'agent_secret': '*', #微信企业号密钥
'to_user': '@all' #发送给谁
},
}
class WeiXinSendMsg:
def __init__(self, wx_conf):
self.corp_id = wx_conf.get('corp_id')
self.agent_secret = wx_conf.get('agent_secret')
self.agent_id = wx_conf.get('agent_id')
self.to_user = wx_conf.get('to_user')
self.token = self.get_token()
self.token_update_time = int(time.time())
def get_token(self):
get_token_url = 'https://qyapi.weixin.qq.com/c