<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -28,38 +28,127 @@
 # 3. This notice may not be removed or altered from any source distribution.
 
 import socket
-import string
 
 
-class Collect(object):
+class Collectd():
 
-    def __init__(self, path='/var/run/collectd-unixsock'):
+    def __init__(self, path='/var/run/collectd-unixsock', noisy=False):
+        self.noisy = noisy
         self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-        self._path = path
-        self._sock.connect(self._path)
+        self._sock.connect(path)
 
-    def list(self):
-        numvalues = self._cmd('LISTVAL')
+    def flush(self, timeout=None, plugins=[], identifiers=[]):
+        &quot;&quot;&quot;Send a FLUSH command.
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#FLUSH
+
+        &quot;&quot;&quot;
+        # have to pass at least one plugin or identifier
+        if not plugins and not identifiers:
+            return None
+        args = []
+        if timeout:
+            args.append(&quot;timeout=%s&quot; % timeout)
+        if plugins:
+            plugin_args = map(lambda x: &quot;plugin=%s&quot; % x, plugins)
+            args.extend(plugin_args)
+        if identifiers:
+            identifier_args = map(lambda x: &quot;identifier=%s&quot; % x, identifiers)
+            args.extend(identifier_args)
+        return self._cmd('FLUSH %s' % ' '.join(args))
+
+    def getthreshold(self, identifier):
+        &quot;&quot;&quot;Send a GETTHRESHOLD command.
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#GETTHRESHOLD
+
+        &quot;&quot;&quot;
+        numvalues = self._cmd('GETTHRESHOLD &quot;%s&quot;' % identifier)
+        lines = []
+        if numvalues:
+            lines = self._readlines(numvalues)
+        return lines
+
+    def getval(self, identifier, flush_after=True):
+        &quot;&quot;&quot;Send a GETVAL command.
+
+        Also flushes the identifier if flush_after is True.
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#GETVAL
+
+        &quot;&quot;&quot;
+        numvalues = self._cmd('GETVAL &quot;%s&quot;' % identifier)
         lines = []
         if numvalues:
             lines = self._readlines(numvalues)
+        if flush_after:
+            self.flush(identifiers=[identifier])
         return lines
 
-    def get(self, val, flush=True):
-        numvalues = self._cmd('GETVAL &quot;' + val + '&quot;')
+    def listval(self):
+        &quot;&quot;&quot;Send a LISTVAL command.
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#LISTVAL
+
+        &quot;&quot;&quot;
+        numvalues = self._cmd('LISTVAL')
         lines = []
         if numvalues:
             lines = self._readlines(numvalues)
-        if flush:
-            self._cmd('FLUSH identifier=&quot;' + val + '&quot;')
         return lines
 
+    def putnotif(self, message, options={}):
+        &quot;&quot;&quot;Send a PUTNOTIF command.
+
+        Options must be passed as a Python dictionary. Example:
+          options={'severity': 'failure', 'host': 'example.com'}
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#PUTNOTIF
+
+        &quot;&quot;&quot;
+        args = []
+        if options:
+            options_args = map(lambda x: &quot;%s=%s&quot; % (x, options[x]), options)
+            args.extend(options_args)
+        args.append('message=&quot;%s&quot;' % message)
+        return self._cmd('PUTNOTIF %s' % ' '.join(args))
+
+    def putval(self, identifier, values, options={}):
+        &quot;&quot;&quot;Send a PUTVAL command.
+
+        Options must be passed as a Python dictionary. Example:
+          options={'interval': 10}
+
+        Full documentation:
+            http://collectd.org/wiki/index.php/Plain_text_protocol#PUTVAL
+
+        &quot;&quot;&quot;
+        args = []
+        args.append('&quot;%s&quot;' % identifier)
+        if options:
+            options_args = map(lambda x: &quot;%s=%s&quot; % (x, options[x]), options)
+            args.extend(options_args)
+        values = map(str, values)
+        args.append(':'.join(values))
+        return self._cmd('PUTVAL %s' % ' '.join(args))
+
     def _cmd(self, c):
+        if self.noisy:
+            print &quot;[send] %s&quot; % c
         self._sock.send(c + &quot;\n&quot;)
-        stat = string.split(self._readline())
-        status = int(stat[0])
-        if status:
-            return status
+        status_message = self._readline()
+        if self.noisy:
+            print &quot;[recive] %s&quot; % status_message
+        if not status_message:
+            return None
+        code, message = status_message.split(' ', 1)
+        if int(code):
+            return int(code)
         return False
 
     def _readline(self):
@@ -96,10 +185,17 @@ class Collect(object):
 if __name__ == '__main__':
     &quot;&quot;&quot;Collect values from socket and dump to STDOUT&quot;&quot;&quot;
 
-    c = Collect('/var/run/collectd-unixsock')
-    list = c.list()
-
+    c = Collectd('/var/run/collectd-unixsock', noisy=True)
+    list = c.listval()
     for val in list:
-        stamp, key = string.split(val)
-        glines = c.get(key)
-        print stamp + ' ' + key + ' ' + ', '.join(glines)
+        stamp, identifier = val.split()
+        print &quot;\n%s&quot; % identifier
+        print &quot;\tUpdate time: %s&quot; % stamp
+
+        values = c.getval(identifier)
+        print &quot;\tValue list: %s&quot; % ', '.join(values)
+
+        # don't fetch thresholds by default because collectd will crash
+        # if there is no treshold for the given identifier
+        #thresholds = c.getthreshold(identifier)
+        #print &quot;\tThresholds: %s&quot; % ', '.join(thresholds)</diff>
      <filename>contrib/collectd_unixsock.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ef868b32154b2711850f70832ab0cd895f431376</id>
    </parent>
  </parents>
  <author>
    <name>Garret Heaton</name>
    <email>powdahound@gmail.com</email>
  </author>
  <url>http://github.com/powdahound/collectd/commit/c901e521eea4a7f066e9e359e8dca49935064d61</url>
  <id>c901e521eea4a7f066e9e359e8dca49935064d61</id>
  <committed-date>2009-10-18T15:34:13-07:00</committed-date>
  <authored-date>2009-10-18T15:34:13-07:00</authored-date>
  <message>Support all plain text protocol commands

Also rename class from Collect to Collectd and improve output when run
as standalone script.</message>
  <tree>e99c23bd7fe64d2169f2d803805efb03045d7956</tree>
  <committer>
    <name>Garret Heaton</name>
    <email>powdahound@gmail.com</email>
  </committer>
</commit>
