예제 #1
0
void make_fake_data(data_set_t *dset, value_list_t *vl,
          const int data_source_type,
          const char *plugin, const char *plugin_instance,
          const char *type, const char *type_instance,
          value_t *values, int num_values)
{
    // initialize data_set_t 
    strcpy(dset->type, type);
    dset->ds_num = num_values;
    dset->ds = (data_source_t *)calloc(num_values, sizeof(data_source_t));

    // initialize data_source_t in data_set_t
    int i;
    for (i = 0; i < num_values; i++) {
        strcpy(dset->ds[i].name, "fake_data_source");
        dset->ds[i].type = data_source_type;
        dset->ds[i].min = 0.0;
        dset->ds[i].max = 65535.0;
    }

    vl->values = values;
    vl->values_len = num_values;
    vl->time = TIME_T_TO_CDTIME_T(0);
    vl->interval = TIME_T_TO_CDTIME_T(10);
    strcpy(vl->host, "localhost");
    strcpy(vl->plugin, plugin);
    strcpy(vl->plugin_instance, plugin_instance);
    strcpy(vl->type, type);
    strcpy(vl->type_instance, type_instance);
}
예제 #2
0
/* vcomplain returns 0 if it did not report, 1 else */
__attribute__((format(printf, 3, 0))) static int
vcomplain(int level, c_complain_t *c, const char *format, va_list ap) {
  cdtime_t now;
  char message[512];

  now = cdtime();

  if (c->last + c->interval > now)
    return 0;

  c->last = now;

  if (c->interval < plugin_get_interval())
    c->interval = plugin_get_interval();
  else
    c->interval *= 2;

  if (c->interval > TIME_T_TO_CDTIME_T(86400))
    c->interval = TIME_T_TO_CDTIME_T(86400);

  vsnprintf(message, sizeof(message), format, ap);
  message[sizeof(message) - 1] = '\0';

  plugin_log(level, "%s", message);
  return 1;
} /* vcomplain */
예제 #3
0
파일: gmond.c 프로젝트: boernd/collectd
static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
{
  switch (msg->id)
  {
    case gmetadata_full:
    {
      Ganglia_metadatadef msg_meta;
      staging_entry_t *se;
      const data_set_t *ds;
      metric_map_t *map;

      msg_meta = msg->Ganglia_metadata_msg_u.gfull;

      if (msg_meta.metric.tmax == 0)
        return (-1);

      map = metric_lookup (msg_meta.metric_id.name);
      if (map == NULL)
      {
        DEBUG ("gmond plugin: Not handling meta data %s.",
            msg_meta.metric_id.name);
        return (0);
      }

      ds = plugin_get_ds (map->type);
      if (ds == NULL)
      {
        WARNING ("gmond plugin: Could not find data set %s.", map->type);
        return (-1);
      }

      DEBUG ("gmond plugin: Received meta data for %s/%s.",
          msg_meta.metric_id.host, msg_meta.metric_id.name);

      pthread_mutex_lock (&staging_lock);
      se = staging_entry_get (msg_meta.metric_id.host,
          msg_meta.metric_id.name,
          map->type, map->type_instance,
          ds->ds_num);
      if (se != NULL)
        se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax);
      pthread_mutex_unlock (&staging_lock);

      if (se == NULL)
      {
        ERROR ("gmond plugin: staging_entry_get failed.");
        return (-1);
      }

      break;
    }

    default:
    {
      return (-1);
    }
  }

  return (0);
} /* }}} int mc_handle_metadata_msg */
예제 #4
0
/**
 * Read configuration.
 */
static int cgps_config (oconfig_item_t *ci)
{
  int i;

  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("Host", child->key) == 0)
      cf_util_get_string (child, &cgps_config_data.host);
    else if (strcasecmp ("Port", child->key) == 0)
      cf_util_get_service (child, &cgps_config_data.port);
    else if (strcasecmp ("Timeout", child->key) == 0)
      cf_util_get_cdtime (child, &cgps_config_data.timeout);
    else if (strcasecmp ("PauseConnect", child->key) == 0)
      cf_util_get_cdtime (child, &cgps_config_data.pause_connect);
    else
      WARNING ("gps plugin: Ignoring unknown config option \"%s\".", child->key);
  }

  // Controlling the value for timeout:
  // If set too high it blocks the reading (> 5 s), too low it gets not reading (< 500 us).
  // To avoid any issues we replace "out of range" value by the default value.
  if (
    cgps_config_data.timeout > TIME_T_TO_CDTIME_T(5)
    ||
    cgps_config_data.timeout < US_TO_CDTIME_T(500)
  ) 
  {
    WARNING ("gps plugin: timeout set to %.6f sec. setting to default (%.6f).", 
      CDTIME_T_TO_DOUBLE(cgps_config_data.timeout),
      CDTIME_T_TO_DOUBLE(CGPS_DEFAULT_TIMEOUT)
    );
    cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
  } 

  return (0);
}
예제 #5
0
static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
    char **ret_buffer,
    size_t *ret_buffer_size)
{
  int sd;
  int status;

  char temp[4096];
  char *buffer = NULL;
  size_t buffer_size = 0;

  struct sockaddr_un sa_unix;

  struct timeval stv_timeout;
  cdtime_t cdt_timeout;

  sd = socket (PF_UNIX, item->socktype, 0);
  if (sd < 0)
  {
    FUNC_ERROR ("socket");
    return (-1);
  }

  memset (&sa_unix, 0, sizeof (sa_unix));
  sa_unix.sun_family = AF_UNIX;
  sstrncpy (sa_unix.sun_path,
      (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
      sizeof (sa_unix.sun_path));

  status = unlink (sa_unix.sun_path);
  if ((status != 0) && (errno != ENOENT))
  {
    FUNC_ERROR ("unlink");
    close (sd);
    return (-1);
  }

  do /* while (0) */
  {
    /* We need to bind to a specific path, because this is a datagram socket
     * and otherwise the daemon cannot answer. */
    status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
    if (status != 0)
    {
      FUNC_ERROR ("bind");
      break;
    }

    /* Make the socket writeable by the daemon.. */
    status = chmod (sa_unix.sun_path, 0666);
    if (status != 0)
    {
      FUNC_ERROR ("chmod");
      break;
    }

    cdt_timeout = plugin_get_interval () * 3 / 4;
    if (cdt_timeout < TIME_T_TO_CDTIME_T (2))
      cdt_timeout = TIME_T_TO_CDTIME_T (2);

    CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout);

    status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout));
    if (status != 0)
    {
      FUNC_ERROR ("setsockopt");
      break;
    }

    status = connect (sd, (struct sockaddr *) &item->sockaddr,
        sizeof (item->sockaddr));
    if (status != 0)
    {
      FUNC_ERROR ("connect");
      break;
    }

    status = send (sd, item->command, strlen (item->command), 0);
    if (status < 0)
    {
      FUNC_ERROR ("send");
      break;
    }

    status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
    if (status < 0)
    {
      FUNC_ERROR ("recv");
      break;
    }
    buffer_size = status + 1;
    status = 0;
  } while (0);

  close (sd);
  unlink (sa_unix.sun_path);

  if (status != 0)
    return (-1);

  assert (buffer_size > 0);
  buffer = (char *) malloc (buffer_size);
  if (buffer == NULL)
  {
    FUNC_ERROR ("malloc");
    return (-1);
  }

  memcpy (buffer, temp, buffer_size - 1);
  buffer[buffer_size - 1] = 0;

  *ret_buffer = buffer;
  *ret_buffer_size = buffer_size;

  return (0);
} /* }}} int powerdns_get_data_dgram */
예제 #6
0
int uc_check_timeout (void)
{
  time_t now;
  cache_entry_t *ce;

  char **keys = NULL;
  int keys_len = 0;

  char *key;
  c_avl_iterator_t *iter;
  int i;
  
  pthread_mutex_lock (&cache_lock);

  now = time (NULL);

  /* Build a list of entries to be flushed */
  iter = c_avl_get_iterator (cache_tree);
  while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
  {
    /* If entry has not been updated, add to `keys' array */
    /* FIXME: Remove macro once "ce->interval" is of type cdtime_t. */
    if ((now - ce->last_update) >= TIME_T_TO_CDTIME_T (timeout_g * ce->interval))
    {
      char **tmp;

      tmp = (char **) realloc ((void *) keys,
	  (keys_len + 1) * sizeof (char *));
      if (tmp == NULL)
      {
	ERROR ("uc_check_timeout: realloc failed.");
	c_avl_iterator_destroy (iter);
	sfree (keys);
	pthread_mutex_unlock (&cache_lock);
	return (-1);
      }

      keys = tmp;
      keys[keys_len] = strdup (key);
      if (keys[keys_len] == NULL)
      {
	ERROR ("uc_check_timeout: strdup failed.");
	continue;
      }
      keys_len++;
    }
  } /* while (c_avl_iterator_next) */

  ce = NULL;

  for (i = 0; i < keys_len; i++)
  {
    int status;

    status = ut_check_interesting (keys[i]);

    if (status < 0)
    {
      ERROR ("uc_check_timeout: ut_check_interesting failed.");
      sfree (keys[i]);
      continue;
    }
    else if (status == 0) /* ``service'' is uninteresting */
    {
      DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
	  keys[i]);
      ce = NULL;
      status = c_avl_remove (cache_tree, keys[i],
	  (void *) &key, (void *) &ce);
      if (status != 0)
      {
	ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
      }
      sfree (keys[i]);
      sfree (key);
      if (ce != NULL)
        cache_free (ce);
      continue;
    }

    /* If we get here, the value is ``interesting''. Query the record from the
     * cache and update the state field. */
    if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
    {
      ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
      /* Do not free `keys[i]' so a notification is sent further down. */
      continue;
    }
    assert (ce != NULL);

    if (status == 2) /* persist */
    {
      DEBUG ("uc_check_timeout: %s is missing, sending notification.",
	  keys[i]);
      ce->state = STATE_MISSING;
      /* Do not free `keys[i]' so a notification is sent further down. */
    }
    else if (status == 1) /* do not persist */
    {
      if (ce->state == STATE_MISSING)
      {
	DEBUG ("uc_check_timeout: %s is missing but "
	    "notification has already been sent.",
	    keys[i]);
	/* Set `keys[i]' to NULL to no notification is sent. */
	sfree (keys[i]);
      }
      else /* (ce->state != STATE_MISSING) */
      {
	DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
	    keys[i]);
	ce->state = STATE_MISSING;
	/* Do not free `keys[i]' so a notification is sent further down. */
      }
    }
    else
    {
      WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
	  "invalid status %i.",
	  keys[i], status);
      sfree (keys[i]);
    }

    /* Make really sure the next iteration doesn't work with this pointer.
     * There have been too many bugs in the past.. :/  -- octo */
    ce = NULL;
  } /* for (keys[i]) */

  c_avl_iterator_destroy (iter);

  pthread_mutex_unlock (&cache_lock);

  for (i = 0; i < keys_len; i++)
  {
    if (keys[i] == NULL)
      continue;

    uc_send_notification (keys[i]);
    sfree (keys[i]);
  }

  sfree (keys);

  return (0);
} /* int uc_check_timeout */
예제 #7
0
cdtime_t plugin_get_interval (void)
{
  return TIME_T_TO_CDTIME_T (10);
}