当etcd群集关闭或不正常时从Python发送电子邮件
我正在进行etcd群集监控,如果群集关闭,我必须发送电子邮件。当集群运行正常且我在代码中使用sendmail()函数时,它工作正常,但当集群关闭/不正常或我已终止进程时,它会显示:当etcd群集关闭或不正常时从Python发送电子邮件,python,mime,smtplib,etcd,Python,Mime,Smtplib,Etcd,我正在进行etcd群集监控,如果群集关闭,我必须发送电子邮件。当集群运行正常且我在代码中使用sendmail()函数时,它工作正常,但当集群关闭/不正常或我已终止进程时,它会显示: requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=2379): Max retries exceeded with url: /health (Caused by NewConnectionError('<
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=2379): Max retries exceeded with url: /health (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1f6de50>: Failed to establish a new connection: [Errno 111] Connection refused',))
requests.exceptions.ConnectionError:HTTPConnectionPool(host='localhost',port=2379):url:/health超过了最大重试次数(由NewConnectionError引起(':未能建立新连接:[Errno 111]连接被拒绝',))
我试图使用状态代码并请求.exception,以使其到达我的代码,但无法这样做。下面是我的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import requests
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
def getClusterHealth():
response = requests.get('http://localhost:2379/health')
data = response.json()
if response.status_code == 111:
sendEmail()
elif data['health']=="true":
print("Cluster is healthy")
else:
print ("Cluster is not healthy")
sendEmail()
def sendEmail():
msg = MIMEText("etcd Cluster Down Sample Mail")
sender = "example@server.com"
recipients = ["example1@server.com,example2@servr.com"]
msg["Subject"] = "etcd Cluster Monitoring Test Multiple ID"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s = smtplib.SMTP('localhost')
s.sendmail(sender,recipients,msg.as_string())
s.quit()
#p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)
#p.communicate(msg.as_string())
if __name__ == "__main__":
if(len(sys.argv) < 2):
print("Usage : python etcdMonitoring.py [health|metrics|all]")
elif(sys.argv[1] == "health"):
getClusterHealth()
#/usr/bin/python
#-*-编码:utf-8-*-
导入json
导入请求
导入系统
导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从子流程导入Popen、PIPE
def getClusterHealth():
response=requests.get('http://localhost:2379/health')
data=response.json()
如果response.status_code==111:
发送电子邮件()
elif数据['health']=“true”:
打印(“群集正常”)
其他:
打印(“群集不正常”)
发送电子邮件()
def sendmail():
msg=MIMEText(“etcd群集关闭示例邮件”)
发送方=”example@server.com"
收件人=[”example1@server.com,example2@servr.com"]
msg[“Subject”]=“etcd群集监控测试多ID”
msg['From']=发件人
msg['To']=“,”。加入(收件人)
s=smtplib.SMTP('localhost')
s、 sendmail(发送者、接收者、msg.as_string())
s、 退出
#p=Popen([“/usr/sbin/sendmail”、“-t”、“-oi”],stdin=PIPE,universal\u newlines=True)
#p、 通信(msg.as_string())
如果名称=“\uuuuu main\uuuuuuuu”:
如果(len(sys.argv)<2):
打印(“用法:python etcdMonitoring.py[health | metrics | all]”)
elif(sys.argv[1]=“运行状况”):
getClusterHealth()
可能的解决方案是什么?您可以捕获ConnectionError异常,评估错误消息,并根据需要发送电子邮件:
def getClusterHealth():
try:
response = requests.get('http://localhost:2379/health')
except ConnectionError as e:
// You can use the value of e to check for specific error message and trigger the email
if str(e) == 'Max retries exceeded with url':
sendEmail()
data = response.json()
if response.status_code == 111:
sendEmail()
elif data['health']=="true":
print("Cluster is healthy")
else:
print ("Cluster is not healthy")
sendEmail()
看起来url超过了最大重试次数,这就是为什么出现此错误。是否有可能在最大重试次数后,它发送了一封邮件?捕获ConnectionError并在其上触发邮件。@min2bro你能给我一个例子吗?我使用了Exception requests.exceptions.ConnectionError作为e而不是Exception ConnectionError作为e。然后它工作得很好,不能。它说有记录但没有公开显示,说需要15个声誉。还是13岁。