Exemple #1
0
static int lua_script_init(lua_script_t *script) /* {{{ */
{
  memset(script, 0, sizeof(*script));

  /* initialize the lua context */
  script->lua_state = luaL_newstate();
  if (script->lua_state == NULL) {
    ERROR("Lua plugin: luaL_newstate() failed.");
    return (-1);
  }

  /* Open up all the standard Lua libraries. */
  luaL_openlibs(script->lua_state);

  /* Load the 'collectd' library */
#if LUA_VERSION_NUM < 502
  lua_pushcfunction(script->lua_state, open_collectd);
  lua_pushstring(script->lua_state, "collectd");
  lua_call(script->lua_state, 1, 0);
#else
  luaL_requiref(script->lua_state, "collectd", open_collectd, 1);
  lua_pop(script->lua_state, 1);
#endif

  /* Prepend BasePath to package.path */
  if (base_path[0] != '\0') {
    lua_getglobal(script->lua_state, "package");
    lua_getfield(script->lua_state, -1, "path");

    const char *cur_path = lua_tostring(script->lua_state, -1);
    char *new_path = ssnprintf_alloc("%s/?.lua;%s", base_path, cur_path);

    lua_pop(script->lua_state, 1);
    lua_pushstring(script->lua_state, new_path);

    free(new_path);

    lua_setfield(script->lua_state, -2, "path");
    lua_pop(script->lua_state, 1);
  }

  return (0);
} /* }}} int lua_script_init */
Exemple #2
0
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);
}
Exemple #3
0
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 */
Exemple #4
0
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 */
Exemple #5
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,
                               });