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

Moved settimeout before zabbix.connect() #11

Open
wants to merge 6 commits 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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def read(fname):

setup(
name = "zbxsend",
version = "0.1.6",
version = "0.1.6.3",
author = "Sergey Kirillov",
author_email = "[email protected]",
description = ("Module used to send metrics to Zabbix."),
Expand All @@ -17,4 +17,4 @@ def read(fname):
classifiers=[
"License :: OSI Approved :: BSD License",
],
)
)
20 changes: 12 additions & 8 deletions zbxsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def __repr__(self):
return 'Metric(%r, %r, %r, %r)' % (self.host, self.key, self.value, self.clock)

def send_to_zabbix(metrics, zabbix_host='127.0.0.1', zabbix_port=10051, timeout=15):
"""Send set of metrics to Zabbix server."""
"""Send set of metrics to Zabbix server."""

j = json.dumps
# Zabbix has very fragile JSON parser, and we cannot use json to dump whole packet
metrics_data = []
for m in metrics:
clock = m.clock or time.time()
clock = int(m.clock or time.time())
metrics_data.append(('\t\t{\n'
'\t\t\t"host":%s,\n'
'\t\t\t"key":%s,\n'
Expand All @@ -37,13 +37,17 @@ def send_to_zabbix(metrics, zabbix_host='127.0.0.1', zabbix_port=10051, timeout=
'\t"request":"sender data",\n'
'\t"data":[\n%s]\n'
'}') % (',\n'.join(metrics_data))

data_len = struct.pack('<Q', len(json_data))
packet = 'ZBXD\1' + data_len + json_data
try:
zabbix = socket.socket()
zabbix.connect((zabbix_host, zabbix_port))
addrs = socket.getaddrinfo(zabbix_host, zabbix_port)
if len(addrs) > 0 and addrs[0][0] == socket.AF_INET6:
zabbix = socket.socket(socket.AF_INET6)
else:
zabbix = socket.socket(socket.AF_INET)
zabbix.settimeout(timeout)
zabbix.connect((zabbix_host, zabbix_port))
# send metrics to zabbix
zabbix.sendall(packet)
# get response header from zabbix
Expand Down Expand Up @@ -72,7 +76,7 @@ def send_to_zabbix(metrics, zabbix_host='127.0.0.1', zabbix_port=10051, timeout=



logger = logging.getLogger('zbxsender')
logger = logging.getLogger('zbxsender')

def _recv_all(sock, count):
buf = ''
Expand All @@ -83,7 +87,7 @@ def _recv_all(sock, count):
buf += chunk
return buf


if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
send_to_zabbix([Metric('localhost', 'bucks_earned', 99999)], 'localhost', 10051)