Example #1
0
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 */
Example #2
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 */
Example #3
0
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 */
Example #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 = (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 */