Merge pull request #21 from elemoine/stacklight-haproxy

Make haproxy emit backend_servers_percent metrics
diff --git a/collectd/files/plugin/collectd_apache_check.py b/collectd/files/plugin/collectd_apache_check.py
index 63ef855..790a60f 100644
--- a/collectd/files/plugin/collectd_apache_check.py
+++ b/collectd/files/plugin/collectd_apache_check.py
@@ -50,10 +50,6 @@
 plugin = ApacheCheckPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -61,6 +57,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/collectd_base.py b/collectd/files/plugin/collectd_base.py
index f042628..28cea12 100644
--- a/collectd/files/plugin/collectd_base.py
+++ b/collectd/files/plugin/collectd_base.py
@@ -17,7 +17,6 @@
 import json
 import signal
 import subprocess
-import sys
 import time
 import traceback
 
@@ -169,8 +168,8 @@
 
             ("foobar\n", "")
 
-            None if the command couldn't be executed or returned a non-zero
-            status code
+            (None, None) if the command couldn't be executed or returned a
+            non-zero status code
         """
         start_time = time.time()
         try:
@@ -186,14 +185,14 @@
         except Exception as e:
             self.logger.error("Cannot execute command '%s': %s : %s" %
                               (cmd, str(e), traceback.format_exc()))
-            return None
+            return (None, None)
 
         returncode = proc.returncode
 
         if returncode != 0:
             self.logger.error("Command '%s' failed (return code %d): %s" %
                               (cmd, returncode, stderr))
-            return None
+            return (None, None)
         if self.debug:
             elapsedtime = time.time() - start_time
             self.logger.info("Command '%s' returned %s in %0.3fs" %
@@ -222,18 +221,16 @@
 
     @staticmethod
     def restore_sigchld():
-        """Restores the SIGCHLD handler for Python <= v2.6.
+        """Restores the SIGCHLD handler.
 
         This should be provided to collectd as the init callback by plugins
-        that execute external programs.
+        that execute external programs and want to check the return code.
 
         Note that it will BREAK the exec plugin!!!
 
-        See https://github.com/deniszh/collectd-iostat-python/issues/2 for
-        details.
+        See contrib/python/getsigchld.py in the collectd project for details.
         """
-        if sys.version_info[0] == 2 and sys.version_info[1] <= 6:
-            signal.signal(signal.SIGCHLD, signal.SIG_DFL)
+        signal.signal(signal.SIGCHLD, signal.SIG_DFL)
 
     def notification_callback(self, notification):
         if not self.depends_on_resource:
diff --git a/collectd/files/plugin/collectd_glusterfs.py b/collectd/files/plugin/collectd_glusterfs.py
new file mode 100644
index 0000000..5457ea3
--- /dev/null
+++ b/collectd/files/plugin/collectd_glusterfs.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+# Copyright 2016 Mirantis, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import collectd
+import re
+
+import collectd_base as base
+
+NAME = 'glusterfs'
+GLUSTER_BINARY = '/usr/sbin/gluster'
+
+peer_re = re.compile(r'^Hostname: (?P<peer>.+)$', re.MULTILINE)
+state_re = re.compile(r'^State: (?P<state>.+)$', re.MULTILINE)
+
+
+class GlusterfsPlugin(base.Base):
+
+    def __init__(self, *args, **kwargs):
+        super(GlusterfsPlugin, self).__init__(*args, **kwargs)
+        self.plugin = NAME
+
+    def itermetrics(self):
+        # Collect peers' status
+        out, err = self.execute([GLUSTER_BINARY, 'peer', 'status'])
+        if not out:
+            raise base.CheckException("Failed to execute gluster")
+
+        for line in out.split('\n\n'):
+            peer_m = peer_re.search(line)
+            state_m = state_re.search(line)
+            if peer_m and state_m:
+                v = 0
+                if state_m.group('state') == 'Peer in Cluster (Connected)':
+                    v = 1
+                yield {
+                    'type_instance': 'peer',
+                    'values': v,
+                    'meta': {
+                        'peer': peer_m.group('peer')
+                    }
+                }
+
+
+plugin = GlusterfsPlugin(collectd)
+
+
+def init_callback():
+    plugin.restore_sigchld()
+
+
+def config_callback(conf):
+    plugin.config_callback(conf)
+
+
+def read_callback():
+    plugin.read_callback()
+
+collectd.register_init(init_callback)
+collectd.register_config(config_callback)
+collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/collectd_libvirt_check.py b/collectd/files/plugin/collectd_libvirt_check.py
index 4660609..d0df216 100644
--- a/collectd/files/plugin/collectd_libvirt_check.py
+++ b/collectd/files/plugin/collectd_libvirt_check.py
@@ -49,10 +49,6 @@
 plugin = LibvirtCheckPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -60,6 +56,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/collectd_memcached_check.py b/collectd/files/plugin/collectd_memcached_check.py
index fb44aeb..5d0dd26 100644
--- a/collectd/files/plugin/collectd_memcached_check.py
+++ b/collectd/files/plugin/collectd_memcached_check.py
@@ -60,10 +60,6 @@
 plugin = MemcachedCheckPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -71,6 +67,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/collectd_mysql_check.py b/collectd/files/plugin/collectd_mysql_check.py
index a42414c..3f59896 100644
--- a/collectd/files/plugin/collectd_mysql_check.py
+++ b/collectd/files/plugin/collectd_mysql_check.py
@@ -103,10 +103,6 @@
 plugin = MySQLCheckPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -114,6 +110,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/collectd_vrrp.py b/collectd/files/plugin/collectd_vrrp.py
new file mode 100644
index 0000000..b020ec2
--- /dev/null
+++ b/collectd/files/plugin/collectd_vrrp.py
@@ -0,0 +1,84 @@
+#!/usr/bin/python
+# Copyright 2016 Mirantis, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import collectd
+
+import collectd_base as base
+
+from pyroute2 import IPRoute
+
+NAME = 'vrrp'
+
+
+class VrrpPlugin(base.Base):
+
+    def __init__(self, *args, **kwargs):
+        super(VrrpPlugin, self).__init__(*args, **kwargs)
+        self.plugin = NAME
+        self.ip_addresses = []
+        self.ipr = IPRoute()
+
+    def config_callback(self, conf):
+        """Parse the plugin configuration.
+
+        Example:
+
+        <Module "collectd_vrrp">
+            <IPAddress>
+                address "172.16.10.254"
+                label "Foo"
+            </IPAddress>
+            <IPAddress>
+                address "172.16.10.253"
+            </IPAddress>
+        </Module>
+        """
+        super(VrrpPlugin, self).config_callback(conf)
+
+        for node in conf.children:
+            if node.key == 'IPAddress':
+                item = {}
+                for child_node in node.children:
+                    if child_node.key not in ('address', 'label'):
+                        continue
+                    item[child_node.key] = child_node.values[0]
+                if 'address' not in item:
+                    self.logger.error("vrrp: Missing 'address' parameter")
+                self.ip_addresses.append(item)
+
+        if len(self.ip_addresses) == 0:
+            self.logger.error("vrrp: Missing 'IPAddress' parameter")
+
+    def itermetrics(self):
+        for ip_address in self.ip_addresses:
+            v = 1 if self.ipr.get_addr(address=ip_address['address']) else 0
+            data = {'values': v, 'meta': {'ip_address': ip_address['address']}}
+            if 'label' in ip_address:
+                data['meta']['label'] = ip_address['label']
+            yield data
+
+
+plugin = VrrpPlugin(collectd)
+
+
+def config_callback(conf):
+    plugin.config_callback(conf)
+
+
+def read_callback():
+    plugin.read_callback()
+
+collectd.register_config(config_callback)
+collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/elasticsearch_cluster.py b/collectd/files/plugin/elasticsearch_cluster.py
index f60c1bc..e08d08a 100644
--- a/collectd/files/plugin/elasticsearch_cluster.py
+++ b/collectd/files/plugin/elasticsearch_cluster.py
@@ -109,10 +109,6 @@
 plugin = ElasticsearchClusterHealthPlugin(collectd, 'elasticsearch')
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -120,6 +116,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/haproxy.py b/collectd/files/plugin/haproxy.py
index ca9b9d8..2299d6e 100644
--- a/collectd/files/plugin/haproxy.py
+++ b/collectd/files/plugin/haproxy.py
@@ -307,10 +307,6 @@
 plugin = HAProxyPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -318,6 +314,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)
diff --git a/collectd/files/plugin/influxdb.py b/collectd/files/plugin/influxdb.py
index 43cd82d..4b7426b 100644
--- a/collectd/files/plugin/influxdb.py
+++ b/collectd/files/plugin/influxdb.py
@@ -129,10 +129,6 @@
 plugin = InfluxDBClusterPlugin(collectd)
 
 
-def init_callback():
-    plugin.restore_sigchld()
-
-
 def config_callback(conf):
     plugin.config_callback(conf)
 
@@ -140,6 +136,5 @@
 def read_callback():
     plugin.read_callback()
 
-collectd.register_init(init_callback)
 collectd.register_config(config_callback)
 collectd.register_read(read_callback)