Hi all!
As already mentioned in the forum: I've created an output module for
PostgreSQL.
The attached patch does the following:
* add plugins/ompgsql/*
* extend configure.ac and Makefile.am
* msg.h
** struct msg_t: add pszTIMESTAMP_PgSQL, add pszRcvdAt_PgSQL, fix typo
* msg.c
** getTimeGenerated(): add handling of tplFmtPgSQLDate
** getTimeReported(): add handling of tplFmtPgSQLDate
* syslogd.h:
** add definition of formatTimestampToPgSQL()
* syslogd.c:
** add StdPgSQLFmt
** mainThread(): include StdPgSQLFmt
* template.h:
** add tplFmtPgSQLDate to tplFormatTypes
* template.c:
** doOptions(): date-pgsql -> tplFmtPgSQLDate
** tplPrintList(): add tplFmtPgSQLDate to switch()
Please note that the patch has not yet been tested thoroughly, so until
that has been done, it is to be considered alpha quality.
If anyone wants to try it: any feedback is appreciated.
Regards,
sur5r
P.S.: I hope the patch survives mailman... ;)
? plugins/ompgsql
Index: Makefile.am
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/Makefile.am,v
retrieving revision 1.36
diff -u -r1.36 Makefile.am
--- Makefile.am 30 Nov 2007 13:43:12 -0000 1.36
+++ Makefile.am 1 Dec 2007 21:31:08 -0000
@@ -83,3 +83,7 @@
if ENABLE_MYSQL
SUBDIRS += plugins/ommysql
endif
+
+if ENABLE_PGSQL
+SUBDIRS += plugins/ompgsql
+endif
Index: configure.ac
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/configure.ac,v
retrieving revision 1.47
diff -u -r1.47 configure.ac
--- configure.ac 30 Nov 2007 13:43:12 -0000 1.47
+++ configure.ac 1 Dec 2007 21:31:09 -0000
@@ -262,7 +262,7 @@
-# SQL support
+# MySQL support
AC_ARG_ENABLE(mysql,
[AS_HELP_STRING([--enable-mysql],[Enable MySql database support
@<:@default=no@:>@])],
[case "${enableval}" in
@@ -299,10 +299,42 @@
AC_SUBST(mysql_cflags)
AC_SUBST(mysql_libs)
+# PostgreSQL support
+AC_ARG_ENABLE(pgsql,
+ [AS_HELP_STRING([--enable-pgsql],[Enable PostgreSQL database support
@<:@default=no@:>@])],
+ [case "${enableval}" in
+ yes) enable_pgsql="yes" ;;
+ no) enable_pgsql="no" ;;
+ *) AC_MSG_ERROR(bad value ${enableval} for --enable-pgsql) ;;
+ esac],
+ [enable_pgsql=no]
+)
+if test "x$enable_pgsql" = "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_PGSQL, test x$enable_pgsql = xyes)
+AC_SUBST(pgsql_cflags)
+AC_SUBST(pgsql_libs)
-AC_CONFIG_FILES([Makefile doc/Makefile plugins/ommysql/Makefile])
+AC_CONFIG_FILES([Makefile doc/Makefile plugins/ommysql/Makefile
plugins/ompgsql/Makefile])
AC_OUTPUT
echo "****************************************************"
@@ -313,6 +345,7 @@
echo "Regular expressions support enabled: $enable_regexp"
echo "Zlib compression support enabled: $enable_zlib"
echo "MySql support enabled: $enable_mysql"
+echo "PostgreSQL support enabled: $enable_pgsql"
echo "Large file support enabled: $enable_largefile"
echo "Networking support enabled: $enable_inet"
echo "Enable GSSAPI Kerberos 5 support: $want_gssapi_krb5"
Index: msg.c
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/msg.c,v
retrieving revision 1.20
diff -u -r1.20 msg.c
--- msg.c 27 Nov 2007 11:42:33 -0000 1.20
+++ msg.c 1 Dec 2007 21:31:12 -0000
@@ -536,6 +536,18 @@
}
MsgUnlock();
return(pM->pszTIMESTAMP_MySQL);
+ case tplFmtPgSQLDate:
+ MsgLock();
+ if(pM->pszTIMESTAMP_PgSQL == NULL) {
+ if((pM->pszTIMESTAMP_PgSQL = malloc(21)) == NULL) {
+ glblHadMemShortage = 1;
+ MsgUnlock();
+ return "";
+ }
+ formatTimestampToPgSQL(&pM->tTIMESTAMP,
pM->pszTIMESTAMP_PgSQL, 21);
+ }
+ MsgUnlock();
+ return(pM->pszTIMESTAMP_PgSQL);
case tplFmtRFC3164Date:
MsgLock();
if(pM->pszTIMESTAMP3164 == NULL) {
@@ -594,6 +606,18 @@
}
MsgUnlock();
return(pM->pszRcvdAt_MySQL);
+ case tplFmtPgSQLDate:
+ MsgLock();
+ if(pM->pszRcvdAt_PgSQL == NULL) {
+ if((pM->pszRcvdAt_PgSQL = malloc(21)) == NULL) {
+ glblHadMemShortage = 1;
+ MsgUnlock();
+ return "";
+ }
+ formatTimestampToPgSQL(&pM->tRcvdAt,
pM->pszRcvdAt_PgSQL, 21);
+ }
+ MsgUnlock();
+ return(pM->pszRcvdAt_PgSQL);
case tplFmtRFC3164Date:
MsgLock();
if(pM->pszRcvdAt3164 == NULL) {
Index: msg.h
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/msg.h,v
retrieving revision 1.5
diff -u -r1.5 msg.h
--- msg.h 5 Sep 2007 15:59:47 -0000 1.5
+++ msg.h 1 Dec 2007 21:31:12 -0000
@@ -90,11 +90,13 @@
struct syslogTime tRcvdAt;/* time the message entered this program */
char *pszRcvdAt3164; /* time as RFC3164 formatted string (always 15
charcters) */
char *pszRcvdAt3339; /* time as RFC3164 formatted string (32
charcters at most) */
- char *pszRcvdAt_MySQL; /* rcvdAt as MySQL formatted string (always 14
charcters) */
+ char *pszRcvdAt_MySQL; /* rcvdAt as MySQL formatted string (always 14
characters) */
+ char *pszRcvdAt_PgSQL; /* rcvdAt as PgSQL formatted string (always 21
characters) */
struct syslogTime tTIMESTAMP;/* (parsed) value of the timestamp */
char *pszTIMESTAMP3164; /* TIMESTAMP as RFC3164 formatted string
(always 15 charcters) */
char *pszTIMESTAMP3339; /* TIMESTAMP as RFC3339 formatted string (32
charcters at most) */
char *pszTIMESTAMP_MySQL;/* TIMESTAMP as MySQL formatted string (always
14 charcters) */
+ char *pszTIMESTAMP_PgSQL; /* TIMESTAMP as PgSQL formatted string
(always 21 characters) */
int msgFlags; /* flags associated with this message */
};
typedef struct msg msg_t; /* new name */
Index: syslogd.c
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/syslogd.c,v
retrieving revision 1.374
diff -u -r1.374 syslogd.c
--- syslogd.c 27 Nov 2007 18:04:21 -0000 1.374
+++ syslogd.c 1 Dec 2007 21:31:21 -0000
@@ -644,6 +644,7 @@
static uchar template_StdFwdFmt[] = "\"<%PRI%>%TIMESTAMP% %HOSTNAME%
%syslogtag%%msg%\"";
static uchar template_StdUsrMsgFmt[] = "\" %syslogtag%%msg%\n\r\"";
static uchar template_StdDBFmt[] = "\"insert into SystemEvents (Message,
Facility, FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID,
SysLogTag) values ('%msg%', %syslogfacility%, '%HOSTNAME%', %syslogpriority%,
'%timereported:::date-mysql%', '%timegenerated:::date-mysql%', %iut%,
'%syslogtag%')\",SQL";
+static uchar template_StdPgSQLFmt[] = "\"insert into SystemEvents (Message,
Facility, FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID,
SysLogTag) values ('%msg%', %syslogfacility%, '%HOSTNAME%', %syslogpriority%,
'%timereported:::date-pgsql%', '%timegenerated:::date-pgsql%', %iut%,
'%syslogtag%')\",STDSQL";
/* end template */
@@ -1440,6 +1441,19 @@
}
+int formatTimestampToPgSQL(struct syslogTime *ts, char *pDst, size_t iLenDst)
+{
+ /* see note in formatTimestampToMySQL, applies here as well */
+ assert(ts != NULL);
+ assert(pDst != NULL);
+
+ if (iLenDst < 21) /* we need 20 bytes + '\n' */
+ return(0);
+
+ return(snprintf(pDst, iLenDst, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
+ ts->year, ts->month, ts->day, ts->hour,
ts->minute, ts->second));
+}
+
/**
* Format a syslogTimestamp to a RFC3339 timestamp string (as
* specified in syslog-protocol).
@@ -6100,7 +6114,9 @@
pTmp = template_StdUsrMsgFmt;
tplAddLine(" StdUsrMsgFmt", &pTmp);
pTmp = template_StdDBFmt;
- tplLastStaticInit(tplAddLine(" StdDBFmt", &pTmp));
+ tplAddLine( "StdDBFmt", &pTmp);
+ pTmp = template_StdPgSQLFmt;
+ tplLastStaticInit(tplAddLine(" StdPgSQLFmt", &pTmp));
dbgprintf("Starting.\n");
init();
Index: syslogd.h
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/syslogd.h,v
retrieving revision 1.21
diff -u -r1.21 syslogd.h
--- syslogd.h 27 Nov 2007 11:42:33 -0000 1.21
+++ syslogd.h 1 Dec 2007 21:31:21 -0000
@@ -59,6 +59,7 @@
int isAllowedSender(struct AllowedSenders *pAllowRoot, struct sockaddr *pFrom,
const char *pszFromHost);
void getCurrTime(struct syslogTime *t);
int formatTimestampToMySQL(struct syslogTime *ts, char* pDst, size_t iLenDst);
+int formatTimestampToPgSQL(struct syslogTime *ts, char* pDst, size_t iLenDst);
int formatTimestamp3339(struct syslogTime *ts, char* pBuf, size_t iLenBuf);
int formatTimestamp3164(struct syslogTime *ts, char* pBuf, size_t iLenBuf);
void untty(void);
Index: template.c
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/template.c,v
retrieving revision 1.41
diff -u -r1.41 template.c
--- template.c 5 Sep 2007 15:59:47 -0000 1.41
+++ template.c 1 Dec 2007 21:31:22 -0000
@@ -424,6 +424,8 @@
*/
if(!strcmp((char*)Buf, "date-mysql")) {
pTpe->data.field.eDateFormat = tplFmtMySQLDate;
+ } else if(!strcmp((char*)Buf, "date-pgsql")) {
+ pTpe->data.field.eDateFormat = tplFmtPgSQLDate;
} else if(!strcmp((char*)Buf, "date-rfc3164")) {
pTpe->data.field.eDateFormat = tplFmtRFC3164Date;
} else if(!strcmp((char*)Buf, "date-rfc3339")) {
@@ -941,6 +943,9 @@
case tplFmtMySQLDate:
dbgprintf("[Format as MySQL-Date] ");
break;
+ case tplFmtPgSQLDate:
+ dbgprintf("[Format as PgSQL-Date] ");
+ break;
case tplFmtRFC3164Date:
dbgprintf("[Format as RFC3164-Date] ");
break;
Index: template.h
===================================================================
RCS file: /cvsroot/rsyslog/rsyslog/template.h,v
retrieving revision 1.24
diff -u -r1.24 template.h
--- template.h 5 Sep 2007 15:59:47 -0000 1.24
+++ template.h 1 Dec 2007 21:31:22 -0000
@@ -34,7 +34,7 @@
enum EntryTypes { UNDEFINED = 0, CONSTANT = 1, FIELD = 2 };
enum tplFormatTypes { tplFmtDefault = 0, tplFmtMySQLDate = 1,
- tplFmtRFC3164Date = 2, tplFmtRFC3339Date = 3 };
+ tplFmtRFC3164Date = 2, tplFmtRFC3339Date = 3,
tplFmtPgSQLDate = 4 };
enum tplFormatCaseConvTypes { tplCaseConvNo = 0, tplCaseConvUpper = 1,
tplCaseConvLower = 2 };
#include "msg.h"
_______________________________________________
rsyslog mailing list
http://lists.adiscon.net/mailman/listinfo/rsyslog