From 6e3980abbdb5d98a66d656456f993374b0be9565 Mon Sep 17 00:00:00 2001 From: Milan Bartos Date: Tue, 11 Sep 2012 10:10:37 +0200 Subject: Added new module, imkmsg, for structured kernel logs from /dev/kmsg. This is still in development, bute ready to be commited to master. modified: Makefile.am modified: configure.ac new file: plugins/imkmsg/Makefile.am new file: plugins/imkmsg/imkmsg.c new file: plugins/imkmsg/imkmsg.h new file: plugins/imkmsg/kmsg.c --- Makefile.am | 4 + configure.ac | 14 ++ plugins/imkmsg/Makefile.am | 8 + plugins/imkmsg/imkmsg.c | 408 +++++++++++++++++++++++++++++++++++++++++++++ plugins/imkmsg/imkmsg.h | 68 ++++++++ plugins/imkmsg/kmsg.c | 257 ++++++++++++++++++++++++++++ 6 files changed, 759 insertions(+) create mode 100644 plugins/imkmsg/Makefile.am create mode 100644 plugins/imkmsg/imkmsg.c create mode 100644 plugins/imkmsg/imkmsg.h create mode 100644 plugins/imkmsg/kmsg.c diff --git a/Makefile.am b/Makefile.am index 95b432c9..e7aaca55 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,6 +80,10 @@ if ENABLE_IMKLOG SUBDIRS += plugins/imklog endif +if ENABLE_IMKMSG +SUBDIRS += plugins/imkmsg +endif + if ENABLE_IMPSTATS SUBDIRS += plugins/impstats endif diff --git a/configure.ac b/configure.ac index 58adecb0..e6806f10 100644 --- a/configure.ac +++ b/configure.ac @@ -330,6 +330,18 @@ AM_CONDITIONAL(ENABLE_IMKLOG_BSD, test x$os_type = xbsd) AM_CONDITIONAL(ENABLE_IMKLOG_LINUX, test x$os_type = xlinux) AM_CONDITIONAL(ENABLE_IMKLOG_SOLARIS, test x$os_type = xsolaris) +# kmsg +AC_ARG_ENABLE(kmsg, + [AS_HELP_STRING([--enable-kmsg],[Kmsg structured kernel logs functionality @<:@default=no@:>@])], + [case "${enableval}" in + yes) enable_kmsg="yes" ;; + no) enable_kmsg="no" ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-kmsg) ;; + esac], + [enable_kmsg="no"] +) +AM_CONDITIONAL(ENABLE_IMKMSG, test x$enable_kmsg = xyes) + # # SYSLOG_UNIXAF @@ -1358,6 +1370,7 @@ AC_CONFIG_FILES([Makefile \ plugins/imuxsock/Makefile \ plugins/immark/Makefile \ plugins/imklog/Makefile \ + plugins/imkmsg/Makefile \ plugins/omhdfs/Makefile \ plugins/omprog/Makefile \ plugins/omstdout/Makefile \ @@ -1415,6 +1428,7 @@ echo " Unlimited select() support enabled: $enable_unlimited_select" echo echo "---{ input plugins }---" echo " Klog functionality enabled: $enable_klog ($os_type)" +echo " /dev/kmsg functionality enabled: $enable_kmsg" echo " plain tcp input module enabled: $enable_imptcp" echo " threaded plain tcp input module enabled: $enable_imttcp" echo " imdiag enabled: $enable_imdiag" diff --git a/plugins/imkmsg/Makefile.am b/plugins/imkmsg/Makefile.am new file mode 100644 index 00000000..87c177d2 --- /dev/null +++ b/plugins/imkmsg/Makefile.am @@ -0,0 +1,8 @@ +pkglib_LTLIBRARIES = imkmsg.la +imkmsg_la_SOURCES = imkmsg.c imkmsg.h + +imkmsg_la_SOURCES += kmsg.c + +imkmsg_la_CPPFLAGS = -I$(top_srcdir) $(PTHREADS_CFLAGS) $(RSRT_CFLAGS) +imkmsg_la_LDFLAGS = -module -avoid-version +imkmsg_la_LIBADD = diff --git a/plugins/imkmsg/imkmsg.c b/plugins/imkmsg/imkmsg.c new file mode 100644 index 00000000..035624c3 --- /dev/null +++ b/plugins/imkmsg/imkmsg.c @@ -0,0 +1,408 @@ +/* The kernel log module. + * + * This is an abstracted module. As Linux and BSD kernel log is conceptually the + * same, we do not do different input plugins for them but use + * imklog in both cases, just with different "backend drivers" for + * the different platforms. This also enables a rsyslog.conf to + * be used on multiple platforms without the need to take care of + * what the kernel log is coming from. + * + * See platform-specific files (e.g. linux.c, bsd.c) in the plugin's + * working directory. For other systems with similar kernel logging + * functionality, no new input plugin shall be written but rather a + * driver be developed for imklog. Please note that imklog itself is + * mostly concerned with handling the interface. Any real action happens + * in the drivers, as things may be pretty different on different + * platforms. + * + * Please note that this file replaces the klogd daemon that was + * also present in pre-v3 versions of rsyslog. + * + * To test under Linux: + * echo test1 > /dev/kmsg + * + * Copyright (C) 2008-2012 Adiscon GmbH + * + * This file is part of rsyslog. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * -or- + * see COPYING.ASL20 in the source distribution + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "config.h" +#include "rsyslog.h" +#include +#include +#include +#include +#include +#include +#include + +#include "dirty.h" +#include "cfsysline.h" +#include "obj.h" +#include "msg.h" +#include "module-template.h" +#include "datetime.h" +#include "imkmsg.h" +#include "net.h" +#include "glbl.h" +#include "prop.h" +#include "errmsg.h" +#include "unicode-helper.h" + +MODULE_TYPE_INPUT +MODULE_TYPE_NOKEEP +MODULE_CNFNAME("imkmsg") + +/* Module static data */ +DEF_IMOD_STATIC_DATA +DEFobjCurrIf(datetime) +DEFobjCurrIf(glbl) +DEFobjCurrIf(prop) +DEFobjCurrIf(net) +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 */ +} configSettings_t; +static configSettings_t cs; + +static modConfData_t *loadModConf = NULL;/* modConf ptr to use for the current load process */ +static modConfData_t *runModConf = NULL;/* modConf ptr to use for the current load process */ +static int bLegacyCnfModGlobalsPermitted;/* are legacy module-global config parameters permitted? */ + +/* module-global parameters */ +static struct cnfparamdescr modpdescr[] = { + { "logpath", eCmdHdlrGetWord, 0 }, + { "permitnonkernelfacility", eCmdHdlrBinary, 0 }, + { "consoleloglevel", eCmdHdlrInt, 0 }, + { "internalmsgfacility", eCmdHdlrFacility, 0 } +}; +static struct cnfparamblk modpblk = + { CNFPARAMBLK_VERSION, + sizeof(modpdescr)/sizeof(struct cnfparamdescr), + 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(); +} + + +/* enqueue the the kernel message into the message queue. + * The provided msg string is not freed - thus must be done + * by the caller. + * rgerhards, 2008-04-12 + */ +static rsRetVal +enqMsg(uchar *msg, uchar* pszTag, int iFacility, int iSeverity, struct timeval *tp) +{ + struct syslogTime st; + msg_t *pMsg; + DEFiRet; + + assert(msg != NULL); + assert(pszTag != NULL); + + if(tp == NULL) { + CHKiRet(msgConstruct(&pMsg)); + } else { + datetime.timeval2syslogTime(tp, &st); + CHKiRet(msgConstructWithTime(&pMsg, &st, tp->tv_sec)); + } + MsgSetFlowControlType(pMsg, eFLOWCTL_LIGHT_DELAY); + MsgSetInputName(pMsg, pInputName); + MsgSetRawMsgWOSize(pMsg, (char*)msg); + MsgSetMSGoffs(pMsg, 0); /* we do not have a header... */ + MsgSetRcvFrom(pMsg, glbl.GetLocalHostNameProp()); + MsgSetRcvFromIP(pMsg, pLocalHostIP); + MsgSetHOSTNAME(pMsg, glbl.GetLocalHostName(), ustrlen(glbl.GetLocalHostName())); + MsgSetTAG(pMsg, pszTag, ustrlen(pszTag)); + pMsg->iFacility = iFacility; + pMsg->iSeverity = iSeverity; + CHKiRet(submitMsg(pMsg)); + +finalize_it: + RETiRet; +} + + +/* log an imklog-internal message + * rgerhards, 2008-04-14 + */ +rsRetVal imklogLogIntMsg(int priority, char *fmt, ...) +{ + DEFiRet; + va_list ap; + uchar msgBuf[2048]; /* we use the same size as sysklogd to remain compatible */ + + va_start(ap, fmt); + vsnprintf((char*)msgBuf, sizeof(msgBuf) / sizeof(char), fmt, ap); + va_end(ap); + + logmsgInternal(NO_ERRCODE ,priority, msgBuf, 0); + + RETiRet; +} + + +/* log a kernel message. If tp is non-NULL, it contains the message creation + * time to use. + * rgerhards, 2008-04-14 + */ +rsRetVal Syslog(int priority, uchar *pMsg, struct timeval *tp) +{ + 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); + + RETiRet; +} + + +/* helper for some klog drivers which need to know the MaxLine global setting. They can + * not obtain it themselfs, because they are no modules and can not query the object hander. + * It would probably be a good idea to extend the interface to support it, but so far + * we create a (sufficiently valid) work-around. -- rgerhards, 2008-11-24 + */ +int klog_getMaxLine(void) +{ + return glbl.GetMaxLine(); +} + + +BEGINrunInput +CODESTARTrunInput + /* this is an endless loop - it is terminated when the thread is + * signalled to do so. This, however, is handled by the framework, + * right into the sleep below. + */ + while(!pThrd->bShallStop) { + /* klogLogKMsg() waits for the next kernel message, obtains it + * and then submits it to the rsyslog main queue. + * rgerhards, 2008-04-09 + */ + CHKiRet(klogLogKMsg(runModConf)); + } +finalize_it: +ENDrunInput + + +BEGINbeginCnfLoad +CODESTARTbeginCnfLoad + loadModConf = pModConf; + pModConf->pConf = pConf; + /* init our settings */ + pModConf->pszPath = NULL; + pModConf->bPermitNonKernel = 0; + pModConf->console_log_level = -1; + pModConf->iFacilIntMsg = klogFacilIntMsg(); + loadModConf->configSetViaV2Method = 0; + bLegacyCnfModGlobalsPermitted = 1; + /* init legacy config vars */ + initConfigSettings(); +ENDbeginCnfLoad + + +BEGINsetModCnf + struct cnfparamvals *pvals = NULL; + int i; +CODESTARTsetModCnf + pvals = nvlstGetParams(lst, &modpblk, NULL); + if(pvals == NULL) { + errmsg.LogError(0, RS_RET_MISSING_CNFPARAMS, "error processing module " + "config parameters [module(...)]"); + ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); + } + + if(Debug) { + dbgprintf("module (global) param blk for imkmsg:\n"); + cnfparamsPrint(&modpblk, pvals); + } + + for(i = 0 ; i < modpblk.nParams ; ++i) { + if(!pvals[i].bUsed) + 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")) { + loadModConf->iFacilIntMsg = (int) pvals[i].val.d.n; + } else { + dbgprintf("imkmsg: program error, non-handled " + "param '%s' in beginCnfLoad\n", modpblk.descr[i].name); + } + } + + /* disable legacy module-global config directives */ + bLegacyCnfModGlobalsPermitted = 0; + loadModConf->configSetViaV2Method = 1; + +finalize_it: + if(pvals != NULL) + cnfparamvalsDestruct(pvals, &modpblk); +ENDsetModCnf + + +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')) { + loadModConf->pszPath = NULL; + if(cs.pszPath != NULL) + free(cs.pszPath); + } else { + loadModConf->pszPath = cs.pszPath; + } + cs.pszPath = NULL; + } + + loadModConf = NULL; /* done loading */ +ENDendCnfLoad + + +BEGINcheckCnf +CODESTARTcheckCnf +ENDcheckCnf + + +BEGINactivateCnfPrePrivDrop +CODESTARTactivateCnfPrePrivDrop + runModConf = pModConf; + iRet = klogWillRun(runModConf); +ENDactivateCnfPrePrivDrop + + +BEGINactivateCnf +CODESTARTactivateCnf +ENDactivateCnf + + +BEGINfreeCnf +CODESTARTfreeCnf +ENDfreeCnf + + +BEGINwillRun +CODESTARTwillRun +ENDwillRun + + +BEGINafterRun +CODESTARTafterRun + iRet = klogAfterRun(runModConf); +ENDafterRun + + +BEGINmodExit +CODESTARTmodExit + if(pInputName != NULL) + prop.Destruct(&pInputName); + if(pLocalHostIP != NULL) + prop.Destruct(&pLocalHostIP); + + /* release objects we used */ + objRelease(glbl, CORE_COMPONENT); + objRelease(net, CORE_COMPONENT); + objRelease(datetime, CORE_COMPONENT); + objRelease(prop, CORE_COMPONENT); + objRelease(errmsg, CORE_COMPONENT); +ENDmodExit + + +BEGINqueryEtryPt +CODESTARTqueryEtryPt +CODEqueryEtryPt_STD_IMOD_QUERIES +CODEqueryEtryPt_STD_CONF2_QUERIES +CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES +CODEqueryEtryPt_STD_CONF2_PREPRIVDROP_QUERIES +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; + } + cs.iFacilIntMsg = klogFacilIntMsg(); + return RS_RET_OK; +} + +BEGINmodInit() +CODESTARTmodInit + *ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ +CODEmodInit_QueryRegCFSLineHdlr + CHKiRet(objUse(datetime, CORE_COMPONENT)); + CHKiRet(objUse(glbl, CORE_COMPONENT)); + CHKiRet(objUse(prop, CORE_COMPONENT)); + CHKiRet(objUse(net, CORE_COMPONENT)); + CHKiRet(objUse(errmsg, CORE_COMPONENT)); + + /* we need to create the inputName property (only once during our lifetime) */ + CHKiRet(prop.CreateStringProp(&pInputName, UCHAR_CONSTANT("imkmsg"), sizeof("imkmsg") - 1)); + CHKiRet(prop.CreateStringProp(&pLocalHostIP, UCHAR_CONSTANT("127.0.0.1"), sizeof("127.0.0.1") - 1)); + + /* init legacy config settings */ + initConfigSettings(); + + CHKiRet(omsdRegCFSLineHdlr((uchar *)"debugprintkernelsymbols", 0, eCmdHdlrGoneAway, + NULL, NULL, STD_LOADABLE_MODULE_ID)); + CHKiRet(regCfSysLineHdlr2((uchar *)"klogpath", 0, eCmdHdlrGetWord, + NULL, &cs.pszPath, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); + CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogsymbollookup", 0, eCmdHdlrGoneAway, + NULL, NULL, STD_LOADABLE_MODULE_ID)); + CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogsymbolstwice", 0, eCmdHdlrGoneAway, + 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, + NULL, &cs.iFacilIntMsg, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); + CHKiRet(omsdRegCFSLineHdlr((uchar *)"resetconfigvariables", 1, eCmdHdlrCustomHandler, + resetConfigVariables, NULL, STD_LOADABLE_MODULE_ID)); +ENDmodInit +/* vim:set ai: + */ diff --git a/plugins/imkmsg/imkmsg.h b/plugins/imkmsg/imkmsg.h new file mode 100644 index 00000000..acfb50ab --- /dev/null +++ b/plugins/imkmsg/imkmsg.h @@ -0,0 +1,68 @@ +/* imklog.h + * These are the definitions for the klog message generation module. + * + * File begun on 2007-12-17 by RGerhards + * Major change: 2008-04-09: switched to a driver interface for + * several platforms + * + * Copyright 2007-2012 Rainer Gerhards and Adiscon GmbH. + * + * This file is part of rsyslog. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * -or- + * see COPYING.ASL20 in the source distribution + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef IMKLOG_H_INCLUDED +#define IMKLOG_H_INCLUDED 1 + +#include "rsyslog.h" +#include "dirty.h" + +/* we need to have the modConf type present in all submodules */ +struct modConfData_s { + rsconf_t *pConf; + int iFacilIntMsg; + uchar *pszPath; + int console_log_level; + sbool bPermitNonKernel; + sbool configSetViaV2Method; +}; + +/* interface to "drivers" + * the platform specific drivers must implement these entry points. Only one + * driver may be active at any given time, thus we simply rely on the linker + * to resolve the addresses. + * rgerhards, 2008-04-09 + */ +rsRetVal klogLogKMsg(modConfData_t *pModConf); +rsRetVal klogWillRun(modConfData_t *pModConf); +rsRetVal klogAfterRun(modConfData_t *pModConf); +int klogFacilIntMsg(); + +/* the functions below may be called by the drivers */ +rsRetVal imklogLogIntMsg(int priority, char *fmt, ...) __attribute__((format(printf,2, 3))); +rsRetVal Syslog(int priority, uchar *msg, struct timeval *tp); + +/* prototypes */ +extern int klog_getMaxLine(void); /* work-around for klog drivers to get configured max line size */ +extern int InitKsyms(modConfData_t*); +extern void DeinitKsyms(void); +extern int InitMsyms(void); +extern void DeinitMsyms(void); +extern char * ExpandKadds(char *, char *); +extern void SetParanoiaLevel(int); + +#endif /* #ifndef IMKLOG_H_INCLUDED */ +/* vi:set ai: + */ diff --git a/plugins/imkmsg/kmsg.c b/plugins/imkmsg/kmsg.c new file mode 100644 index 00000000..2513b2ad --- /dev/null +++ b/plugins/imkmsg/kmsg.c @@ -0,0 +1,257 @@ +/* imkmsg driver for Linux /dev/kmsg structured logging + * + * This contains OS-specific functionality to read the BSD + * or Linux kernel log. For a general overview, see head comment in + * imklog.c. This started out as the BSD-specific drivers, but it + * turned out that on modern Linux the implementation details + * are very small, and so we use a single driver for both OS's with + * a little help of conditional compilation. + * + * Copyright 2008-2012 Adiscon GmbH + * + * This file is part of rsyslog. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * -or- + * see COPYING.ASL20 in the source distribution + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rsyslog.h" +#include "srUtils.h" +#include "debug.h" +#include "imkmsg.h" + +/* globals */ +static int fklog = -1; /* kernel log fd */ + +#ifndef _PATH_KLOG +# define _PATH_KLOG "/dev/kmsg" +#endif + +/* submit a message to imklog Syslog() API. In this function, we parse + * necessary information from kernel log line, and make json string + * from the rest. + */ +static void +submitSyslog(uchar *buf) +{ + struct timeval tv; + long offs = 0; + long int timestamp = 0; + struct timespec monotonic; + struct timespec realtime; + char outMsg[8096]; + int priority = 0; + + offs = snprintf(outMsg, 8, "%s", "@cee: {"); + + /* get priority */ + for (; isdigit(*buf); buf++) { + priority += (priority * 10) + (*buf - '0'); + } + buf++; + + /* messages sequence number */ + offs += snprintf(outMsg+offs, 12, "%s", "\"sequnum\":\""); + for (; isdigit(*buf); buf++, offs++) { + outMsg[offs] = *buf; + } + buf++; /* skip , */ + + /* get timestamp */ + for (; isdigit(*buf); buf++) { + timestamp += (timestamp * 10) + (*buf - '0'); + } + buf++; /* skip ; */ + + offs += snprintf(outMsg+offs, 10, "%s", "\",\"msg\":\""); + + for (; *buf != '\n' && *buf != '\0'; buf++, offs++) { + outMsg[offs] = *buf; + } + + if (*buf != '\0') /* message has appended properties, skip \n */ + buf++; + + while (strlen((char *)buf)) { + offs += snprintf(outMsg+offs, 4, "%s", "\",\""); + buf++; /* skip ' ' */ + for (; *buf != '=' && *buf != ' '; buf++, offs++) { /* separator is = or ' ' */ + outMsg[offs] = *buf; + } + buf++; /* skip = */ + + offs += snprintf(outMsg+offs, 4, "%s", "\":\""); + for (; *buf != '\n' && *buf != '\0'; buf++, offs++) { + outMsg[offs] = *buf; + } + + if (*buf != '\0') + buf++; /* another property, skip \n */ + } + offs += snprintf(outMsg+offs, 3, "%s", "\"}"); + + outMsg[offs] = '\0'; + + /* calculate timestamp */ + clock_gettime(CLOCK_MONOTONIC, &monotonic); + clock_gettime(CLOCK_REALTIME, &realtime); + 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); +} + + +static uchar *GetPath(modConfData_t *pModConf) +{ + return pModConf->pszPath ? pModConf->pszPath : (uchar*) _PATH_KLOG; +} + +/* open the kernel log - will be called inside the willRun() imklog + * entry point. -- rgerhards, 2008-04-09 + */ +rsRetVal +klogWillRun(modConfData_t *pModConf) +{ + char errmsg[2048]; + int r; + DEFiRet; + + fklog = open((char*)GetPath(pModConf), O_RDONLY, 0); + if (fklog < 0) { + imklogLogIntMsg(RS_RET_ERR_OPEN_KLOG, "imkmsg: cannot open kernel log(%s): %s.", + GetPath(pModConf), rs_strerror_r(errno, errmsg, sizeof(errmsg))); + ABORT_FINALIZE(RS_RET_ERR_OPEN_KLOG); + } + + /* Set level of kernel console messaging.. */ + if(pModConf->console_log_level != -1) { + r = klogctl(8, NULL, pModConf->console_log_level); + if(r != 0) { + imklogLogIntMsg(LOG_WARNING, "imkmsg: cannot set console log level: %s", + rs_strerror_r(errno, errmsg, sizeof(errmsg))); + /* make sure we do not try to re-set! */ + pModConf->console_log_level = -1; + } + } + +finalize_it: + RETiRet; +} + +/* Read kernel log while data are available, split into lines. + */ +static void +readklog(void) +{ + int i; + uchar pRcv[1024+1]; /* LOG_LINE_MAX is 1024 */ + 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("imklog(BSD/Linux) 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); + + if (i > 0) { + /* successful read of message of nonzero length */ + pRcv[i] = '\0'; + } else { + /* something went wrong - error or zero length message */ + if (i < 0 && errno != EINTR && errno != EAGAIN) { + /* error occured */ + imklogLogIntMsg(LOG_ERR, + "imkmsg: error reading kernel log - shutting down: %s", + rs_strerror_r(errno, errmsg, sizeof(errmsg))); + fklog = -1; + } + break; + } + + submitSyslog(pRcv); + } +} + + +/* to be called in the module's AfterRun entry point + * rgerhards, 2008-04-09 + */ +rsRetVal klogAfterRun(modConfData_t *pModConf) +{ + DEFiRet; + if(fklog != -1) + close(fklog); + /* Turn on logging of messages to console, but only if a log level was speficied */ + if(pModConf->console_log_level != -1) + klogctl(7, NULL, 0); + RETiRet; +} + + +/* to be called in the module's WillRun entry point, this is the main + * "message pull" mechanism. + * rgerhards, 2008-04-09 + */ +rsRetVal klogLogKMsg(modConfData_t __attribute__((unused)) *pModConf) +{ + DEFiRet; + readklog(); + RETiRet; +} + + +/* provide the (system-specific) default facility for internal messages + * rgerhards, 2008-04-14 + */ +int +klogFacilIntMsg(void) +{ + return LOG_SYSLOG; +} -- cgit v1.2.3 From 91948f610807cb5f42d15af5448126315fd9a0dd Mon Sep 17 00:00:00 2001 From: Milan Bartos Date: Wed, 12 Sep 2012 08:25:39 +0200 Subject: Modified comments. modified: imkmsg.c modified: imkmsg.h modified: kmsg.c --- plugins/imkmsg/imkmsg.c | 25 ++++++------------------- plugins/imkmsg/imkmsg.h | 10 +++------- plugins/imkmsg/kmsg.c | 26 ++++++++++++-------------- 3 files changed, 21 insertions(+), 40 deletions(-) diff --git a/plugins/imkmsg/imkmsg.c b/plugins/imkmsg/imkmsg.c index 035624c3..dc8c1b93 100644 --- a/plugins/imkmsg/imkmsg.c +++ b/plugins/imkmsg/imkmsg.c @@ -1,22 +1,9 @@ /* The kernel log module. * - * This is an abstracted module. As Linux and BSD kernel log is conceptually the - * same, we do not do different input plugins for them but use - * imklog in both cases, just with different "backend drivers" for - * the different platforms. This also enables a rsyslog.conf to - * be used on multiple platforms without the need to take care of - * what the kernel log is coming from. - * - * See platform-specific files (e.g. linux.c, bsd.c) in the plugin's - * working directory. For other systems with similar kernel logging - * functionality, no new input plugin shall be written but rather a - * driver be developed for imklog. Please note that imklog itself is - * mostly concerned with handling the interface. Any real action happens - * in the drivers, as things may be pretty different on different - * platforms. - * - * Please note that this file replaces the klogd daemon that was - * also present in pre-v3 versions of rsyslog. + * This is rsyslog Linux only module for reading structured kernel logs. + * Module is based on imklog module so it retains its structure + * and other part is currently in kmsg.c file instead of this (imkmsg.c) + * For more information see that file. * * To test under Linux: * echo test1 > /dev/kmsg @@ -154,10 +141,10 @@ finalize_it: } -/* log an imklog-internal message +/* log an imkmsg-internal message * rgerhards, 2008-04-14 */ -rsRetVal imklogLogIntMsg(int priority, char *fmt, ...) +rsRetVal imkmsgLogIntMsg(int priority, char *fmt, ...) { DEFiRet; va_list ap; diff --git a/plugins/imkmsg/imkmsg.h b/plugins/imkmsg/imkmsg.h index acfb50ab..d33186b0 100644 --- a/plugins/imkmsg/imkmsg.h +++ b/plugins/imkmsg/imkmsg.h @@ -1,9 +1,5 @@ -/* imklog.h - * These are the definitions for the klog message generation module. - * - * File begun on 2007-12-17 by RGerhards - * Major change: 2008-04-09: switched to a driver interface for - * several platforms +/* imkmsg.h + * These are the definitions for the kmsg message generation module. * * Copyright 2007-2012 Rainer Gerhards and Adiscon GmbH. * @@ -51,7 +47,7 @@ rsRetVal klogAfterRun(modConfData_t *pModConf); int klogFacilIntMsg(); /* the functions below may be called by the drivers */ -rsRetVal imklogLogIntMsg(int priority, char *fmt, ...) __attribute__((format(printf,2, 3))); +rsRetVal imkmsgLogIntMsg(int priority, char *fmt, ...) __attribute__((format(printf,2, 3))); rsRetVal Syslog(int priority, uchar *msg, struct timeval *tp); /* prototypes */ diff --git a/plugins/imkmsg/kmsg.c b/plugins/imkmsg/kmsg.c index 2513b2ad..88e00502 100644 --- a/plugins/imkmsg/kmsg.c +++ b/plugins/imkmsg/kmsg.c @@ -1,11 +1,8 @@ /* imkmsg driver for Linux /dev/kmsg structured logging * - * This contains OS-specific functionality to read the BSD - * or Linux kernel log. For a general overview, see head comment in - * imklog.c. This started out as the BSD-specific drivers, but it - * turned out that on modern Linux the implementation details - * are very small, and so we use a single driver for both OS's with - * a little help of conditional compilation. + * This contains Linux-specific functionality to read /dev/kmsg + * For a general overview, see head comment in imkmsg.c. + * This is heavily based on imklog bsd.c file. * * Copyright 2008-2012 Adiscon GmbH * @@ -49,7 +46,7 @@ static int fklog = -1; /* kernel log fd */ # define _PATH_KLOG "/dev/kmsg" #endif -/* submit a message to imklog Syslog() API. In this function, we parse +/* submit a message to imkmsg Syslog() API. In this function, we parse * necessary information from kernel log line, and make json string * from the rest. */ @@ -129,8 +126,7 @@ static uchar *GetPath(modConfData_t *pModConf) return pModConf->pszPath ? pModConf->pszPath : (uchar*) _PATH_KLOG; } -/* open the kernel log - will be called inside the willRun() imklog - * entry point. -- rgerhards, 2008-04-09 +/* open the kernel log - will be called inside the willRun() imkmsg entry point */ rsRetVal klogWillRun(modConfData_t *pModConf) @@ -141,7 +137,7 @@ klogWillRun(modConfData_t *pModConf) fklog = open((char*)GetPath(pModConf), O_RDONLY, 0); if (fklog < 0) { - imklogLogIntMsg(RS_RET_ERR_OPEN_KLOG, "imkmsg: cannot open kernel log(%s): %s.", + imkmsgLogIntMsg(RS_RET_ERR_OPEN_KLOG, "imkmsg: cannot open kernel log(%s): %s.", GetPath(pModConf), rs_strerror_r(errno, errmsg, sizeof(errmsg))); ABORT_FINALIZE(RS_RET_ERR_OPEN_KLOG); } @@ -150,7 +146,7 @@ klogWillRun(modConfData_t *pModConf) if(pModConf->console_log_level != -1) { r = klogctl(8, NULL, pModConf->console_log_level); if(r != 0) { - imklogLogIntMsg(LOG_WARNING, "imkmsg: cannot set console log level: %s", + imkmsgLogIntMsg(LOG_WARNING, "imkmsg: cannot set console log level: %s", rs_strerror_r(errno, errmsg, sizeof(errmsg))); /* make sure we do not try to re-set! */ pModConf->console_log_level = -1; @@ -161,7 +157,8 @@ finalize_it: RETiRet; } -/* Read kernel log while data are available, split into lines. +/* Read kernel log while data are available, each read() reads one + * record of printk buffer. */ static void readklog(void) @@ -195,7 +192,7 @@ XXX not sure if LOG_LINE_MAX is 1024 #endif for (;;) { - dbgprintf("imklog(BSD/Linux) waiting for kernel log line\n"); + 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); @@ -207,7 +204,7 @@ XXX not sure if LOG_LINE_MAX is 1024 /* something went wrong - error or zero length message */ if (i < 0 && errno != EINTR && errno != EAGAIN) { /* error occured */ - imklogLogIntMsg(LOG_ERR, + imkmsgLogIntMsg(LOG_ERR, "imkmsg: error reading kernel log - shutting down: %s", rs_strerror_r(errno, errmsg, sizeof(errmsg))); fklog = -1; @@ -255,3 +252,4 @@ klogFacilIntMsg(void) { return LOG_SYSLOG; } + -- cgit v1.2.3 From 498b8600707e979f78ee02641d56702784138b70 Mon Sep 17 00:00:00 2001 From: Milan Bartos Date: Wed, 12 Sep 2012 09:39:34 +0200 Subject: Made imkmsg parse the messages instead of creating string to be parsed later. modified: imkmsg.c modified: imkmsg.h modified: kmsg.c --- plugins/imkmsg/imkmsg.c | 30 +++-------------- plugins/imkmsg/imkmsg.h | 2 +- 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 #include #include +#include #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; } -- cgit v1.2.3 From 432934b3dd2f5b2802a76f568b634c01a382e4a8 Mon Sep 17 00:00:00 2001 From: Milan Bartos Date: Mon, 17 Sep 2012 10:14:45 +0200 Subject: Remove unnecessary config options in imkmsg modified: plugins/imkmsg/imkmsg.c modified: plugins/imkmsg/kmsg.c --- plugins/imkmsg/imkmsg.c | 80 ------------------------------------------------- plugins/imkmsg/kmsg.c | 9 ++---- 2 files changed, 2 insertions(+), 87 deletions(-) diff --git a/plugins/imkmsg/imkmsg.c b/plugins/imkmsg/imkmsg.c index 20c49f60..2a97f82d 100644 --- a/plugins/imkmsg/imkmsg.c +++ b/plugins/imkmsg/imkmsg.c @@ -64,8 +64,6 @@ DEFobjCurrIf(errmsg) /* config settings */ typedef struct configSettings_s { int iFacilIntMsg; /* the facility to use for internal messages (set by driver) */ - uchar *pszPath; - int console_log_level; /* still used for BSD */ } configSettings_t; static configSettings_t cs; @@ -73,26 +71,12 @@ static modConfData_t *loadModConf = NULL;/* modConf ptr to use for the current l static modConfData_t *runModConf = NULL;/* modConf ptr to use for the current load process */ static int bLegacyCnfModGlobalsPermitted;/* are legacy module-global config parameters permitted? */ -/* module-global parameters */ -static struct cnfparamdescr modpdescr[] = { - { "logpath", eCmdHdlrGetWord, 0 }, - { "consoleloglevel", eCmdHdlrInt, 0 }, - { "internalmsgfacility", eCmdHdlrFacility, 0 } -}; -static struct cnfparamblk modpblk = - { CNFPARAMBLK_VERSION, - sizeof(modpdescr)/sizeof(struct cnfparamdescr), - 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.console_log_level = -1; - cs.pszPath = NULL; cs.iFacilIntMsg = klogFacilIntMsg(); } @@ -198,9 +182,6 @@ CODESTARTbeginCnfLoad loadModConf = pModConf; pModConf->pConf = pConf; /* init our settings */ - pModConf->pszPath = NULL; - pModConf->bPermitNonKernel = 0; - pModConf->console_log_level = -1; pModConf->iFacilIntMsg = klogFacilIntMsg(); loadModConf->configSetViaV2Method = 0; bLegacyCnfModGlobalsPermitted = 1; @@ -209,61 +190,11 @@ CODESTARTbeginCnfLoad ENDbeginCnfLoad -BEGINsetModCnf - struct cnfparamvals *pvals = NULL; - int i; -CODESTARTsetModCnf - pvals = nvlstGetParams(lst, &modpblk, NULL); - if(pvals == NULL) { - errmsg.LogError(0, RS_RET_MISSING_CNFPARAMS, "error processing module " - "config parameters [module(...)]"); - ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); - } - - if(Debug) { - dbgprintf("module (global) param blk for imkmsg:\n"); - cnfparamsPrint(&modpblk, pvals); - } - - for(i = 0 ; i < modpblk.nParams ; ++i) { - if(!pvals[i].bUsed) - 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, "consoleloglevel")) { - loadModConf->console_log_level= (int) pvals[i].val.d.n; - } else if(!strcmp(modpblk.descr[i].name, "internalmsgfacility")) { - loadModConf->iFacilIntMsg = (int) pvals[i].val.d.n; - } else { - dbgprintf("imkmsg: program error, non-handled " - "param '%s' in beginCnfLoad\n", modpblk.descr[i].name); - } - } - - /* disable legacy module-global config directives */ - bLegacyCnfModGlobalsPermitted = 0; - loadModConf->configSetViaV2Method = 1; - -finalize_it: - if(pvals != NULL) - cnfparamvalsDestruct(pvals, &modpblk); -ENDsetModCnf - - BEGINendCnfLoad CODESTARTendCnfLoad if(!loadModConf->configSetViaV2Method) { /* persist module-specific settings from legacy config system */ loadModConf->iFacilIntMsg = cs.iFacilIntMsg; - loadModConf->console_log_level = cs.console_log_level; - if((cs.pszPath == NULL) || (cs.pszPath[0] == '\0')) { - loadModConf->pszPath = NULL; - if(cs.pszPath != NULL) - free(cs.pszPath); - } else { - loadModConf->pszPath = cs.pszPath; - } - cs.pszPath = NULL; } loadModConf = NULL; /* done loading */ @@ -323,16 +254,11 @@ BEGINqueryEtryPt CODESTARTqueryEtryPt CODEqueryEtryPt_STD_IMOD_QUERIES CODEqueryEtryPt_STD_CONF2_QUERIES -CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES CODEqueryEtryPt_STD_CONF2_PREPRIVDROP_QUERIES ENDqueryEtryPt static rsRetVal resetConfigVariables(uchar __attribute__((unused)) *pp, void __attribute__((unused)) *pVal) { - if(cs.pszPath != NULL) { - free(cs.pszPath); - cs.pszPath = NULL; - } cs.iFacilIntMsg = klogFacilIntMsg(); return RS_RET_OK; } @@ -356,18 +282,12 @@ CODEmodInit_QueryRegCFSLineHdlr CHKiRet(omsdRegCFSLineHdlr((uchar *)"debugprintkernelsymbols", 0, eCmdHdlrGoneAway, NULL, NULL, STD_LOADABLE_MODULE_ID)); - CHKiRet(regCfSysLineHdlr2((uchar *)"klogpath", 0, eCmdHdlrGetWord, - NULL, &cs.pszPath, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogsymbollookup", 0, eCmdHdlrGoneAway, NULL, NULL, STD_LOADABLE_MODULE_ID)); CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogsymbolstwice", 0, eCmdHdlrGoneAway, NULL, NULL, STD_LOADABLE_MODULE_ID)); CHKiRet(omsdRegCFSLineHdlr((uchar *)"klogusesyscallinterface", 0, eCmdHdlrGoneAway, NULL, NULL, STD_LOADABLE_MODULE_ID)); - CHKiRet(regCfSysLineHdlr2((uchar *)"klogconsoleloglevel", 0, eCmdHdlrInt, - NULL, &cs.console_log_level, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); - CHKiRet(regCfSysLineHdlr2((uchar *)"kloginternalmsgfacility", 0, eCmdHdlrFacility, - NULL, &cs.iFacilIntMsg, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted)); CHKiRet(omsdRegCFSLineHdlr((uchar *)"resetconfigvariables", 1, eCmdHdlrCustomHandler, resetConfigVariables, NULL, STD_LOADABLE_MODULE_ID)); ENDmodInit diff --git a/plugins/imkmsg/kmsg.c b/plugins/imkmsg/kmsg.c index 9f1de60f..9ad98da4 100644 --- a/plugins/imkmsg/kmsg.c +++ b/plugins/imkmsg/kmsg.c @@ -134,11 +134,6 @@ submitSyslog(uchar *buf) } -static uchar *GetPath(modConfData_t *pModConf) -{ - return pModConf->pszPath ? pModConf->pszPath : (uchar*) _PATH_KLOG; -} - /* open the kernel log - will be called inside the willRun() imkmsg entry point */ rsRetVal @@ -148,10 +143,10 @@ klogWillRun(modConfData_t *pModConf) int r; DEFiRet; - fklog = open((char*)GetPath(pModConf), O_RDONLY, 0); + fklog = open(_PATH_KLOG, O_RDONLY, 0); if (fklog < 0) { imkmsgLogIntMsg(RS_RET_ERR_OPEN_KLOG, "imkmsg: cannot open kernel log(%s): %s.", - GetPath(pModConf), rs_strerror_r(errno, errmsg, sizeof(errmsg))); + _PATH_KLOG, rs_strerror_r(errno, errmsg, sizeof(errmsg))); ABORT_FINALIZE(RS_RET_ERR_OPEN_KLOG); } -- cgit v1.2.3 From c8fc40bf330f815196a937390a43709a138b1aee Mon Sep 17 00:00:00 2001 From: Milan Bartos Date: Mon, 17 Sep 2012 11:04:16 +0200 Subject: Documentation for new imkmsg module new file: doc/imkmsg.html --- doc/imkmsg.html | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 doc/imkmsg.html diff --git a/doc/imkmsg.html b/doc/imkmsg.html new file mode 100644 index 00000000..61068d09 --- /dev/null +++ b/doc/imkmsg.html @@ -0,0 +1,48 @@ + + +/dev/kmsg Log Input Module (imkmsg) + + + +back + +

/dev/kmsg Log Input Module

+

Module Name:    imkmsg

+

Authors: Rainer Gerhards +<rgerhards@adiscon.com>
+Milan Bartos +<mbartos@redhat.com>

+

Description:

+

Reads messages from the /dev/kmsg structured kernel log and submits them to the +syslog engine.

+

+The printk log buffer constains log records. These records are exported by /dev/kmsg +device as structured data in the following format:
+ "level,sequnum,timestamp;<message text>\n"
+There could be continuation lines starting with space that contains key/value pairs.
+
+Log messages are parsed as necessary into rsyslog msg_t structure. Continuation lines are parsed +as json key/value pairs and added into rsyslog's message json representation. +

+

Configuration Directives:

+This module has no configuration directives. +Caveats/Known Bugs: +

This is Linux specific module and requires /dev/kmsg device with structured kernel logs. +

Sample:

+

The following sample pulls messages from the /dev/kmsg log device. All +parameters are left by default, which is usually a good idea. Please +note that loading the plugin is sufficient to activate it. No directive +is needed to start pulling messages.
+

+ +

[rsyslog.conf overview] +[manual index] [rsyslog site]

+

This documentation is part of the +rsyslog +project.
+Copyright © 2008-2009 by Rainer +Gerhards and +Adiscon. +Released under the GNU GPL version 3 or higher.

+ -- cgit v1.2.3