From ee8e701cddaac8e786f388beff89b57f84a1a346 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 11 Aug 2012 08:21:43 +0200 Subject: Send fields specified by a template to mongodb. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- plugins/ommongodb/ommongodb.c | 43 +++++++++++++++++++++++-------------------- runtime/rsyslog.h | 1 + 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/plugins/ommongodb/ommongodb.c b/plugins/ommongodb/ommongodb.c index 8873d5ac..874ee63c 100644 --- a/plugins/ommongodb/ommongodb.c +++ b/plugins/ommongodb/ommongodb.c @@ -302,6 +302,7 @@ static rsRetVal writeMongoDB_fields(struct templateField *fields, instanceData *pData) { bson *doc = NULL; + char *fieldStr = NULL; size_t i; DEFiRet; @@ -310,39 +311,41 @@ writeMongoDB_fields(struct templateField *fields, instanceData *pData) CHKiRet(initMongoDB(pData, 0)); } - dbgprintf("+++start+++\n"); - for(i = 0; fields[i].fieldName != NULL; i++) { - dbgprintf("%.*s: %s\n", (int)es_strlen(fields[i].fieldName), - es_getBufAddr(fields[i].fieldName), fields[i].value); + doc = bson_new(); + if(doc == NULL) { + reportMongoError(pData); + dbgprintf("ommongodb: error creating BSON doc\n"); + ABORT_FINALIZE(RS_RET_SUSPENDED); } - dbgprintf("---end---\n"); -#if 0 - doc = bson_build(BSON_TYPE_STRING, "sys", sys, sys_len, - BSON_TYPE_UTC_DATETIME, "time", ts_gen, - BSON_TYPE_UTC_DATETIME, "time_rcvd", ts_rcv, - BSON_TYPE_STRING, "msg", msg, msg_len, - BSON_TYPE_INT32, "syslog_fac", facil, - BSON_TYPE_INT32, "syslog_sever", severity, - BSON_TYPE_STRING, "syslog_tag", tag, tag_len, - BSON_TYPE_STRING, "procid", procid, procid_len, - BSON_TYPE_STRING, "pid", pid, pid_len, - BSON_TYPE_STRING, "level", getLumberjackLevel(pMsg->iSeverity), -1, - BSON_TYPE_NONE); - if(doc == NULL) { + for(i = 0; fields[i].fieldName != NULL && doc != NULL; i++) { + CHKmalloc(fieldStr = es_str2cstr(fields[i].fieldName, NULL)); + if (!bson_validate_key(fieldStr, FALSE, TRUE)) { + dbgprintf("ommongodb: field name \"%s\" is invalid\n", + fieldStr); + } else if (!bson_append_string(doc, fieldStr, + (const gchar *)fields[i].value, -1)) { + reportMongoError(pData); + dbgprintf("ommongodb: error creating BSON doc\n"); + ABORT_FINALIZE(RS_RET_SUSPENDED); + } + free(fieldStr); + fieldStr = NULL; + } + if(!bson_finish(doc)) { reportMongoError(pData); dbgprintf("ommongodb: error creating BSON doc\n"); ABORT_FINALIZE(RS_RET_SUSPENDED); } - bson_finish(doc); + if(!mongo_sync_cmd_insert(pData->conn, (char*)pData->dbNcoll, doc, NULL)) { reportMongoError(pData); dbgprintf("ommongodb: insert error\n"); ABORT_FINALIZE(RS_RET_SUSPENDED); } -#endif finalize_it: + free(fieldStr); if(doc != NULL) bson_free(doc); RETiRet; diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h index 8d91f0e7..16ddb594 100644 --- a/runtime/rsyslog.h +++ b/runtime/rsyslog.h @@ -381,6 +381,7 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth RS_RET_MODULE_ALREADY_IN_CONF = -2221, /**< module already in current configuration */ RS_RET_PARAM_NOT_PERMITTED = -2222, /**< legacy parameter no longer permitted (usally already set by v2) */ RS_RET_NO_FIELD_PASSING = -2223, /**< output module interface parameter passing mode "FIELDS" is not available but required */ + RS_RET_INVALID_FIELD_NAME = -2224, /**< field name is invalid */ /* RainerScript error messages (range 1000.. 1999) */ RS_RET_SYSVAR_NOT_FOUND = 1001, /**< system variable could not be found (maybe misspelled) */ -- cgit