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 */
Exemplo n.º 2
0
static int agg_instance_create_name (agg_instance_t *inst, /* {{{ */
    value_list_t const *vl, aggregation_t const *agg)
{
#define COPY_FIELD(buffer, buffer_size, field, group_mask, all_value) do { \
  if (agg->set_ ## field != NULL) \
    sstrncpy (buffer, agg->set_ ## field, buffer_size); \
  else if ((agg->regex_fields & group_mask) \
      && (agg->group_by & group_mask)) \
    sstrncpy (buffer, vl->field, buffer_size); \
  else if ((agg->regex_fields & group_mask) \
      && (AGG_MATCHES_ALL (agg->ident.field))) \
    sstrncpy (buffer, all_value, buffer_size); \
  else \
    sstrncpy (buffer, agg->ident.field, buffer_size); \
} while (0)

  /* Host */
  COPY_FIELD (inst->ident.host, sizeof (inst->ident.host),
      host, LU_GROUP_BY_HOST, "global");

  /* Plugin */
  if (agg->set_plugin != NULL)
    sstrncpy (inst->ident.plugin, agg->set_plugin,
        sizeof (inst->ident.plugin));
  else
    sstrncpy (inst->ident.plugin, "aggregation", sizeof (inst->ident.plugin));

  /* Plugin instance */
  if (agg->set_plugin_instance != NULL)
    sstrncpy (inst->ident.plugin_instance, agg->set_plugin_instance,
        sizeof (inst->ident.plugin_instance));
  else
  {
    char tmp_plugin[DATA_MAX_NAME_LEN];
    char tmp_plugin_instance[DATA_MAX_NAME_LEN] = "";

    if ((agg->regex_fields & LU_GROUP_BY_PLUGIN)
        && (agg->group_by & LU_GROUP_BY_PLUGIN))
      sstrncpy (tmp_plugin, vl->plugin, sizeof (tmp_plugin));
    else if ((agg->regex_fields & LU_GROUP_BY_PLUGIN)
        && (AGG_MATCHES_ALL (agg->ident.plugin)))
      sstrncpy (tmp_plugin, "", sizeof (tmp_plugin));
    else
      sstrncpy (tmp_plugin, agg->ident.plugin, sizeof (tmp_plugin));

    if ((agg->regex_fields & LU_GROUP_BY_PLUGIN_INSTANCE)
        && (agg->group_by & LU_GROUP_BY_PLUGIN_INSTANCE))
      sstrncpy (tmp_plugin_instance, vl->plugin_instance,
          sizeof (tmp_plugin_instance));
    else if ((agg->regex_fields & LU_GROUP_BY_PLUGIN_INSTANCE)
        && (AGG_MATCHES_ALL (agg->ident.plugin_instance)))
      sstrncpy (tmp_plugin_instance, "", sizeof (tmp_plugin_instance));
    else
      sstrncpy (tmp_plugin_instance, agg->ident.plugin_instance,
          sizeof (tmp_plugin_instance));

    if ((strcmp ("", tmp_plugin) == 0)
        && (strcmp ("", tmp_plugin_instance) == 0))
      sstrncpy (inst->ident.plugin_instance, AGG_FUNC_PLACEHOLDER,
          sizeof (inst->ident.plugin_instance));
    else if (strcmp ("", tmp_plugin) != 0)
      ssnprintf (inst->ident.plugin_instance,
          sizeof (inst->ident.plugin_instance),
          "%s-%s", tmp_plugin, AGG_FUNC_PLACEHOLDER);
    else if (strcmp ("", tmp_plugin_instance) != 0)
      ssnprintf (inst->ident.plugin_instance,
          sizeof (inst->ident.plugin_instance),
          "%s-%s", tmp_plugin_instance, AGG_FUNC_PLACEHOLDER);
    else
      ssnprintf (inst->ident.plugin_instance,
          sizeof (inst->ident.plugin_instance),
          "%s-%s-%s", tmp_plugin, tmp_plugin_instance, AGG_FUNC_PLACEHOLDER);
  }

  /* Type */
  sstrncpy (inst->ident.type, agg->ident.type, sizeof (inst->ident.type));

  /* Type instance */
  COPY_FIELD (inst->ident.type_instance, sizeof (inst->ident.type_instance),
      type_instance, LU_GROUP_BY_TYPE_INSTANCE, "");

#undef COPY_FIELD

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