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.
36 def __init__(self, path='/var/run/collectd-unixsock', noisy=False):
39 self._sock = self._connect()
41 def flush(self, timeout=None, plugins=[], identifiers=[]):
42 """Send a FLUSH command.
45 http://collectd.org/wiki/index.php/Plain_text_protocol#FLUSH
48 # have to pass at least one plugin or identifier
49 if not plugins and not identifiers:
53 args.append("timeout=%s" % timeout)
55 plugin_args = map(lambda x: "plugin=%s" % x, plugins)
56 args.extend(plugin_args)
58 identifier_args = map(lambda x: "identifier=%s" % x, identifiers)
59 args.extend(identifier_args)
60 return self._cmd('FLUSH %s' % ' '.join(args))
62 def getthreshold(self, identifier):
63 """Send a GETTHRESHOLD command.
66 http://collectd.org/wiki/index.php/Plain_text_protocol#GETTHRESHOLD
69 numvalues = self._cmd('GETTHRESHOLD "%s"' % identifier)
71 if not numvalues or numvalues < 0:
72 raise KeyError("Identifier '%s' not found" % identifier)
73 lines = self._readlines(numvalues)
76 def getval(self, identifier, flush_after=True):
77 """Send a GETVAL command.
79 Also flushes the identifier if flush_after is True.
82 http://collectd.org/wiki/index.php/Plain_text_protocol#GETVAL
85 numvalues = self._cmd('GETVAL "%s"' % identifier)
87 if not numvalues or numvalues < 0:
88 raise KeyError("Identifier '%s' not found" % identifier)
89 lines = self._readlines(numvalues)
91 self.flush(identifiers=[identifier])
95 """Send a LISTVAL command.
98 http://collectd.org/wiki/index.php/Plain_text_protocol#LISTVAL
101 numvalues = self._cmd('LISTVAL')
104 lines = self._readlines(numvalues)
107 def putnotif(self, message, options={}):
108 """Send a PUTNOTIF command.
110 Options must be passed as a Python dictionary. Example:
111 options={'severity': 'failure', 'host': 'example.com'}
114 http://collectd.org/wiki/index.php/Plain_text_protocol#PUTNOTIF
119 options_args = map(lambda x: "%s=%s" % (x, options[x]), options)
120 args.extend(options_args)
121 args.append('message="%s"' % message)
122 return self._cmd('PUTNOTIF %s' % ' '.join(args))
124 def putval(self, identifier, values, options={}):
125 """Send a PUTVAL command.
127 Options must be passed as a Python dictionary. Example:
128 options={'interval': 10}
131 http://collectd.org/wiki/index.php/Plain_text_protocol#PUTVAL
135 args.append('"%s"' % identifier)
137 options_args = map(lambda x: "%s=%s" % (x, options[x]), options)
138 args.extend(options_args)
139 values = map(str, values)
140 args.append(':'.join(values))
141 return self._cmd('PUTVAL %s' % ' '.join(args))
145 return self._cmdattempt(c)
146 except socket.error, (errno, errstr):
147 sys.stderr.write("[error] Sending to socket failed: [%d] %s\n"
149 self._sock = self._connect()
150 return self._cmdattempt(c)
152 def _cmdattempt(self, c):
154 print "[send] %s" % c
156 sys.stderr.write("[error] Socket unavailable. Can not send.")
158 self._sock.send(c + "\n")
159 status_message = self._readline()
161 print "[receive] %s" % status_message
162 if not status_message:
164 code, message = status_message.split(' ', 1)
171 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
172 sock.connect(self.path)
174 print "[socket] connected to %s" % self.path
176 except socket.error, (errno, errstr):
177 sys.stderr.write("[error] Connecting to socket failed: [%d] %s"
182 """Read single line from socket"""
184 sys.stderr.write("[error] Socket unavailable. Can not read.")
189 recv = self._sock.recv
197 except socket.error, (errno, errstr):
198 sys.stderr.write("[error] Reading from socket failed: [%d] %s"
200 self._sock = self._connect()
203 def _readlines(self, sizehint=0):
204 """Read multiple lines from socket"""
208 line = self._readline()
213 if sizehint and total >= sizehint:
222 except socket.error, (errno, errstr):
223 sys.stderr.write("[error] Closing socket failed: [%d] %s"
227 if __name__ == '__main__':
228 """Collect values from socket and dump to STDOUT"""
230 c = Collectd('/var/run/collectd-unixsock', noisy=True)
233 stamp, identifier = val.split()
234 print "\n%s" % identifier
235 print "\tUpdate time: %s" % stamp
237 values = c.getval(identifier)
238 print "\tValue list: %s" % ', '.join(values)
240 # don't fetch thresholds by default because collectd will crash
241 # if there is no treshold for the given identifier
242 #thresholds = c.getthreshold(identifier)
243 #print "\tThresholds: %s" % ', '.join(thresholds)