summaryrefslogtreecommitdiffstats
path: root/plugins/ommongodb/ommongodb.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/ommongodb/ommongodb.c')
-rw-r--r--plugins/ommongodb/ommongodb.c43
1 files changed, 23 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;