Skip to content

Commit 1019071

Browse files
committed
ZabbixSender: add agent_hostname option, read it from config if enabled; if ZabbixMetric host attribute is None or '-', use aforementioned value from ZabbixSender which matches the behaviour of zabbix_sender tool
1 parent 8d2b855 commit 1019071

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

pyzabbix/sender.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ class ZabbixSender(object):
168168
:type timeout: int
169169
:param timeout: Number of seconds before call to Zabbix server times out
170170
Default: 10
171+
172+
:type agent_hostname: str
173+
:param agent_hostname: Default metric host field if not specified in metric.
174+
This corresponds to 'Hostname' agent configuration option.
175+
Default: None
176+
171177
>>> from pyzabbix import ZabbixMetric, ZabbixSender
172178
>>> metrics = []
173179
>>> m = ZabbixMetric('localhost', 'cpu[usage]', 20)
@@ -182,16 +188,18 @@ def __init__(self,
182188
use_config=None,
183189
chunk_size=250,
184190
socket_wrapper=None,
185-
timeout=10):
191+
timeout=10,
192+
agent_hostname=None):
186193

187194
self.chunk_size = chunk_size
188195
self.timeout = timeout
189196

190197
self.socket_wrapper = socket_wrapper
191198
if use_config:
192-
self.zabbix_uri = self._load_from_config(use_config)
199+
self.zabbix_uri, self.agent_hostname = self._load_from_config(use_config)
193200
else:
194201
self.zabbix_uri = [(zabbix_server, zabbix_port)]
202+
self.agent_hostname = agent_hostname
195203

196204
def __repr__(self):
197205
"""Represent detailed ZabbixSender view."""
@@ -255,9 +263,14 @@ def _load_from_config(self, config_file):
255263
server, port = serverport.split(':')
256264
serverport = (server, int(port))
257265
result.append(serverport)
258-
logger.debug("Loaded params: %s", result)
259266

260-
return result
267+
agent_hostname = None
268+
if config.has_option('root', 'Hostname'):
269+
agent_hostname = config.get('root', 'Hostname')
270+
271+
logger.debug("Loaded params: %s, %s", result, agent_hostname)
272+
273+
return result, agent_hostname
261274

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

294307
# Fill the list of messages
295308
for m in metrics:
309+
if m.host is None or m.host == '-':
310+
m.host = self.agent_hostname
296311
messages.append(str(m))
297312

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

0 commit comments

Comments
 (0)