コード例 #1
0
ファイル: utils_cache.c プロジェクト: tabletcorry/collectd
static void cache_free (cache_entry_t *ce)
{
  if (ce == NULL)
    return;

  sfree (ce->values_gauge);
  sfree (ce->values_raw);
  sfree (ce->history);
  if (ce->meta != NULL)
  {
    meta_data_destroy (ce->meta);
    ce->meta = NULL;
  }
  sfree (ce);
} /* void cache_free */
コード例 #2
0
ファイル: target_set.c プロジェクト: mleinart/collectd
static int ts_destroy (void **user_data) /* {{{ */
{
  ts_data_t *data;

  if (user_data == NULL)
    return (-EINVAL);

  data = *user_data;
  if (data == NULL)
    return (0);

  free (data->host);
  free (data->plugin);
  free (data->plugin_instance);
  /* free (data->type); */
  free (data->type_instance);
  meta_data_destroy(data->meta);
  free (data);

  return (0);
} /* }}} int ts_destroy */
コード例 #3
0
static int agg_instance_read (agg_instance_t *inst, cdtime_t t) /* {{{ */
{
  value_list_t vl = VALUE_LIST_INIT;
  char pi_prefix[DATA_MAX_NAME_LEN];

  /* Pre-set all the fields in the value list that will not change per
   * aggregation type (sum, average, ...). The struct will be re-used and must
   * therefore be dispatched using the "secure" function. */

  vl.time = t;
  vl.interval = 0;

  vl.meta = meta_data_create ();
  if (vl.meta == NULL)
  {
    ERROR ("aggregation plugin: meta_data_create failed.");
    return (-1);
  }
  meta_data_add_boolean (vl.meta, "aggregation:created", 1);

  if (AGG_MATCHES_ALL (inst->ident.host))
    sstrncpy (vl.host, "global", sizeof (vl.host));
  else
    sstrncpy (vl.host, inst->ident.host, sizeof (vl.host));

  sstrncpy (vl.plugin, "aggregation", sizeof (vl.plugin));

  if (AGG_MATCHES_ALL (inst->ident.plugin))
  {
    if (AGG_MATCHES_ALL (inst->ident.plugin_instance))
      sstrncpy (pi_prefix, "", sizeof (pi_prefix));
    else
      sstrncpy (pi_prefix, inst->ident.plugin_instance, sizeof (pi_prefix));
  }
  else
  {
    if (AGG_MATCHES_ALL (inst->ident.plugin_instance))
      sstrncpy (pi_prefix, inst->ident.plugin, sizeof (pi_prefix));
    else
      ssnprintf (pi_prefix, sizeof (pi_prefix),
          "%s-%s", inst->ident.plugin, inst->ident.plugin_instance);
  }

  sstrncpy (vl.type, inst->ident.type, sizeof (vl.type));

  if (!AGG_MATCHES_ALL (inst->ident.type_instance))
    sstrncpy (vl.type_instance, inst->ident.type_instance,
        sizeof (vl.type_instance));

#define READ_FUNC(func, rate) do { \
  if (inst->state_ ## func != NULL) { \
    agg_instance_read_func (inst, #func, rate, \
        inst->state_ ## func, &vl, pi_prefix, t); \
  } \
} while (0)

  pthread_mutex_lock (&inst->lock);

  READ_FUNC (num, (gauge_t) inst->num);

  /* All other aggregations are only defined when there have been any values
   * at all. */
  if (inst->num > 0)
  {
    READ_FUNC (sum, inst->sum);
    READ_FUNC (average, (inst->sum / ((gauge_t) inst->num)));
    READ_FUNC (min, inst->min);
    READ_FUNC (max, inst->max);
    READ_FUNC (stddev, sqrt((((gauge_t) inst->num) * inst->squares_sum)
          - (inst->sum * inst->sum)) / ((gauge_t) inst->num));
  }

  /* Reset internal state. */
  inst->num = 0;
  inst->sum = 0.0;
  inst->squares_sum = 0.0;
  inst->min = NAN;
  inst->max = NAN;

  pthread_mutex_unlock (&inst->lock);

  meta_data_destroy (vl.meta);
  vl.meta = NULL;

  return (0);
} /* }}} int agg_instance_read */
コード例 #4
0
/*
 * Result private functions
 */
static int udb_result_submit (udb_result_t *r, /* {{{ */
    udb_result_preparation_area_t *r_area,
    udb_query_t const *q, udb_query_preparation_area_t *q_area)
{
  value_list_t vl = VALUE_LIST_INIT;
  size_t i;
  int status;

  assert (r != NULL);
  assert (r_area->ds != NULL);
  assert (((size_t) r_area->ds->ds_num) == r->values_num);
  assert (r->values_num > 0);

  vl.values = calloc (r->values_num, sizeof (*vl.values));
  if (vl.values == NULL)
  {
    ERROR ("db query utils: calloc failed.");
    return (-1);
  }
  vl.values_len = r_area->ds->ds_num;

  for (i = 0; i < r->values_num; i++)
  {
    char *value_str = r_area->values_buffer[i];

    if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type))
    {
      ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
          value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type));
      errno = EINVAL;
      free (vl.values);
      return (-1);
    }
  }

  if (q_area->interval > 0)
    vl.interval = q_area->interval;

  sstrncpy (vl.host, q_area->host, sizeof (vl.host));
  sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
  sstrncpy (vl.type, r->type, sizeof (vl.type));

  /* Set vl.plugin_instance */
  if (q->plugin_instance_from != NULL) {
    sstrncpy (vl.plugin_instance, r_area->plugin_instance, sizeof (vl.plugin_instance));
  }
  else {
    sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance));
  }

  /* Set vl.type_instance {{{ */
  if (r->instances_num == 0)
  {
    if (r->instance_prefix == NULL)
      vl.type_instance[0] = 0;
    else
      sstrncpy (vl.type_instance, r->instance_prefix,
          sizeof (vl.type_instance));
  }
  else /* if ((r->instances_num > 0) */
  {
    if (r->instance_prefix == NULL)
    {
      strjoin (vl.type_instance, sizeof (vl.type_instance),
          r_area->instances_buffer, r->instances_num, "-");
    }
    else
    {
      char tmp[DATA_MAX_NAME_LEN];

      strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
          r->instances_num, "-");
      tmp[sizeof (tmp) - 1] = 0;

      snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
          r->instance_prefix, tmp);
    }
  }
  vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
  /* }}} */

  /* Annotate meta data. {{{ */
  if (r->metadata_num > 0)
  {
    vl.meta = meta_data_create ();
    if (vl.meta == NULL)
    {
      ERROR ("db query utils:: meta_data_create failed.");
      return (-ENOMEM);
    }

    for (i = 0; i < r->metadata_num; i++)
    {
      status = meta_data_add_string (vl.meta, r->metadata[i],
          r_area->metadata_buffer[i]);
      if (status != 0)
      {
        ERROR ("db query utils:: meta_data_add_string failed.");
        meta_data_destroy (vl.meta);
        vl.meta = NULL;
        return (status);
      }
    }
  }
  /* }}} */

  plugin_dispatch_values (&vl);

  if (r->metadata_num > 0)
  {
    meta_data_destroy (vl.meta);
    vl.meta = NULL;
  }
  sfree (vl.values);
  return (0);
} /* }}} void udb_result_submit */
コード例 #5
0
ファイル: target_replace.c プロジェクト: ajdiaz/collectd
static int tr_meta_data_action_invoke(/* {{{ */
                                      tr_meta_data_action_t *act_head,
                                      meta_data_t **dest) {
  int status;
  regmatch_t matches[8] = {[0] = {0}};

  if (act_head == NULL)
    return (-EINVAL);

  if ((*dest) == NULL) /* nothing to do */
    return (0);

  for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
    char temp[DATA_MAX_NAME_LEN];
    char *subst_status;
    int value_type;
    int meta_data_status;
    char *value;
    meta_data_t *result;

    value_type = meta_data_type(*dest, act->key);
    if (value_type == 0) /* not found */
      continue;
    if (value_type != MD_TYPE_STRING) {
      WARNING("Target `replace': Attempting replace on metadata key `%s', "
              "which isn't a string.",
              act->key);
      continue;
    }

    meta_data_status = meta_data_get_string(*dest, act->key, &value);
    if (meta_data_status != 0) {
      ERROR("Target `replace': Unable to retrieve metadata value for `%s'.",
            act->key);
      return (meta_data_status);
    }

    DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
          "old value = `%s'",
          act->key, value);

    status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
                     /* flags = */ 0);
    if (status == REG_NOMATCH) {
      sfree(value);
      continue;
    } else if (status != 0) {
      char errbuf[1024] = "";

      regerror(status, &act->re, errbuf, sizeof(errbuf));
      ERROR("Target `replace': Executing a regular expression failed: %s.",
            errbuf);
      sfree(value);
      continue;
    }

    if (act->replacement == NULL) {
      /* no replacement; delete the key */
      DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
            "deleting `%s'",
            act->key);
      meta_data_delete(*dest, act->key);
      sfree(value);
      continue;
    }

    subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so,
                         (size_t)matches[0].rm_eo, act->replacement);
    if (subst_status == NULL) {
      ERROR("Target `replace': subst (value = %s, start = %zu, end = %zu, "
            "replacement = %s) failed.",
            value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
            act->replacement);
      sfree(value);
      continue;
    }

    DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
          "value `%s' -> `%s'",
          act->key, value, temp);

    if ((result = meta_data_create()) == NULL) {
      ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
      sfree(value);
      return (-ENOMEM);
    }

    meta_data_status = meta_data_add_string(result, act->key, temp);
    if (meta_data_status != 0) {
      ERROR("Target `replace': Unable to set metadata value for `%s'.",
            act->key);
      meta_data_destroy(result);
      sfree(value);
      return (meta_data_status);
    }

    meta_data_clone_merge(dest, result);
    meta_data_destroy(result);
    sfree(value);
  } /* for (act = act_head; act != NULL; act = act->next) */

  return (0);
} /* }}} int tr_meta_data_action_invoke */
コード例 #6
0
static int network_dispatch_values (value_list_t *vl, /* {{{ */
    const char *username)
{
  int status;
  
  // DEBUG("host: %s", vl->host);
  // DEBUG("plugin: %s", vl->plugin);
  // DEBUG("plugin_instance: %s", vl->plugin_instance);
  // DEBUG("type: %s", vl->type);
  // DEBUG("type_instance: %s", vl->type_instance);

  if ((vl->time <= 0)
      || (strlen (vl->host) <= 0)
      || (strlen (vl->plugin) <= 0)
      || (strlen (vl->type) <= 0))
    return (-EINVAL);
  
  if (!check_receive_okay (vl))
  {
#if COLLECT_DEBUG
    char name[6*DATA_MAX_NAME_LEN];
    FORMAT_VL (name, sizeof (name), vl);
    name[sizeof (name) - 1] = 0;
    DEBUG ("network plugin: network_dispatch_values: "
	"NOT dispatching %s.", name);
#endif
    stats_values_not_dispatched++;
    return (0);
  }

  assert (vl->meta == NULL);

  vl->meta = meta_data_create ();
  if (vl->meta == NULL)
  {
    ERROR ("network plugin: meta_data_create failed.");
    return (-ENOMEM);
  }

  status = meta_data_add_boolean (vl->meta, "zeromq:received", 1);
  if (status != 0)
  {
    ERROR ("network plugin: meta_data_add_boolean failed.");
    meta_data_destroy (vl->meta);
    vl->meta = NULL;
    return (status);
  }

  if (username != NULL)
  {
    status = meta_data_add_string (vl->meta, "zeromq:username", username);
    if (status != 0)
    {
      ERROR ("network plugin: meta_data_add_string failed.");
      meta_data_destroy (vl->meta);
      vl->meta = NULL;
      return (status);
    }
  }
  
  // DEBUG("dispatching %d values", vl->values_len);
  plugin_dispatch_values (vl);
  stats_values_dispatched++;

  meta_data_destroy (vl->meta);
  vl->meta = NULL;

  return (0);
} /* }}} int network_dispatch_values */