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

ZabbixSender: match zabbix_sender command line tool behaviour regarding metric 'host' attribute #131

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
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
8 changes: 5 additions & 3 deletions tests/test_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,17 @@ def test_load_from_config(self):
filename = os.path.join(folder, 'data/zabbix_agentd.conf')
zs = ZabbixSender()
result = zs._load_from_config(config_file=filename)
self.assertEqual(result, [('192.168.1.2', 10051)])
self.assertEqual(result, ([('192.168.1.2', 10051)], 'n150'))

def test_create_messages(self):
m = [ZabbixMetric('host1', 'key1', 1),
ZabbixMetric('host2', 'key2', 2)]
ZabbixMetric('host2', 'key2', 2),
ZabbixMetric('-', 'key3', 3),
ZabbixMetric(None, 'key4', 4)]
zs = ZabbixSender()
result = zs._create_messages(m)
self.assertIsInstance(result, list)
self.assertEqual(len(result), 2)
self.assertEqual(len(result), len(m))

def test_create_request(self):
message = [
Expand Down