Add plugin to check bond member status up/down

Change-Id: Ia07d4c473bf64d98170f51599caaedb46645ede3
diff --git a/collectd/files/plugin/bond_status.py b/collectd/files/plugin/bond_status.py
new file mode 100644
index 0000000..fe941cb
--- /dev/null
+++ b/collectd/files/plugin/bond_status.py
@@ -0,0 +1,106 @@
+#!/usr/bin/python
+# Copyright 2017 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.
+
+if __name__ == '__main__':
+    import collectd_fake as collectd
+else:
+    import collectd
+import os
+import requests
+
+import collectd_base as base
+
+NAME = 'bond_status'
+
+
+class BondStatusPlugin(base.Base):
+    def __init__(self, *args, **kwargs):
+        super(BondStatusPlugin, self).__init__(*args, **kwargs)
+        self.plugin = NAME
+        self.bonds = []
+        self.bond_dir = '/proc/net/bonding/'
+
+    def config_callback(self, conf):
+        super(BondStatusPlugin, self).config_callback(conf)
+
+        for node in conf.children:
+            if node.key == 'Bond':
+                self.bonds.append(node.values[0])
+
+    def itermetrics(self):
+        if "all" in self.bonds or len(self.bonds) == 0:
+            try:
+                self.bonds = os.listdir(self.bond_dir)
+            except OSError as e:
+                msg = "Error listing all bonds in {}".format(self.bond_dir)
+                raise base.CheckException(msg)
+
+        for bond in self.bonds:
+            try:
+                with open(self.bond_dir + bond, 'r') as fp:
+                    bond_info = fp.readlines()
+            except IOError as e:
+                msg = "Error reading bond info for {}".format(bond)
+                self.logger.error(msg)
+                continue
+
+            links_total = 0
+            links_down = 0
+            skip_first_mii_status = True
+            for line in bond_info:
+                if line.startswith("MII Status:"):
+                    # First occurance of "MII Status" is for the bond as a
+                    # whole, but we only want individual links.
+                    if skip_first_mii_status:
+                        skip_first_mii_status = False
+                        continue
+
+                    status = line[12:]
+                    if status == "down":
+                        links_down += 1
+                    links_total += 1
+
+            yield {
+                'type': 'links',
+                'type_instance': 'total',
+                'values': [links_total],
+                'meta': {'interface': bond}
+            }
+            yield {
+                'type': 'links',
+                'type_instance': 'down',
+                'values': [links_down],
+                'meta': {'interface': bond}
+            }
+
+
+plugin = BondStatusPlugin(collectd)
+
+
+def config_callback(conf):
+    plugin.config_callback(conf)
+
+
+def read_callback():
+    plugin.read_callback()
+
+
+if __name__ == '__main__':
+    collectd.load_configuration(plugin)
+    plugin.bonds = ["bond0"]
+    plugin.read_callback()
+else:
+    collectd.register_config(config_callback)
+    collectd.register_read(read_callback)