Add ability to test plugins outside of collectd

This change adds a collectd_fake.py module that mimics the Python
module exposed by collectd. With a few changes to the plugin code, it
allows to run plugins from the command-line. The first enabled plugin
is http_check.

Change-Id: Ica9db6be04bcbd10d955f7afae4cab58d771c189
diff --git a/collectd/files/plugin/collectd_fake.py b/collectd/files/plugin/collectd_fake.py
new file mode 100644
index 0000000..2f96747
--- /dev/null
+++ b/collectd/files/plugin/collectd_fake.py
@@ -0,0 +1,73 @@
+#!/usr/bin/python
+# Copyright 2015 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 logging
+import os
+
+
+log_level = logging.INFO
+if os.getenv('COLLECTD_DEBUG', '') == '1':
+    log_level = logging.DEBUG
+logging.basicConfig(level=log_level)
+
+
+class Values(object):
+
+    def __init__(self, type=None, values=None, plugin_instance=None,
+                 type_instance=None, plugin=None, host=None, time=None,
+                 meta=None, interval=None):
+        self._type = type
+        self._values = values
+        self._plugin_instance = plugin_instance
+        self._type_instance = type_instance
+        self._plugin = plugin
+        self._host = host
+        self._time = time
+        self._meta = meta
+        self._interval = interval
+
+    def dispatch(self, type=None, values=None, plugin_instance=None,
+                 type_instance=None, plugin=None, host=None, time=None,
+                 meta=None, interval=None):
+        info("plugin={plugin} plugin_instance={plugin_instance} " \
+             "type={type} type_instance={type_instance} " \
+             "values={values} meta={meta}".format(
+                 plugin=plugin or self._plugin,
+                 plugin_instance=plugin_instance or self._plugin_instance,
+                 type=type or self._type,
+                 type_instance=type_instance or self._type_instance,
+                 values=values or self._values,
+                 meta=meta or self._meta,
+             ))
+
+
+def error(msg):
+    logging.error(msg)
+
+
+def warning(msg):
+    logging.warning(msg)
+
+
+def notice(msg):
+    logging.notice(msg)
+
+
+def info(msg):
+    logging.error(msg)
+
+
+def debug(msg):
+    logging.debug(msg)
diff --git a/collectd/files/plugin/http_check.py b/collectd/files/plugin/http_check.py
index 82c85b2..62dcbce 100644
--- a/collectd/files/plugin/http_check.py
+++ b/collectd/files/plugin/http_check.py
@@ -13,7 +13,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import collectd
+try:
+    import collectd
+except ImportError:
+    import collectd_fake as collectd
+
 import requests
 
 import collectd_base as base
@@ -68,24 +72,31 @@
                     yield {'type_instance': name, 'values': self.FAIL}
                 else:
                     self.logger.debug(
-                        "Got response from {}: '{}'".format(url, r.text))
+                        "Got response from {}: '{}'".format(url, r.content))
                     yield {'type_instance': name, 'values': self.OK}
 
 
 plugin = HTTPCheckPlugin(collectd, disable_check_metric=True)
 
 
-def config_callback(conf):
-    plugin.config_callback(conf)
+if __name__ == '__main__':
+    plugin.urls['google_ok'] = 'https://www.google.com'
+    plugin.urls['google_fail'] = 'https://www.google.com/not_found'
+    plugin.expected_codes['google_ok'] = 200
+    plugin.expected_codes['github_fail'] = 200
+    plugin.read_callback()
+else:
+    def config_callback(conf):
+        plugin.config_callback(conf)
 
 
-def notification_callback(notification):
-    plugin.notification_callback(notification)
+    def notification_callback(notification):
+        plugin.notification_callback(notification)
 
 
-def read_callback():
-    plugin.conditional_read_callback()
+    def read_callback():
+        plugin.conditional_read_callback()
 
-collectd.register_config(config_callback)
-collectd.register_notification(notification_callback)
-collectd.register_read(read_callback, base.INTERVAL)
+    collectd.register_config(config_callback)
+    collectd.register_notification(notification_callback)
+    collectd.register_read(read_callback, base.INTERVAL)