summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2176c4f)
raw | patch | inline | side by side (parent: 2176c4f)
author | Garret Heaton <powdahound@gmail.com> | |
Sun, 18 Oct 2009 21:19:26 +0000 (14:19 -0700) | ||
committer | Garret Heaton <powdahound@gmail.com> | |
Sun, 18 Oct 2009 21:19:26 +0000 (14:19 -0700) |
Also remove comment which seems to refer to a file outside of the
collectd project.
collectd project.
contrib/collectd_unixsock.py | patch | blob | history |
index 2d7430d29d44e27d696a71d3bd8b7cfafaf8a2c7..f8355f80028923e26f11cb3e4e5739193ead0aa0 100644 (file)
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
-import socket, string
+import socket
+import string
+
class Collect(object):
def __init__(self, path='/var/run/collectd-unixsock'):
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- self._path = path
+ self._path = path
self._sock.connect(self._path)
-
+
def list(self):
numvalues = self._cmd('LISTVAL')
lines = []
if numvalues:
lines = self._readlines(numvalues)
return lines
-
+
def get(self, val, flush=True):
numvalues = self._cmd('GETVAL "' + val + '"')
lines = []
if flush:
self._cmd('FLUSH identifier="' + val + '"')
return lines
-
+
def _cmd(self, c):
self._sock.send(c + "\n")
stat = string.split(self._readline())
if status:
return status
return False
-
- '''
- _readline and _readlines methods borrowed from the _fileobject class
- in sockets.py, tweaked a little bit for use in the collectd context.
- '''
+
def _readline(self):
+ """Read single line from socket"""
data = ''
buf = []
recv = self._sock.recv
if data != "\n":
buf.append(data)
return ''.join(buf)
-
+
def _readlines(self, sizehint=0):
+ """Read multiple lines from socket"""
total = 0
list = []
while True:
if sizehint and total >= sizehint:
break
return list
-
- def __del__(self):
- self._sock.close()
+ def __del__(self):
+ self._sock.close()
if __name__ == '__main__':
-
- '''
- Example usage:
- Collect values from socket and dump to STDOUT.
- '''
-
+ """Collect values from socket and dump to STDOUT"""
+
c = Collect('/var/run/collectd-unixsock')
list = c.list()
stamp, key = string.split(val)
glines = c.get(key)
print stamp + ' ' + key + ' ' + ', '.join(glines)
-