1 #-*- coding: ISO-8859-1 -*-
2 # collect.py: the python collectd-unixsock module.
4 # Requires collectd to be configured with the unixsock plugin, like so:
8 # SocketFile "/var/run/collectd-unixsock"
12 # Copyright (C) 2008 Clay Loveless <clay@killersoft.com>
14 # This software is provided 'as-is', without any express or implied
15 # warranty. In no event will the author be held liable for any damages
16 # arising from the use of this software.
18 # Permission is granted to anyone to use this software for any purpose,
19 # including commercial applications, and to alter it and redistribute it
20 # freely, subject to the following restrictions:
22 # 1. The origin of this software must not be misrepresented; you must not
23 # claim that you wrote the original software. If you use this software
24 # in a product, an acknowledgment in the product documentation would be
25 # appreciated but is not required.
26 # 2. Altered source versions must be plainly marked as such, and must not be
27 # misrepresented as being the original software.
28 # 3. This notice may not be removed or altered from any source distribution.
34 class Collect(object):
36 def __init__(self, path='/var/run/collectd-unixsock'):
37 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
39 self._sock.connect(self._path)
42 numvalues = self._cmd('LISTVAL')
45 lines = self._readlines(numvalues)
48 def get(self, val, flush=True):
49 numvalues = self._cmd('GETVAL "' + val + '"')
52 lines = self._readlines(numvalues)
54 self._cmd('FLUSH identifier="' + val + '"')
58 self._sock.send(c + "\n")
59 stat = string.split(self._readline())
66 """Read single line from socket"""
69 recv = self._sock.recv
78 def _readlines(self, sizehint=0):
79 """Read multiple lines from socket"""
83 line = self._readline()
88 if sizehint and total >= sizehint:
96 if __name__ == '__main__':
97 """Collect values from socket and dump to STDOUT"""
99 c = Collect('/var/run/collectd-unixsock')
103 stamp, key = string.split(val)
105 print stamp + ' ' + key + ' ' + ', '.join(glines)