src/utils_format_graphite.[ch]: Add the "store_rates" option.
[collectd.git] / src / utils_format_graphite.c
index b0ebc1e..b9b906f 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/utils_format_graphite.c
  * Copyright (C) 2012  Thomas Meson
+ * Copyright (C) 2012  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -15,8 +16,9 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
- * Author:
+ * Authors:
  *   Thomas Meson <zllak at hycik.org>
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
@@ -31,7 +33,8 @@
  * Largely taken from write_graphite.c as it remains the same formatting */
 
 static int gr_format_values (char *ret, size_t ret_len,
-        int ds_num, const data_set_t *ds, const value_list_t *vl)
+        int ds_num, const data_set_t *ds, const value_list_t *vl,
+        gauge_t const *rates)
 {
     size_t offset = 0;
     int status;
@@ -57,6 +60,8 @@ static int gr_format_values (char *ret, size_t ret_len,
 
     if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
         BUFFER_ADD ("%f", vl->values[ds_num].gauge);
+    else if (rates != NULL)
+        BUFFER_ADD ("%f", rates[ds_num]);
     else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
         BUFFER_ADD ("%llu", vl->values[ds_num].counter);
     else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
@@ -75,7 +80,7 @@ static int gr_format_values (char *ret, size_t ret_len,
     return (0);
 }
 
-static void copy_escape_part (char *dst, const char *src, size_t dst_len,
+static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len,
     char escape_char)
 {
     size_t i;
@@ -124,15 +129,15 @@ static int gr_format_name (char *ret, int ret_len,
     if (postfix == NULL)
         postfix = "";
 
-    copy_escape_part (n_host, vl->host,
+    gr_copy_escape_part (n_host, vl->host,
             sizeof (n_host), escape_char);
-    copy_escape_part (n_plugin, vl->plugin,
+    gr_copy_escape_part (n_plugin, vl->plugin,
             sizeof (n_plugin), escape_char);
-    copy_escape_part (n_plugin_instance, vl->plugin_instance,
+    gr_copy_escape_part (n_plugin_instance, vl->plugin_instance,
             sizeof (n_plugin_instance), escape_char);
-    copy_escape_part (n_type, vl->type,
+    gr_copy_escape_part (n_type, vl->type,
             sizeof (n_type), escape_char);
-    copy_escape_part (n_type_instance, vl->type_instance,
+    gr_copy_escape_part (n_type_instance, vl->type_instance,
             sizeof (n_type_instance), escape_char);
 
     if (n_plugin_instance[0] != '\0')
@@ -163,12 +168,17 @@ static int gr_format_name (char *ret, int ret_len,
 
 int format_graphite (char *buffer, size_t buffer_size,
     const data_set_t *ds, const value_list_t *vl, char *prefix,
-    char *postfix, char escape_char)
+    char *postfix, char escape_char,
+    _Bool store_rates)
 {
     int status = 0;
     int i;
     int buffer_pos = 0;
 
+    gauge_t *rates = NULL;
+    if (store_rates)
+      rates = uc_get_rate (ds, vl);
+
     for (i = 0; i < ds->ds_num; i++)
     {
         const char *ds_name = NULL;
@@ -184,17 +194,19 @@ int format_graphite (char *buffer, size_t buffer_size,
                     prefix, postfix, escape_char);
         if (status != 0)
         {
-            ERROR ("amqp plugin: error with gr_format_name");
+            ERROR ("format_graphite: error with gr_format_name");
+            sfree (rates);
             return (status);
         }
 
         escape_string (key, sizeof (key));
         /* Convert the values to an ASCII representation and put that into
          * `values'. */
-        status = gr_format_values (values, sizeof (values), i, ds, vl);
+        status = gr_format_values (values, sizeof (values), i, ds, vl, rates);
         if (status != 0)
         {
             ERROR ("format_graphite: error with gr_format_values");
+            sfree (rates);
             return (status);
         }
 
@@ -207,6 +219,7 @@ int format_graphite (char *buffer, size_t buffer_size,
         if (message_len >= sizeof (message)) {
             ERROR ("format_graphite: message buffer too small: "
                     "Need %zu bytes.", message_len + 1);
+            sfree (rates);
             return (-ENOMEM);
         }
 
@@ -214,11 +227,13 @@ int format_graphite (char *buffer, size_t buffer_size,
         if ((buffer_pos + message_len) >= buffer_size)
         {
             ERROR ("format_graphite: target buffer too small");
+            sfree (rates);
             return (-ENOMEM);
         }
         memcpy((void *) (buffer + buffer_pos), message, message_len);
         buffer_pos += message_len;
     }
+    sfree (rates);
     return (status);
 } /* int format_graphite */