-
Notifications
You must be signed in to change notification settings - Fork 160
Added method to ask for active checks #72
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .api import ZabbixAPI, ZabbixAPIException, ssl_context_compat | ||
from .sender import ZabbixMetric, ZabbixSender, ZabbixResponse | ||
from .sender import (ZabbixMetric, ZabbixSender, ZabbixResponse, | ||
ZabbixActiveChecksResponse, ZabbixCheck) | ||
|
||
__version__ = '1.1.3' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,70 @@ def __repr__(self): | |
return result | ||
|
||
|
||
class ZabbixActiveChecksResponse(object): | ||
"""The :class:`ZabbixActiveChecksResponse` | ||
contains the parsed response from Zabbix. | ||
""" | ||
def __init__(self): | ||
self.checks = None | ||
|
||
def __repr__(self): | ||
"""Represent detailed ZabbixActiveChecksResponse view.""" | ||
if self.checks is None: | ||
return '' | ||
else: | ||
return "[%s]" % ", ".join([ch.__repr__() for ch in self.checks]) | ||
|
||
def parse(self, response): | ||
"""Parse zabbix response for active checks.""" | ||
self.checks = [] | ||
data = response.get('data') | ||
for o in data: | ||
# lastlogsize is optional in 2.2; required in 2.4, 3.0, 3.2, 3.4 | ||
# mtime is unused in 3.2; required in 2.4, 3.0, 3.2, 3.4 | ||
ch = ZabbixCheck( | ||
o.get('key'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If some keys are required and not optional, we should get them explicitly to raise exception on error. Otherwise we may get empty checks on error. |
||
o.get('delay'), | ||
o.get('lastlogsize'), | ||
o.get('mtime')) | ||
self.checks.append(ch) | ||
|
||
|
||
class ZabbixCheck(object): | ||
"""The :class:`ZabbixCheck` contains one active check | ||
the Zabbix server would like. | ||
|
||
:type key: str | ||
:param key: Key by which you will identify this metric. | ||
|
||
:type delay: int | ||
:param delay: Period (in seconds) to collect this metric. | ||
|
||
:type lastlogsize: int | ||
:param lastlogsize: Point in the log file already known by the server. | ||
|
||
:type mtime: int | ||
:param mtime: Last time the server heard about this metric. | ||
|
||
>>> from pyzabbix import ZabbixCheck | ||
>>> ZabbixCheck('cpu[usage]', 60, 0, 0) | ||
""" | ||
|
||
def __init__(self, key, delay, lastlogsize, mtime): | ||
self.key = str(key) | ||
self.delay = delay | ||
self.lastlogsize = lastlogsize | ||
self.mtime = mtime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's usefull decorate properties as actually properties.
|
||
|
||
def __repr__(self): | ||
"""Represent detailed ZabbixCheck view.""" | ||
|
||
result = json.dumps(self.__dict__) | ||
logger.debug('%s: %s', self.__class__.__name__, result) | ||
|
||
return result | ||
|
||
|
||
class ZabbixSender(object): | ||
"""The :class:`ZabbixSender` send metrics to Zabbix server. | ||
|
||
|
@@ -385,3 +449,57 @@ 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 _create_active_checks_request(self, host): | ||
"""Create a formatted request to zabbix from a host name. | ||
|
||
:type host: str | ||
:param host: The host to ask about | ||
|
||
:rtype: str | ||
:return: Formatted zabbix request | ||
""" | ||
request = '{"request":"active checks","host":"%s"}' % host | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better use |
||
request = request.encode("utf-8") | ||
logger.debug('Request: %s', request) | ||
return request | ||
|
||
def send_active_checks(self, host): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function returns list of active checks from zabbix server, then why it calls |
||
"""Get active checks from the zabbix server. | ||
|
||
:type host: str | ||
:param host: host to ask about | ||
|
||
:rtype: :class:`pyzabbix.sender.ZabbixActiveChecksResponse` | ||
:return: Parsed response from Zabbix Server | ||
""" | ||
request = self._create_active_checks_request(host) | ||
packet = self._create_packet(request) | ||
|
||
for host_addr in self.zabbix_uri: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this part must be refactored as a function |
||
logger.debug('Sending data to %s', host_addr) | ||
|
||
# create socket object | ||
connection = socket.socket() | ||
|
||
# server and port must be tuple | ||
connection.connect(host_addr) | ||
|
||
try: | ||
connection.sendall(packet) | ||
except Exception as err: | ||
# In case of error we should close connection, | ||
# otherwise we will close it after data will be received. | ||
connection.close() | ||
raise Exception(err) | ||
|
||
response = self._get_response(connection) | ||
logger.debug('%s response: %s', host_addr, response) | ||
|
||
if response and response.get('response') != 'success': | ||
logger.debug('Response error: %s}', response) | ||
raise Exception(response) | ||
|
||
result = ZabbixActiveChecksResponse() | ||
result.parse(response) | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better represent it as json. Which you doing in
ZabbixCheck.__repr__
. I think it should be consisted.