diff options
-rw-r--r-- | plugins/imkmsg/imkmsg.c | 30 | ||||
-rw-r--r-- | plugins/imkmsg/imkmsg.h | 2 | ||||
-rw-r--r-- | plugins/imkmsg/kmsg.c | 89 |
3 files changed, 45 insertions, 76 deletions
diff --git a/plugins/imkmsg/imkmsg.c b/plugins/imkmsg/imkmsg.c index dc8c1b93..20c49f60 100644 --- a/plugins/imkmsg/imkmsg.c +++ b/plugins/imkmsg/imkmsg.c @@ -63,7 +63,6 @@ DEFobjCurrIf(errmsg) /* config settings */ typedef struct configSettings_s { - int bPermitNonKernel; /* permit logging of messages not having LOG_KERN facility */ int iFacilIntMsg; /* the facility to use for internal messages (set by driver) */ uchar *pszPath; int console_log_level; /* still used for BSD */ @@ -77,7 +76,6 @@ static int bLegacyCnfModGlobalsPermitted;/* are legacy module-global config para /* module-global parameters */ static struct cnfparamdescr modpdescr[] = { { "logpath", eCmdHdlrGetWord, 0 }, - { "permitnonkernelfacility", eCmdHdlrBinary, 0 }, { "consoleloglevel", eCmdHdlrInt, 0 }, { "internalmsgfacility", eCmdHdlrFacility, 0 } }; @@ -87,16 +85,12 @@ static struct cnfparamblk modpblk = modpdescr }; - - static prop_t *pInputName = NULL; /* there is only one global inputName for all messages generated by this module */ static prop_t *pLocalHostIP = NULL; /* a pseudo-constant propterty for 127.0.0.1 */ - static inline void initConfigSettings(void) { - cs.bPermitNonKernel = 0; cs.console_log_level = -1; cs.pszPath = NULL; cs.iFacilIntMsg = klogFacilIntMsg(); @@ -109,7 +103,7 @@ initConfigSettings(void) * rgerhards, 2008-04-12 */ static rsRetVal -enqMsg(uchar *msg, uchar* pszTag, int iFacility, int iSeverity, struct timeval *tp) +enqMsg(uchar *msg, uchar* pszTag, int iFacility, int iSeverity, struct timeval *tp, struct json_object *json) { struct syslogTime st; msg_t *pMsg; @@ -134,6 +128,7 @@ enqMsg(uchar *msg, uchar* pszTag, int iFacility, int iSeverity, struct timeval * MsgSetTAG(pMsg, pszTag, ustrlen(pszTag)); pMsg->iFacility = iFacility; pMsg->iSeverity = iSeverity; + pMsg->json = json; CHKiRet(submitMsg(pMsg)); finalize_it: @@ -160,21 +155,12 @@ rsRetVal imkmsgLogIntMsg(int priority, char *fmt, ...) } -/* log a kernel message. If tp is non-NULL, it contains the message creation - * time to use. - * rgerhards, 2008-04-14 +/* log a message from /dev/kmsg */ -rsRetVal Syslog(int priority, uchar *pMsg, struct timeval *tp) +rsRetVal Syslog(int priority, uchar *pMsg, struct timeval *tp, struct json_object *json) { DEFiRet; - - // XXX - support it? - /* ignore non-kernel messages if not permitted */ -// if(cs.bPermitNonKernel == 0 && LOG_FAC(priority) != LOG_KERN) -// FINALIZE; /* silently ignore */ - - iRet = enqMsg((uchar*)pMsg, (uchar*) "kernel:", LOG_FAC(priority), LOG_PRI(priority), tp); - + iRet = enqMsg((uchar*)pMsg, (uchar*) "kernel:", LOG_FAC(priority), LOG_PRI(priority), tp, json); RETiRet; } @@ -244,8 +230,6 @@ CODESTARTsetModCnf continue; if(!strcmp(modpblk.descr[i].name, "logpath")) { loadModConf->pszPath = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL); - } else if(!strcmp(modpblk.descr[i].name, "permitnonkernelfacility")) { - loadModConf->bPermitNonKernel = (int) pvals[i].val.d.n; } else if(!strcmp(modpblk.descr[i].name, "consoleloglevel")) { loadModConf->console_log_level= (int) pvals[i].val.d.n; } else if(!strcmp(modpblk.descr[i].name, "internalmsgfacility")) { @@ -270,7 +254,6 @@ BEGINendCnfLoad CODESTARTendCnfLoad if(!loadModConf->configSetViaV2Method) { /* persist module-specific settings from legacy config system */ - loadModConf->bPermitNonKernel = cs.bPermitNonKernel; loadModConf->iFacilIntMsg = cs.iFacilIntMsg; loadModConf->console_log_level = cs.console_log_level; if((cs.pszPath == NULL) || (cs.pszPath[0] == '\0')) { @@ -346,7 +329,6 @@ ENDqueryEtryPt static rsRetVal resetConfigVariables(uchar __attribute__((unused)) *pp, void __attribute__((unused)) *pVal) { - cs.bPermitNonKernel = 0; if(cs.pszPath != NULL) { free(cs.pszPath); cs.pszPath = NULL; @@ -382,8 +364,6 @@ CODEmodInit_QueryRegCFSLineHdlr NULL, NULL, STD_LOADABLE_MODULE_ID)); CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogusesyscallinterface", 0, eCmdHdlrGoneAway, NULL, NULL, STD_LOADABLE_MODULE_ID)); - CHKiRet(regCfSysLineHdlr2((uchar *)"klogpermitnonkernelfacility", 0, eCmdHdlrBinary, - NULL, &cs.bPermitNonKernel, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); CHKiRet(regCfSysLineHdlr2((uchar *)"klogconsoleloglevel", 0, eCmdHdlrInt, NULL, &cs.console_log_level, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); CHKiRet(regCfSysLineHdlr2((uchar *)"kloginternalmsgfacility", 0, eCmdHdlrFacility, diff --git a/plugins/imkmsg/imkmsg.h b/plugins/imkmsg/imkmsg.h index d33186b0..220a1634 100644 --- a/plugins/imkmsg/imkmsg.h +++ b/plugins/imkmsg/imkmsg.h @@ -48,7 +48,7 @@ int klogFacilIntMsg(); /* the functions below may be called by the drivers */ rsRetVal imkmsgLogIntMsg(int priority, char *fmt, ...) __attribute__((format(printf,2, 3))); -rsRetVal Syslog(int priority, uchar *msg, struct timeval *tp); +rsRetVal Syslog(int priority, uchar *msg, struct timeval *tp, struct json_object *json); /* prototypes */ extern int klog_getMaxLine(void); /* work-around for klog drivers to get configured max line size */ diff --git a/plugins/imkmsg/kmsg.c b/plugins/imkmsg/kmsg.c index 88e00502..9f1de60f 100644 --- a/plugins/imkmsg/kmsg.c +++ b/plugins/imkmsg/kmsg.c @@ -33,6 +33,7 @@ #include <string.h> #include <ctype.h> #include <sys/klog.h> +#include <json/json.h> #include "rsyslog.h" #include "srUtils.h" @@ -53,15 +54,20 @@ static int fklog = -1; /* kernel log fd */ static void submitSyslog(uchar *buf) { - struct timeval tv; long offs = 0; + struct timeval tv; long int timestamp = 0; struct timespec monotonic; struct timespec realtime; - char outMsg[8096]; + char name[1024]; + char value[1024]; + char msg[1024]; int priority = 0; + long int sequnum = 0; + struct json_object *json = NULL, *jval; - offs = snprintf(outMsg, 8, "%s", "@cee: {"); + /* create new json object */ + json = json_object_new_object(); /* get priority */ for (; isdigit(*buf); buf++) { @@ -69,12 +75,13 @@ submitSyslog(uchar *buf) } buf++; - /* messages sequence number */ - offs += snprintf(outMsg+offs, 12, "%s", "\"sequnum\":\""); - for (; isdigit(*buf); buf++, offs++) { - outMsg[offs] = *buf; + /* get messages sequence number and add it to json */ + for (; isdigit(*buf); buf++) { + sequnum = (sequnum * 10) + (*buf - '0'); } buf++; /* skip , */ + jval = json_object_new_int(sequnum); + json_object_object_add(json, "sequnum", jval); /* get timestamp */ for (; isdigit(*buf); buf++) { @@ -82,34 +89,40 @@ submitSyslog(uchar *buf) } buf++; /* skip ; */ - offs += snprintf(outMsg+offs, 10, "%s", "\",\"msg\":\""); - + /* get message */ + offs = 0; for (; *buf != '\n' && *buf != '\0'; buf++, offs++) { - outMsg[offs] = *buf; + msg[offs] = *buf; } + msg[offs] = '\0'; + jval = json_object_new_string((char*)msg); + json_object_object_add(json, "msg", jval); if (*buf != '\0') /* message has appended properties, skip \n */ buf++; while (strlen((char *)buf)) { - offs += snprintf(outMsg+offs, 4, "%s", "\",\""); + /* get name of the property */ buf++; /* skip ' ' */ - for (; *buf != '=' && *buf != ' '; buf++, offs++) { /* separator is = or ' ' */ - outMsg[offs] = *buf; + offs = 0; + for (; *buf != '=' && *buf != ' '; buf++, offs++) { + name[offs] = *buf; } - buf++; /* skip = */ + name[offs] = '\0'; + buf++; /* skip = or ' ' */; - offs += snprintf(outMsg+offs, 4, "%s", "\":\""); + offs = 0; for (; *buf != '\n' && *buf != '\0'; buf++, offs++) { - outMsg[offs] = *buf; + value[offs] = *buf; } - - if (*buf != '\0') + value[offs] = '\0'; + if (*buf != '\0') { buf++; /* another property, skip \n */ - } - offs += snprintf(outMsg+offs, 3, "%s", "\"}"); + } - outMsg[offs] = '\0'; + jval = json_object_new_string((char*)value); + json_object_object_add(json, name, jval); + } /* calculate timestamp */ clock_gettime(CLOCK_MONOTONIC, &monotonic); @@ -117,7 +130,7 @@ submitSyslog(uchar *buf) tv.tv_sec = realtime.tv_sec + ((timestamp / 1000000l) - monotonic.tv_sec); tv.tv_usec = (realtime.tv_nsec + ((timestamp / 1000000000l) - monotonic.tv_nsec)) / 1000; - Syslog(priority, (uchar *)outMsg, &tv); + Syslog(priority, (uchar *)msg, &tv, json); } @@ -161,41 +174,17 @@ finalize_it: * record of printk buffer. */ static void -readklog(void) +readkmsg(void) { int i; - uchar pRcv[1024+1]; /* LOG_LINE_MAX is 1024 */ + uchar pRcv[8096+1]; char errmsg[2048]; -#if 0 -XXX not sure if LOG_LINE_MAX is 1024 -- int iMaxLine; -- uchar bufRcv[128*1024+1]; -+ uchar pRcv[1024+1]; /* LOG_LINE_MAX is 1024 */ - char errmsg[2048]; -- uchar *pRcv = NULL; /* receive buffer */ -- -- iMaxLine = klog_getMaxLine(); -- -- /* we optimize performance: if iMaxLine is below our fixed size buffer (which -- * usually is sufficiently large), we use this buffer. if it is higher, heap memory -- * is used. We could use alloca() to achive a similar aspect, but there are so -- * many issues with alloca() that I do not want to take that route. -- * rgerhards, 2008-09-02 -- */ -- if((size_t) iMaxLine < sizeof(bufRcv) - 1) { -- pRcv = bufRcv; -- } else { -- if((pRcv = (uchar*) MALLOC(sizeof(uchar) * (iMaxLine + 1))) == NULL) -- iMaxLine = sizeof(bufRcv) - 1; /* better this than noting */ -- } -#endif - for (;;) { dbgprintf("imkmsg waiting for kernel log line\n"); /* every read() from the opened device node receives one record of the printk buffer */ - i = read(fklog, pRcv, 1024); + i = read(fklog, pRcv, 8096); if (i > 0) { /* successful read of message of nonzero length */ @@ -239,7 +228,7 @@ rsRetVal klogAfterRun(modConfData_t *pModConf) rsRetVal klogLogKMsg(modConfData_t __attribute__((unused)) *pModConf) { DEFiRet; - readklog(); + readkmsg(); RETiRet; } |