Esempio n. 1
0
gboolean
test_env_setup (void)
{
  config.primary_host = config.secondary_host = NULL;
  config.primary_port = config.secondary_port = 27017;
  config.db = g_strdup ("test");
  config.coll = g_strdup ("libmongo");

  if (getenv ("TEST_DB"))
    {
      g_free (config.db);
      config.db = g_strdup (getenv ("TEST_DB"));
    }
  if (getenv ("TEST_COLLECTION"))
    {
      g_free (config.coll);
      config.coll = g_strdup (getenv ("TEST_COLLECTION"));
    }
  config.ns = g_strconcat (config.db, ".", config.coll, NULL);

  config.gfs_prefix = g_strconcat (config.ns, ".", "grid", NULL);

  if (!getenv ("TEST_PRIMARY") || strlen (getenv ("TEST_PRIMARY")) == 0)
    return FALSE;

  if (!mongo_util_parse_addr (getenv ("TEST_PRIMARY"), &config.primary_host,
                              &config.primary_port))
    return FALSE;

  if (getenv ("TEST_SECONDARY") && strlen (getenv ("TEST_SECONDARY")) > 0)
    mongo_util_parse_addr (getenv ("TEST_SECONDARY"), &config.secondary_host,
                           &config.secondary_port);

  return TRUE;
}
Esempio n. 2
0
static gboolean
afmongodb_dd_init(LogPipe *s)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  ValuePairsTransformSet *vpts;

  if (!log_dest_driver_init_method(s))
    return FALSE;

  log_template_options_init(&self->template_options, cfg);

  /* Always replace a leading dot with an underscore. */
  vpts = value_pairs_transform_set_new(".*");
  value_pairs_transform_set_add_func(vpts, value_pairs_new_transform_replace_prefix(".", "_"));
  value_pairs_add_transforms(self->vp, vpts);

  if (self->port != MONGO_CONN_LOCAL)
    {
      if (self->address)
        {
          gchar *srv = g_strdup_printf ("%s:%d", self->address,
                                        (self->port) ? self->port : 27017);
          self->servers = g_list_prepend (self->servers, srv);
          g_free (self->address);
        }

      if (!self->servers)
        afmongodb_dd_set_servers((LogDriver *)self, g_list_append (NULL, g_strdup ("127.0.0.1:27017")));

      self->address = NULL;
      self->port = 27017;
      if (!mongo_util_parse_addr(g_list_nth_data(self->servers, 0),
                                 &self->address,
                                 &self->port))
        {
          msg_error("Cannot parse the primary host",
                    evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
                    NULL);
          return FALSE;
        }
    }

  if (self->port == MONGO_CONN_LOCAL)
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                NULL);
  else
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_int("port", self->port),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                NULL);

  return log_threaded_dest_driver_start(s);
}
Esempio n. 3
0
static gboolean
afmongodb_dd_connect(MongoDBDestDriver *self, gboolean reconnect)
{
  GList *l;

  if (reconnect && self->conn)
    return TRUE;

  self->conn = mongo_sync_connect(self->host, self->port, FALSE);
  if (!self->conn)
    {
      msg_error ("Error connecting to MongoDB", NULL);
      return FALSE;
    }

  mongo_sync_conn_set_safe_mode(self->conn, self->safe_mode);

  l = self->servers;
  while ((l = g_list_next(l)) != NULL)
    {
      gchar *host = NULL;
      gint port = 27017;

      if (!mongo_util_parse_addr(l->data, &host, &port))
	{
	  msg_warning("Cannot parse MongoDB server address, ignoring",
		      evt_tag_str("address", l->data),
		      NULL);
	  continue;
	}
      mongo_sync_conn_seed_add (self->conn, host, port);
      msg_verbose("Added MongoDB server seed",
		  evt_tag_str("host", host),
		  evt_tag_int("port", port),
		  NULL);
      g_free(host);
    }

  /*
  if (self->user || self->password)
    {
      if (!self->user || !self->password)
	{
	  msg_error("Neither the username, nor the password can be empty", NULL);
	  return FALSE;
	}

      if (mongo_cmd_authenticate(&self->mongo_conn, self->db, self->user, self->password) != 1)
	{
	  msg_error("MongoDB authentication failed", NULL);
	  return FALSE;
	}
    }
  */

  return TRUE;
}
Esempio n. 4
0
static gboolean
afmongodb_dd_init(LogPipe *s)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)s;
  GlobalConfig *cfg = log_pipe_get_config(s);

  if (!log_dest_driver_init_method(s))
    return FALSE;

  if (cfg)
    self->time_reopen = cfg->time_reopen;

  if (!self->vp)
    {
      self->vp = value_pairs_new();
      value_pairs_add_scope(self->vp, "selected-macros");
      value_pairs_add_scope(self->vp, "nv-pairs");
    }

  self->host = NULL;
  self->port = 27017;
  if (!mongo_util_parse_addr(g_list_nth_data(self->servers, 0), &self->host,
			     &self->port))
    {
      msg_error("Cannot parse the primary host",
		evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
		NULL);
      return FALSE;
    }

  msg_verbose("Initializing MongoDB destination",
	      evt_tag_str("host", self->host),
	      evt_tag_int("port", self->port),
	      evt_tag_str("database", self->db),
	      evt_tag_str("collection", self->coll),
	      NULL);

  self->queue = log_dest_driver_acquire_queue(&self->super, afmongodb_dd_format_persist_name(self));

  stats_lock();
  stats_register_counter(0, SCS_MONGODB | SCS_DESTINATION, self->super.super.id,
			 afmongodb_dd_format_stats_instance(self),
			 SC_TYPE_STORED, &self->stored_messages);
  stats_register_counter(0, SCS_MONGODB | SCS_DESTINATION, self->super.super.id,
			 afmongodb_dd_format_stats_instance(self),
			 SC_TYPE_DROPPED, &self->dropped_messages);
  stats_unlock();

  log_queue_set_counters(self->queue, self->stored_messages, self->dropped_messages);
  afmongodb_dd_start_thread(self);

  return TRUE;
}
Esempio n. 5
0
mongo_sync_pool *
mongo_sync_pool_new (const gchar *host,
                     gint port,
                     gint nmasters, gint nslaves)
{
  mongo_sync_pool *pool;
  mongo_sync_pool_connection *conn;
  gint i, j = 0;

  if (!host || port < 0)
    {
      errno = EINVAL;
      return NULL;
    }
  if (nmasters < 0 || nslaves < 0)
    {
      errno = ERANGE;
      return NULL;
    }
  if (nmasters + nslaves <= 0)
    {
      errno = EINVAL;
      return NULL;
    }

  conn = _mongo_sync_pool_connect (host, port, FALSE);
  if (!conn)
    return FALSE;

  if (!mongo_sync_cmd_is_master ((mongo_sync_connection *)conn))
    {
      mongo_sync_disconnect ((mongo_sync_connection *)conn);
      errno = EPROTO;
      return NULL;
    }

  pool = g_new0 (mongo_sync_pool, 1);
  pool->nmasters = nmasters;
  pool->nslaves = nslaves;

  for (i = 0; i < pool->nmasters; i++)
    {
      mongo_sync_pool_connection *c;

      c = _mongo_sync_pool_connect (host, port, FALSE);
      c->pool_id = i;

      pool->masters = g_list_append (pool->masters, c);
    }

  for (i = 0; i < pool->nslaves; i++)
    {
      mongo_sync_pool_connection *c;
      gchar *shost = NULL;
      gint sport = 27017;
      GList *l;
      gboolean found = FALSE;
      gboolean need_restart = (j != 0);

      /* Select the next secondary */
      l = g_list_nth (conn->super.rs.hosts, j);

      do
        {
          j++;
          if (l && mongo_util_parse_addr ((gchar *)l->data, &shost, &sport))
            {
              if (sport != port || strcmp (host, shost) != 0)
                {
                  found = TRUE;
                  break;
                }
            }
          l = g_list_next (l);
          if (!l && need_restart)
            {
              need_restart = FALSE;
              j = 0;
              l = g_list_nth (conn->super.rs.hosts, j);
            }
        }
      while (l);

      if (!found)
        {
          pool->nslaves = i - 1;
          break;
        }

      /* Connect to it*/
      c = _mongo_sync_pool_connect (shost, sport, TRUE);
      c->pool_id = pool->nmasters + i + 1;

      pool->slaves = g_list_append (pool->slaves, c);
    }

  mongo_sync_disconnect ((mongo_sync_connection *)conn);
  return pool;
}
Esempio n. 6
0
static gboolean
afmongodb_dd_init(LogPipe *s)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)s;
  GlobalConfig *cfg = log_pipe_get_config(s);
  ValuePairsTransformSet *vpts;

  if (!log_dest_driver_init_method(s))
    return FALSE;

  if (cfg)
    self->time_reopen = cfg->time_reopen;

  if (!self->vp)
    {
      self->vp = value_pairs_new();
      value_pairs_add_scope(self->vp, "selected-macros");
      value_pairs_add_scope(self->vp, "nv-pairs");
    }

  /* Always replace a leading dot with an underscore. */
  vpts = value_pairs_transform_set_new(".*");
  value_pairs_transform_set_add_func(vpts, value_pairs_new_transform_replace(".", "_"));
  value_pairs_add_transforms(self->vp, vpts);

  if (self->port != MONGO_CONN_LOCAL)
    {
      if (self->address)
        {
          gchar *srv = g_strdup_printf ("%s:%d", self->address,
                                        (self->port) ? self->port : 27017);
          self->servers = g_list_prepend (self->servers, srv);
          g_free (self->address);
        }

      if (!self->servers)
        afmongodb_dd_set_servers((LogDriver *)self, g_list_append (NULL, g_strdup ("127.0.0.1:27017")));

      self->address = NULL;
      self->port = 27017;
      if (!mongo_util_parse_addr(g_list_nth_data(self->servers, 0),
                                 &self->address,
                                 &self->port))
        {
          msg_error("Cannot parse the primary host",
                    evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
                    NULL);
          return FALSE;
        }
    }

  if (self->port == MONGO_CONN_LOCAL)
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                NULL);
  else
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_int("port", self->port),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                NULL);

  self->queue = log_dest_driver_acquire_queue(&self->super, afmongodb_dd_format_persist_name(self));

  stats_lock();
  stats_register_counter(0, SCS_MONGODB | SCS_DESTINATION, self->super.super.id,
			 afmongodb_dd_format_stats_instance(self),
			 SC_TYPE_STORED, &self->stored_messages);
  stats_register_counter(0, SCS_MONGODB | SCS_DESTINATION, self->super.super.id,
			 afmongodb_dd_format_stats_instance(self),
			 SC_TYPE_DROPPED, &self->dropped_messages);
  stats_unlock();

  log_queue_set_counters(self->queue, self->stored_messages, self->dropped_messages);
  afmongodb_dd_start_thread(self);

  return TRUE;
}
Esempio n. 7
0
static gboolean
afmongodb_dd_init(LogPipe *s)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)s;
  GlobalConfig *cfg = log_pipe_get_config(s);

  if (!log_dest_driver_init_method(s))
    return FALSE;

  log_template_options_init(&self->template_options, cfg);

  if (!afmongodb_dd_check_auth_options(self))
    return FALSE;

  afmongodb_dd_init_value_pairs_dot_to_underscore_transformation(self);
  if (self->port != MONGO_CONN_LOCAL)
    {
      if (self->address)
        {
          gchar *srv = g_strdup_printf ("%s:%d", self->address,
                                        (self->port) ? self->port : 27017);
          self->servers = g_list_prepend (self->servers, srv);
          g_free (self->address);
        }

      if (self->servers)
        {
          GList *l;

          for (l=self->servers; l; l = g_list_next(l))
            {
              gchar *host = NULL;
              gint port = 27017;

              if (!mongo_util_parse_addr(l->data, &host, &port))
                {
                  msg_warning("Cannot parse MongoDB server address, ignoring",
                              evt_tag_str("address", l->data),
                              evt_tag_str("driver", self->super.super.super.id),
                              NULL);
                  continue;
                }
              mongo_sync_conn_recovery_cache_seed_add (self->recovery_cache, host, port);
              msg_verbose("Added MongoDB server seed",
                          evt_tag_str("host", host),
                          evt_tag_int("port", port),
                          evt_tag_str("driver", self->super.super.super.id),
                          NULL);
              g_free(host);
            }
        }
      else
        {
          afmongodb_dd_set_servers((LogDriver *)self, g_list_append (NULL, g_strdup ("127.0.0.1:27017")));
          mongo_sync_conn_recovery_cache_seed_add (self->recovery_cache, "127.0.0.1", 27017);
        }

      self->address = NULL;
      self->port = 27017;
      if (!mongo_util_parse_addr(g_list_nth_data(self->servers, 0),
                                 &self->address,
                                 &self->port))
        {
          msg_error("Cannot parse the primary host",
                    evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
                    evt_tag_str("driver", self->super.super.super.id),
                    NULL);
          return FALSE;
        }
    }
  else
    {
      mongo_sync_conn_recovery_cache_seed_add (self->recovery_cache, self->address, self->port);
    }

  if (self->port == MONGO_CONN_LOCAL)
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                evt_tag_str("driver", self->super.super.super.id),
                NULL);
  else
    msg_verbose("Initializing MongoDB destination",
                evt_tag_str("address", self->address),
                evt_tag_int("port", self->port),
                evt_tag_str("database", self->db),
                evt_tag_str("collection", self->coll),
                evt_tag_str("driver", self->super.super.super.id),
                NULL);

  return log_threaded_dest_driver_start(s);
}