Esempio n. 1
0
static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
{
  cj_t *db;
  int status = 0;

  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 = calloc (1, sizeof (*db));
  if (db == NULL)
  {
    ERROR ("curl_json plugin: calloc failed.");
    return (-1);
  }

  db->timeout = -1;

  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);
    cj_free (db);
    return (-1);
  }
  if (status != 0)
  {
    sfree (db);
    return (status);
  }

  /* Fill the `cj_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 ("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 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_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 = { 0 };
    char *cb_name;

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

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

    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,
                                  &ud);
    sfree (cb_name);
  }
  else
  {
    cj_free (db);
    return (-1);
  }

  return (0);
}
Esempio n. 2
0
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,
                               });
Esempio n. 3
0
File: curl.c Progetto: brd/collectd
static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
{
  web_page_t *page;
  int status;

  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->stats = NULL;

  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 (int 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 if (strcasecmp ("Statistics", child->key) == 0) {
      page->stats = curl_stats_from_config (child);
      if (page->stats == NULL)
        status = -1;
    }
    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->stats == NULL
        && !page->response_time && !page->response_code)
    {
      assert (page->instance != NULL);
      WARNING ("curl plugin: No (valid) `Match' block "
          "or Statistics 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 */