Beispiel #1
0
static int ethstat_add_map(const oconfig_item_t *ci) /* {{{ */
{
  value_map_t *map;
  int status;
  char *key;

  if ((ci->values_num < 2) || (ci->values_num > 3) ||
      (ci->values[0].type != OCONFIG_TYPE_STRING) ||
      (ci->values[1].type != OCONFIG_TYPE_STRING) ||
      ((ci->values_num == 3) && (ci->values[2].type != OCONFIG_TYPE_STRING))) {
    ERROR("ethstat plugin: The %s option requires "
          "two or three string arguments.",
          ci->key);
    return -1;
  }

  key = strdup(ci->values[0].value.string);
  if (key == NULL) {
    ERROR("ethstat plugin: strdup(3) failed.");
    return ENOMEM;
  }

  map = calloc(1, sizeof(*map));
  if (map == NULL) {
    sfree(key);
    ERROR("ethstat plugin: calloc failed.");
    return ENOMEM;
  }

  sstrncpy(map->type, ci->values[1].value.string, sizeof(map->type));
  if (ci->values_num == 3)
    sstrncpy(map->type_instance, ci->values[2].value.string,
             sizeof(map->type_instance));

  if (value_map == NULL) {
    value_map = c_avl_create((int (*)(const void *, const void *))strcmp);
    if (value_map == NULL) {
      sfree(map);
      sfree(key);
      ERROR("ethstat plugin: c_avl_create() failed.");
      return -1;
    }
  }

  status = c_avl_insert(value_map,
                        /* key = */ key,
                        /* value = */ map);
  if (status != 0) {
    if (status > 0)
      ERROR("ethstat plugin: Multiple mappings for \"%s\".", key);
    else
      ERROR("ethstat plugin: c_avl_insert(\"%s\") failed.", key);

    sfree(map);
    sfree(key);
    return -1;
  }

  return 0;
} /* }}} int ethstat_add_map */
Beispiel #2
0
/* Must hold metrics_lock when calling this function. */
static statsd_metric_t *statsd_metric_lookup_unsafe(char const *name, /* {{{ */
                                                    metric_type_t type) {
  char key[DATA_MAX_NAME_LEN + 2];
  char *key_copy;
  statsd_metric_t *metric;
  int status;

  switch (type) {
  case STATSD_COUNTER:
    key[0] = 'c';
    break;
  case STATSD_TIMER:
    key[0] = 't';
    break;
  case STATSD_GAUGE:
    key[0] = 'g';
    break;
  case STATSD_SET:
    key[0] = 's';
    break;
  default:
    return NULL;
  }

  key[1] = ':';
  sstrncpy(&key[2], name, sizeof(key) - 2);

  status = c_avl_get(metrics_tree, key, (void *)&metric);
  if (status == 0)
    return metric;

  key_copy = strdup(key);
  if (key_copy == NULL) {
    ERROR("statsd plugin: strdup failed.");
    return NULL;
  }

  metric = calloc(1, sizeof(*metric));
  if (metric == NULL) {
    ERROR("statsd plugin: calloc failed.");
    sfree(key_copy);
    return NULL;
  }

  metric->type = type;
  metric->latency = NULL;
  metric->set = NULL;

  status = c_avl_insert(metrics_tree, key_copy, metric);
  if (status != 0) {
    ERROR("statsd plugin: c_avl_insert failed.");
    sfree(key_copy);
    sfree(metric);
    return NULL;
  }

  return metric;
} /* }}} statsd_metric_lookup_unsafe */
Beispiel #3
0
static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */
{
  value_map_t *map;
  int status;

  if ((ci->values_num < 2)
      || (ci->values_num > 3)
      || (ci->values[0].type != OCONFIG_TYPE_STRING)
      || (ci->values[1].type != OCONFIG_TYPE_STRING)
      || ((ci->values_num == 3)
        && (ci->values[2].type != OCONFIG_TYPE_STRING)))
  {
    ERROR ("ethstat plugin: The %s option requires "
        "two or three string arguments.", ci->key);
    return (-1);
  }

  map = malloc (sizeof (*map));
  if (map == NULL)
  {
    ERROR ("ethstat plugin: malloc(3) failed.");
    return (ENOMEM);
  }
  memset (map, 0, sizeof (*map));

  sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type));
  if (ci->values_num == 2)
    sstrncpy (map->type_instance, ci->values[2].value.string,
        sizeof (map->type_instance));

  if (value_map == NULL)
  {
    value_map = c_avl_create ((void *) strcmp);
    if (value_map == NULL)
    {
      sfree (map);
      ERROR ("ethstat plugin: c_avl_create() failed.");
      return (-1);
    }
  }

  status = c_avl_insert (value_map,
      /* key = */ ci->values[0].value.string,
      /* value = */ map);
  if (status != 0)
  {
    sfree (map);
    if (status > 0)
      ERROR ("ethstat plugin: Multiple mappings for \"%s\".",
          ci->values[0].value.string);
    else
      ERROR ("ethstat plugin: c_avl_insert(\"%s\") failed.",
          ci->values[0].value.string);
    return (-1);
  }

  return (0);
} /* }}} int ethstat_add_map */
Beispiel #4
0
/*
 * int ut_threshold_add
 *
 * Adds a threshold configuration to the list of thresholds. The threshold_t
 * structure is copied and may be destroyed after this call. Returns zero on
 * success, non-zero otherwise.
 */
static int ut_threshold_add(const threshold_t *th) { /* {{{ */
  char name[6 * DATA_MAX_NAME_LEN];
  char *name_copy;
  threshold_t *th_copy;
  threshold_t *th_ptr;
  int status = 0;

  if (format_name(name, sizeof(name), th->host, th->plugin, th->plugin_instance,
                  th->type, th->type_instance) != 0) {
    ERROR("ut_threshold_add: format_name failed.");
    return -1;
  }

  name_copy = strdup(name);
  if (name_copy == NULL) {
    ERROR("ut_threshold_add: strdup failed.");
    return -1;
  }

  th_copy = malloc(sizeof(*th_copy));
  if (th_copy == NULL) {
    sfree(name_copy);
    ERROR("ut_threshold_add: malloc failed.");
    return -1;
  }
  memcpy(th_copy, th, sizeof(threshold_t));

  DEBUG("ut_threshold_add: Adding entry `%s'", name);

  pthread_mutex_lock(&threshold_lock);

  th_ptr = threshold_get(th->host, th->plugin, th->plugin_instance, th->type,
                         th->type_instance);

  while ((th_ptr != NULL) && (th_ptr->next != NULL))
    th_ptr = th_ptr->next;

  if (th_ptr == NULL) /* no such threshold yet */
  {
    status = c_avl_insert(threshold_tree, name_copy, th_copy);
  } else /* th_ptr points to the last threshold in the list */
  {
    th_ptr->next = th_copy;
    /* name_copy isn't needed */
    sfree(name_copy);
  }

  pthread_mutex_unlock(&threshold_lock);

  if (status != 0) {
    ERROR("ut_threshold_add: c_avl_insert (%s) failed.", name);
    sfree(name_copy);
    sfree(th_copy);
  }

  return status;
} /* }}} int ut_threshold_add */
Beispiel #5
0
static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
    const char *name,
    const char *type, const char *type_instance,
    int values_len)
{
  char key[2 * DATA_MAX_NAME_LEN];
  staging_entry_t *se;
  int status;

  if (staging_tree == NULL)
    return (NULL);

  ssnprintf (key, sizeof (key), "%s/%s/%s", host, type,
      (type_instance != NULL) ? type_instance : "");

  se = NULL;
  status = c_avl_get (staging_tree, key, (void *) &se);
  if (status == 0)
    return (se);

  /* insert new entry */
  se = (staging_entry_t *) malloc (sizeof (*se));
  if (se == NULL)
    return (NULL);
  memset (se, 0, sizeof (*se));

  sstrncpy (se->key, key, sizeof (se->key));
  se->flags = 0;

  se->vl.values = (value_t *) calloc (values_len, sizeof (*se->vl.values));
  if (se->vl.values == NULL)
  {
    sfree (se);
    return (NULL);
  }
  se->vl.values_len = values_len;

  se->vl.time = 0;
  se->vl.interval = 0;
  sstrncpy (se->vl.host, host, sizeof (se->vl.host));
  sstrncpy (se->vl.plugin, "gmond", sizeof (se->vl.plugin));
  sstrncpy (se->vl.type, type, sizeof (se->vl.type));
  if (type_instance != NULL)
    sstrncpy (se->vl.type_instance, type_instance,
        sizeof (se->vl.type_instance));

  status = c_avl_insert (staging_tree, se->key, se);
  if (status != 0)
  {
    ERROR ("gmond plugin: c_avl_insert failed.");
    sfree (se->vl.values);
    sfree (se);
    return (NULL);
  }

  return (se);
} /* }}} staging_entry_t *staging_entry_get */
Beispiel #6
0
static int statsd_handle_set (statsd_config_t *conf, char const *name, /* {{{ */
    char const *set_key_orig)
{
  statsd_metric_t *metric = NULL;
  char *set_key;
  int status;


  pthread_mutex_lock (&conf->metrics_lock);

  metric = statsd_metric_lookup_unsafe (conf, name, STATSD_SET);
  if (metric == NULL)
  {
    pthread_mutex_unlock (&conf->metrics_lock);
    return (-1);
  }

  /* Make sure metric->set exists. */
  if (metric->set == NULL)
    metric->set = c_avl_create ((void *) strcmp);

  if (metric->set == NULL)
  {
    pthread_mutex_unlock (&conf->metrics_lock);
    ERROR ("statsd plugin: c_avl_create failed.");
    return (-1);
  }

  set_key = strdup (set_key_orig);
  if (set_key == NULL)
  {
    pthread_mutex_unlock (&conf->metrics_lock);
    ERROR ("statsd plugin: strdup failed.");
    return (-1);
  }

  status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
  if (status < 0)
  {
    pthread_mutex_unlock (&conf->metrics_lock);
    if (status < 0)
      ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
          set_key, status);
    sfree (set_key);
    return (-1);
  }
  else if (status > 0) /* key already exists */
  {
    sfree (set_key);
  }

  metric->updates_num++;

  pthread_mutex_unlock (&conf->metrics_lock);
  return (0);
} /* }}} int statsd_handle_set */
Beispiel #7
0
static int lu_add_by_plugin(by_type_entry_t *by_type, /* {{{ */
                            user_class_list_t *user_class_list) {
  user_class_list_t *ptr = NULL;
  identifier_match_t const *match = &user_class_list->entry.match;

  /* Lookup user_class_list from the per-plugin structure. If this is the first
   * user_class to be added, the block returns immediately. Otherwise they will
   * set "ptr" to non-NULL. */
  if (match->plugin.is_regex) {
    if (by_type->wildcard_plugin_list == NULL) {
      by_type->wildcard_plugin_list = user_class_list;
      return (0);
    }

    ptr = by_type->wildcard_plugin_list;
  }    /* if (plugin is wildcard) */
  else /* (plugin is not wildcard) */
  {
    int status;

    status =
        c_avl_get(by_type->by_plugin_tree, match->plugin.str, (void *)&ptr);

    if (status != 0) /* plugin not yet in tree */
    {
      char *plugin_copy = strdup(match->plugin.str);

      if (plugin_copy == NULL) {
        ERROR("utils_vl_lookup: strdup failed.");
        sfree(user_class_list);
        return (ENOMEM);
      }

      status =
          c_avl_insert(by_type->by_plugin_tree, plugin_copy, user_class_list);
      if (status != 0) {
        ERROR("utils_vl_lookup: c_avl_insert(\"%s\") failed with status %i.",
              plugin_copy, status);
        sfree(plugin_copy);
        sfree(user_class_list);
        return (status);
      } else {
        return (0);
      }
    } /* if (plugin not yet in tree) */
  }   /* if (plugin is not wildcard) */

  assert(ptr != NULL);

  while (ptr->next != NULL)
    ptr = ptr->next;
  ptr->next = user_class_list;

  return (0);
} /* }}} int lu_add_by_plugin */
Beispiel #8
0
static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */
    char const *type, _Bool allocate_if_missing)
{
  by_type_entry_t *by_type;
  char *type_copy;
  int status;

  status = c_avl_get (obj->by_type_tree, type, (void *) &by_type);
  if (status == 0)
    return (by_type);

  if (!allocate_if_missing)
    return (NULL);

  type_copy = strdup (type);
  if (type_copy == NULL)
  {
    ERROR ("utils_vl_lookup: strdup failed.");
    return (NULL);
  }

  by_type = malloc (sizeof (*by_type));
  if (by_type == NULL)
  {
    ERROR ("utils_vl_lookup: malloc failed.");
    sfree (type_copy);
    return (NULL);
  }
  memset (by_type, 0, sizeof (*by_type));
  by_type->wildcard_plugin_list = NULL;
  
  by_type->by_plugin_tree = c_avl_create ((void *) strcmp);
  if (by_type->by_plugin_tree == NULL)
  {
    ERROR ("utils_vl_lookup: c_avl_create failed.");
    sfree (by_type);
    sfree (type_copy);
    return (NULL);
  }

  status = c_avl_insert (obj->by_type_tree,
      /* key = */ type_copy, /* value = */ by_type);
  assert (status <= 0); /* >0 => entry exists => race condition. */
  if (status != 0)
  {
    ERROR ("utils_vl_lookup: c_avl_insert failed.");
    c_avl_destroy (by_type->by_plugin_tree);
    sfree (by_type);
    sfree (type_copy);
    return (NULL);
  }
  
  return (by_type);
} /* }}} by_type_entry_t *lu_search_by_type */
Beispiel #9
0
static zone_stats_t *zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid) {
  zone_stats_t *ret = NULL;
  zoneid_t *key = NULL;

  if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) {
    if (!(ret = malloc(sizeof(*ret)))) {
      WARNING("zone plugin: no memory");
      return (NULL);
    }
    if (!(key = malloc(sizeof(*key)))) {
      WARNING("zone plugin: no memory");
      free(ret);
      return (NULL);
    }
    *key = zoneid;
    if (c_avl_insert(tree, key, ret)) {
      WARNING("zone plugin: error inserting into tree");
      return (NULL);
    }
  }
  return (ret);
}
Beispiel #10
0
static int cj_config_add_key (cj_t *db, /* {{{ */
                              oconfig_item_t *ci)
{
    cj_key_t *key;
    int status;
    int i;

    if ((ci->values_num != 1)
            || (ci->values[0].type != OCONFIG_TYPE_STRING))
    {
        WARNING ("curl_json plugin: The `Key' block "
                 "needs exactly one string argument.");
        return (-1);
    }

    key = (cj_key_t *) malloc (sizeof (*key));
    if (key == NULL)
    {
        ERROR ("curl_json plugin: malloc failed.");
        return (-1);
    }
    memset (key, 0, sizeof (*key));
    key->magic = CJ_KEY_MAGIC;

    if (strcasecmp ("Key", ci->key) == 0)
    {
        status = cf_util_get_string (ci, &key->path);
        if (status != 0)
        {
            sfree (key);
            return (status);
        }
    }
    else
    {
        ERROR ("curl_json plugin: cj_config: "
               "Invalid key: %s", ci->key);
        return (-1);
    }

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

        if (strcasecmp ("Type", child->key) == 0)
            status = cf_util_get_string (child, &key->type);
        else if (strcasecmp ("Instance", child->key) == 0)
            status = cf_util_get_string (child, &key->instance);
        else
        {
            WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
            status = -1;
        }

        if (status != 0)
            break;
    } /* for (i = 0; i < ci->children_num; i++) */

    while (status == 0)
    {
        if (key->type == NULL)
        {
            WARNING ("curl_json plugin: `Type' missing in `Key' block.");
            status = -1;
        }

        break;
    } /* while (status == 0) */

    /* store path in a tree that will match the json map structure, example:
     * "httpd/requests/count",
     * "httpd/requests/current" ->
     * { "httpd": { "requests": { "count": $key, "current": $key } } }
     */
    if (status == 0)
    {
        char *ptr;
        char *name;
        char ent[PATH_MAX];
        c_avl_tree_t *tree;

        if (db->tree == NULL)
            db->tree = cj_avl_create();

        tree = db->tree;
        name = key->path;
        ptr = key->path;
        if (*ptr == '/')
            ++ptr;

        name = ptr;
        while (*ptr)
        {
            if (*ptr == '/')
            {
                c_avl_tree_t *value;
                int len;

                len = ptr-name;
                if (len == 0)
                    break;
                len = COUCH_MIN(len, sizeof (ent)-1);
                sstrncpy (ent, name, len+1);

                if (c_avl_get (tree, ent, (void *) &value) != 0)
                {
                    value = cj_avl_create ();
                    c_avl_insert (tree, strdup (ent), value);
                }

                tree = value;
                name = ptr+1;
            }
            ++ptr;
        }
        if (*name)
            c_avl_insert (tree, strdup(name), key);
        else
        {
            ERROR ("curl_json plugin: invalid key: %s", key->path);
            status = -1;
        }
    }

    return (status);
} /* }}} int cj_config_add_key */
static int
basic_aggregator_read_config_file_and_update_aggregator_definitions(char *filename) {
		oconfig_item_t *ci;
		int status = 0;
		int i;
		int nb_aggregators = 0;
		char *k;
		aggregator_definition_t *agg;

		ci = oconfig_parse_file (filename);
		if(NULL == ci) {
				WARNING (OUTPUT_PREFIX_STRING "Failed to read default config ('%s').", filename);
				return(-1);
		}

		/* Free the aggregator tree : it will be reconfigured from scratch from the file */
		if(NULL != aggregator) {
				while (c_avl_pick (aggregator, (void *)&k, (void *)&agg) == 0) {
						aggregator_definition_free(agg,1);
				}
				c_avl_destroy (aggregator);
		}
		aggregator = c_avl_create((void *) strcmp);
		if(NULL == aggregator) return (-1);

		/* Parse the configuration file */
		for (i = 0; i < ci->children_num; i++)
		{
				oconfig_item_t *child = ci->children + i;

				if (strcasecmp ("aggregator", child->key) == 0) {
						agg = basic_aggregator_config_aggregator (child);
						if(agg) {
								int r;
								r = c_avl_insert(aggregator, agg->resultvalue, agg);
								if(r != 0) {
										ERROR (OUTPUT_PREFIX_STRING "Could not insert aggregator '%s' in the list of aggregators (duplicate ?)",  agg->resultvalue);
										aggregator_definition_free(agg,1);
								} else {
										nb_aggregators++;
								}
						}
				} else if (strcasecmp ("database", child->key) == 0) {
						if ((child->values_num < 1) || (child->values[0].type != OCONFIG_TYPE_STRING)) {
								WARNING (OUTPUT_PREFIX_STRING "`database' needs 1 string arguments.");
								status = -1;
						} else {
								if (strcasecmp ("mysql", child->values[0].value.string) == 0) {
										/* Not implemented yet
										   basic_aggregator_config_mysql_database (child);
										   */
								} else if (strcasecmp ("postgresql", child->values[0].value.string) == 0) {

										/* Not supported yet */
								} else {
										WARNING (OUTPUT_PREFIX_STRING "'%s' is not a known type for `database'.", ci->values[0].value.string);
										status = -1;
								}
						}
				} else
						WARNING (OUTPUT_PREFIX_STRING "Option \"%s\" not allowed here.",
										child->key);
		}

		INFO(OUTPUT_PREFIX_STRING "Registered %d aggregators", nb_aggregators);
		return(status);
}
static int
basic_aggregator_update_aggregator(char *identifier, c_avl_tree_t *ds_data, aggregator_definition_t *agg) {
		char *identifier_copy;
		char *hostname;
		char *plugin;
		char *plugin_instance;
		char *type;
		char *type_instance;
		gauge_t *values;
		size_t values_num;
		int status;
		size_t i;
		const data_set_t *ds;

		/* parse_identifier() modifies its first argument,
		 * returning pointers into it */
		identifier_copy = sstrdup (identifier);

		status = parse_identifier (identifier_copy, &hostname,
						&plugin, &plugin_instance,
						&type, &type_instance);
		if (status != 0)
		{
			WARNING (OUTPUT_PREFIX_STRING "Cannot parse value `%s'.", identifier);
			WARNING (OUTPUT_PREFIX_STRING "Value `%s' is removed from the aggregator '%s'.", identifier, agg->resultvalue);
			sfree (identifier_copy);
			return(1);
		}

		ds = plugin_get_ds (type);
		if (ds == NULL)
		{
			WARNING (OUTPUT_PREFIX_STRING "plugin_get_ds (%s) == NULL;", type);
			WARNING (OUTPUT_PREFIX_STRING "Value `%s' is removed from the aggregator '%s'.", identifier, agg->resultvalue);
			sfree (identifier_copy);
			return(1);
		}

		values = NULL;
		values_num = 0;
		status = uc_get_rate_by_name (identifier, &values, &values_num);
		if (status != 0)
		{
			DEBUG(OUTPUT_PREFIX_STRING "uc_get_rate_by_name failed for %s", identifier);
			sfree (identifier_copy);
			return(2);
		}

		if ((size_t) ds->ds_num != values_num)
		{
			ERROR ("ds[%s]->ds_num = %i, "
							"but uc_get_rate_by_name returned %u values.",
							ds->type, ds->ds_num, (unsigned int) values_num);
			sfree (values);
			sfree (identifier_copy);
			return (-1);
		}

		for (i = 0; i < values_num; i++)
		{
			if ( ! isnan (values[i]))
			{
				char *k;
				c_avl_tree_t *t;
				value_and_nb_t *v;

				if(0 != c_avl_get(ds_data, type_instance?type_instance:"",(void*)&t)) {
					if(NULL == (t = c_avl_create((void *) strcmp))) {
						ERROR (OUTPUT_PREFIX_STRING "Could not allocate memory for tree");
						sfree (identifier_copy);
						return(-1);

					}
					if(NULL == (k=strdup(type_instance?type_instance:""))) {
						ERROR (OUTPUT_PREFIX_STRING "Could not allocate memory");
						c_avl_destroy(t);
						sfree (identifier_copy);
						return(-1);
					}
					if(0 != c_avl_insert(ds_data, k,t)) {
						ERROR (OUTPUT_PREFIX_STRING "Could insert data into AVL tree");
						c_avl_destroy(t);
						sfree (identifier_copy);
						return(-1);
					}
				}
				if(0 != c_avl_get(t, ds->ds[i].name,(void*)&v)) {
					if(NULL == (k=strdup(ds->ds[i].name))) {
						ERROR (OUTPUT_PREFIX_STRING "Could not allocate memory");
						sfree (identifier_copy);
						return(-1);
					}
					if(NULL == (v=malloc(sizeof(*v)))) {
						ERROR (OUTPUT_PREFIX_STRING "Could not allocate memory");
						sfree (identifier_copy);
						return(-1);
					}
					v->val=0;
					v->nb=0;
					if(0 != c_avl_insert(t, k,v)) {
						ERROR (OUTPUT_PREFIX_STRING "Could insert data into AVL tree");
						sfree (identifier_copy);
						return(-1);
					}
				}
				v->val += values[i];
				v->nb +=1;
#ifdef DEBUG_THIS
				if(values[i] > 1000) {

INFO(OUTPUT_PREFIX_STRING "DEBUG : ATTENTION '%s/%s-%s/%s%s%s DS '%s'=%12e", hostname, plugin, plugin_instance, type,
	                                                               (type_instance && type_instance[0])?"-":"",
																   type_instance?type_instance:"(null)",
																   ds->ds[i].name, values[i]
																   );
				}
#endif

			}
		}

		sfree (values);
		sfree (identifier_copy);
		return(status);
}
static int
instances_of_types_tree_update (void) {
	char **names = NULL;
	cdtime_t *times = NULL;
	size_t number = 0;
	int i;
	int status = 0;

	if(NULL == instances_of_types_tree) {
		instances_of_types_tree = c_avl_create ((void *) strcmp);
	}

	status = uc_get_names (&names, &times, &number);
	if (status != 0)
	{
			size_t j; 
			DEBUG (OUTPUT_PREFIX_STRING "uc_get_names failed with status %i", status);
			for (j = 0; j < number; j++) { 
					sfree(names[j]); 
			} 
			sfree(names); 
			sfree(times); 
			return(status);
	}

	if(number == 0) {
		return(0);
	}
	pthread_mutex_lock (&instances_of_types_mutex);
	for(i=0; i<number; i++) {
		char *type;
		int l1,l2;
		int pos =0;
		instances_list_t *v;
		char *type_instance;
		int type_instance_is_missing = 1;

		for(l1=0; names[i][l1] && (pos < 2); l1++) {
			if(names[i][l1] == '/') pos++;
		}
		if(names[i][l1] == '\0') {
			sfree(names[i]);
			continue;
		}
		l2 = l1;
		while(names[i][l2] && (names[i][l2] != '-')) l2++;
		if(names[i][l2] == '\0') {
			sfree(names[i]);
			continue;
		}
		type = names[i]+l1;
		names[i][l2] = '\0';
		type_instance = names[i]+l2+1;


		if(0 == c_avl_get(instances_of_types_tree, type,(void*)&v)) {
			int i;
			for(i=0; v->instance[i]; i++) {
				if(0 == strcmp(v->instance[i], type_instance)) {
					type_instance_is_missing = 0;
					break;
				}
			}
			if(type_instance_is_missing) {
				if(i >= v->nb) {
					v->nb += 128;
					if(NULL == (v->instance = realloc(v->instance, v->nb*sizeof(*(v->instance))))) {
						ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
						pthread_mutex_unlock (&instances_of_types_mutex);
						return(-1);
					}
					}
				v->instance[i+1] = NULL;
				if(NULL == (v->instance[i] = sstrdup(type_instance))) {
					ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
					pthread_mutex_unlock (&instances_of_types_mutex);
					return(-1);
				}
			}
		} else {
			char *k;
			if(NULL == (v = malloc(sizeof(*v)))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			v->nb = 128;
			if(NULL == (v->instance = malloc(v->nb*sizeof(*(v->instance))))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			if(NULL == (k = sstrdup(type))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			if(NULL == (v->instance[0] = sstrdup(type_instance))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			v->instance[1] = NULL;
			if(0 != c_avl_insert(instances_of_types_tree, k,v)) {
				ERROR (OUTPUT_PREFIX_STRING "Could insert data into AVL tree");
				sfree(v->instance[0]);
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
		}
		sfree(names[i]); 
	}
	pthread_mutex_unlock (&instances_of_types_mutex);
	sfree(names); 
	sfree(times); 
	return(0);
}
Beispiel #14
0
static int uc_insert (const data_set_t *ds, const value_list_t *vl,
    const char *key)
{
  int i;
  char *key_copy;
  cache_entry_t *ce;

  /* `cache_lock' has been locked by `uc_update' */

  key_copy = strdup (key);
  if (key_copy == NULL)
  {
    ERROR ("uc_insert: strdup failed.");
    return (-1);
  }

  ce = cache_alloc (ds->ds_num);
  if (ce == NULL)
  {
    sfree (key_copy);
    ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
    return (-1);
  }

  sstrncpy (ce->name, key, sizeof (ce->name));

  for (i = 0; i < ds->ds_num; i++)
  {
    switch (ds->ds[i].type)
    {
      case DS_TYPE_COUNTER:
	ce->values_gauge[i] = NAN;
	ce->values_raw[i].counter = vl->values[i].counter;
	break;

      case DS_TYPE_GAUGE:
	ce->values_gauge[i] = vl->values[i].gauge;
	ce->values_raw[i].gauge = vl->values[i].gauge;
	break;

      case DS_TYPE_DERIVE:
	ce->values_gauge[i] = NAN;
	ce->values_raw[i].derive = vl->values[i].derive;
	break;

      case DS_TYPE_ABSOLUTE:
	ce->values_gauge[i] = NAN;
	if (vl->interval > 0)
	  ce->values_gauge[i] = ((double) vl->values[i].absolute)
	    / ((double) vl->interval);
	ce->values_raw[i].absolute = vl->values[i].absolute;
	break;
	
      default:
	/* This shouldn't happen. */
	ERROR ("uc_insert: Don't know how to handle data source type %i.",
	    ds->ds[i].type);
	return (-1);
    } /* switch (ds->ds[i].type) */
  } /* for (i) */

  /* Prune invalid gauge data */
  uc_check_range (ds, ce);

  ce->last_time = vl->time;
  ce->last_update = time (NULL);
  ce->interval = vl->interval;
  ce->state = STATE_OKAY;

  if (c_avl_insert (cache_tree, key_copy, ce) != 0)
  {
    sfree (key_copy);
    ERROR ("uc_insert: c_avl_insert failed.");
    return (-1);
  }

  DEBUG ("uc_insert: Added %s to the cache.", key);
  return (0);
} /* int uc_insert */
Beispiel #15
0
static int fbh_read_file (fbhash_t *h) /* {{{ */
{
  FILE *fh;
  char buffer[4096];
  struct flock fl;
  c_avl_tree_t *tree;
  int status;

  fh = fopen (h->filename, "r");
  if (fh == NULL)
    return (-1);

  memset (&fl, 0, sizeof (fl));
  fl.l_type = F_RDLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0; /* == entire file */
  /* TODO: Lock file? -> fcntl */

  status = fcntl (fileno (fh), F_SETLK, &fl);
  if (status != 0)
  {
    fclose (fh);
    return (-1);
  }

  tree = c_avl_create ((void *) strcmp);
  if (tree == NULL)
  {
    fclose (fh);
    return (-1);
  }

  /* Read `fh' into `tree' */
  while (fgets (buffer, sizeof (buffer), fh) != NULL) /* {{{ */
  {
    size_t len;
    char *key;
    char *value;

    char *key_copy;
    char *value_copy;

    buffer[sizeof (buffer) - 1] = 0;
    len = strlen (buffer);

    /* Remove trailing newline characters. */
    while ((len > 0)
        && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
    {
      len--;
      buffer[len] = 0;
    }

    /* Seek first non-space character */
    key = buffer;
    while ((*key != 0) && isspace ((int) *key))
      key++;

    /* Skip empty lines and comments */
    if ((key[0] == 0) || (key[0] == '#'))
      continue;

    /* Seek first colon */
    value = strchr (key, ':');
    if (value == NULL)
      continue;

    /* Null-terminate `key'. */
    *value = 0;
    value++;

    /* Skip leading whitespace */
    while ((*value != 0) && isspace ((int) *value))
      value++;

    /* Skip lines without value */
    if (value[0] == 0)
      continue;

    key_copy = strdup (key);
    value_copy = strdup (value);

    if ((key_copy == NULL) || (value_copy == NULL))
    {
      free (key_copy);
      free (value_copy);
      continue;
    }

    status = c_avl_insert (tree, key_copy, value_copy);
    if (status != 0)
    {
      free (key_copy);
      free (value_copy);
      continue;
    }

    DEBUG ("utils_fbhash: fbh_read_file: key = %s; value = %s;",
        key, value);
  } /* }}} while (fgets) */

  fclose (fh);

  fbh_free_tree (h->tree);
  h->tree = tree;

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