Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function to recover active checks #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pyzabbix/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ def _chunk_send(self, metrics):
"""
messages = self._create_messages(metrics)
request = self._create_request(messages)

return self._send_request(request)

def _send_request(self, request):
"""Send the formated zabbix request to zabbix server.

:type request: str
:param request: formatted zabbix request

:rtype: str
:return: Response from Zabbix Server
"""
packet = self._create_packet(request)

for host_addr in self.zabbix_uri:
Expand Down Expand Up @@ -435,3 +447,22 @@ def send(self, metrics):
for m in range(0, len(metrics), self.chunk_size):
result.parse(self._chunk_send(metrics[m:m + self.chunk_size]))
return result

def get_active_checks(self, host):
"""Send a request to retrieve active checks

Format of returned items:
{'key': 'KEYITEM', 'delay': 30, 'lastlogsize': 0, 'mtime': 0}

:type host: string
:param host: host name to retrive active checks

:rtype: list
:return: List of active checks
"""
request = '{{"request":"active checks","host":"{host}"}}'.format(host=host)
request = request.encode("utf-8")
logger.debug('Request: %s', request)

response = self._send_request(request)
return response.get("data")
17 changes: 17 additions & 0 deletions tests/test_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,20 @@ def test_send_failed(self, mock_socket):
zs = ZabbixSender()
with self.assertRaises(Exception):
zs.send([zm])

@patch('pyzabbix.sender.socket.socket', autospec=autospec)
def test_get_active_checks(self, mock_socket):
mock_data = b'\x01\\\x00\x00\x00\x00\x00\x00\x00'
mock_socket.return_value = mock_socket
mock_socket.recv.side_effect = (b'ZBXD', mock_data, b'''
{"response":"success","data":[{"key":"KEYITEM1","delay":30,"lastlogsize":0, \
"mtime":0},{"key":"KEYITEM2","delay":300,"lastlogsize":10,"mtime":20}]}
''')

zs = ZabbixSender()
result = zs.get_active_checks("host1")
self.assertIsInstance(result, list)
self.assertEqual(result[0], {"key": "KEYITEM1", "delay": 30,
"lastlogsize": 0, "mtime": 0})
self.assertEqual(result[1], {"key": "KEYITEM2", "delay": 300,
"lastlogsize": 10, "mtime": 20})