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.
35 def __init__(self, path='/var/run/collectd-unixsock', noisy=False):
37 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
38 self._sock.connect(path)
40 def flush(self, timeout=None, plugins=[], identifiers=[]):
41 """Send a FLUSH command.
44 http://collectd.org/wiki/index.php/Plain_text_protocol#FLUSH
47 # have to pass at least one plugin or identifier
48 if not plugins and not identifiers:
52 args.append("timeout=%s" % timeout)
54 plugin_args = map(lambda x: "plugin=%s" % x, plugins)
55 args.extend(plugin_args)
57 identifier_args = map(lambda x: "identifier=%s" % x, identifiers)
58 args.extend(identifier_args)
59 return self._cmd('FLUSH %s' % ' '.join(args))
61 def getthreshold(self, identifier):
62 """Send a GETTHRESHOLD command.
65 http://collectd.org/wiki/index.php/Plain_text_protocol#GETTHRESHOLD
68 numvalues = self._cmd('GETTHRESHOLD "%s"' % identifier)
71 lines = self._readlines(numvalues)
74 def getval(self, identifier, flush_after=True):
75 """Send a GETVAL command.
77 Also flushes the identifier if flush_after is True.
80 http://collectd.org/wiki/index.php/Plain_text_protocol#GETVAL
83 numvalues = self._cmd('GETVAL "%s"' % identifier)
86 lines = self._readlines(numvalues)
88 self.flush(identifiers=[identifier])
92 """Send a LISTVAL command.
95 http://collectd.org/wiki/index.php/Plain_text_protocol#LISTVAL
98 numvalues = self._cmd('LISTVAL')
101 lines = self._readlines(numvalues)
104 def putnotif(self, message, options={}):
105 """Send a PUTNOTIF command.
107 Options must be passed as a Python dictionary. Example:
108 options={'severity': 'failure', 'host': 'example.com'}
111 http://collectd.org/wiki/index.php/Plain_text_protocol#PUTNOTIF
116 options_args = map(lambda x: "%s=%s" % (x, options[x]), options)
117 args.extend(options_args)
118 args.append('message="%s"' % message)
119 return self._cmd('PUTNOTIF %s' % ' '.join(args))
121 def putval(self, identifier, values, options={}):
122 """Send a PUTVAL command.
124 Options must be passed as a Python dictionary. Example:
125 options={'interval': 10}
128 http://collectd.org/wiki/index.php/Plain_text_protocol#PUTVAL
132 args.append('"%s"' % identifier)
134 options_args = map(lambda x: "%s=%s" % (x, options[x]), options)
135 args.extend(options_args)
136 values = map(str, values)
137 args.append(':'.join(values))
138 return self._cmd('PUTVAL %s' % ' '.join(args))
142 print "[send] %s" % c
143 self._sock.send(c + "\n")
144 status_message = self._readline()
146 print "[recive] %s" % status_message
147 if not status_message:
149 code, message = status_message.split(' ', 1)
155 """Read single line from socket"""
158 recv = self._sock.recv
167 def _readlines(self, sizehint=0):
168 """Read multiple lines from socket"""
172 line = self._readline()
177 if sizehint and total >= sizehint:
185 if __name__ == '__main__':
186 """Collect values from socket and dump to STDOUT"""
188 c = Collectd('/var/run/collectd-unixsock', noisy=True)
191 stamp, identifier = val.split()
192 print "\n%s" % identifier
193 print "\tUpdate time: %s" % stamp
195 values = c.getval(identifier)
196 print "\tValue list: %s" % ', '.join(values)
198 # don't fetch thresholds by default because collectd will crash
199 # if there is no treshold for the given identifier
200 #thresholds = c.getthreshold(identifier)
201 #print "\tThresholds: %s" % ', '.join(thresholds)