当 etcd 集群关闭或不健康时从 Python 发送电子邮件
Send email from Python when etcd cluster is down or unhealthy
我正在处理 etcd 集群监控,如果集群出现故障,我必须在其中发送电子邮件。当集群健康并且我在我的代码中使用 sendEmail() 函数时,它工作正常,但是当集群是 down/unhealthy 或者我已经终止了进程时,它说:
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.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()
可能的解决方案是什么?
您可以捕获 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()
我正在处理 etcd 集群监控,如果集群出现故障,我必须在其中发送电子邮件。当集群健康并且我在我的代码中使用 sendEmail() 函数时,它工作正常,但是当集群是 down/unhealthy 或者我已经终止了进程时,它说:
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.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()
可能的解决方案是什么?
您可以捕获 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()