pycollectd

Python interface to collectd-unixsock
git clone https://code.literati.org/pycollectd.git
Log | Files | Refs

commit bbbe422a12a754fe29fd3c4d735b72111122866c
parent 907b07b2476807a3c5ec2fdddf3afc6791f6fd01
Author: Sean Lynch <seanl@literati.org>
Date:   Tue, 12 Jan 2010 00:16:35 +0000

Fix putval method to work properly.

Diffstat:
Mpycollectd/collectd.py | 21++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/pycollectd/collectd.py b/pycollectd/collectd.py @@ -4,6 +4,10 @@ import sys, socket, re from datetime import datetime +class CollectdError(Exception): + pass + + class Identifier(object): _name_re = re.compile('(?P<hostname>[^/]+)/' '(?P<plugin>[^-/]+)(?:-(?P<plugin_instance>[^/]+))?/' @@ -59,7 +63,7 @@ class UnixSock(object): def _recvline(self): data = '' - while data[-1] != '\n': + while not data or data[-1] != '\n': data = data + self._sock.recv(4096) return data @@ -83,7 +87,7 @@ class UnixSock(object): """ - return ''.join((' ' + '='.join(k, str(v)) for k, v in options.iteritems())) + return ''.join((' ' + '{0}={1}'.format(k, v) for k, v in options.iteritems())) @staticmethod def _parse_values(lines): @@ -104,16 +108,19 @@ class UnixSock(object): self._send('GETVAL {0}\n'.format(name)) return self._parse_values(self._recvlines()) - def putval(self, name, values, **kwargs): - self._send('PUTVAL {0}{1} {2}\n'.format(name, ':'.join((str(v) for v in values)), - self._format_options(kwargs))) - return int(self._recvline().split()[0]) + def putval(self, name, time, values, **kwargs): + cmd = 'PUTVAL {0}{1} {2}:{3}\n'.format(name, self._format_options(kwargs), int(time), ':'.join((str(v) for v in values))) + self._send(cmd) + result = self._recvline() + status = result.split()[0] + if status != '0': + raise CollectdError, result def putnotif(self, time, name, severity, message): if not isinstance(name, Identifier): name = Identifier.fromstring(name) - self._send('PUTVAL time={0} severity={1}{2} message={3}'.format(time, severity, self._format_options(name.todict()))) + self._send('PUTVAL time={0} severity={1}{2} message={3}\n'.format(time, severity, self._format_options(name.todict()))) def main():