Skip to content

Commit

Permalink
ZabbixSender: add agent_hostname option, read it from config if enabl…
Browse files Browse the repository at this point in the history
…ed; if ZabbixMetric host attribute is None or '-', use aforementioned value from ZabbixSender which matches the behaviour of zabbix_sender tool
  • Loading branch information
m-khvoinitsky committed May 19, 2020
1 parent 8d2b855 commit 1019071
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pyzabbix/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ class ZabbixSender(object):
:type timeout: int
:param timeout: Number of seconds before call to Zabbix server times out
Default: 10
:type agent_hostname: str
:param agent_hostname: Default metric host field if not specified in metric.
This corresponds to 'Hostname' agent configuration option.
Default: None
>>> from pyzabbix import ZabbixMetric, ZabbixSender
>>> metrics = []
>>> m = ZabbixMetric('localhost', 'cpu[usage]', 20)
Expand All @@ -182,16 +188,18 @@ def __init__(self,
use_config=None,
chunk_size=250,
socket_wrapper=None,
timeout=10):
timeout=10,
agent_hostname=None):

self.chunk_size = chunk_size
self.timeout = timeout

self.socket_wrapper = socket_wrapper
if use_config:
self.zabbix_uri = self._load_from_config(use_config)
self.zabbix_uri, self.agent_hostname = self._load_from_config(use_config)
else:
self.zabbix_uri = [(zabbix_server, zabbix_port)]
self.agent_hostname = agent_hostname

def __repr__(self):
"""Represent detailed ZabbixSender view."""
Expand Down Expand Up @@ -255,9 +263,14 @@ def _load_from_config(self, config_file):
server, port = serverport.split(':')
serverport = (server, int(port))
result.append(serverport)
logger.debug("Loaded params: %s", result)

return result
agent_hostname = None
if config.has_option('root', 'Hostname'):
agent_hostname = config.get('root', 'Hostname')

logger.debug("Loaded params: %s, %s", result, agent_hostname)

return result, agent_hostname

def _receive(self, sock, count):
"""Reads socket to receive data from zabbix server.
Expand Down Expand Up @@ -293,6 +306,8 @@ def _create_messages(self, metrics):

# Fill the list of messages
for m in metrics:
if m.host is None or m.host == '-':
m.host = self.agent_hostname
messages.append(str(m))

logger.debug('Messages: %s', messages)
Expand Down

0 comments on commit 1019071

Please sign in to comment.