src/utils_parse_json.c: Implement parse_json().
[collectd.git] / src / utils_parse_json.c
diff --git a/src/utils_parse_json.c b/src/utils_parse_json.c
new file mode 100644 (file)
index 0000000..db2cee4
--- /dev/null
@@ -0,0 +1,285 @@
+/**
+ * collectd - src/utils_parse_json.c
+ * Copyright (C) 2018  Florian Forster
+ *
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ *   Florian Forster <octo at collectd.org>
+ **/
+
+#include "collectd.h"
+
+#include "common.h"
+#include "plugin.h"
+#include "utils_parse_json.h"
+
+#include <yajl/yajl_tree.h>
+
+static int parse_json_gauge(yajl_val value_val, value_t *v) /* {{{ */
+{
+  if (YAJL_IS_DOUBLE(value_val))
+    v->gauge = (gauge_t)YAJL_GET_DOUBLE(value_val);
+  else if (YAJL_IS_NULL(value_val))
+    v->gauge = NAN;
+  else
+    return EINVAL;
+
+  return 0;
+} /* }}} int parse_json_gauge */
+
+static int parse_json_derive(yajl_val value_val, value_t *v) /* {{{ */
+{
+  if (!YAJL_IS_INTEGER(value_val)) {
+    if (YAJL_IS_NUMBER(value_val)) {
+      WARNING("parse_json_derive: %s is not an integer. The most common cause "
+              "is that the publisher has the StoreRates option enabled.",
+              YAJL_GET_NUMBER(value_val));
+    }
+    return EINVAL;
+  }
+
+  v->derive = (derive_t)YAJL_GET_INTEGER(value_val);
+  return 0;
+} /* }}} int parse_json_derive */
+
+static int parse_json_counter(yajl_val value_val, value_t *v) /* {{{ */
+{
+  if (!YAJL_IS_INTEGER(value_val)) {
+    if (YAJL_IS_NUMBER(value_val)) {
+      WARNING("parse_json_counter: %s is not an integer. The most common cause "
+              "is that the publisher has the StoreRates option enabled.",
+              YAJL_GET_NUMBER(value_val));
+    }
+    return EINVAL;
+  }
+
+  v->counter = (counter_t)YAJL_GET_INTEGER(value_val);
+  return 0;
+} /* }}} int parse_json_counter */
+
+static int parse_json_absolute(yajl_val value_val, value_t *v) /* {{{ */
+{
+  if (!YAJL_IS_INTEGER(value_val)) {
+    if (YAJL_IS_NUMBER(value_val)) {
+      WARNING("parse_json_absolute: %s is not an integer. The most common "
+              "cause is that the publisher has the StoreRates option enabled.",
+              YAJL_GET_NUMBER(value_val));
+    }
+    return EINVAL;
+  }
+
+  v->absolute = (absolute_t)YAJL_GET_INTEGER(value_val);
+  return 0;
+} /* }}} int parse_json_absolute */
+
+static int parse_json_value(yajl_val value_val, yajl_val dstype_val,
+                            value_t *v) /* {{{ */
+{
+  char const *dstype = YAJL_GET_STRING(dstype_val);
+
+  if (dstype == NULL) {
+    NOTICE("parse_json_value: unable to get data source type");
+    return EINVAL;
+  } else if (strcasecmp("gauge", dstype) == 0)
+    return parse_json_gauge(value_val, v);
+  else if (strcasecmp("derive", dstype) == 0)
+    return parse_json_derive(value_val, v);
+  else if (strcasecmp("counter", dstype) == 0)
+    return parse_json_counter(value_val, v);
+  else if (strcasecmp("absolute", dstype) == 0)
+    return parse_json_absolute(value_val, v);
+  else {
+    NOTICE("parse_json_value: unexpected data source type \"%s\"", dstype);
+    return EINVAL;
+  }
+} /* }}} int parse_json_value */
+
+static int parse_json_values(yajl_val root, value_list_t *vl) /* {{{ */
+{
+  char const *values_path[] = {"values", NULL};
+  char const *dstypes_path[] = {"dstypes", NULL};
+
+  yajl_val values_val = yajl_tree_get(root, values_path, yajl_t_array);
+  yajl_val dstypes_val = yajl_tree_get(root, dstypes_path, yajl_t_array);
+
+  size_t i;
+
+  if (values_val == NULL) {
+    NOTICE("parse_json_values: key \"values\" not found (or not an array)");
+    return EINVAL;
+  }
+  if (dstypes_val == NULL) {
+    NOTICE("parse_json_values: key \"dstypes\" not found (or not an array)");
+    return EINVAL;
+  }
+  if (YAJL_GET_ARRAY(values_val)->len != YAJL_GET_ARRAY(dstypes_val)->len) {
+    NOTICE("parse_json_values: lengths of \"values\" (%zu) and "
+           "\"dstypes\" (%zu) differ",
+           YAJL_GET_ARRAY(values_val)->len, YAJL_GET_ARRAY(dstypes_val)->len);
+    return EINVAL;
+  }
+
+  vl->values = calloc(YAJL_GET_ARRAY(values_val)->len, sizeof(*vl->values));
+  if (vl->values == NULL)
+    return ENOMEM;
+  vl->values_len = (int)YAJL_GET_ARRAY(values_val)->len;
+
+  for (i = 0; i < YAJL_GET_ARRAY(values_val)->len; i++) {
+    int status = parse_json_value(YAJL_GET_ARRAY(values_val)->values[i],
+                                  YAJL_GET_ARRAY(dstypes_val)->values[i],
+                                  &vl->values[i]);
+    if (status != 0) {
+      sfree(vl->values);
+      vl->values = NULL;
+      vl->values_len = 0;
+      return status;
+    }
+  }
+
+  return 0;
+} /* }}} int parse_json_values */
+
+static int parse_json_string(yajl_val root, char *key, char *buffer,
+                             size_t buffer_size) /* {{{ */
+{
+  char const *path[] = {key, NULL};
+  yajl_val string_val = yajl_tree_get(root, path, yajl_t_string);
+
+  if (!YAJL_IS_STRING(string_val))
+    return EINVAL;
+
+  sstrncpy(buffer, YAJL_GET_STRING(string_val), buffer_size);
+  return 0;
+} /* }}} int parse_json_string */
+
+static int parse_json_time(yajl_val root, char *key, cdtime_t *t) /* {{{ */
+{
+  char const *path[] = {key, NULL};
+  yajl_val double_val = yajl_tree_get(root, path, yajl_t_number);
+
+  if (!YAJL_IS_DOUBLE(double_val))
+    return EINVAL;
+
+  *t = DOUBLE_TO_CDTIME_T(YAJL_GET_DOUBLE(double_val));
+  return 0;
+} /* }}} int parse_json_time */
+
+static int parse_json_vl(yajl_val root, value_list_t *vl) /* {{{ */
+{
+  int status;
+
+  memset(vl, 0, sizeof(*vl));
+
+  status = parse_json_string(root, "host", vl->host, sizeof(vl->host));
+  if (status != 0) {
+    NOTICE("parse_json: parsing key \"host\" failed");
+    return status;
+  }
+
+  status = parse_json_string(root, "plugin", vl->plugin, sizeof(vl->plugin));
+  if (status != 0) {
+    NOTICE("parse_json: parsing key \"plugin\" failed");
+    return status;
+  }
+  parse_json_string(root, "plugin_instance", vl->plugin_instance,
+                    sizeof(vl->plugin_instance));
+
+  status = parse_json_string(root, "type", vl->type, sizeof(vl->type));
+  if (status != 0) {
+    NOTICE("parse_json: parsing key \"type\" failed");
+    return status;
+  }
+  parse_json_string(root, "type_instance", vl->type_instance,
+                    sizeof(vl->type_instance));
+
+  status = parse_json_time(root, "time", &vl->time);
+  if (status != 0) {
+    NOTICE("parse_json: parsing key \"time\" failed");
+    return status;
+  }
+
+  status = parse_json_time(root, "interval", &vl->interval);
+  if (status != 0) {
+    NOTICE("parse_json: parsing key \"interval\" failed");
+    return status;
+  }
+
+  status = parse_json_values(root, vl);
+  if (status != 0) {
+    return status;
+  }
+
+  return 0;
+} /* }}} int parse_json_vl */
+
+int parse_json(char const *json, value_list_t ***ret_vls,
+               size_t *ret_vls_num) /* {{{ */
+{
+  yajl_val root;
+  char errbuf[1024];
+  size_t i;
+
+  value_list_t **vls = NULL;
+  size_t vls_num = 0;
+
+  root = yajl_tree_parse(json, errbuf, sizeof(errbuf));
+  if (root == NULL) {
+    ERROR("parse_json: yajl_tree_parse failed: %s", errbuf);
+    return -1;
+  }
+
+  if (!YAJL_IS_ARRAY(root)) {
+    NOTICE("parse_json: root is not an array");
+    return -1;
+  }
+
+  for (i = 0; i < YAJL_GET_ARRAY(root)->len; i++) {
+    yajl_val vl_json = YAJL_GET_ARRAY(root)->values[i];
+    value_list_t *vl;
+
+    vl = malloc(sizeof(*vl));
+    if (vl == NULL)
+      continue;
+
+    int status = parse_json_vl(vl_json, vl);
+    if (status != 0) {
+      sfree(vl);
+      continue;
+    }
+
+    value_list_t **tmp = realloc(vls, sizeof(*vls) * (vls_num + 1));
+    if (tmp == NULL) {
+      ERROR("parse_json: realloc failed");
+      sfree(vl->values);
+      sfree(vl);
+      continue;
+    }
+    vls = tmp;
+
+    vls[vls_num] = vl;
+    vls_num++;
+  }
+
+  *ret_vls = vls;
+  *ret_vls_num = vls_num;
+  return 0;
+} /* }}} int parse_json */