Introduce a basic way to interact with graphite/carbon.

Thin wrapper to send UDP data to chromeos-stats.

TEST=Sent data to graphite server as a test.
BUG=None

Change-Id: If474edc0a3875f654edc4cd5b114ed0c7137884d
Reviewed-on: https://gerrit.chromium.org/gerrit/42752
Commit-Queue: Scott Zawalski <scottz@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Tested-by: Scott Zawalski <scottz@chromium.org>
diff --git a/global_config.ini b/global_config.ini
index ba6e3b0..38076b8 100644
--- a/global_config.ini
+++ b/global_config.ini
@@ -165,3 +165,7 @@
 
 lab_status_url: http://chromiumos-lab.appspot.com/current?format=json
 
+# carbon server feeding Graphite (chromeos-stats.corp.google.com)
+CARBON_SERVER: chromeos-stats.corp.google.com
+CARBON_PORT: 2003
+
diff --git a/site_utils/graphite/__init__.py b/site_utils/graphite/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/site_utils/graphite/__init__.py
diff --git a/site_utils/graphite/carbon.py b/site_utils/graphite/carbon.py
new file mode 100644
index 0000000..5642c17
--- /dev/null
+++ b/site_utils/graphite/carbon.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+import time
+from socket import socket
+
+import common
+
+from autotest_lib.client.common_lib import global_config
+
+
+CARBON_SERVER = global_config.global_config.get_config_value('CROS',
+        'CARBON_SERVER')
+CARBON_PORT = global_config.global_config.get_config_value('CROS',
+        'CARBON_PORT', int)
+
+
+def send_data(lines, add_time=True, debug=False, process_queue=20):
+    """Send data to the statsd/graphite server.
+
+    Example of a line "autotest.scheduler.running_agents_5m 300"
+    5m is the frequency we are sampling (It is not required but it adds clarity
+    to the metric).
+    @param lines: A list of lines of the format "category value"
+    @param add_time: Optional, if you do not want send_data to automatically
+        add a timestamp set this to False. However your lines of data will need
+        to be of the format "category value timestamp" [default: True]
+    @param debug: Print out what you would send but do not send anything.
+        [default: False]
+    @param process_queue: How many lines to send to the statsd server at a
+        time. [defualt: 20]
+    @returns True on success, False on failure.
+    """
+    sock = socket()
+    if add_time:
+        now = int(time.time())
+        for index in xrange(0, len(lines)):
+            lines[index] += ' %d' % now
+
+    try:
+      sock.connect( (CARBON_SERVER,CARBON_PORT) )
+    except:
+        return False
+
+    slices = [lines[i:i+process_queue] for i in range(0, len(lines),
+                                                      process_queue)]
+    for lines in slices:
+        data = '\n'.join(lines) + '\n'
+        if debug:
+            print 'Slice:\n%s' % data
+            continue
+        sock.sendall(data)
+
+    return True
diff --git a/site_utils/graphite/common.py b/site_utils/graphite/common.py
new file mode 100644
index 0000000..41607e1
--- /dev/null
+++ b/site_utils/graphite/common.py
@@ -0,0 +1,8 @@
+import os, sys
+dirname = os.path.dirname(sys.modules[__name__].__file__)
+autotest_dir = os.path.abspath(os.path.join(dirname, "..", ".."))
+client_dir = os.path.join(autotest_dir, "client")
+sys.path.insert(0, client_dir)
+import setup_modules
+sys.path.pop(0)
+setup_modules.setup(base_path=autotest_dir, root_module_name="autotest_lib")