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 */
Пример #2
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 */