コード例 #1
0
ファイル: statsd.c プロジェクト: maniacs-ops/collectd
static int statsd_config (oconfig_item_t *ci) /* {{{ */
{
  for (int 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, &conf_node);
    else if (strcasecmp ("Port", child->key) == 0)
      cf_util_get_service (child, &conf_service);
    else if (strcasecmp ("DeleteCounters", child->key) == 0)
      cf_util_get_boolean (child, &conf_delete_counters);
    else if (strcasecmp ("DeleteTimers", child->key) == 0)
      cf_util_get_boolean (child, &conf_delete_timers);
    else if (strcasecmp ("DeleteGauges", child->key) == 0)
      cf_util_get_boolean (child, &conf_delete_gauges);
    else if (strcasecmp ("DeleteSets", child->key) == 0)
      cf_util_get_boolean (child, &conf_delete_sets);
    else if (strcasecmp ("CounterSum", child->key) == 0)
      cf_util_get_boolean (child, &conf_counter_sum);
    else if (strcasecmp ("TimerLower", child->key) == 0)
      cf_util_get_boolean (child, &conf_timer_lower);
    else if (strcasecmp ("TimerUpper", child->key) == 0)
      cf_util_get_boolean (child, &conf_timer_upper);
    else if (strcasecmp ("TimerSum", child->key) == 0)
      cf_util_get_boolean (child, &conf_timer_sum);
    else if (strcasecmp ("TimerCount", child->key) == 0)
      cf_util_get_boolean (child, &conf_timer_count);
    else if (strcasecmp ("TimerPercentile", child->key) == 0)
      statsd_config_timer_percentile (child);
    else
      ERROR ("statsd plugin: The \"%s\" config option is not valid.",
          child->key);
  }

  return (0);
} /* }}} int statsd_config */
コード例 #2
0
ファイル: apcups.c プロジェクト: strizhechenko/collectd
static int apcups_config (oconfig_item_t *ci)
{
	int i;
	_Bool persistent_conn_set = 0;

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

		if (strcasecmp (child->key, "Host") == 0)
			cf_util_get_string (child, &conf_node);
		else if (strcasecmp (child->key, "Port") == 0)
			cf_util_get_service (child, &conf_service);
		else if (strcasecmp (child->key, "ReportSeconds") == 0)
			cf_util_get_boolean (child, &conf_report_seconds);
		else if (strcasecmp (child->key, "PersistentConnection") == 0) {
			cf_util_get_boolean (child, &conf_persistent_conn);
			persistent_conn_set = 1;
		}
		else
			ERROR ("apcups plugin: Unknown config option \"%s\".", child->key);
	}

	if (!persistent_conn_set) {
		double interval = CDTIME_T_TO_DOUBLE(plugin_get_interval());
		if (interval > APCUPS_SERVER_TIMEOUT) {
			NOTICE ("apcups plugin: Plugin poll interval set to %.3f seconds. "
				"Apcupsd NIS socket timeout is %.3f seconds, "
				"PersistentConnection disabled by default.",
				interval, APCUPS_SERVER_TIMEOUT);
			conf_persistent_conn = 0;
		}
	}

	return (0);
} /* int apcups_config */
コード例 #3
0
ファイル: dbi.c プロジェクト: 4thAce/collectd
static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
{
  cdbi_database_t *db;
  int status;
  int i;

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

  db = calloc (1, sizeof (*db));
  if (db == NULL)
  {
    ERROR ("dbi plugin: calloc failed.");
    return (-1);
  }

  status = cf_util_get_string (ci, &db->name);
  if (status != 0)
  {
    sfree (db);
    return (status);
  }

  /* Fill the `cdbi_database_t' structure.. */
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("Driver", child->key) == 0)
      status = cf_util_get_string (child, &db->driver);
    else if (strcasecmp ("DriverOption", child->key) == 0)
      status = cdbi_config_add_database_driver_option (db, child);
    else if (strcasecmp ("SelectDB", child->key) == 0)
      status = cf_util_get_string (child, &db->select_db);
    else if (strcasecmp ("Query", child->key) == 0)
      status = udb_query_pick_from_list (child, queries, queries_num,
          &db->queries, &db->queries_num);
    else if (strcasecmp ("Host", child->key) == 0)
      status = cf_util_get_string (child, &db->host);
    else if (strcasecmp ("Interval", child->key) == 0)
      status = cf_util_get_cdtime(child, &db->interval);
    else
    {
      WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  /* Check that all necessary options have been given. */
  while (status == 0)
  {
    if (db->driver == NULL)
    {
      WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
      status = -1;
    }
    if (db->driver_options_num == 0)
    {
      WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
          "This will likely not work.", db->name);
    }

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

  while ((status == 0) && (db->queries_num > 0))
  {
    size_t j;

    db->q_prep_areas = calloc (db->queries_num, sizeof (*db->q_prep_areas));
    if (db->q_prep_areas == NULL)
    {
      WARNING ("dbi plugin: calloc failed");
      status = -1;
      break;
    }

    for (j = 0; j < db->queries_num; ++j)
    {
      db->q_prep_areas[j]
        = udb_query_allocate_preparation_area (db->queries[j]);

      if (db->q_prep_areas[j] == NULL)
      {
        WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
        status = -1;
        break;
      }
    }

    break;
  }

  /* If all went well, add this database to the global list of databases. */
  if (status == 0)
  {
    cdbi_database_t **temp;

    temp = realloc (databases,
        sizeof (*databases) * (databases_num + 1));
    if (temp == NULL)
    {
      ERROR ("dbi plugin: realloc failed");
      status = -1;
    }
    else
    {
      user_data_t ud;
      char *name = NULL;

      databases = temp;
      databases[databases_num] = db;
      databases_num++;

      memset (&ud, 0, sizeof (ud));
      ud.data = (void *) db;
      ud.free_func = NULL;
      name = ssnprintf_alloc("dbi:%s", db->name);

      plugin_register_complex_read (/* group = */ NULL,
          /* name = */ name ? name : db->name,
          /* callback = */ cdbi_read_database,
          /* interval = */ (db->interval > 0) ? db->interval : 0,
          /* user_data = */ &ud);
      free (name);
    }
  }

  if (status != 0)
  {
    cdbi_database_free (db);
    return (-1);
  }

  return (0);
} /* }}} int cdbi_config_add_database */
コード例 #4
0
ファイル: snmp.c プロジェクト: kmiku7/collectd
static int csnmp_config_add_data (oconfig_item_t *ci)
{
  data_definition_t *dd;
  int status = 0;
  int i;

  dd = (data_definition_t *) malloc (sizeof (data_definition_t));
  if (dd == NULL)
    return (-1);
  memset (dd, '\0', sizeof (data_definition_t));

  status = cf_util_get_string(ci, &dd->name);
  if (status != 0)
  {
    free (dd);
    return (-1);
  }

  dd->scale = 1.0;
  dd->shift = 0.0;

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

    if (strcasecmp ("Type", option->key) == 0)
      status = cf_util_get_string(option, &dd->type);
    else if (strcasecmp ("Table", option->key) == 0)
      status = cf_util_get_boolean(option, &dd->is_table);
    else if (strcasecmp ("Instance", option->key) == 0)
      status = csnmp_config_add_data_instance (dd, option);
    else if (strcasecmp ("InstancePrefix", option->key) == 0)
      status = csnmp_config_add_data_instance_prefix (dd, option);
    else if (strcasecmp ("Values", option->key) == 0)
      status = csnmp_config_add_data_values (dd, option);
    else if (strcasecmp ("Shift", option->key) == 0)
      status = cf_util_get_double(option, &dd->shift);
    else if (strcasecmp ("Scale", option->key) == 0)
      status = cf_util_get_double(option, &dd->scale);
    else if (strcasecmp ("Ignore", option->key) == 0)
      status = csnmp_config_add_data_blacklist(dd, option);
    else if (strcasecmp ("InvertMatch", option->key) == 0)
      status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
    else
    {
      WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
      status = -1;
    }

    if (status != 0)
      break;
  } /* for (ci->children) */

  while (status == 0)
  {
    if (dd->type == NULL)
    {
      WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
      status = -1;
      break;
    }
    if (dd->values == NULL)
    {
      WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
      status = -1;
      break;
    }

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

  if (status != 0)
  {
    sfree (dd->name);
    sfree (dd->instance_prefix);
    sfree (dd->values);
    sfree (dd->ignores);
    sfree (dd);
    return (-1);
  }

  DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %zu }",
      dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);

  if (data_head == NULL)
    data_head = dd;
  else
  {
    data_definition_t *last;
    last = data_head;
    while (last->next != NULL)
      last = last->next;
    last->next = dd;
  }

  return (0);
} /* int csnmp_config_add_data */
コード例 #5
0
ファイル: write_kafka.c プロジェクト: adanin/collectd
static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
{
    int                          status;
    int                          i;
    struct kafka_topic_context  *tctx;
    char                        *key;
    char                        *val;
    char                         callback_name[DATA_MAX_NAME_LEN];
    char                         errbuf[1024];
    user_data_t                  ud;
	oconfig_item_t              *child;
    rd_kafka_conf_res_t          ret;

	if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
		ERROR ("write_kafka plugin: calloc failed.");
        return;
	}

    tctx->escape_char = '.';
    tctx->store_rates = 1;

    rd_kafka_conf_set_log_cb(conf, kafka_log);
    if ((tctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
                                    errbuf, sizeof(errbuf))) == NULL) {
        sfree(tctx);
        ERROR("write_kafka plugin: cannot create kafka handle.");
        return;
    }
    conf = NULL;

    if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
        rd_kafka_destroy(tctx->kafka);
        sfree(tctx);
        ERROR ("write_kafka plugin: cannot create topic configuration.");
        return;
    }

    if (ci->values_num != 1) {
        WARNING("kafka topic name needed.");
        goto errout;
    }

    if (ci->values[0].type != OCONFIG_TYPE_STRING) {
        WARNING("kafka topic needs a string argument.");
        goto errout;
    }

    if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
        ERROR("write_kafka plugin: cannot copy topic name.");
        goto errout;
    }

	for (i = 0; i < ci->children_num; i++) {
		/*
		 * The code here could be simplified but makes room
		 * for easy adding of new options later on.
		 */
		child = &ci->children[i];
		status = 0;

		if (strcasecmp ("Property", child->key) == 0) {
			if (child->values_num != 2) {
				WARNING("kafka properties need both a key and a value.");
                goto errout;
			}
			if (child->values[0].type != OCONFIG_TYPE_STRING ||
			    child->values[1].type != OCONFIG_TYPE_STRING) {
				WARNING("kafka properties needs string arguments.");
                goto errout;
			}
            key = child->values[0].value.string;
            val = child->values[0].value.string;
            ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
                                          errbuf, sizeof(errbuf));
            if (ret != RD_KAFKA_CONF_OK) {
				WARNING("cannot set kafka topic property %s to %s: %s.",
                        key, val, errbuf);
                goto errout;
			}

        } else if (strcasecmp ("Key", child->key) == 0)  {
            char *tmp_buf = NULL;
            status = cf_util_get_string(child, &tmp_buf);
            if (status != 0) {
                WARNING("write_kafka plugin: invalid key supplied");
                break;
            }

            if (strcasecmp(tmp_buf, "Random") != 0) {
                tctx->has_key = 1;
                tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
            }
            sfree(tmp_buf);

        } else if (strcasecmp ("Format", child->key) == 0) {
            status = cf_util_get_string(child, &key);
            if (status != 0)
                goto errout;

            assert(key != NULL);

            if (strcasecmp(key, "Command") == 0) {

                tctx->format = KAFKA_FORMAT_COMMAND;

            } else if (strcasecmp(key, "Graphite") == 0) {
                tctx->format = KAFKA_FORMAT_GRAPHITE;

            } else if (strcasecmp(key, "Json") == 0) {
                tctx->format = KAFKA_FORMAT_JSON;

            } else {
                WARNING ("write_kafka plugin: Invalid format string: %s",
                         key);
            }
            sfree(key);

        } else if (strcasecmp ("StoreRates", child->key) == 0) {
            status = cf_util_get_boolean (child, &tctx->store_rates);
            (void) cf_util_get_flag (child, &tctx->graphite_flags,
                                     GRAPHITE_STORE_RATES);

        } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
            status = cf_util_get_flag (child, &tctx->graphite_flags,
                                       GRAPHITE_SEPARATE_INSTANCES);

        } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
            status = cf_util_get_flag (child, &tctx->graphite_flags,
                                       GRAPHITE_ALWAYS_APPEND_DS);

        } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
            status = cf_util_get_string (child, &tctx->prefix);
        } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
            status = cf_util_get_string (child, &tctx->postfix);
        } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
            char *tmp_buff = NULL;
            status = cf_util_get_string (child, &tmp_buff);
            if (strlen (tmp_buff) > 1)
                WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
                        "only one character. Others will be ignored.");
            tctx->escape_char = tmp_buff[0];
            sfree (tmp_buff);
        } else {
            WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
        }

        if (status != 0)
            break;
    }

    rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
    rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);

    if ((tctx->topic = rd_kafka_topic_new(tctx->kafka, tctx->topic_name,
                                       tctx->conf)) == NULL) {
        ERROR("write_kafka plugin: cannot create topic.");
        goto errout;
    }
    tctx->conf = NULL;

    ssnprintf(callback_name, sizeof(callback_name),
              "write_kafka/%s", tctx->topic_name);

    ud.data = tctx;
    ud.free_func = kafka_topic_context_free;

	status = plugin_register_write (callback_name, kafka_write, &ud);
	if (status != 0) {
		WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
				"failed with status %i.",
				callback_name, status);
        goto errout;
    }
    return;
 errout:
    if (conf != NULL)
        rd_kafka_conf_destroy(conf);
    if (tctx->kafka != NULL)
        rd_kafka_destroy(tctx->kafka);
    if (tctx->topic != NULL)
        rd_kafka_topic_destroy(tctx->topic);
    if (tctx->topic_name != NULL)
        free(tctx->topic_name);
    if (tctx->conf != NULL)
        rd_kafka_topic_conf_destroy(tctx->conf);
    sfree(tctx);
} /* }}} int kafka_config_topic */
コード例 #6
0
ファイル: rrdcached.c プロジェクト: 01BTC10/collectd
static int rc_config (oconfig_item_t *ci)
{
  int i;

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

    if (strcasecmp ("DataDir", key) == 0)
    {
      status = cf_util_get_string (child, &datadir);
      if (status == 0)
      {
        int len = strlen (datadir);

        while ((len > 0) && (datadir[len - 1] == '/'))
        {
          len--;
          datadir[len] = 0;
        }

        if (len <= 0)
          sfree (datadir);
      }
    }
    else if (strcasecmp ("DaemonAddress", key) == 0)
      status = cf_util_get_string (child, &daemon_address);
    else if (strcasecmp ("CreateFiles", key) == 0)
      status = cf_util_get_boolean (child, &config_create_files);
    else if (strcasecmp ("CreateFilesAsync", key) == 0)
      status = cf_util_get_boolean (child, &rrdcreate_config.async);
    else if (strcasecmp ("CollectStatistics", key) == 0)
      status = cf_util_get_boolean (child, &config_collect_stats);
    else if (strcasecmp ("StepSize", key) == 0)
    {
      int tmp = -1;

      status = rc_config_get_int_positive (child, &tmp);
      if (status == 0)
        rrdcreate_config.stepsize = (unsigned long) tmp;
    }
    else if (strcasecmp ("HeartBeat", key) == 0)
      status = rc_config_get_int_positive (child, &rrdcreate_config.heartbeat);
    else if (strcasecmp ("RRARows", key) == 0)
      status = rc_config_get_int_positive (child, &rrdcreate_config.rrarows);
    else if (strcasecmp ("RRATimespan", key) == 0)
    {
      int tmp = -1;
      status = rc_config_get_int_positive (child, &tmp);
      if (status == 0)
        status = rc_config_add_timespan (tmp);
    }
    else if (strcasecmp ("XFF", key) == 0)
      status = rc_config_get_xff (child, &rrdcreate_config.xff);
    else
    {
      WARNING ("rrdcached plugin: Ignoring invalid option %s.", key);
      continue;
    }

    if (status != 0)
      WARNING ("rrdcached plugin: Handling the \"%s\" option failed.", key);
  }

  if (daemon_address != NULL)
  {
    plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
    plugin_register_flush ("rrdcached", rc_flush, /* user_data = */ NULL);
  }
  return (0);
} /* int rc_config */
コード例 #7
0
ファイル: curl.c プロジェクト: NymbleTechnology/collectd
static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
{
  web_page_t *page;
  int status;
  int i;

  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
  {
    WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
    return (-1);
  }

  page = calloc (1, sizeof (*page));
  if (page == NULL)
  {
    ERROR ("curl plugin: calloc failed.");
    return (-1);
  }
  page->url = NULL;
  page->user = NULL;
  page->pass = NULL;
  page->digest = 0;
  page->verify_peer = 1;
  page->verify_host = 1;
  page->response_time = 0;
  page->response_code = 0;
  page->timeout = -1;

  page->instance = strdup (ci->values[0].value.string);
  if (page->instance == NULL)
  {
    ERROR ("curl plugin: strdup failed.");
    sfree (page);
    return (-1);
  }

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

    if (strcasecmp ("URL", child->key) == 0)
      status = cf_util_get_string (child, &page->url);
    else if (strcasecmp ("User", child->key) == 0)
      status = cf_util_get_string (child, &page->user);
    else if (strcasecmp ("Password", child->key) == 0)
      status = cf_util_get_string (child, &page->pass);
    else if (strcasecmp ("Digest", child->key) == 0)
      status = cf_util_get_boolean (child, &page->digest);
    else if (strcasecmp ("VerifyPeer", child->key) == 0)
      status = cf_util_get_boolean (child, &page->verify_peer);
    else if (strcasecmp ("VerifyHost", child->key) == 0)
      status = cf_util_get_boolean (child, &page->verify_host);
    else if (strcasecmp ("MeasureResponseTime", child->key) == 0)
      status = cf_util_get_boolean (child, &page->response_time);
    else if (strcasecmp ("MeasureResponseCode", child->key) == 0)
      status = cf_util_get_boolean (child, &page->response_code);
    else if (strcasecmp ("CACert", child->key) == 0)
      status = cf_util_get_string (child, &page->cacert);
    else if (strcasecmp ("Match", child->key) == 0)
      /* Be liberal with failing matches => don't set `status'. */
      cc_config_add_match (page, child);
    else if (strcasecmp ("Header", child->key) == 0)
      status = cc_config_append_string ("Header", &page->headers, child);
    else if (strcasecmp ("Post", child->key) == 0)
      status = cf_util_get_string (child, &page->post_body);
    else if (strcasecmp ("Timeout", child->key) == 0)
      status = cf_util_get_int (child, &page->timeout);
    else
    {
      WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

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

  /* Additionial sanity checks and libCURL initialization. */
  while (status == 0)
  {
    if (page->url == NULL)
    {
      WARNING ("curl plugin: `URL' missing in `Page' block.");
      status = -1;
    }

    if (page->matches == NULL && !page->response_time && !page->response_code)
    {
      assert (page->instance != NULL);
      WARNING ("curl plugin: No (valid) `Match' block "
          "or MeasureResponseTime or MeasureResponseCode within "
          "`Page' block `%s'.", page->instance);
      status = -1;
    }

    if (status == 0)
      status = cc_page_init_curl (page);

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

  if (status != 0)
  {
    cc_web_page_free (page);
    return (status);
  }

  /* Add the new page to the linked list */
  if (pages_g == NULL)
    pages_g = page;
  else
  {
    web_page_t *prev;

    prev = pages_g;
    while (prev->next != NULL)
      prev = prev->next;
    prev->next = page;
  }

  return (0);
} /* }}} int cc_config_add_page */
コード例 #8
0
ファイル: curl_json.c プロジェクト: karcaw/collectd
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 */
コード例 #9
0
ファイル: oracle.c プロジェクト: strizhechenko/collectd
static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
{
  o_database_t *db;
  int status;
  int i;

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

  db = calloc (1, sizeof (*db));
  if (db == NULL)
  {
    ERROR ("oracle plugin: calloc failed.");
    return (-1);
  }
  db->name = NULL;
  db->host = NULL;
  db->connect_id = NULL;
  db->username = NULL;
  db->password = NULL;

  status = cf_util_get_string (ci, &db->name);
  if (status != 0)
  {
    sfree (db);
    return (status);
  }

  /* Fill the `o_database_t' structure.. */
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("ConnectID", child->key) == 0)
      status = cf_util_get_string (child, &db->connect_id);
    else if (strcasecmp ("Host", child->key) == 0)
      status = cf_util_get_string (child, &db->host);
    else if (strcasecmp ("Username", child->key) == 0)
      status = cf_util_get_string (child, &db->username);
    else if (strcasecmp ("Password", child->key) == 0)
      status = cf_util_get_string (child, &db->password);
    else if (strcasecmp ("Query", child->key) == 0)
      status = udb_query_pick_from_list (child, queries, queries_num,
          &db->queries, &db->queries_num);
    else
    {
      WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  /* Check that all necessary options have been given. */
  while (status == 0)
  {
    if (db->connect_id == NULL)
    {
      WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
      status = -1;
    }
    if (db->username == NULL)
    {
      WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
      status = -1;
    }
    if (db->password == NULL)
    {
      WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
      status = -1;
    }

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

  while ((status == 0) && (db->queries_num > 0))
  {
    db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
        db->queries_num, sizeof (*db->q_prep_areas));

    if (db->q_prep_areas == NULL)
    {
      WARNING ("oracle plugin: calloc failed");
      status = -1;
      break;
    }

    for (i = 0; i < db->queries_num; ++i)
    {
      db->q_prep_areas[i]
        = udb_query_allocate_preparation_area (db->queries[i]);

      if (db->q_prep_areas[i] == NULL)
      {
        WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
        status = -1;
        break;
      }
    }

    break;
  }

  /* If all went well, add this query to the list of queries within the
   * database structure. */
  if (status == 0)
  {
    o_database_t **temp;

    temp = realloc (databases,
        sizeof (*databases) * (databases_num + 1));
    if (temp == NULL)
    {
      ERROR ("oracle plugin: realloc failed");
      status = -1;
    }
    else
    {
      databases = temp;
      databases[databases_num] = db;
      databases_num++;
    }
  }

  if (status != 0)
  {
    o_database_free (db);
    return (-1);
  }

  return (0);
} /* }}} int o_config_add_database */
コード例 #10
0
ファイル: varnish.c プロジェクト: QualityUnit/collectd
static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
{
	user_config_t *conf;
	user_data_t ud;
	char callback_name[DATA_MAX_NAME_LEN];
	int i;

	conf = malloc (sizeof (*conf));
	if (conf == NULL)
		return (ENOMEM);
	memset (conf, 0, sizeof (*conf));
	conf->instance = NULL;

	varnish_config_apply_default (conf);

	if (ci->values_num == 1)
	{
		int status;

		status = cf_util_get_string (ci, &conf->instance);
		if (status != 0)
		{
			sfree (conf);
			return (status);
		}
		assert (conf->instance != NULL);

		if (strcmp ("localhost", conf->instance) == 0)
		{
			sfree (conf->instance);
			conf->instance = NULL;
		}
	}
	else if (ci->values_num > 1)
	{
		WARNING ("Varnish plugin: \"Instance\" blocks accept only "
				"one argument.");
		return (EINVAL);
	}

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

		if (strcasecmp ("CollectCache", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_cache);
		else if (strcasecmp ("CollectConnections", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_connections);
		else if (strcasecmp ("CollectESI", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_esi);
#ifdef HAVE_VARNISH_V3
		else if (strcasecmp ("CollectDirectorDNS", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_dirdns);
#endif
		else if (strcasecmp ("CollectBackend", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_backend);
		else if (strcasecmp ("CollectFetch", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_fetch);
		else if (strcasecmp ("CollectHCB", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_hcb);
		else if (strcasecmp ("CollectObjects", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_objects);
#if HAVE_VARNISH_V2
		else if (strcasecmp ("CollectPurge", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_purge);
#else
		else if (strcasecmp ("CollectBan", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_ban);
#endif
		else if (strcasecmp ("CollectSession", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_session);
		else if (strcasecmp ("CollectSHM", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_shm);
		else if (strcasecmp ("CollectSMS", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_sms);
#if HAVE_VARNISH_V2
		else if (strcasecmp ("CollectSMA", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_sma);
		else if (strcasecmp ("CollectSM", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_sm);
#endif
		else if (strcasecmp ("CollectStruct", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_struct);
		else if (strcasecmp ("CollectTotals", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_totals);
#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
		else if (strcasecmp ("CollectUptime", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_uptime);
#endif
		else if (strcasecmp ("CollectVCL", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_vcl);
		else if (strcasecmp ("CollectWorkers", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_workers);
#if HAVE_VARNISH_V4
		else if (strcasecmp ("CollectVSM", child->key) == 0)
			cf_util_get_boolean (child, &conf->collect_vsm);
#endif
		else
		{
			WARNING ("Varnish plugin: Ignoring unknown "
					"configuration option: \"%s\". Did "
					"you forget to add an <Instance /> "
					"block around the configuration?",
					child->key);
		}
	}

	if (!conf->collect_cache
			&& !conf->collect_connections
			&& !conf->collect_esi
			&& !conf->collect_backend
#ifdef HAVE_VARNISH_V3
			&& !conf->collect_dirdns
#endif
			&& !conf->collect_fetch
			&& !conf->collect_hcb
			&& !conf->collect_objects
#if HAVE_VARNISH_V2
			&& !conf->collect_purge
#else
			&& !conf->collect_ban
#endif
			&& !conf->collect_session
			&& !conf->collect_shm
			&& !conf->collect_sms
#if HAVE_VARNISH_V2
			&& !conf->collect_sma
			&& !conf->collect_sm
#endif
			&& !conf->collect_struct
			&& !conf->collect_totals
#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
			&& !conf->collect_uptime
#endif
			&& !conf->collect_vcl
			&& !conf->collect_workers
#if HAVE_VARNISH_V4
			&& !conf->collect_vsm
#endif
	)
	{
		WARNING ("Varnish plugin: No metric has been configured for "
				"instance \"%s\". Disabling this instance.",
				(conf->instance == NULL) ? "localhost" : conf->instance);
		return (EINVAL);
	}

	ssnprintf (callback_name, sizeof (callback_name), "varnish/%s",
			(conf->instance == NULL) ? "localhost" : conf->instance);

	ud.data = conf;
	ud.free_func = varnish_config_free;

	plugin_register_complex_read (/* group = */ "varnish",
			/* name      = */ callback_name,
			/* callback  = */ varnish_read,
			/* interval  = */ 0,
			/* user data = */ &ud);

	have_instance = 1;

	return (0);
} /* }}} int varnish_config_instance */
コード例 #11
0
ファイル: dbi.c プロジェクト: adrahon/collectd
static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
{
  cdbi_database_t *db;
  int status;
  int i;

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

  db = (cdbi_database_t *) malloc (sizeof (*db));
  if (db == NULL)
  {
    ERROR ("dbi plugin: malloc failed.");
    return (-1);
  }
  memset (db, 0, sizeof (*db));

  status = cf_util_get_string (ci, &db->name);
  if (status != 0)
  {
    sfree (db);
    return (status);
  }

  /* Fill the `cdbi_database_t' structure.. */
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("Driver", child->key) == 0)
      status = cf_util_get_string (child, &db->driver);
    else if (strcasecmp ("DriverOption", child->key) == 0)
      status = cdbi_config_add_database_driver_option (db, child);
    else if (strcasecmp ("SelectDB", child->key) == 0)
      status = cf_util_get_string (child, &db->select_db);
    else if (strcasecmp ("Query", child->key) == 0)
      status = udb_query_pick_from_list (child, queries, queries_num,
          &db->queries, &db->queries_num);
    else
    {
      WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  /* Check that all necessary options have been given. */
  while (status == 0)
  {
    if (db->driver == NULL)
    {
      WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
      status = -1;
    }
    if (db->driver_options_num == 0)
    {
      WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
          "This will likely not work.", db->name);
    }

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

  while ((status == 0) && (db->queries_num > 0))
  {
    db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
        db->queries_num, sizeof (*db->q_prep_areas));

    if (db->q_prep_areas == NULL)
    {
      WARNING ("dbi plugin: malloc failed");
      status = -1;
      break;
    }

    for (i = 0; i < db->queries_num; ++i)
    {
      db->q_prep_areas[i]
        = udb_query_allocate_preparation_area (db->queries[i]);

      if (db->q_prep_areas[i] == NULL)
      {
        WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
        status = -1;
        break;
      }
    }

    break;
  }

  /* If all went well, add this database to the global list of databases. */
  if (status == 0)
  {
    cdbi_database_t **temp;

    temp = (cdbi_database_t **) realloc (databases,
        sizeof (*databases) * (databases_num + 1));
    if (temp == NULL)
    {
      ERROR ("dbi plugin: realloc failed");
      status = -1;
    }
    else
    {
      databases = temp;
      databases[databases_num] = db;
      databases_num++;
    }
  }

  if (status != 0)
  {
    cdbi_database_free (db);
    return (-1);
  }

  return (0);
} /* }}} int cdbi_config_add_database */
コード例 #12
0
ファイル: tail.c プロジェクト: collectd/collectd
static int ctail_config_add_match(cu_tail_match_t *tm, const char *plugin_name,
                                  const char *plugin_instance,
                                  oconfig_item_t *ci) {
  ctail_config_match_t cm = {0};
  int status;

  if (ci->values_num != 0) {
    WARNING("tail plugin: Ignoring arguments for the `Match' block.");
  }

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

    if (strcasecmp("Regex", option->key) == 0)
      status = cf_util_get_string(option, &cm.regex);
    else if (strcasecmp("ExcludeRegex", option->key) == 0)
      status = cf_util_get_string(option, &cm.excluderegex);
    else if (strcasecmp("DSType", option->key) == 0)
      status = ctail_config_add_match_dstype(&cm, option);
    else if (strcasecmp("Type", option->key) == 0)
      status = cf_util_get_string(option, &cm.type);
    else if (strcasecmp("Instance", option->key) == 0)
      status = cf_util_get_string(option, &cm.type_instance);
    else {
      WARNING("tail plugin: Option `%s' not allowed here.", option->key);
      status = -1;
    }

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

  while (status == 0) {
    if (cm.regex == NULL) {
      WARNING("tail plugin: `Regex' missing in `Match' block.");
      status = -1;
      break;
    }

    if (cm.type == NULL) {
      WARNING("tail plugin: `Type' missing in `Match' block.");
      status = -1;
      break;
    }

    if (cm.flags == 0) {
      WARNING("tail plugin: `DSType' missing in `Match' block.");
      status = -1;
      break;
    }

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

  if (status == 0) {
    // TODO(octo): there's nothing "simple" about the latency stuff …
    status = tail_match_add_match_simple(
        tm, cm.regex, cm.excluderegex, cm.flags,
        (plugin_name != NULL) ? plugin_name : "tail", plugin_instance, cm.type,
        cm.type_instance, cm.latency);

    if (status != 0)
      ERROR("tail plugin: tail_match_add_match_simple failed.");
  }

  sfree(cm.regex);
  sfree(cm.excluderegex);
  sfree(cm.type);
  sfree(cm.type_instance);
  latency_config_free(cm.latency);

  return status;
} /* int ctail_config_add_match */
コード例 #13
0
ファイル: modbus.c プロジェクト: kimor79/collectd
static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
{
  mb_data_t data;
  int status;
  int i;

  memset (&data, 0, sizeof (data));
  data.name = NULL;
  data.register_type = REG_TYPE_UINT16;
  data.next = NULL;

  status = cf_util_get_string (ci, &data.name);
  if (status != 0)
    return (status);

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

    if (strcasecmp ("Type", child->key) == 0)
      status = cf_util_get_string_buffer (child,
          data.type, sizeof (data.type));
    else if (strcasecmp ("Instance", child->key) == 0)
      status = cf_util_get_string_buffer (child,
          data.instance, sizeof (data.instance));
    else if (strcasecmp ("RegisterBase", child->key) == 0)
      status = cf_util_get_int (child, &data.register_base);
    else if (strcasecmp ("RegisterType", child->key) == 0)
    {
      char tmp[16];
      status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
      if (status != 0)
        /* do nothing */;
      else if (strcasecmp ("Uint16", tmp) == 0)
        data.register_type = REG_TYPE_UINT16;
      else if (strcasecmp ("Uint32", tmp) == 0)
        data.register_type = REG_TYPE_UINT32;
      else if (strcasecmp ("Float", tmp) == 0)
        data.register_type = REG_TYPE_FLOAT;
      else
      {
        ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
        status = -1;
      }
    }
    else
    {
      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
      status = -1;
    }

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

  assert (data.name != NULL);
  if (data.type[0] == 0)
  {
    ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
        data.name);
    status = -1;
  }

  if (status == 0)
    data_copy (&data_definitions, &data);

  sfree (data.name);

  return (status);
} /* }}} int mb_config_add_data */
コード例 #14
0
ファイル: curl_xml.c プロジェクト: EMSL-MSC/collectd
static int cx_config_add_url(oconfig_item_t *ci) /* {{{ */
{
  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
    WARNING("curl_xml plugin: The `URL' block "
            "needs exactly one string argument.");
    return -1;
  }

  cx_t *db = calloc(1, sizeof(*db));
  if (db == NULL) {
    ERROR("curl_xml plugin: calloc failed.");
    return -1;
  }

  db->instance = strdup("default");
  if (db->instance == NULL) {
    ERROR("curl_xml plugin: strdup failed.");
    sfree(db);
    return -1;
  }

  db->xpath_list = llist_create();
  if (db->xpath_list == NULL) {
    ERROR("curl_xml plugin: list creation failed.");
    sfree(db->instance);
    sfree(db);
    return -1;
  }

  db->timeout = -1;

  int status = cf_util_get_string(ci, &db->url);
  if (status != 0) {
    llist_destroy(db->xpath_list);
    sfree(db->instance);
    sfree(db);
    return status;
  }

  /* Fill the `cx_t' structure.. */
  for (int i = 0; i < ci->children_num; i++) {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp("Instance", child->key) == 0)
      status = cf_util_get_string(child, &db->instance);
    else if (strcasecmp("Plugin", child->key) == 0)
      status = cf_util_get_string(child, &db->plugin_name);
    else if (strcasecmp("Host", child->key) == 0)
      status = cf_util_get_string(child, &db->host);
    else if (strcasecmp("User", child->key) == 0)
      status = cf_util_get_string(child, &db->user);
    else if (strcasecmp("Password", child->key) == 0)
      status = cf_util_get_string(child, &db->pass);
    else if (strcasecmp("Digest", child->key) == 0)
      status = cf_util_get_boolean(child, &db->digest);
    else if (strcasecmp("VerifyPeer", child->key) == 0)
      status = cf_util_get_boolean(child, &db->verify_peer);
    else if (strcasecmp("VerifyHost", child->key) == 0)
      status = cf_util_get_boolean(child, &db->verify_host);
    else if (strcasecmp("CACert", child->key) == 0)
      status = cf_util_get_string(child, &db->cacert);
    else if (strcasecmp("xpath", child->key) == 0)
      status = cx_config_add_xpath(db, child);
    else if (strcasecmp("Header", child->key) == 0)
      status = cx_config_append_string("Header", &db->headers, child);
    else if (strcasecmp("Post", child->key) == 0)
      status = cf_util_get_string(child, &db->post_body);
    else if (strcasecmp("Namespace", child->key) == 0)
      status = cx_config_add_namespace(db, child);
    else if (strcasecmp("Timeout", child->key) == 0)
      status = cf_util_get_int(child, &db->timeout);
    else if (strcasecmp("Statistics", child->key) == 0) {
      db->stats = curl_stats_from_config(child);
      if (db->stats == NULL)
        status = -1;
    } else {
      WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  if (status != 0) {
    cx_free(db);
    return status;
  }

  if (llist_size(db->xpath_list) == 0) {
    WARNING("curl_xml plugin: No `xpath' block within `URL' block `%s'.",
            db->url);
    cx_free(db);
    return -1;
  }

  if (cx_init_curl(db) != 0) {
    cx_free(db);
    return -1;
  }

  /* If all went well, register this database for reading */
  DEBUG("curl_xml plugin: Registering new read callback: %s", db->instance);

  char *cb_name = ssnprintf_alloc("curl_xml-%s-%s", db->instance, db->url);

  plugin_register_complex_read(/* group = */ "curl_xml", cb_name, cx_read,
                               /* interval = */ 0,
                               &(user_data_t){
                                   .data = db, .free_func = cx_free,
                               });
コード例 #15
0
ファイル: curl_xml.c プロジェクト: EMSL-MSC/collectd
static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
{
  cx_xpath_t *xpath = calloc(1, sizeof(*xpath));
  if (xpath == NULL) {
    ERROR("curl_xml plugin: calloc failed.");
    return -1;
  }

  int status = cf_util_get_string(ci, &xpath->path);
  if (status != 0) {
    cx_xpath_free(xpath);
    return status;
  }

  /* error out if xpath->path is an empty string */
  if (strlen(xpath->path) == 0) {
    ERROR("curl_xml plugin: invalid xpath. "
          "xpath value can't be an empty string");
    cx_xpath_free(xpath);
    return -1;
  }

  status = 0;
  for (int 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, &xpath->type);
    else if (strcasecmp("InstancePrefix", child->key) == 0)
      status = cf_util_get_string(child, &xpath->instance_prefix);
    else if (strcasecmp("InstanceFrom", child->key) == 0)
      status = cf_util_get_string(child, &xpath->instance);
    else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
      status = cf_util_get_string(child, &xpath->plugin_instance_from);
    else if (strcasecmp("ValuesFrom", child->key) == 0)
      status = cx_config_add_values("ValuesFrom", xpath, child);
    else {
      WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

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

  if (status != 0) {
    cx_xpath_free(xpath);
    return status;
  }

  if (xpath->type == NULL) {
    WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
    cx_xpath_free(xpath);
    return -1;
  }

  if (xpath->values_len == 0) {
    WARNING("curl_xml plugin: `ValuesFrom' missing in `xpath' block.");
    cx_xpath_free(xpath);
    return -1;
  }

  llentry_t *le = llentry_create(xpath->path, xpath);
  if (le == NULL) {
    ERROR("curl_xml plugin: llentry_create failed.");
    cx_xpath_free(xpath);
    return -1;
  }

  llist_append(db->xpath_list, le);
  return 0;
} /* }}} int cx_config_add_xpath */
コード例 #16
0
ファイル: memcached.c プロジェクト: lassizci/collectd
/* Configuration handling functiions
 * <Plugin memcached>
 *   <Instance "instance_name">
 *     Host foo.zomg.com
 *     Port "1234"
 *   </Instance>
 * </Plugin>
 */
static int config_add_instance(oconfig_item_t *ci)
{
  memcached_t *st;
  int i;
  int status = 0;

  /* Disable automatic generation of default instance in the init callback. */
  memcached_have_instances = 1;

  st = malloc (sizeof (*st));
  if (st == NULL)
  {
    ERROR ("memcached plugin: malloc failed.");
    return (-1);
  }

  memset (st, 0, sizeof (*st));
  st->name = NULL;
  st->socket = NULL;
  st->host = NULL;
  st->port = NULL;
  st->use_global_hostn = MEMCACHED_DEF_G_HOSTN;

  if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
    st->name = sstrdup ("__legacy__");
  else /* <Instance /> block */
    status = cf_util_get_string (ci, &st->name);
  if (status != 0)
  {
    sfree (st);
    return (status);
  }
  assert (st->name != NULL);

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

    if (strcasecmp ("Socket", child->key) == 0)
      status = cf_util_get_string (child, &st->socket);
    else if (strcasecmp ("Host", child->key) == 0)
      status = cf_util_get_string (child, &st->host);
    else if (strcasecmp ("Port", child->key) == 0)
      status = cf_util_get_service (child, &st->port);
    else if (strcasecmp ("UseGlobalHostname", child->key) == 0)
      status = cf_util_get_boolean (child, &st->use_global_hostn);
    else
    {
      WARNING ("memcached plugin: Option `%s' not allowed here.",
          child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  if (status == 0)
    status = memcached_add_read_callback (st);

  if (status != 0)
  {
    memcached_free(st);
    return (-1);
  }

  return (0);
}
コード例 #17
0
ファイル: mysql.c プロジェクト: ChrisRut/collectd
/* Configuration handling functions {{{
 *
 * <Plugin mysql>
 *   <Database "plugin_instance1">
 *     Host "localhost"
 *     Port 22000
 *     ...
 *   </Database>
 * </Plugin>
 */
static int mysql_config_database (oconfig_item_t *ci) /* {{{ */
{
	mysql_database_t *db;
	int status = 0;
	int i;

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

	db = (mysql_database_t *) malloc (sizeof (*db));
	if (db == NULL)
	{
		ERROR ("mysql plugin: malloc failed.");
		return (-1);
	}
	memset (db, 0, sizeof (*db));

	/* initialize all the pointers */
	db->host     = NULL;
	db->user     = NULL;
	db->pass     = NULL;
	db->database = NULL;
	db->socket   = NULL;
	db->con      = NULL;

	/* trigger a notification, if it's not running */
	db->slave_io_running  = 1;
	db->slave_sql_running = 1;

	status = cf_util_get_string (ci, &db->instance);
	if (status != 0)
	{
		sfree (db);
		return (status);
	}
	assert (db->instance != NULL);

	/* Fill the `mysql_database_t' structure.. */
	for (i = 0; i < ci->children_num; i++)
	{
		oconfig_item_t *child = ci->children + i;

		if (strcasecmp ("Host", child->key) == 0)
			status = cf_util_get_string (child, &db->host);
		else if (strcasecmp ("User", child->key) == 0)
			status = cf_util_get_string (child, &db->user);
		else if (strcasecmp ("Password", child->key) == 0)
			status = cf_util_get_string (child, &db->pass);
		else if (strcasecmp ("Port", child->key) == 0)
		{
			status = cf_util_get_port_number (child);
			if (status > 0)
			{
				db->port = status;
				status = 0;
			}
		}
		else if (strcasecmp ("Socket", child->key) == 0)
			status = cf_util_get_string (child, &db->socket);
		else if (strcasecmp ("Database", child->key) == 0)
			status = cf_util_get_string (child, &db->database);
		else if (strcasecmp ("MasterStats", child->key) == 0)
			status = cf_util_get_boolean (child, &db->master_stats);
		else if (strcasecmp ("SlaveStats", child->key) == 0)
			status = cf_util_get_boolean (child, &db->slave_stats);
		else if (strcasecmp ("SlaveNotifications", child->key) == 0)
			status = cf_util_get_boolean (child, &db->slave_notif);
		else
		{
			WARNING ("mysql plugin: Option `%s' not allowed here.", child->key);
			status = -1;
		}

		if (status != 0)
			break;
	}

	/* If all went well, register this database for reading */
	if (status == 0)
	{
		user_data_t ud;
		char cb_name[DATA_MAX_NAME_LEN];

		DEBUG ("mysql plugin: Registering new read callback: %s",
				(db->database != NULL) ? db->database : "<default>");

		memset (&ud, 0, sizeof (ud));
		ud.data = (void *) db;
		ud.free_func = mysql_database_free;

		if (db->instance != NULL)
			ssnprintf (cb_name, sizeof (cb_name), "mysql-%s",
					db->instance);
		else
			sstrncpy (cb_name, "mysql", sizeof (cb_name));

		plugin_register_complex_read (/* group = */ NULL, cb_name,
					      mysql_read,
					      /* interval = */ NULL, &ud);
	}
	else
	{
		mysql_database_free (db);
		return (-1);
	}

	return (0);
} /* }}} int mysql_config_database */
コード例 #18
0
ファイル: write_riemann.c プロジェクト: iriscouch/collectd
static int
riemann_config_node(oconfig_item_t *ci)
{
	struct riemann_host	*host = NULL;
	int			 status = 0;
	int			 i;
	oconfig_item_t		*child;
	char			 callback_name[DATA_MAX_NAME_LEN];
	user_data_t		 ud;

	if ((host = calloc(1, sizeof (*host))) == NULL) {
		ERROR ("write_riemann plugin: calloc failed.");
		return ENOMEM;
	}
	pthread_mutex_init (&host->lock, NULL);
	host->reference_count = 1;
	host->node = NULL;
	host->service = NULL;
	host->store_rates = 1;
	host->always_append_ds = 0;
	host->use_tcp = 0;

	status = cf_util_get_string (ci, &host->name);
	if (status != 0) {
		WARNING("write_riemann plugin: Required host name is missing.");
		riemann_free (host);
		return -1;
	}

	for (i = 0; i < ci->children_num; i++) {
		/*
		 * The code here could be simplified but makes room
		 * for easy adding of new options later on.
		 */
		child = &ci->children[i];
		status = 0;

		if (strcasecmp ("Host", child->key) == 0) {
			status = cf_util_get_string (child, &host->node);
			if (status != 0)
				break;
		} else if (strcasecmp ("Port", child->key) == 0) {
			status = cf_util_get_service (child, &host->service);
			if (status != 0) {
				ERROR ("write_riemann plugin: Invalid argument "
						"configured for the \"Port\" "
						"option.");
				break;
			}
		} else if (strcasecmp ("Protocol", child->key) == 0) {
			char tmp[16];
			status = cf_util_get_string_buffer (child,
					tmp, sizeof (tmp));
			if (status != 0)
			{
				ERROR ("write_riemann plugin: cf_util_get_"
						"string_buffer failed with "
						"status %i.", status);
				break;
			}

			if (strcasecmp ("UDP", tmp) == 0)
				host->use_tcp = 0;
			else if (strcasecmp ("TCP", tmp) == 0)
				host->use_tcp = 1;
			else
				WARNING ("write_riemann plugin: The value "
						"\"%s\" is not valid for the "
						"\"Protocol\" option. Use "
						"either \"UDP\" or \"TCP\".",
						tmp);
		} else if (strcasecmp ("StoreRates", child->key) == 0) {
			status = cf_util_get_boolean (child, &host->store_rates);
			if (status != 0)
				break;
		} else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
			status = cf_util_get_boolean (child,
					&host->always_append_ds);
			if (status != 0)
				break;
		} else {
			WARNING("write_riemann plugin: ignoring unknown config "
				"option: \"%s\"", child->key);
		}
	}
	if (status != 0) {
		riemann_free (host);
		return status;
	}

	ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
			host->name);
	ud.data = host;
	ud.free_func = riemann_free;

	pthread_mutex_lock (&host->lock);

	status = plugin_register_write (callback_name, riemann_write, &ud);
	if (status != 0)
		WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
				"failed with status %i.",
				callback_name, status);
	else /* success */
		host->reference_count++;

	status = plugin_register_notification (callback_name,
			riemann_notification, &ud);
	if (status != 0)
		WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
				"failed with status %i.",
				callback_name, status);
	else /* success */
		host->reference_count++;

	if (host->reference_count <= 1)
	{
		/* Both callbacks failed => free memory.
		 * We need to unlock here, because riemann_free() will lock.
		 * This is not a race condition, because we're the only one
		 * holding a reference. */
		pthread_mutex_unlock (&host->lock);
		riemann_free (host);
		return (-1);
	}

	host->reference_count--;
	pthread_mutex_unlock (&host->lock);

	return status;
}
コード例 #19
0
ファイル: statsd.c プロジェクト: skolot/collectd
static int statsd_config (oconfig_item_t *ci) /* {{{ */
{
  int i;

  for (i = 0; i < ci->children_num; i++) {

    oconfig_item_t *child = ci->children + i;
    statsd_config_t *conf = NULL;
    statsd_thread_t *tmp = NULL;

    if (NULL == statsd_threads) {
      statsd_threads = malloc(sizeof(statsd_thread_t));

      if (NULL == statsd_threads) {
        ERROR ("statsd plugin: malloc failed.");
        return (ENOMEM);
      }

      memset(statsd_threads, 0, sizeof(statsd_thread_t));

    } else {
      tmp = realloc(statsd_threads, sizeof (statsd_config_t) * (statsd_threads_num + 1));

      if (NULL == tmp) {
        ERROR ("statsd plugin: realloc failed.");
        statsd_threads_free();
        return (ENOMEM);
      }

      statsd_threads = tmp;
      memset(statsd_threads + statsd_threads_num, 0, sizeof(statsd_config_t));
    }

    statsd_threads_num += 1;
    conf = statsd_config_alloc();

    if (NULL == conf) {
      ERROR ("statsd plugin: malloc failed.");
      statsd_threads_free();
      return (ENOMEM);
    }

    statsd_threads[statsd_threads_num - 1].conf = conf;

    if (strcasecmp ("Node", child->key) == 0) {
      cf_util_get_string (child, &conf->node_name);

      if (NULL == conf->node_name) {
        conf->node_name = strdup(STATSD_DEFAULT_NODE_NAME);
        if (NULL == conf->node_name) {
          ERROR ("statsd plugin: malloc failed.");
          statsd_threads_free();
          return (ENOMEM);
        }
      }

    } else
      ERROR ("statsd plugin: The \"%s\" config option is not valid.",
             child->key);

    statsd_config_node(conf, child);
  }

  return (0);
} /* }}} int statsd_config */
コード例 #20
0
ファイル: tail_csv.c プロジェクト: Mindera/collectd
/* Parse metric  */
static int tcsv_config_add_metric(oconfig_item_t *ci){
    metric_definition_t *md;
    int status;
    int i;

    md = (metric_definition_t *)malloc(sizeof(*md));
    if (md == NULL)
        return (-1);
    memset(md, 0, sizeof(*md));
    md->name = NULL;
    md->type = NULL;
    md->instance = NULL;
    md->data_source_type = -1;
    md->value_from = -1;
    md->next = NULL;

    status = cf_util_get_string (ci, &md->name);
    if (status != 0) {
        sfree (md);
        return (-1);
    }

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

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

        if (status != 0)
            break;
    }

    if (status != 0){
        tcsv_metric_definition_destroy(md);
        return (-1);
    }

    /* Verify all necessary options have been set. */
    if (md->type == NULL) {
        WARNING("tail_csv plugin: Option `Type' must be set.");
        status = -1;
    } else if (md->value_from < 0) {
        WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
        status = -1;
    }
    if (status != 0) {
        tcsv_metric_definition_destroy(md);
        return (status);
    }

    if (metric_head == NULL)
        metric_head = md;
    else {
        metric_definition_t *last;
        last = metric_head;
        while (last->next != NULL)
            last = last->next;
        last->next = md;
    }

    return (0);
}
コード例 #21
0
ファイル: curl_json.c プロジェクト: karcaw/collectd
static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
{
    cj_t *db;
    int status = 0;
    int i;

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

    db = (cj_t *) malloc (sizeof (*db));
    if (db == NULL)
    {
        ERROR ("curl_json plugin: malloc failed.");
        return (-1);
    }
    memset (db, 0, sizeof (*db));

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

    /* Fill the `cj_t' structure.. */
    for (i = 0; i < ci->children_num; i++)
    {
        oconfig_item_t *child = ci->children + i;

        if (strcasecmp ("Instance", child->key) == 0)
            status = cf_util_get_string (child, &db->instance);
        else if (strcasecmp ("Host", child->key) == 0)
            status = cf_util_get_string (child, &db->host);
        else if (db->url && strcasecmp ("User", child->key) == 0)
            status = cf_util_get_string (child, &db->user);
        else if (db->url && strcasecmp ("Password", child->key) == 0)
            status = cf_util_get_string (child, &db->pass);
        else if (strcasecmp ("Digest", child->key) == 0)
            status = cf_util_get_boolean (child, &db->digest);
        else if (db->url && strcasecmp ("VerifyPeer", child->key) == 0)
            status = cf_util_get_boolean (child, &db->verify_peer);
        else if (db->url && strcasecmp ("VerifyHost", child->key) == 0)
            status = cf_util_get_boolean (child, &db->verify_host);
        else if (db->url && strcasecmp ("CACert", child->key) == 0)
            status = cf_util_get_string (child, &db->cacert);
        else if (db->url && strcasecmp ("Header", child->key) == 0)
            status = cj_config_append_string ("Header", &db->headers, child);
        else if (db->url && strcasecmp ("Post", child->key) == 0)
            status = cf_util_get_string (child, &db->post_body);
        else if (strcasecmp ("Key", child->key) == 0)
            status = cj_config_add_key (db, child);
        else if (strcasecmp ("Interval", child->key) == 0)
            status = cf_util_get_cdtime(child, &db->interval);
        else
        {
            WARNING ("curl_json plugin: Option `%s' not allowed here.", child->key);
            status = -1;
        }

        if (status != 0)
            break;
    }

    if (status == 0)
    {
        if (db->tree == NULL)
        {
            WARNING ("curl_json plugin: No (valid) `Key' block within `%s' \"`%s'\".",
                     db->url ? "URL" : "Sock", db->url ? db->url : db->sock);
            status = -1;
        }
        if (status == 0 && db->url)
            status = cj_init_curl (db);
    }

    /* If all went well, register this database for reading */
    if (status == 0)
    {
        user_data_t ud;
        char *cb_name;
        struct timespec interval = { 0, 0 };

        CDTIME_T_TO_TIMESPEC (db->interval, &interval);

        if (db->instance == NULL)
            db->instance = strdup("default");

        DEBUG ("curl_json plugin: Registering new read callback: %s",
               db->instance);

        memset (&ud, 0, sizeof (ud));
        ud.data = (void *) db;
        ud.free_func = cj_free;

        cb_name = ssnprintf_alloc ("curl_json-%s-%s",
                                   db->instance, db->url ? db->url : db->sock);

        plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
                /* interval = */ (db->interval > 0) ? &interval : NULL,
                &ud);
        sfree (cb_name);
    }
    else
    {
        cj_free (db);
        return (-1);
    }

    return (0);
}
コード例 #22
0
ファイル: amqp.c プロジェクト: hume-github/collectd
static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
        _Bool publish)
{
    camqp_config_t *conf;
    int status;
    int i;

    conf = malloc (sizeof (*conf));
    if (conf == NULL)
    {
        ERROR ("amqp plugin: malloc failed.");
        return (ENOMEM);
    }

    /* Initialize "conf" {{{ */
    memset (conf, 0, sizeof (*conf));
    conf->publish = publish;
    conf->name = NULL;
    conf->format = CAMQP_FORMAT_COMMAND;
    conf->host = NULL;
    conf->port = 5672;
    conf->vhost = NULL;
    conf->user = NULL;
    conf->password = NULL;
    conf->exchange = NULL;
    conf->routing_key = NULL;
    /* publish only */
    conf->delivery_mode = CAMQP_DM_VOLATILE;
    conf->store_rates = 0;
    /* publish & graphite only */
    conf->prefix = NULL;
    conf->postfix = NULL;
    conf->escape_char = '_';
    /* subscribe only */
    conf->exchange_type = NULL;
    conf->queue = NULL;
    /* general */
    conf->connection = NULL;
    pthread_mutex_init (&conf->lock, /* attr = */ NULL);
    /* }}} */

    status = cf_util_get_string (ci, &conf->name);
    if (status != 0)
    {
        sfree (conf);
        return (status);
    }

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

        if (strcasecmp ("Host", child->key) == 0)
            status = cf_util_get_string (child, &conf->host);
        else if (strcasecmp ("Port", child->key) == 0)
        {
            status = cf_util_get_port_number (child);
            if (status > 0)
            {
                conf->port = status;
                status = 0;
            }
        }
        else if (strcasecmp ("VHost", child->key) == 0)
            status = cf_util_get_string (child, &conf->vhost);
        else if (strcasecmp ("User", child->key) == 0)
            status = cf_util_get_string (child, &conf->user);
        else if (strcasecmp ("Password", child->key) == 0)
            status = cf_util_get_string (child, &conf->password);
        else if (strcasecmp ("Exchange", child->key) == 0)
            status = cf_util_get_string (child, &conf->exchange);
        else if ((strcasecmp ("ExchangeType", child->key) == 0) && !publish)
            status = cf_util_get_string (child, &conf->exchange_type);
        else if ((strcasecmp ("Queue", child->key) == 0) && !publish)
            status = cf_util_get_string (child, &conf->queue);
        else if (strcasecmp ("RoutingKey", child->key) == 0)
            status = cf_util_get_string (child, &conf->routing_key);
        else if ((strcasecmp ("Persistent", child->key) == 0) && publish)
        {
            _Bool tmp = 0;
            status = cf_util_get_boolean (child, &tmp);
            if (tmp)
                conf->delivery_mode = CAMQP_DM_PERSISTENT;
            else
                conf->delivery_mode = CAMQP_DM_VOLATILE;
        }
        else if ((strcasecmp ("StoreRates", child->key) == 0) && publish)
            status = cf_util_get_boolean (child, &conf->store_rates);
        else if ((strcasecmp ("Format", child->key) == 0) && publish)
            status = camqp_config_set_format (child, conf);
        else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish)
            status = cf_util_get_string (child, &conf->prefix);
        else if ((strcasecmp ("GraphitePostfix", child->key) == 0) && publish)
            status = cf_util_get_string (child, &conf->postfix);
        else if ((strcasecmp ("GraphiteEscapeChar", child->key) == 0) && publish)
        {
            char *tmp_buff = NULL;
            status = cf_util_get_string (child, &tmp_buff);
            if (strlen (tmp_buff) > 1)
                WARNING ("amqp plugin: The option \"GraphiteEscapeChar\" handles "
                        "only one character. Others will be ignored.");
            conf->escape_char = tmp_buff[0];
            sfree (tmp_buff);
        }
        else
            WARNING ("amqp plugin: Ignoring unknown "
                    "configuration option \"%s\".", child->key);

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

    if ((status == 0) && (conf->exchange == NULL))
    {
        if (conf->exchange_type != NULL)
            WARNING ("amqp plugin: The option \"ExchangeType\" was given "
                    "without the \"Exchange\" option. It will be ignored.");

        if (!publish && (conf->routing_key != NULL))
            WARNING ("amqp plugin: The option \"RoutingKey\" was given "
                    "without the \"Exchange\" option. It will be ignored.");

    }

    if (status != 0)
    {
        camqp_config_free (conf);
        return (status);
    }

    if (conf->exchange != NULL)
    {
        DEBUG ("amqp plugin: camqp_config_connection: exchange = %s;",
                conf->exchange);
    }

    if (publish)
    {
        char cbname[128];
        user_data_t ud = { conf, camqp_config_free };

        ssnprintf (cbname, sizeof (cbname), "amqp/%s", conf->name);

        status = plugin_register_write (cbname, camqp_write, &ud);
        if (status != 0)
        {
            camqp_config_free (conf);
            return (status);
        }
    }
    else
    {
        status = camqp_subscribe_init (conf);
        if (status != 0)
        {
            camqp_config_free (conf);
            return (status);
        }
    }

    return (0);
} /* }}} int camqp_config_connection */
コード例 #23
0
ファイル: postgresql.c プロジェクト: clopez/collectd
static int c_psql_config_database (oconfig_item_t *ci)
{
	c_psql_database_t *db;

	char cb_name[DATA_MAX_NAME_LEN];
	struct timespec cb_interval = { 0, 0 };
	user_data_t ud;

	int i;

	if ((1 != ci->values_num)
			|| (OCONFIG_TYPE_STRING != ci->values[0].type)) {
		log_err ("<Database> expects a single string argument.");
		return 1;
	}

	memset (&ud, 0, sizeof (ud));

	db = c_psql_database_new (ci->values[0].value.string);
	if (db == NULL)
		return -1;

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

		if (0 == strcasecmp (c->key, "Host"))
			cf_util_get_string (c, &db->host);
		else if (0 == strcasecmp (c->key, "Port"))
			cf_util_get_service (c, &db->port);
		else if (0 == strcasecmp (c->key, "User"))
			cf_util_get_string (c, &db->user);
		else if (0 == strcasecmp (c->key, "Password"))
			cf_util_get_string (c, &db->password);
		else if (0 == strcasecmp (c->key, "Instance"))
			cf_util_get_string (c, &db->instance);
		else if (0 == strcasecmp (c->key, "SSLMode"))
			cf_util_get_string (c, &db->sslmode);
		else if (0 == strcasecmp (c->key, "KRBSrvName"))
			cf_util_get_string (c, &db->krbsrvname);
		else if (0 == strcasecmp (c->key, "Service"))
			cf_util_get_string (c, &db->service);
		else if (0 == strcasecmp (c->key, "Query"))
			udb_query_pick_from_list (c, queries, queries_num,
					&db->queries, &db->queries_num);
		else if (0 == strcasecmp (c->key, "Interval"))
			cf_util_get_cdtime (c, &db->interval);
		else
			log_warn ("Ignoring unknown config key \"%s\".", c->key);
	}

	/* If no `Query' options were given, add the default queries.. */
	if (db->queries_num == 0) {
		for (i = 0; i < def_queries_num; i++)
			udb_query_pick_from_list_by_name (def_queries[i],
					queries, queries_num,
					&db->queries, &db->queries_num);
	}

	if (db->queries_num > 0) {
		db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
				db->queries_num, sizeof (*db->q_prep_areas));

		if (db->q_prep_areas == NULL) {
			log_err ("Out of memory.");
			c_psql_database_delete (db);
			return -1;
		}
	}

	for (i = 0; (size_t)i < db->queries_num; ++i) {
		c_psql_user_data_t *data;
		data = udb_query_get_user_data (db->queries[i]);
		if ((data != NULL) && (data->params_num > db->max_params_num))
			db->max_params_num = data->params_num;

		db->q_prep_areas[i]
			= udb_query_allocate_preparation_area (db->queries[i]);

		if (db->q_prep_areas[i] == NULL) {
			log_err ("Out of memory.");
			c_psql_database_delete (db);
			return -1;
		}
	}

	ud.data = db;
	ud.free_func = c_psql_database_delete;

	ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->instance);

	CDTIME_T_TO_TIMESPEC (db->interval, &cb_interval);

	plugin_register_complex_read ("postgresql", cb_name, c_psql_read,
			/* interval = */ (db->interval > 0) ? &cb_interval : NULL,
			&ud);
	return 0;
} /* c_psql_config_database */
コード例 #24
0
ファイル: snmp.c プロジェクト: kmiku7/collectd
static int csnmp_config_add_host (oconfig_item_t *ci)
{
  host_definition_t *hd;
  int status = 0;
  int i;

  /* Registration stuff. */
  char cb_name[DATA_MAX_NAME_LEN];
  user_data_t cb_data;

  hd = (host_definition_t *) malloc (sizeof (host_definition_t));
  if (hd == NULL)
    return (-1);
  memset (hd, '\0', sizeof (host_definition_t));
  hd->version = 2;
  C_COMPLAIN_INIT (&hd->complaint);

  status = cf_util_get_string(ci, &hd->name);
  if (status != 0)
  {
    sfree (hd);
    return status;
  }

  hd->sess_handle = NULL;
  hd->interval = 0;

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

    if (strcasecmp ("Address", option->key) == 0)
      status = cf_util_get_string(option, &hd->address);
    else if (strcasecmp ("Community", option->key) == 0)
      status = cf_util_get_string(option, &hd->community);
    else if (strcasecmp ("Version", option->key) == 0)
      status = csnmp_config_add_host_version (hd, option);
    else if (strcasecmp ("Collect", option->key) == 0)
      csnmp_config_add_host_collect (hd, option);
    else if (strcasecmp ("Interval", option->key) == 0)
      cf_util_get_cdtime (option, &hd->interval);
    else if (strcasecmp ("Username", option->key) == 0)
      status = cf_util_get_string(option, &hd->username);
    else if (strcasecmp ("AuthProtocol", option->key) == 0)
      status = csnmp_config_add_host_auth_protocol (hd, option);
    else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
      status = csnmp_config_add_host_priv_protocol (hd, option);
    else if (strcasecmp ("AuthPassphrase", option->key) == 0)
      status = cf_util_get_string(option, &hd->auth_passphrase);
    else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
      status = cf_util_get_string(option, &hd->priv_passphrase);
    else if (strcasecmp ("SecurityLevel", option->key) == 0)
      status = csnmp_config_add_host_security_level (hd, option);
    else if (strcasecmp ("Context", option->key) == 0)
      status = cf_util_get_string(option, &hd->context);
    else
    {
      WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
      status = -1;
    }

    if (status != 0)
      break;
  } /* for (ci->children) */

  while (status == 0)
  {
    if (hd->address == NULL)
    {
      WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
      status = -1;
      break;
    }
    if (hd->community == NULL && hd->version < 3)
    {
      WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
      status = -1;
      break;
    }
    if (hd->version == 3)
    {
      if (hd->username == NULL)
      {
        WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
        status = -1;
        break;
      }
      if (hd->security_level == 0)
      {
        WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
        status = -1;
        break;
      }
      if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
      {
	if (hd->auth_protocol == NULL)
	{
	  WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
	  status = -1;
	  break;
	}
	if (hd->auth_passphrase == NULL)
	{
	  WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
	  status = -1;
	  break;
	}
      }
      if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
      {
	if (hd->priv_protocol == NULL)
	{
	  WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
	  status = -1;
	  break;
	}
	if (hd->priv_passphrase == NULL)
	{
	  WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
	  status = -1;
	  break;
	}
      }
    }

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

  if (status != 0)
  {
    csnmp_host_definition_destroy (hd);
    return (-1);
  }

  DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
      hd->name, hd->address, hd->community, hd->version);

  ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);

  memset (&cb_data, 0, sizeof (cb_data));
  cb_data.data = hd;
  cb_data.free_func = csnmp_host_definition_destroy;

  status = plugin_register_complex_read (/* group = */ NULL, cb_name,
      csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
  if (status != 0)
  {
    ERROR ("snmp plugin: Registering complex read function failed.");
    csnmp_host_definition_destroy (hd);
    return (-1);
  }

  return (0);
} /* int csnmp_config_add_host */
コード例 #25
0
ファイル: tail_csv.c プロジェクト: Mindera/collectd
/* <File /> block */
static int tcsv_config_add_file(oconfig_item_t *ci)
{
    instance_definition_t* id;
    int status = 0;
    int i;

    /* Registration variables */
    char cb_name[DATA_MAX_NAME_LEN];
    user_data_t cb_data;

    id = malloc(sizeof(*id));
    if (id == NULL)
        return (-1);
    memset(id, 0, sizeof(*id));
    id->instance = NULL;
    id->path = NULL;
    id->metric_list = NULL;
    id->time_from = -1;
    id->next = NULL;

    status = cf_util_get_string (ci, &id->path);
    if (status != 0) {
        sfree (id);
        return (status);
    }

    /* Use default interval. */
    id->interval = plugin_get_interval();

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

        if (strcasecmp("Instance", option->key) == 0)
            status = cf_util_get_string(option, &id->instance);
        else if (strcasecmp("Collect", option->key) == 0)
            status = tcsv_config_add_instance_collect(id, option);
        else if (strcasecmp("Interval", option->key) == 0)
            cf_util_get_cdtime(option, &id->interval);
        else if (strcasecmp("TimeFrom", option->key) == 0)
            status = tcsv_config_get_index (option, &id->time_from);
        else {
            WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
            status = -1;
        }

        if (status != 0)
            break;
    }

    if (status != 0){
        tcsv_instance_definition_destroy(id);
        return (-1);
    }

    /* Verify all necessary options have been set. */
    if (id->path == NULL){
        WARNING("tail_csv plugin: Option `Path' must be set.");
        status = -1;
    } else if (id->metric_list == NULL){
        WARNING("tail_csv plugin: Option `Collect' must be set.");
        status = -1;
   }

    if (status != 0){
        tcsv_instance_definition_destroy(id);
        return (-1);
    }

    ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path);
    memset(&cb_data, 0, sizeof(cb_data));
    cb_data.data = id;
    cb_data.free_func = tcsv_instance_definition_destroy;
    status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval, &cb_data);

    if (status != 0){
        ERROR("tail_csv plugin: Registering complex read function failed.");
        tcsv_instance_definition_destroy(id);
        return (-1);
    }

    return (0);
}
コード例 #26
0
ファイル: curl_xml.c プロジェクト: Mindera/collectd
static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
{
  cx_xpath_t *xpath;
  char *name;
  llentry_t *le;
  int status;
  int i;

  xpath = malloc (sizeof (*xpath));
  if (xpath == NULL)
  {
    ERROR ("curl_xml plugin: malloc failed.");
    return (-1);
  }
  memset (xpath, 0, sizeof (*xpath));

  status = cf_util_get_string (ci, &xpath->path);
  if (status != 0)
  {
    cx_xpath_free (xpath);
    return (status);
  }

  /* error out if xpath->path is an empty string */
  if (strlen (xpath->path) == 0)
  {
    ERROR ("curl_xml plugin: invalid xpath. "
           "xpath value can't be an empty string");
    cx_xpath_free (xpath);
    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, &xpath->type);
    else if (strcasecmp ("InstancePrefix", child->key) == 0)
      status = cf_util_get_string (child, &xpath->instance_prefix);
    else if (strcasecmp ("InstanceFrom", child->key) == 0)
      status = cf_util_get_string (child, &xpath->instance);
    else if (strcasecmp ("ValuesFrom", child->key) == 0)
      status = cx_config_add_values ("ValuesFrom", xpath, child);
    else
    {
      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

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

  if (status != 0)
  {
    cx_xpath_free (xpath);
    return status;
  }

  if (xpath->type == NULL)
  {
    WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
    cx_xpath_free (xpath);
    return -1;
  }

  if (db->list == NULL)
  {
    db->list = llist_create();
    if (db->list == NULL)
    {
      ERROR ("curl_xml plugin: list creation failed.");
      cx_xpath_free (xpath);
      return (-1);
    }
  }

  name = strdup (xpath->path);
  if (name == NULL)
  {
    ERROR ("curl_xml plugin: strdup failed.");
    cx_xpath_free (xpath);
    return (-1);
  }

  le = llentry_create (name, xpath);
  if (le == NULL)
  {
    ERROR ("curl_xml plugin: llentry_create failed.");
    cx_xpath_free (xpath);
    sfree (name);
    return (-1);
  }

  llist_append (db->list, le);
  return (0);
} /* }}} int cx_config_add_xpath */
コード例 #27
0
ファイル: curl.c プロジェクト: NymbleTechnology/collectd
static int cc_config_add_match (web_page_t *page, /* {{{ */
    oconfig_item_t *ci)
{
  web_match_t *match;
  int status;
  int i;

  if (ci->values_num != 0)
  {
    WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
  }

  match = calloc (1, sizeof (*match));
  if (match == NULL)
  {
    ERROR ("curl plugin: calloc failed.");
    return (-1);
  }

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

    if (strcasecmp ("Regex", child->key) == 0)
      status = cf_util_get_string (child, &match->regex);
    else if (strcasecmp ("ExcludeRegex", child->key) == 0)
      status = cf_util_get_string (child, &match->exclude_regex);
    else if (strcasecmp ("DSType", child->key) == 0)
      status = cc_config_add_match_dstype (&match->dstype, child);
    else if (strcasecmp ("Type", child->key) == 0)
      status = cf_util_get_string (child, &match->type);
    else if (strcasecmp ("Instance", child->key) == 0)
      status = cf_util_get_string (child, &match->instance);
    else
    {
      WARNING ("curl 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 (match->regex == NULL)
    {
      WARNING ("curl plugin: `Regex' missing in `Match' block.");
      status = -1;
    }

    if (match->type == NULL)
    {
      WARNING ("curl plugin: `Type' missing in `Match' block.");
      status = -1;
    }

    if (match->dstype == 0)
    {
      WARNING ("curl plugin: `DSType' missing in `Match' block.");
      status = -1;
    }

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

  if (status != 0)
  {
    cc_web_match_free (match);
    return (status);
  }

  match->match = match_create_simple (match->regex, match->exclude_regex,
      match->dstype);
  if (match->match == NULL)
  {
    ERROR ("curl plugin: tail_match_add_match_simple failed.");
    cc_web_match_free (match);
    return (-1);
  }
  else
  {
    web_match_t *prev;

    prev = page->matches;
    while ((prev != NULL) && (prev->next != NULL))
      prev = prev->next;

    if (prev == NULL)
      page->matches = match;
    else
      prev->next = match;
  }

  return (0);
} /* }}} int cc_config_add_match */
コード例 #28
0
ファイル: curl_xml.c プロジェクト: Mindera/collectd
static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
{
  cx_t *db;
  int status = 0;
  int i;

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

  db = (cx_t *) malloc (sizeof (*db));
  if (db == NULL)
  {
    ERROR ("curl_xml plugin: malloc failed.");
    return (-1);
  }
  memset (db, 0, sizeof (*db));

  db->timeout = -1;

  if (strcasecmp ("URL", ci->key) == 0)
  {
    status = cf_util_get_string (ci, &db->url);
    if (status != 0)
    {
      sfree (db);
      return (status);
    }
  }
  else
  {
    ERROR ("curl_xml plugin: cx_config: "
           "Invalid key: %s", ci->key);
    cx_free (db);
    return (-1);
  }

  /* Fill the `cx_t' structure.. */
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("Instance", child->key) == 0)
      status = cf_util_get_string (child, &db->instance);
    else if (strcasecmp ("Host", child->key) == 0)
      status = cf_util_get_string (child, &db->host);
    else if (strcasecmp ("User", child->key) == 0)
      status = cf_util_get_string (child, &db->user);
    else if (strcasecmp ("Password", child->key) == 0)
      status = cf_util_get_string (child, &db->pass);
    else if (strcasecmp ("Digest", child->key) == 0)
      status = cf_util_get_boolean (child, &db->digest);
    else if (strcasecmp ("VerifyPeer", child->key) == 0)
      status = cf_util_get_boolean (child, &db->verify_peer);
    else if (strcasecmp ("VerifyHost", child->key) == 0)
      status = cf_util_get_boolean (child, &db->verify_host);
    else if (strcasecmp ("CACert", child->key) == 0)
      status = cf_util_get_string (child, &db->cacert);
    else if (strcasecmp ("xpath", child->key) == 0)
      status = cx_config_add_xpath (db, child);
    else if (strcasecmp ("Header", child->key) == 0)
      status = cx_config_append_string ("Header", &db->headers, child);
    else if (strcasecmp ("Post", child->key) == 0)
      status = cf_util_get_string (child, &db->post_body);
    else if (strcasecmp ("Namespace", child->key) == 0)
      status = cx_config_add_namespace (db, child);
    else if (strcasecmp ("Timeout", child->key) == 0)
      status = cf_util_get_int (child, &db->timeout);
    else
    {
      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  if (status == 0)
  {
    if (db->list == NULL)
    {
      WARNING ("curl_xml plugin: No (valid) `Key' block "
               "within `URL' block `%s'.", db->url);
      status = -1;
    }
    if (status == 0)
      status = cx_init_curl (db);
  }

  /* If all went well, register this database for reading */
  if (status == 0)
  {
    user_data_t ud;
    char *cb_name;

    if (db->instance == NULL)
      db->instance = strdup("default");

    DEBUG ("curl_xml plugin: Registering new read callback: %s",
           db->instance);

    memset (&ud, 0, sizeof (ud));
    ud.data = (void *) db;
    ud.free_func = cx_free;

    cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
    plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
                                  /* interval = */ 0, &ud);
    sfree (cb_name);
  }
  else
  {
    cx_free (db);
    return (-1);
  }

  return (0);
} /* }}} int cx_config_add_url */
コード例 #29
0
ファイル: write_redis.c プロジェクト: 01BTC10/collectd
static int wr_config_node (oconfig_item_t *ci) /* {{{ */
{
  wr_node_t *node;
  int timeout;
  int status;
  int i;

  node = calloc (1, sizeof (*node));
  if (node == NULL)
    return (ENOMEM);
  node->host = NULL;
  node->port = 0;
  node->timeout.tv_sec = 0;
  node->timeout.tv_usec = 1000;
  node->conn = NULL;
  node->prefix = NULL;
  node->database = 0;
  node->max_set_size = -1;
  node->store_rates = 1;
  pthread_mutex_init (&node->lock, /* attr = */ NULL);

  status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
  if (status != 0)
  {
    sfree (node);
    return (status);
  }

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

    if (strcasecmp ("Host", child->key) == 0)
      status = cf_util_get_string (child, &node->host);
    else if (strcasecmp ("Port", child->key) == 0)
    {
      status = cf_util_get_port_number (child);
      if (status > 0)
      {
        node->port = status;
        status = 0;
      }
    }
    else if (strcasecmp ("Timeout", child->key) == 0) {
      status = cf_util_get_int (child, &timeout);
      if (status == 0) node->timeout.tv_usec = timeout;
    }
    else if (strcasecmp ("Prefix", child->key) == 0) {
      status = cf_util_get_string (child, &node->prefix);
    }
    else if (strcasecmp ("Database", child->key) == 0) {
      status = cf_util_get_int (child, &node->database);
    }
    else if (strcasecmp ("MaxSetSize", child->key) == 0) {
      status = cf_util_get_int (child, &node->max_set_size);
    }
    else if (strcasecmp ("StoreRates", child->key) == 0) {
      status = cf_util_get_boolean (child, &node->store_rates);
    }
    else
      WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
          child->key);

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

  if (status == 0)
  {
    char cb_name[DATA_MAX_NAME_LEN];
    user_data_t ud;

    ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);

    ud.data = node;
    ud.free_func = wr_config_free;

    status = plugin_register_write (cb_name, wr_write, &ud);
  }

  if (status != 0)
    wr_config_free (node);

  return (status);
} /* }}} int wr_config_node */
コード例 #30
0
ファイル: powerdns.c プロジェクト: ajdiaz/collectd
static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */
{
  char *socket_temp;

  list_item_t *item;
  int status;

  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
    WARNING("powerdns plugin: `%s' needs exactly one string argument.",
            ci->key);
    return (-1);
  }

  item = calloc(1, sizeof(*item));
  if (item == NULL) {
    ERROR("powerdns plugin: calloc failed.");
    return (-1);
  }

  item->instance = strdup(ci->values[0].value.string);
  if (item->instance == NULL) {
    ERROR("powerdns plugin: strdup failed.");
    sfree(item);
    return (-1);
  }

  /*
   * Set default values for the members of list_item_t
   */
  if (strcasecmp("Server", ci->key) == 0) {
    item->server_type = SRV_AUTHORITATIVE;
    item->func = powerdns_read_server;
    item->socktype = SOCK_STREAM;
    socket_temp = strdup(SERVER_SOCKET);
  } else if (strcasecmp("Recursor", ci->key) == 0) {
    item->server_type = SRV_RECURSOR;
    item->func = powerdns_read_recursor;
    item->socktype = SOCK_DGRAM;
    socket_temp = strdup(RECURSOR_SOCKET);
  } else {
    /* We must never get here.. */
    assert(0);
    return (-1);
  }

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

    if (strcasecmp("Collect", option->key) == 0)
      status = powerdns_config_add_collect(item, option);
    else if (strcasecmp("Socket", option->key) == 0)
      status = cf_util_get_string(option, &socket_temp);
    else {
      ERROR("powerdns plugin: Option `%s' not allowed here.", option->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  while (status == 0) {
    llentry_t *e;

    if (socket_temp == NULL) {
      ERROR("powerdns plugin: socket_temp == NULL.");
      status = -1;
      break;
    }

    item->sockaddr.sun_family = AF_UNIX;
    sstrncpy(item->sockaddr.sun_path, socket_temp,
             sizeof(item->sockaddr.sun_path));

    e = llentry_create(item->instance, item);
    if (e == NULL) {
      ERROR("powerdns plugin: llentry_create failed.");
      status = -1;
      break;
    }
    llist_append(list, e);

    break;
  }

  if (status != 0) {
    sfree(socket_temp);
    sfree(item);
    return (-1);
  }

  DEBUG("powerdns plugin: Add server: instance = %s;", item->instance);

  sfree(socket_temp);
  return (0);
} /* }}} int powerdns_config_add_server */