int main() { mongo conn[1]; bson b; int res; if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) { printf("Failed to connect"); exit(1); } res = mongo_simple_str_command( conn, "test", "$eval", "for(i=0; i<100000; i++) { db.foo.find() }", &b ); ASSERT( res == MONGO_OK ); /* 50ms timeout */ mongo_set_op_timeout( conn, 50 ); ASSERT( conn->err == 0 ); res = mongo_simple_str_command( conn, "test", "$eval", "for(i=0; i<100000; i++) { db.foo.find() }", &b ); ASSERT( res == MONGO_ERROR ); ASSERT( conn->err == MONGO_IO_ERROR ); return 0; }
/* Test read timeout by causing the * server to sleep for 10s on a query. */ int test_read_timeout( void ) { mongo conn[1]; bson b, obj, out, fields; int res; CONN_CLIENT_TEST; bson_init( &b ); bson_append_code( &b, "$where", "sleep( 10 * 1000 );"); bson_finish( &b ); bson_init( &obj ); bson_append_string( &obj, "foo", "bar"); bson_finish( &obj ); res = mongo_insert( conn, "test.foo", &obj, NULL ); /* Set the connection timeout here. */ if( mongo_set_op_timeout( conn, 1000 ) != MONGO_OK ) { printf("Could not set socket timeout!."); exit(1); } res = mongo_find_one( conn, "test.foo", &b, bson_empty(&fields), &out ); ASSERT( res == MONGO_ERROR ); ASSERT( conn->err == MONGO_IO_ERROR ); ASSERT( conn->errcode == WSAETIMEDOUT ); return 0; }
SEXP rmongo_connect(SEXP mongo_conn) { mongo* conn = _checkMongo(mongo_conn); mongo_host_port hp; SEXP host = getAttrib(mongo_conn, sym_host); int len = LENGTH(host); int i; if (len == 0) error("No hosts defined\n"); const char* name = CHAR(STRING_ELT(getAttrib(mongo_conn, sym_name), 0)); if (name[0] == '\0') { for (i = 0; i < len; i++) { mongo_parse_host(CHAR(STRING_ELT(host, i)), &hp); if (mongo_connect(conn, hp.host, hp.port) == MONGO_OK) break; } if (i == len) { if (len == 1) Rprintf("Unable to connect to %s:%d, error code = %d\n", hp.host, hp.port, conn->err); else Rprintf("Unable to connect to any of the given hosts, error code = %d\n", conn->err); return mongo_conn; } } else { mongo_replset_init(conn, name); for (i = 0; i < len; i++) { mongo_parse_host(CHAR(STRING_ELT(host, i)), &hp); mongo_replset_add_seed(conn, hp.host, hp.port); } if (mongo_replset_connect(conn) != MONGO_OK) Rprintf("Unable to connect to replset\n"); } int timeout = asInteger(getAttrib(mongo_conn, sym_timeout)); if (timeout > 0) mongo_set_op_timeout(conn, timeout); SEXP username = getAttrib(mongo_conn, sym_username); if (CHAR(STRING_ELT(username, 0))[0] != '\0') { SEXP password = getAttrib(mongo_conn, install("password")); SEXP db = getAttrib(mongo_conn, install("db")); SEXP ret = mongo_authenticate(mongo_conn, username, password, db); if (!LOGICAL(ret)[0]) { mongo_disconnect(conn); Rprintf("Authentication failed.\n"); } } return mongo_conn; }
bool TMongoDriver::open(const QString &db, const QString &user, const QString &password, const QString &host, quint16 port, const QString &) { if (host.isEmpty()) { return false; } if (!port) port = MONGO_DEFAULT_PORT; mongo_clear_errors(mongoConnection); mongo_set_op_timeout(mongoConnection, 1000); int status = mongo_client(mongoConnection, qPrintable(host), port); if (status != MONGO_OK) { switch (mongoConnection->err) { case MONGO_CONN_NO_SOCKET: tSystemError("MongoDB socket error: %s", mongoConnection->lasterrstr); break; case MONGO_CONN_FAIL: tSystemError("MongoDB connection failed: %s", mongoConnection->lasterrstr); break; case MONGO_CONN_NOT_MASTER: tSystemDebug("MongoDB not master: %s", mongoConnection->lasterrstr); break; default: tSystemError("MongoDB error: %s", mongoConnection->lasterrstr); break; } return false; } if (!user.isEmpty()) { status = mongo_cmd_authenticate(mongoConnection, qPrintable(db), qPrintable(user), qPrintable(password)); if (status != MONGO_OK) { tSystemDebug("MongoDB authentication error: %s", mongoConnection->lasterrstr); return false; } } return (status == MONGO_OK); }
SEXP mongo_set_timeout(SEXP mongo_conn, SEXP timeout) { mongo* conn = _checkMongo(mongo_conn); int _timeout = asInteger(timeout); mongo_set_op_timeout(conn, _timeout); return mongo_conn; }
static void ngx_wirte_tracking_into_mongodb(ngx_http_request_t *r, ngx_http_vesb_aio_log_main_conf_t *valcf,ngx_http_vesb_aio_log_loc_conf_t *vallcf,int timeuse) { mongo conn[1]; char *conn_str; int port,status,insertstatus; time_t timenow; ngx_list_part_t *part = &r->headers_in.headers.part; ngx_table_elt_t *header = part->elts; ngx_uint_t i; if(r->headers_out.status == NGX_HTTP_OK) { conn_str = (char *)valcf->mongodb_conn_str.data; port = (int)valcf->mongodb_conn_port; status = mongo_client( conn, conn_str, port ); if( status == MONGO_OK ) { mongo_set_op_timeout( conn, 10000 );//time oust 10000 ms time ( &timenow ); bson b[1]; bson_init( b ); bson_append_new_oid( b, "_id" ); if( r->headers_in.x_forwarded_for == NULL ) { bson_append_string_n( b, "realclientip" , (char *)r->connection->addr_text.data, r->connection->addr_text.len); } else { bson_append_string_n( b, "realclientip" , (char *)r->headers_in.x_forwarded_for->value.data,r->headers_in.x_forwarded_for->value.len); } bson_append_int( b,"statuscode",r->headers_out.status); bson_append_int( b,"usetime",timeuse); bson_append_long( b,"requestsize",r->request_length); bson_append_long( b,"responsesize",r->headers_out.content_length_n); bson_append_time_t( b,"invoketime",timenow ); if(vallcf->app_name.data != NULL) { bson_append_string_n( b,"appname",(char *)vallcf->app_name.data,vallcf->app_name.len); } else { bson_append_string( b,"appname","undefine"); } /*get method name*/ for(i=0;/* void */; ++i) { if(i >= part->nelts) { if(part->next == NULL) { break; } part = part->next; header = part->elts; i=0; } if(header[i].hash == 0) { continue; } if(ngx_strstr(header[i].key.data,"SOAPAction") != NULL) { bson_append_string_n( b,"SOAPAction",(char *)header[i].value.data,header[i].value.len); } else if(ngx_strstr(header[i].key.data,"Content-Type") != NULL) { bson_append_string_n( b,"Content-Type",(char *)header[i].value.data,header[i].value.len); } } bson_finish( b ); insertstatus = mongo_insert( conn,"vesb.tracking", b , NULL ); if( insertstatus != MONGO_OK ) { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "insert tracking log in mongodb is failed!(error:%d)",conn[0].err); } bson_destroy( b ); } else { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "mongodb is unconnection!(error:%d)",status); } mongo_destroy( conn ); } }
int flush_to_mongo(st_http_request * p, int counter) { mongo conn; mongo_init(&conn); mongo_set_op_timeout(&conn, 10000); int status = mongo_client(&conn, globalArgs.host, globalArgs.port); if (status != MONGO_OK) { switch (conn.err) { case MONGO_CONN_SUCCESS: printf("Connected to mongo\n"); case MONGO_CONN_NO_SOCKET: printf("FAIL: Could not create a socket!\n"); break; case MONGO_CONN_ADDR_FAIL: printf("FAIL: MONGO_CONN_ADDR_FAIL: %s\n", globalArgs.host); break; case MONGO_CONN_NOT_MASTER: printf("FAIL: MONGO_CONN_NOT_MASTER\n"); break; case MONGO_CONN_BAD_SET_NAME: printf("FAIL: MONGO_CONN_BAD_SET_NAME\n"); break; case MONGO_CONN_NO_PRIMARY: printf("FAIL: MONGO_CONN_NO_PRIMARY\n"); break; case MONGO_IO_ERROR: printf("FAIL: MONGO_IO_ERROR\n"); break; case MONGO_SOCKET_ERROR: printf("FAIL: MONGO_SOCKET_ERROR\n"); break; case MONGO_READ_SIZE_ERROR: printf("FAIL: MONGO_READ_SIZE_ERROR\n"); break; case MONGO_COMMAND_FAILED: printf("FAIL: MONGO_COMMAND_FAILED\n"); break; case MONGO_WRITE_ERROR: printf("FAIL: MONGO_WRITE_ERROR\n"); break; case MONGO_NS_INVALID: printf("FAIL: MONGO_NS_INVALID\n"); break; case MONGO_BSON_INVALID: printf("FAIL: MONGO_BSON_INVALIDr\n"); break; case MONGO_BSON_NOT_FINISHED: printf("FAIL: MONGO_BSON_NOT_FINISHED\n"); break; case MONGO_BSON_TOO_LARGE: printf("FAIL: MONGO_BSON_TOO_LARGEr\n"); break; case MONGO_WRITE_CONCERN_INVALID: printf("FAIL: Mongo write concern invalid\n"); break; case MONGO_CONN_FAIL: printf ("FAIL: Mongo connection fail. Make sure it's listening at %s:%d\n", globalArgs.host, globalArgs.port); break; } exit(EXIT_FAILURE); } bson **bps; bps = (bson **) malloc(sizeof(bson *) * counter); char *fields = globalArgs.fields; int i = 0; for (i = 0; i < counter; i++) { bson *bp = (bson *) malloc(sizeof(bson)); bson_init(bp); bson_append_new_oid(bp, "_id"); if (fields == NULL || strstr(fields, "req_ip") != NULL) bson_append_string(bp, "req_ip", p[i].req_ip); if (fields == NULL || strstr(fields, "req_ident") != NULL) bson_append_string(bp, "req_ident", p[i].req_ident); if (fields == NULL || strstr(fields, "req_user") != NULL) bson_append_string(bp, "req_user", p[i].req_user); if (fields == NULL || strstr(fields, "req_datetime") != NULL) bson_append_string(bp, "req_datetime", p[i].req_datetime); if (fields == NULL || strstr(fields, "req_method") != NULL) bson_append_string(bp, "req_method", p[i].req_method); if (fields == NULL || strstr(fields, "req_uri") != NULL) bson_append_string(bp, "req_uri", p[i].req_uri); if (fields == NULL || strstr(fields, "req_proto") != NULL) bson_append_string(bp, "req_proto", p[i].req_proto); if (fields == NULL || strstr(fields, "resp_code") != NULL) bson_append_int(bp, "resp_code", p[i].resp_code); if (fields == NULL || strstr(fields, "resp_bytes") != NULL) bson_append_int(bp, "resp_bytes", atoi(p[i].resp_bytes)); if (fields == NULL || strstr(fields, "req_referer") != NULL) bson_append_string(bp, "req_referer", p[i].req_referer); if (fields == NULL || strstr(fields, "req_agent") != NULL) bson_append_string(bp, "req_agent", p[i].req_agent); bson_finish(bp); bps[i] = bp; } mongo_insert_batch(&conn, globalArgs.collection, (const bson **) bps, counter, 0, MONGO_CONTINUE_ON_ERROR); mongo_destroy(&conn); int j = 0; for (j = 0; j < counter; j++) { bson_destroy(bps[j]); free(bps[j]); } free(bps); return (0); }
/** * \brief This function tries to connect to MongoDB server */ int vs_mongo_conn_init(struct VS_CTX *vs_ctx) { int status; vs_ctx->mongo_conn = mongo_alloc(); mongo_init(vs_ctx->mongo_conn); /* Set connection timeout */ mongo_set_op_timeout(vs_ctx->mongo_conn, 1000); /* Connect to MongoDB server */ status = mongo_client(vs_ctx->mongo_conn, vs_ctx->mongodb_server, vs_ctx->mongodb_port); if( status != MONGO_OK ) { switch ( vs_ctx->mongo_conn->err ) { case MONGO_CONN_NO_SOCKET: v_print_log(VRS_PRINT_ERROR, "No MongoDB server %s:%d socket\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_FAIL: v_print_log(VRS_PRINT_ERROR, "Connection to MongoDB server %s:%d failed\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_NOT_MASTER: v_print_log(VRS_PRINT_ERROR, "MongoDB server %s:%d is not master\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; default: v_print_log(VRS_PRINT_ERROR, "Failed to connect to MongoDB server %s:%d , error: %d\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port, vs_ctx->mongo_conn->err); break; } mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Connection to MongoDB server %s:%d succeeded\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); /* There has to be some db name defined */ if(vs_ctx->mongodb_db_name == NULL) { v_print_log(VRS_PRINT_ERROR, "No database name defined\n"); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } /* Try to do when authentication is configured */ if(vs_ctx->mongodb_user != NULL && vs_ctx->mongodb_pass != NULL) { status = mongo_cmd_authenticate(vs_ctx->mongo_conn, vs_ctx->mongodb_db_name, vs_ctx->mongodb_user, vs_ctx->mongodb_pass); if(status != MONGO_OK) { v_print_log(VRS_PRINT_ERROR, "Authentication to %s database failed, error: %s\n", vs_ctx->mongodb_db_name, mongo_get_server_err_string(vs_ctx->mongo_conn)); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Authentication to %s database succeeded\n", vs_ctx->mongodb_db_name); } else { v_print_log(VRS_PRINT_DEBUG_MSG, "No MongoDB authentication required\n"); } /* Namespace used for storing nodes */ vs_ctx->mongo_node_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 6 + 1)); strcpy(vs_ctx->mongo_node_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_node_ns, ".nodes"); /* Namespace used for storing tag groups and tags */ vs_ctx->mongo_tg_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 11 + 1)); strcpy(vs_ctx->mongo_tg_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_tg_ns, ".tag_groups"); /* Namespace used for storing layers */ vs_ctx->mongo_layer_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 7 + 1)); strcpy(vs_ctx->mongo_layer_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_layer_ns, ".layers"); return 1; }
static int wm_write (const data_set_t *ds, /* {{{ */ const value_list_t *vl, user_data_t *ud) { wm_node_t *node = ud->data; char collection_name[512]; bson *bson_record; int status; ssnprintf (collection_name, sizeof (collection_name), "collectd.%s", vl->plugin); bson_record = wm_create_bson (ds, vl, node->store_rates); if (bson_record == NULL) return (ENOMEM); pthread_mutex_lock (&node->lock); if (!mongo_is_connected (node->conn)) { INFO ("write_mongodb plugin: Connecting to [%s]:%i", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : MONGO_DEFAULT_PORT); status = mongo_connect (node->conn, node->host, node->port); if (status != MONGO_OK) { ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : MONGO_DEFAULT_PORT); mongo_destroy (node->conn); pthread_mutex_unlock (&node->lock); return (-1); } if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL)) { status = mongo_cmd_authenticate (node->conn, node->db, node->user, node->passwd); if (status != MONGO_OK) { ERROR ("write_mongodb plugin: Authenticating to [%s]%i for database " "\"%s\" as user \"%s\" failed.", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : MONGO_DEFAULT_PORT, node->db, node->user); mongo_destroy (node->conn); pthread_mutex_unlock (&node->lock); return (-1); } } if (node->timeout > 0) { status = mongo_set_op_timeout (node->conn, node->timeout); if (status != MONGO_OK) { WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s", node->timeout, node->conn->errstr); } } } /* Assert if the connection has been established */ assert (mongo_is_connected (node->conn)); #if MONGO_MINOR >= 6 /* There was an API change in 0.6.0 as linked below */ /* https://github.com/mongodb/mongo-c-driver/blob/master/HISTORY.md */ status = mongo_insert (node->conn, collection_name, bson_record, NULL); #else status = mongo_insert (node->conn, collection_name, bson_record); #endif if (status != MONGO_OK) { ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err); if (node->conn->err != MONGO_BSON_INVALID) ERROR ("write_mongodb plugin: %s", node->conn->errstr); else ERROR ("write_mongodb plugin: Invalid BSON structure, error = %#x", (unsigned int) bson_record->err); /* Disconnect except on data errors. */ if ((node->conn->err != MONGO_BSON_INVALID) && (node->conn->err != MONGO_BSON_NOT_FINISHED)) mongo_destroy (node->conn); } pthread_mutex_unlock (&node->lock); /* free our resource as not to leak memory */ bson_destroy (bson_record); /* matches bson_init() */ bson_dealloc (bson_record); /* matches bson_alloc() */ return (0); } /* }}} int wm_write */
static int wm_write (const data_set_t *ds, /* {{{ */ const value_list_t *vl, user_data_t *ud) { wm_node_t *node = ud->data; char collection_name[512]; bson *bson_record; int status; ssnprintf (collection_name, sizeof (collection_name), "collectd.%s", vl->plugin); bson_record = wm_create_bson (ds, vl, node->store_rates); if (bson_record == NULL) return (ENOMEM); pthread_mutex_lock (&node->lock); if (!mongo_is_connected (node->conn)) { INFO ("write_mongodb plugin: Connecting to [%s]:%i", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : MONGO_DEFAULT_PORT); status = mongo_connect (node->conn, node->host, node->port); if (status != MONGO_OK) { ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : MONGO_DEFAULT_PORT); mongo_destroy (node->conn); pthread_mutex_unlock (&node->lock); return (-1); } if (node->timeout > 0) { status = mongo_set_op_timeout (node->conn, node->timeout); if (status != MONGO_OK) { WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s", node->timeout, node->conn->errstr); } } } /* Assert if the connection has been established */ assert (mongo_is_connected (node->conn)); status = mongo_insert (node->conn, collection_name, bson_record); if(status != MONGO_OK) { ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err); if (node->conn->err != MONGO_BSON_INVALID) ERROR ("write_mongodb plugin: %s", node->conn->errstr); else if (bson_record->err) ERROR ("write_mongodb plugin: %s", bson_record->errstr); /* Disconnect except on data errors. */ if ((node->conn->err != MONGO_BSON_INVALID) && (node->conn->err != MONGO_BSON_NOT_FINISHED)) mongo_destroy (node->conn); } pthread_mutex_unlock (&node->lock); /* free our resource as not to leak memory */ bson_dispose (bson_record); return (0); } /* }}} int wm_write */