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.
32 class Collect(object):
34 def __init__(self, path='/var/run/collectd-unixsock'):
35 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
37 self._sock.connect(self._path)
40 numvalues = self._cmd('LISTVAL')
43 lines = self._readlines(numvalues)
46 def get(self, val, flush=True):
47 numvalues = self._cmd('GETVAL "' + val + '"')
50 lines = self._readlines(numvalues)
52 self._cmd('FLUSH identifier="' + val + '"')
56 self._sock.send(c + "\n")
57 stat = string.split(self._readline())
64 _readline and _readlines methods borrowed from the _fileobject class
65 in sockets.py, tweaked a little bit for use in the collectd context.
70 recv = self._sock.recv
79 def _readlines(self, sizehint=0):
83 line = self._readline()
88 if sizehint and total >= sizehint:
97 if __name__ == '__main__':
101 Collect values from socket and dump to STDOUT.
104 c = Collect('/var/run/collectd-unixsock')
108 stamp, key = string.split(val)
110 print stamp + ' ' + key + ' ' + ', '.join(glines)