pycollectd

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

commit c81bbabd741eeee4a2403b1d2a95bdbfb94c5a83
parent 0a1e3b89a003e2e383c59c1fe7353b20fc45c723
Author: Sean Lynch <seanl@literati.org>
Date:   Tue, 23 Mar 2010 01:19:22 +0000

Various neat stuff

Diffstat:
Mpycollectd/__init__.py | 4++--
Mpycollectd/collectd.py | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
2 files changed, 71 insertions(+), 36 deletions(-)

diff --git a/pycollectd/__init__.py b/pycollectd/__init__.py @@ -1,4 +1,4 @@ -from collectd import UnixSock, Identifier +from collectd import UnixSock, Exec, Identifier -__all__ = ['UnixSock', 'Identifier'] +__all__ = ['UnixSock', 'Exec', 'Identifier'] diff --git a/pycollectd/collectd.py b/pycollectd/collectd.py @@ -1,6 +1,6 @@ #!/usr/bin/python -import sys, socket, re +import sys, socket, re, time from datetime import datetime @@ -53,32 +53,24 @@ class Identifier(object): return dict((line.split('=') for line in lines)) -class UnixSock(object): - def __init__(self, socket_name='/var/run/collectd-unixsock'): - self._sock = socket.socket(socket.AF_UNIX) - self._sock.connect(socket_name) - +class CollectdBase(object): def _send(self, data): - self._sock.sendall(data) + raise NotImplementedError def _recvline(self): - data = '' - while not data or data[-1] != '\n': - data = data + self._sock.recv(4096) - - return data + raise NotImplementedError def _recvlines(self): - lines = [''] - while len(lines) == 1 or lines[-1] != '' or lines[0] and len(lines) <= int(lines[0].split()[0]) + 1: - data = self._sock.recv(4096) - if not data: - continue - new_lines = data.split('\n') - lines[-1] += new_lines[0] - lines.extend(new_lines[1:]) + raise NotImplementedError - return lines[1:-1] + def _check_result(self): + if self.readonly: + return + + result = self._recvline() + status = result.split()[0] + if status != '0': + raise CollectdError, result def _format_options(self, options): """ @@ -104,29 +96,72 @@ class UnixSock(object): lines = self._recvlines() return (self._parse_line(line) for line in lines) - def getval(self, name): - self._send('GETVAL {0}\n'.format(name)) + def getval(self, identifier): + self._send('GETVAL {0}\n'.format(identifier)) return self._parse_values(self._recvlines()) - 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))) + def putval(self, identifier, timestamp, values, **kwargs): + if isinstance(timestamp, datetime): + timestamp = time.mktime(timestamp.timetuple()) + + cmd = 'PUTVAL {0}{1} {2}:{3}\n'.format(identifier, self._format_options(kwargs), int(timestamp), ':'.join((str(v) for v in values))) self._send(cmd) - result = self._recvline() - status = result.split()[0] - if status != '0': - raise CollectdError, result + self._check_result() - def putnotif(self, time, name, severity, message): - if not isinstance(name, Identifier): - name = Identifier.fromstring(name) + def putnotif(self, timestamp, identifier, severity, message): + if isinstance(timestamp, datetime): + timestamp = time.mktime(timestamp.timetuple()) - self._send('PUTVAL time={0} severity={1}{2} message={3}\n'.format(time, severity, self._format_options(name.todict()))) + self._send('PUTVAL time={0} severity={1}{2} message={3}\n'.format(timestamp, severity, self._format_options(identifier.todict()))) + self._check_result() + + +class UnixSock(CollectdBase): + readonly = False + def __init__(self, socket_name='/var/run/collectd-unixsock'): + self._sock = socket.socket(socket.AF_UNIX) + self._sock.connect(socket_name) + + def _send(self, data): + self._sock.sendall(data) + + def _recvline(self): + data = '' + while not data or data[-1] != '\n': + data = data + self._sock.recv(4096) + + return data + + def _recvlines(self): + lines = [''] + while len(lines) == 1 or lines[-1] != '' or lines[0] and len(lines) <= int(lines[0].split()[0]) + 1: + data = self._sock.recv(4096) + if not data: + continue + new_lines = data.split('\n') + lines[-1] += new_lines[0] + lines.extend(new_lines[1:]) + + return lines[1:-1] + + +class Exec(CollectdBase): + readonly = True + def __init__(self, fp=sys.stdout): + self._fp = fp + + def _send(self, data): + self._fp.write(data) def main(): c = UnixSock() + e = Exec() for timestamp, name in c.listval(): - print name, timestamp, c.getval(name) + values = c.getval(name) + #print name, timestamp, values + e.putval(name, timestamp, values.values(), interval=10) + if __name__ == '__main__':