def connect_zabbix(self, payload):
"""
Method used to send information to Zabbix
:param payload: refers to the json message prepared to send to Zabbix
:rtype : returns the response received by the Zabbix API
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.zabbix_host, int(self.zabbix_port)))
s.send(payload)
# read its response, the first five bytes are the header again
response_header = s.recv(5, socket.MSG_WAITALL)
if not response_header == 'ZBXD\1':
raise ValueError('Got invalid response')
# read the data header to get the length of the response
response_data_header = s.recv(8, socket.MSG_WAITALL)
response_data_header = response_data_header[:4]
response_len = struct.unpack('i', response_data_header)[0]
# read the whole rest of the response now that we know the length
response_raw = s.recv(response_len, socket.MSG_WAITALL)
s.close()
LOG.info(response_raw)
response = json.loads(response_raw)
return response
评论列表
文章目录