我一直在开发一个到icinga2的chatbot接口,但是没有找到一个持久的方法来在icinga2服务器重新启动/重新加载后幸存下来。在使用请求会话等移动try/except块一周之后,是时候联系社区了。在
以下是请求函数的当前迭代:
def i2api_request(url, headers={}, data={}, stream=False, *, auth=api_auth, ca=api_ca):
''' Do not call this function directly; it's a helper for the i2* command functions '''
# Adapted from http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/icinga2-api
# Section 11.10.3.1
try:
r = requests.post(url,
headers=headers,
auth=auth,
data=json.dumps(data),
verify=ca,
stream=stream
)
except (requests.exceptions.ChunkedEncodingError,requests.packages.urllib3.exceptions.ProtocolError, http.client.IncompleteRead,ValueError) as drop:
return("No connection to Icinga API")
if r.status_code == 200:
for line in r.iter_lines():
try:
if stream == True:
yield(json.loads(line.decode('utf-8')))
else:
return(json.loads(line.decode('utf-8')))
except:
debug("Could not produce JSON from "+line)
continue
else:
#r.raise_for_status()
debug('Received a bad response from Icinga API: '+str(r.status_code))
print('Icinga2 API connection lost.')
(调试函数只是标记并将指示的错误打印到控制台。)
这段代码可以很好地处理来自API的事件并将它们发送到chatbot,但是如果重新加载icinga服务器(在/etc/icinga2中添加新的服务器定义之后需要这样做),侦听器就会崩溃。在
以下是重新启动服务器时得到的错误响应:
^{pr2}$对于Icinga2.4,每次重新启动服务器时都会发生这种崩溃。我原以为升级到2.5后问题就消失了,但现在看来它变成了海森堡。在
最后我得到了IRC的建议,重新排序try/except块,并确保它们位于正确的位置。这是工作结果。在
相关问题 更多 >
编程相关推荐