index 0222cdbdb3b3d526e14f3f3f08ffda4b866aba07..809f19dee65d2ec89a030a762bacb5e98041d8aa 100644 (file)
#
# Copyright © 2009 Adrian Perez <aperez@igalia.com>
#
-# Distributed under terms of the GPLv2 license.
+# Distributed under terms of the GPLv2 license or newer.
#
-# Frank Marien (frank@apsu.be) 4 Sep 2012
+# Frank Marien (frank@apsu.be) 6 Sep 2012
# - quick fixes for 5.1 binary protocol
-# - updated for python 3
+# - updated to python 3
# - fixed for larger packet sizes (possible on lo interface)
+# - fixed comment typo (decode_network_string decodes a string)
"""
Collectd network protocol implementation.
"""
-import socket
-import struct
-try:
- from io import StringIO
-except ImportError:
- from cStringIO import StringIO
+import socket,struct,sys
+import platform
+if platform.python_version() < '2.8.0':
+ # Python 2.7 and below io.StringIO does not like unicode
+ from StringIO import StringIO
+else:
+ try:
+ from io import StringIO
+ except ImportError:
+ from cStringIO import StringIO
from datetime import datetime
from copy import deepcopy
DEFAULT_IPv6_GROUP = "ff18::efc0:4a42"
"""Default IPv6 multicast group"""
-
+HR_TIME_DIV = (2.0**30)
# Message kinds
TYPE_HOST = 0x0000
DS_TYPE_DERIVE = 2
DS_TYPE_ABSOLUTE = 3
-
header = struct.Struct("!2H")
number = struct.Struct("!Q")
short = struct.Struct("!H")
double = struct.Struct("<d")
-
def decode_network_values(ptype, plen, buf):
"""Decodes a list of DS values in collectd network format
"""
assert double.size == number.size
result = []
- for dstype in buf[header.size+short.size:off]:
+ for dstype in [ord(x) for x in buf[header.size+short.size:off]]:
if dstype == DS_TYPE_COUNTER:
result.append((dstype, number.unpack_from(buf, off)[0]))
off += valskip
def decode_network_number(ptype, plen, buf):
- """Decodes a number (64-bit unsigned) in collectd network format.
+ """Decodes a number (64-bit unsigned) from collectd network format.
"""
return number.unpack_from(buf, header.size)[0]
def decode_network_string(msgtype, plen, buf):
- """Decodes a floating point number (64-bit) in collectd network format.
+ """Decodes a string from collectd network format.
"""
return buf[header.size:plen-1]
if kind == TYPE_TIME:
vl.time = nt.time = data
elif kind == TYPE_TIME_HR:
- vl.time = nt.time = data
+ vl.time = nt.time = data / HR_TIME_DIV
elif kind == TYPE_INTERVAL:
vl.interval = data
elif kind == TYPE_INTERVAL_HR:
- vl.interval = data
+ vl.interval = data / HR_TIME_DIV
elif kind == TYPE_HOST:
vl.host = nt.host = data
elif kind == TYPE_PLUGIN: