diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/ommysql/Makefile.am | 9 | ||||
-rw-r--r-- | plugins/ommysql/configure.ac | 48 | ||||
-rw-r--r-- | plugins/ommysql/contrib/delete_mysql | 52 | ||||
-rw-r--r-- | plugins/ommysql/createDB.sql | 37 | ||||
-rw-r--r-- | plugins/ommysql/ommysql.c | 293 | ||||
-rw-r--r-- | plugins/ommysql/ommysql.h | 36 |
6 files changed, 475 insertions, 0 deletions
diff --git a/plugins/ommysql/Makefile.am b/plugins/ommysql/Makefile.am new file mode 100644 index 00000000..48987838 --- /dev/null +++ b/plugins/ommysql/Makefile.am @@ -0,0 +1,9 @@ +pkglibdir = $(libdir)/rsyslog +pkglib_LTLIBRARIES = ommysql.la + +ommysql_la_SOURCES=ommysql.c ommysql.h +ommysql_la_CPPFLAGS=$(mysql_cflags) -I$(srcdir)/../.. +ommysql_la_LDFLAGS=-module -avoid-version +ommysql_la_LIBADD=$(mysql_libs) + +EXTRA_DIST = createDB.sql contrib/delete_mysql diff --git a/plugins/ommysql/configure.ac b/plugins/ommysql/configure.ac new file mode 100644 index 00000000..57d92e7c --- /dev/null +++ b/plugins/ommysql/configure.ac @@ -0,0 +1,48 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.61) +AC_INIT([rsyslog-ommysql],[1.19.8],[rsyslog@lists.adiscon.com.]) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_SRCDIR([ommysql.c]) +AC_CONFIG_HEADER([ommysql-config.h]) + +# Checks for programs. +AC_PROG_CC +AM_PROG_CC_C_O +if test "$GCC" = "yes" +then CFLAGS="$CFLAGS -W -Wall -Wformat-security -Wshadow -Wcast-align -Wpointer-arith -Wmissing-format-attribute -g" +fi +AC_DISABLE_STATIC +AC_PROG_LIBTOOL +AC_CANONICAL_HOST + +# Checks for libraries. + +# Checks for header files. +AC_HEADER_STDC +AC_CHECK_HEADERS([stdlib.h string.h syslog.h sys/param.h]) + +# Checks for typedefs, structures, and compiler characteristics. + +# Checks for library functions. + +# SQL support +AC_CHECK_HEADERS( + [mysql/mysql.h],, + [AC_MSG_FAILURE([MySQL headers are missing])] +) +AC_SEARCH_LIBS( + [mysql_init], + [mysqlclient], + [mysql_cflags=`mysql_config --cflags` + mysql_libs=`mysql_config --libs` + ], + [AC_MSG_FAILURE([MySQL library is missing])], + [`mysql_config --libs`] +) +AC_SUBST(mysql_cflags) +AC_SUBST(mysql_libs) + +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT diff --git a/plugins/ommysql/contrib/delete_mysql b/plugins/ommysql/contrib/delete_mysql new file mode 100644 index 00000000..3ed84d17 --- /dev/null +++ b/plugins/ommysql/contrib/delete_mysql @@ -0,0 +1,52 @@ +#!/bin/bash + +# Database maintance script which can be used for rsyslog +# and phplogcon default database schema. +# Michael Mansour suggested it to be included - thx! + +# This program was original part of of PHPloghost +# Copyright (C) 2004 Tuatha de Dana +# some modifications for rsyslog by mmeckelein at 2007-08-08 +# 2007-08-13 mmeckelein: added dbhost and some other improvements +# suggested by Michael Mansour - thx a lot! + +# This program 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 2 +# of the License, or (at your option) any later version. + +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111- 1307, USA. + +# Change these variables to reflect your situation. +database=sqlrsyslogd +dbhost="localhost" +export table=systemevents +sqluser="" +password="" + +# Location of the mysql daemon: +mysqld=/usr/bin/mysql + +# A couple of steps should be taken to maintain your database. +# If not, the number of messages will fill your database. +# By default, logs are deleted after they're 30 days old. +# Change this to meet your requirements. +# rsyslog's default database template use two date columns +# ReceivedAt and DeviceReportedTime. You can use either of +# the two and in most cases it doesn't make a huge difference. +# See the property replacer doc at http://www.rsyslog.com/doc +# for details on the two dates. +SQL_DELETE="DELETE FROM $table WHERE ReceivedAt < CURDATE() - INTERVAL 30 DAY;" + +# After a large amount of rows have been deleted, we should # optimize the table. +SQL_OPT="OPTIMIZE TABLE $table;"; + +$mysqld -u$sqluser -p$password -h$dbhost -e"$SQL_DELETE" -D$database +$mysqld -u$sqluser -p$password -h$dbhost -e"$SQL_OPT" -D$database diff --git a/plugins/ommysql/createDB.sql b/plugins/ommysql/createDB.sql new file mode 100644 index 00000000..211cfb0e --- /dev/null +++ b/plugins/ommysql/createDB.sql @@ -0,0 +1,37 @@ +CREATE DATABASE Syslog; +USE Syslog; +CREATE TABLE SystemEvents +( + ID int unsigned not null auto_increment primary key, + CustomerID bigint, + ReceivedAt datetime NULL, + DeviceReportedTime datetime NULL, + Facility smallint NULL, + Priority smallint NULL, + FromHost varchar(60) NULL, + Message text, + NTSeverity int NULL, + Importance int NULL, + EventSource varchar(60), + EventUser varchar(60) NULL, + EventCategory int NULL, + EventID int NULL, + EventBinaryData text NULL, + MaxAvailable int NULL, + CurrUsage int NULL, + MinUsage int NULL, + MaxUsage int NULL, + InfoUnitID int NULL , + SysLogTag varchar(60), + EventLogType varchar(60), + GenericFileName VarChar(60), + SystemID int NULL +); + +CREATE TABLE SystemEventsProperties +( + ID int unsigned not null auto_increment primary key, + SystemEventID int NULL , + ParamName varchar(255) NULL , + ParamValue text NULL +); diff --git a/plugins/ommysql/ommysql.c b/plugins/ommysql/ommysql.c new file mode 100644 index 00000000..0f7a9b9c --- /dev/null +++ b/plugins/ommysql/ommysql.c @@ -0,0 +1,293 @@ +/* ommysql.c + * This is the implementation of the build-in output module for MySQL. + * + * NOTE: read comments in module-template.h to understand how this file + * works! + * + * File begun on 2007-07-20 by RGerhards (extracted from syslogd.c) + * + * Copyright 2007 Rainer Gerhards and Adiscon GmbH. + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * A copy of the GPL can be found in the file "COPYING" in this distribution. + */ +#include "ommysql-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 "ommysql.h" +#include "module-template.h" + +/* 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 + + +BEGINonSelectReadyWrite +CODESTARTonSelectReadyWrite +ENDonSelectReadyWrite + + +BEGINgetWriteFDForSelect +CODESTARTgetWriteFDForSelect +ENDgetWriteFDForSelect + + +/* 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; + } + } + + return iRet; +} + + +/* 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); + + /* 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 */ + } + + return iRet; +} + + +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 */ + if(*p != '>') { + 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; + + p++; /* eat '>' '*/ + + /* 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 { + CHKiRet(initMySQL(pData, 0)); + } + +CODE_STD_FINALIZERparseSelectorAct +ENDparseSelectorAct + + +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: + */ diff --git a/plugins/ommysql/ommysql.h b/plugins/ommysql/ommysql.h new file mode 100644 index 00000000..a8179bf7 --- /dev/null +++ b/plugins/ommysql/ommysql.h @@ -0,0 +1,36 @@ +/* omusrmsg.c + * These are the definitions for the build-in MySQL output module. + * + * File begun on 2007-07-13 by RGerhards (extracted from syslogd.c) + * + * Copyright 2007 Rainer Gerhards and Adiscon GmbH. + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * A copy of the GPL can be found in the file "COPYING" in this distribution. + */ +#ifndef OMMYSQL_H_INCLUDED +#define OMMYSQL_H_INCLUDED 1 + +/* prototypes */ +/* prototypes will be removed as syslogd needs no longer to directly + * call into the module! + */ +rsRetVal modInitMySQL(int iIFVersRequested __attribute__((unused)), int *ipIFVersProvided, rsRetVal (**pQueryEtryPt)(), rsRetVal (*pHostQueryEtryPt)(uchar*, rsRetVal (**)())); + +#endif /* #ifndef OMMYSQL_H_INCLUDED */ +/* + * vi:set ai: + */ |