From 968d1bd9a97944a52820b6d542089765b59f9bf2 Mon Sep 17 00:00:00 2001 From: Paul Sadauskas Date: Sun, 14 Jun 2009 18:43:50 -0600 Subject: [PATCH] http plugin: Separate values into multiple lines --- src/http.c | 106 ++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/src/http.c b/src/http.c index fdcd34fb..2cce5803 100644 --- a/src/http.c +++ b/src/http.c @@ -91,63 +91,52 @@ static int http_init(void) } static int value_list_to_string (char *buffer, int buffer_len, - const data_set_t *ds, const value_list_t *vl) + const data_set_t *ds, const value_list_t *vl, int index) { int offset = 0; int status; - int i; gauge_t *rates = NULL; assert (0 == strcmp (ds->type, vl->type)); memset (buffer, '\0', buffer_len); - for (i = 0; i < ds->ds_num; i++) + if ((ds->ds[index].type != DS_TYPE_COUNTER) + && (ds->ds[index].type != DS_TYPE_GAUGE)) + return (-1); + + if (ds->ds[index].type == DS_TYPE_COUNTER) { - if (i > 0) + if (rates == NULL) + rates = uc_get_rate (ds, vl); + if (rates == NULL) { - status = ssnprintf (buffer + offset, - buffer_len - offset, - ":"); - offset += status; - } - if ((ds->ds[i].type != DS_TYPE_COUNTER) - && (ds->ds[i].type != DS_TYPE_GAUGE)) + WARNING ("http plugin: " + "uc_get_rate failed."); return (-1); - - if (ds->ds[i].type == DS_TYPE_COUNTER) - { - if (rates == NULL) - rates = uc_get_rate (ds, vl); - if (rates == NULL) - { - WARNING ("http plugin: " - "uc_get_rate failed."); - return (-1); - } - if (isnan(rates[i])) - { - /* dont output */ - return (-1); - } - status = ssnprintf (buffer + offset, - buffer_len - offset, - "%lf", rates[i]); } - else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */ - { - status = ssnprintf (buffer + offset, buffer_len - offset, - "%lf", vl->values[i].gauge); - } - - if ((status < 1) || (status >= (buffer_len - offset))) + if (isnan(rates[index])) { - sfree (rates); + /* dont output */ return (-1); } + status = ssnprintf (buffer + offset, + buffer_len - offset, + "%lf", rates[index]); + } + else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */ + { + status = ssnprintf (buffer + offset, buffer_len - offset, + "%lf", vl->values[index].gauge); + } - offset += status; - } /* for ds->ds_num */ + if ((status < 1) || (status >= (buffer_len - offset))) + { + sfree (rates); + return (-1); + } + + offset += status; sfree (rates); return (0); @@ -223,7 +212,7 @@ static int value_list_to_metric_name (char *buffer, int buffer_len, offset += status; } - return (0); + return (offset); } /* int value_list_to_metric_name */ static int http_config (const char *key, const char *value) @@ -299,19 +288,25 @@ static int http_write (const data_set_t *ds, const value_list_t *vl, user_data_t __attribute__((unused)) *user_data) { char metric_name[512]; - char values[512]; + int metric_prefix_len; + char value[512]; char timestamp[512]; - char csv_buffer[1536]; + char csv_buffer[10240]; int status; + int offset = 0; + int i; if (0 != strcmp (ds->type, vl->type)) { ERROR ("http plugin: DS type does not match value list type"); return -1; } - if (value_list_to_metric_name (metric_name, sizeof (metric_name), ds, vl) != 0) + metric_prefix_len = value_list_to_metric_name (metric_name, + sizeof (metric_name), ds, vl); + + if (metric_prefix_len == -1) return (-1); DEBUG ("http plugin: http_write: metric_name = %s;", metric_name); @@ -319,14 +314,25 @@ static int http_write (const data_set_t *ds, const value_list_t *vl, if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0) return (-1); - if (value_list_to_string (values, sizeof (values), ds, vl) != 0) - return (-1); + for (i = 0; i < ds->ds_num; i++) + { + + if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0) + return (-1); + + ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len, + ",%s", ds->ds[i].name); + + escape_string (metric_name, sizeof (metric_name)); + + status = ssnprintf (csv_buffer + offset, sizeof (csv_buffer) - offset, + "\"%s\",%s,%s\n", + metric_name, timestamp, value); + offset += status; - escape_string (metric_name, sizeof (metric_name)); + } /* for */ - status = ssnprintf (csv_buffer, 1536, - "\"%s\",%s,%s\n", - metric_name, timestamp, values); + printf(csv_buffer); curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer); status = curl_easy_perform (curl); -- 2.30.2