diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2008-02-14 16:24:08 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2008-02-14 16:24:08 +0000 |
commit | a43ff0d7c08a5c8cbebba10c6c522ebf61d1c875 (patch) | |
tree | 27daf230c1283517201dceb2538e05f17bbac3d4 | |
parent | 52b6b6f2211718101e43de05e5292f555e8198a8 (diff) | |
download | rsyslog-a43ff0d7c08a5c8cbebba10c6c522ebf61d1c875.tar.gz rsyslog-a43ff0d7c08a5c8cbebba10c6c522ebf61d1c875.tar.xz rsyslog-a43ff0d7c08a5c8cbebba10c6c522ebf61d1c875.zip |
created a bare template for omlibdbi (dbi output action)
-rw-r--r-- | Makefile.am | 4 | ||||
-rw-r--r-- | configure.ac | 36 | ||||
-rw-r--r-- | plugins/omlibdbi/.cvsignore | 6 | ||||
-rw-r--r-- | plugins/omlibdbi/Makefile.am | 8 | ||||
-rw-r--r-- | plugins/omlibdbi/omlibdbi.c | 305 |
5 files changed, 359 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index bc6360a1..bde71b8b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -103,6 +103,10 @@ if ENABLE_MYSQL SUBDIRS += plugins/ommysql endif +if ENABLE_OMLIBDBI +SUBDIRS += plugins/omlibdbi +endif + if ENABLE_PGSQL SUBDIRS += plugins/ompgsql endif diff --git a/configure.ac b/configure.ac index dcac5608..49b020af 100644 --- a/configure.ac +++ b/configure.ac @@ -379,6 +379,40 @@ AC_SUBST(pgsql_cflags) AC_SUBST(pgsql_libs) +# libdbi support +AC_ARG_ENABLE(libdbi, + [AS_HELP_STRING([--enable-libdbi],[Enable libdbi database support @<:@default=no@:>@])], + [case "${enableval}" in + yes) enable_libdbi="yes" ;; + no) enable_libdbi="no" ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-libdbi) ;; + esac], + [enable_libdbi=no] +) +#if test "x$enable_libdbi" = "xyes"; then +# AC_CHECK_PROG( +# [HAVE_PGSQL_CONFIG], +# [pg_config], +# [yes],,, +# ) +# if test "x${HAVE_PGSQL_CONFIG}" != "xyes"; then +# AC_MSG_FAILURE([pg_config not found in PATH]) +# fi +# AC_CHECK_LIB( +# [pq], +# [PQconnectdb], +# [pgsql_cflags="-I`pg_config --includedir`" +# pgsql_libs="`pg_config --libdir` -lpq" +# ], +# [AC_MSG_FAILURE([PgSQL library is missing])], +# [-L`pg_config --libdir`] +# ) +#fi +AM_CONDITIONAL(ENABLE_OMLIBDBI, test x$enable_libdbi = xyes) +AC_SUBST(libdbi_cflags) +AC_SUBST(libdbi_libs) + + # SNMP support AC_ARG_ENABLE(snmp, [AS_HELP_STRING([--enable-snmp],[Enable SNMP support @<:@default=no@:>@])], @@ -461,6 +495,7 @@ AC_CONFIG_FILES([Makefile \ plugins/omgssapi/Makefile \ plugins/ommysql/Makefile \ plugins/ompgsql/Makefile \ + plugins/omlibdbi/Makefile \ plugins/omsnmp/Makefile]) AC_OUTPUT @@ -472,6 +507,7 @@ echo "Klogd functionality enabled: $enable_klogd" echo "Regular expressions support enabled: $enable_regexp" echo "Zlib compression support enabled: $enable_zlib" echo "MySql support enabled: $enable_mysql" +echo "libdbi support enabled: $enable_libdbi" echo "PostgreSQL support enabled: $enable_pgsql" echo "SNMP support enabled: $enable_snmp" echo "file input module enabled: $enable_imfile" diff --git a/plugins/omlibdbi/.cvsignore b/plugins/omlibdbi/.cvsignore new file mode 100644 index 00000000..9730646f --- /dev/null +++ b/plugins/omlibdbi/.cvsignore @@ -0,0 +1,6 @@ +.deps +.libs +Makefile +Makefile.in +*.la +*.lo diff --git a/plugins/omlibdbi/Makefile.am b/plugins/omlibdbi/Makefile.am new file mode 100644 index 00000000..99ee63c9 --- /dev/null +++ b/plugins/omlibdbi/Makefile.am @@ -0,0 +1,8 @@ +pkglib_LTLIBRARIES = omlibdbi.la + +omlibdbi_la_SOURCES = omlibdbi.c +omlibdbi_la_CPPFLAGS = -I$(top_srcdir) $(libdbi_cflags) $(pthreads_cflags) $(mudflap_cflags) +omlibdbi_la_LDFLAGS = $(mudflap_libs) -module -avoid-version +omlibdbi_la_LIBADD = $(libdbi_libs) + +EXTRA_DIST = createDB.sql diff --git a/plugins/omlibdbi/omlibdbi.c b/plugins/omlibdbi/omlibdbi.c new file mode 100644 index 00000000..bfe11f4c --- /dev/null +++ b/plugins/omlibdbi/omlibdbi.c @@ -0,0 +1,305 @@ +/* omlibdbi.c + * This is the implementation of the dbi output module. + * + * NOTE: read comments in module-template.h to understand how this file + * works! + * + * File begun on 2008-02-14 by RGerhards (extracted from syslogd.c) + * + * Copyright 2008 Rainer Gerhards and Adiscon GmbH. + * + * This file is part of rsyslog. + * + * Rsyslog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Rsyslog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Rsyslog. If not, see <http://www.gnu.org/licenses/>. + * + * A copy of the GPL can be found in the file "COPYING" in this distribution. + */ +#include "config.h" +#include "rsyslog.h" +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <signal.h> +#include <errno.h> +#include <time.h> +#include <mysql/mysql.h> +#include <mysql/errmsg.h> +#include "syslogd.h" +#include "syslogd-types.h" +#include "srUtils.h" +#include "template.h" +#include "module-template.h" + +MODULE_TYPE_OUTPUT + +/* internal structures + */ +DEF_OMOD_STATIC_DATA + +typedef struct _instanceData { + MYSQL *f_hmysql; /* handle to MySQL */ + char f_dbsrv[MAXHOSTNAMELEN+1]; /* IP or hostname of DB server*/ + char f_dbname[_DB_MAXDBLEN+1]; /* DB name */ + char f_dbuid[_DB_MAXUNAMELEN+1]; /* DB user */ + char f_dbpwd[_DB_MAXPWDLEN+1]; /* DB user's password */ + unsigned uLastMySQLErrno; /* last errno returned by MySQL or 0 if all is well */ +} instanceData; + + +BEGINcreateInstance +CODESTARTcreateInstance +ENDcreateInstance + + +BEGINisCompatibleWithFeature +CODESTARTisCompatibleWithFeature + if(eFeat == sFEATURERepeatedMsgReduction) + iRet = RS_RET_OK; +ENDisCompatibleWithFeature + + +/* The following function is responsible for closing a + * MySQL connection. + * Initially added 2004-10-28 + */ +static void closeMySQL(instanceData *pData) +{ + ASSERT(pData != NULL); + + if(pData->f_hmysql != NULL) { /* just to be on the safe side... */ + mysql_server_end(); + mysql_close(pData->f_hmysql); + pData->f_hmysql = NULL; + } +} + +BEGINfreeInstance +CODESTARTfreeInstance + closeMySQL(pData); +ENDfreeInstance + + +BEGINneedUDPSocket +CODESTARTneedUDPSocket +ENDneedUDPSocket + + +BEGINdbgPrintInstInfo +CODESTARTdbgPrintInstInfo + /* nothing special here */ +ENDdbgPrintInstInfo + + +/* log a database error with descriptive message. + * We check if we have a valid MySQL handle. If not, we simply + * report an error, but can not be specific. RGerhards, 2007-01-30 + */ +static void reportDBError(instanceData *pData, int bSilent) +{ + char errMsg[512]; + unsigned uMySQLErrno; + + ASSERT(pData != NULL); + + /* output log message */ + errno = 0; + if(pData->f_hmysql == NULL) { + logerror("unknown DB error occured - could not obtain MySQL handle"); + } else { /* we can ask mysql for the error description... */ + uMySQLErrno = mysql_errno(pData->f_hmysql); + snprintf(errMsg, sizeof(errMsg)/sizeof(char), "db error (%d): %s\n", uMySQLErrno, + mysql_error(pData->f_hmysql)); + if(bSilent || uMySQLErrno == pData->uLastMySQLErrno) + dbgprintf("mysql, DBError(silent): %s\n", errMsg); + else { + pData->uLastMySQLErrno = uMySQLErrno; + logerror(errMsg); + } + } + + return; +} + + +/* The following function is responsible for initializing a + * MySQL connection. + * Initially added 2004-10-28 mmeckelein + */ +static rsRetVal initMySQL(instanceData *pData, int bSilent) +{ + DEFiRet; + + ASSERT(pData != NULL); + ASSERT(pData->f_hmysql == NULL); + + pData->f_hmysql = mysql_init(NULL); + if(pData->f_hmysql == NULL) { + logerror("can not initialize MySQL handle"); + iRet = RS_RET_SUSPENDED; + } else { /* we could get the handle, now on with work... */ + /* Connect to database */ + if(mysql_real_connect(pData->f_hmysql, pData->f_dbsrv, pData->f_dbuid, + pData->f_dbpwd, pData->f_dbname, 0, NULL, 0) == NULL) { + reportDBError(pData, bSilent); + closeMySQL(pData); /* ignore any error we may get */ + iRet = RS_RET_SUSPENDED; + } + } + + RETiRet; +} + + +/* The following function writes the current log entry + * to an established MySQL session. + * Initially added 2004-10-28 mmeckelein + */ +rsRetVal writeMySQL(uchar *psz, instanceData *pData) +{ + DEFiRet; + + ASSERT(psz != NULL); + ASSERT(pData != NULL); + + /* see if we are ready to proceed */ + if(pData->f_hmysql == NULL) { + CHKiRet(initMySQL(pData, 0)); + } + + /* try insert */ + if(mysql_query(pData->f_hmysql, (char*)psz)) { + /* error occured, try to re-init connection and retry */ + closeMySQL(pData); /* close the current handle */ + CHKiRet(initMySQL(pData, 0)); /* try to re-open */ + if(mysql_query(pData->f_hmysql, (char*)psz)) { /* re-try insert */ + /* we failed, giving up for now */ + reportDBError(pData, 0); + closeMySQL(pData); /* free ressources */ + ABORT_FINALIZE(RS_RET_SUSPENDED); + } + } + +finalize_it: + if(iRet == RS_RET_OK) { + pData->uLastMySQLErrno = 0; /* reset error for error supression */ + } + + RETiRet; +} + + +BEGINtryResume +CODESTARTtryResume + if(pData->f_hmysql == NULL) { + iRet = initMySQL(pData, 1); + } +ENDtryResume + +BEGINdoAction +CODESTARTdoAction + dbgprintf("\n"); + iRet = writeMySQL(ppString[0], pData); +ENDdoAction + + +BEGINparseSelectorAct + int iMySQLPropErr = 0; +CODESTARTparseSelectorAct +CODE_STD_STRING_REQUESTparseSelectorAct(1) + /* first check if this config line is actually for us + * The first test [*p == '>'] can be skipped if a module shall only + * support the newer slection syntax [:modname:]. This is in fact + * recommended for new modules. Please note that over time this part + * will be handled by rsyslogd itself, but for the time being it is + * a good compromise to do it at the module level. + * rgerhards, 2007-10-15 + */ + if(*p == '>') { + p++; /* eat '>' '*/ + } else if(!strncmp((char*) p, ":ommysql:", sizeof(":ommysql:") - 1)) { + p += sizeof(":ommysql:") - 1; /* eat indicator sequence (-1 because of '\0'!) */ + } else { + ABORT_FINALIZE(RS_RET_CONFLINE_UNPROCESSED); + } + + /* ok, if we reach this point, we have something for us */ + if((iRet = createInstance(&pData)) != RS_RET_OK) + goto finalize_it; + + + /* rger 2004-10-28: added support for MySQL + * >server,dbname,userid,password + * Now we read the MySQL connection properties + * and verify that the properties are valid. + */ + if(getSubString(&p, pData->f_dbsrv, MAXHOSTNAMELEN+1, ',')) + iMySQLPropErr++; + if(*pData->f_dbsrv == '\0') + iMySQLPropErr++; + if(getSubString(&p, pData->f_dbname, _DB_MAXDBLEN+1, ',')) + iMySQLPropErr++; + if(*pData->f_dbname == '\0') + iMySQLPropErr++; + if(getSubString(&p, pData->f_dbuid, _DB_MAXUNAMELEN+1, ',')) + iMySQLPropErr++; + if(*pData->f_dbuid == '\0') + iMySQLPropErr++; + if(getSubString(&p, pData->f_dbpwd, _DB_MAXPWDLEN+1, ';')) + iMySQLPropErr++; + /* now check for template + * We specify that the SQL option must be present in the template. + * This is for your own protection (prevent sql injection). + */ + if(*(p-1) == ';') + --p; /* TODO: the whole parsing of the MySQL module needs to be re-thought - but this here + * is clean enough for the time being -- rgerhards, 2007-07-30 + */ + CHKiRet(cflineParseTemplateName(&p, *ppOMSR, 0, OMSR_RQD_TPL_OPT_SQL, (uchar*) " StdDBFmt")); + + /* If we detect invalid properties, we disable logging, + * because right properties are vital at this place. + * Retries make no sense. + */ + if (iMySQLPropErr) { + logerror("Trouble with MySQL connection properties. -MySQL logging disabled"); + ABORT_FINALIZE(RS_RET_INVALID_PARAMS); + } else { + pData->f_hmysql = NULL; /* initialize, but connect only on first message (important for queued mode!) */ + } + +CODE_STD_FINALIZERparseSelectorAct +ENDparseSelectorAct + + +BEGINmodExit +CODESTARTmodExit +ENDmodExit + + +BEGINqueryEtryPt +CODESTARTqueryEtryPt +CODEqueryEtryPt_STD_OMOD_QUERIES +ENDqueryEtryPt + + +BEGINmodInit() +CODESTARTmodInit + *ipIFVersProvided = 1; /* so far, we only support the initial definition */ +CODEmodInit_QueryRegCFSLineHdlr +ENDmodInit +/* + * vi:set ai: + */ |