From 6138090532ecfc07b00b10287f835dfe5a0bef3b Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 10 Jul 2013 16:55:16 +0200 Subject: [PATCH] syslog plugin: Added plugin to log to syslog. --- configure.ac | 2 + src/Makefile.am | 9 ++++ src/daemon/sysdbd.conf.sample | 8 +++ src/plugins/syslog.c | 99 +++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/plugins/syslog.c diff --git a/configure.ac b/configure.ac index e967867..6847c08 100644 --- a/configure.ac +++ b/configure.ac @@ -244,6 +244,8 @@ AC_SDB_PLUGIN([mk-livestatus], [yes], [backend accessing Nagios/Icinga/Shinken using MK Livestatus]) AC_SDB_PLUGIN([puppet-storeconfigs], [$puppet_storeconfigs_default], [backend accessing the Puppet stored configuration database]) +AC_SDB_PLUGIN([syslog], [yes], + [plugin logging to syslog]) AM_CONDITIONAL([BUILD_DOCUMENTATION], test "x$build_documentation" = "xyes") diff --git a/src/Makefile.am b/src/Makefile.am index 35ac77d..09b81b7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,6 +55,7 @@ pkgbackendlibdir = $(pkglibdir)/backend pkgbackendcollectdlibdir = $(pkgbackendlibdir)/collectd pkgbackendpuppetlibdir = $(pkgbackendlibdir)/puppet +pkglib_LTLIBRARIES = pkgbackendlib_LTLIBRARIES = pkgbackendcollectdlib_LTLIBRARIES = pkgbackendpuppetlib_LTLIBRARIES = @@ -83,6 +84,14 @@ libsysdb_la_LIBADD += -dlopen backend/puppet/store-configs.la libsysdb_la_DEPENDENCIES += backend/puppet/store-configs.la endif +if BUILD_PLUGIN_SYSLOG +pkglib_LTLIBRARIES += plugins/syslog.la +plugins_syslog_la_SOURCE = plugins/syslog.c +plugins_syslog_la_LDFLAGS = -module -avoid-version +libsysdb_la_LIBADD += -dlopen plugins/syslog.la +libsysdb_la_DEPENDENCIES += plugins/syslog.la +endif + include/sysdb.h: include/sysdb.h.in ../version source ../version; sed \ -e "s/@SDB_VERSION_MAJOR@/$$VERSION_MAJOR/g" \ diff --git a/src/daemon/sysdbd.conf.sample b/src/daemon/sysdbd.conf.sample index 304b0bb..40210fa 100644 --- a/src/daemon/sysdbd.conf.sample +++ b/src/daemon/sysdbd.conf.sample @@ -10,6 +10,14 @@ # default interval used for actively polling plugins Interval 300 +#----------------------------------------------------------------------------# +# Logging settings: # +# These plugins should be loaded first. Else, any log messages will be # +# written to the standard error channel which is closed after the daemon has # +# started. # +#----------------------------------------------------------------------------# +LoadPlugin "syslog" + #----------------------------------------------------------------------------# # Plugins: # # Plugins are the working horses of SysDB. Load any of the following plugins # diff --git a/src/plugins/syslog.c b/src/plugins/syslog.c new file mode 100644 index 0000000..b548988 --- /dev/null +++ b/src/plugins/syslog.c @@ -0,0 +1,99 @@ +/* + * SysDB - src/plugins/syslog.c + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "sysdb.h" +#include "core/plugin.h" +#include "core/error.h" + +#include +#include + +SDB_PLUGIN_MAGIC; + +#if (SDB_LOG_EMERG != LOG_EMERG) \ + || (SDB_LOG_ERR != LOG_ERR) \ + || (SDB_LOG_WARNING != LOG_WARNING) \ + || (SDB_LOG_NOTICE != LOG_NOTICE) \ + || (SDB_LOG_INFO != LOG_INFO) \ + || (SDB_LOG_DEBUG != LOG_DEBUG) +# define SDB_LOG_PRIO_TO_SYSLOG(prio) \ + (((prio) == SDB_LOG_EMERG) ? LOG_EMERG \ + : ((prio) == SDB_LOG_ERR) ? LOG_ERR \ + : ((prio) == SDB_LOG_WARNING) ? LOG_WARNING \ + : ((prio) == SDB_LOG_NOTICE) ? LOG_NOTICE \ + : ((prio) == SDB_LOG_INFO) ? LOG_INFO \ + : ((prio) == SDB_LOG_DEBUG) ? LOG_DEBUG : LOG_ERR) +#else +# define SDB_LOG_PRIO_TO_SYSLOG(prio) (prio) +#endif + +/* + * plugin API + */ + +static int +sdb_syslog_init(sdb_object_t __attribute__((unused)) *user_data) +{ + openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON); + return 0; +} /* sdb_syslog_init */ + +static int +sdb_syslog_log(int prio, const char *msg, + sdb_object_t __attribute__((unused)) *user_data) +{ + syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg); + return 0; +} /* sdb_syslog_log */ + +static int +sdb_syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data) +{ + closelog(); + return 0; +} /* sdb_syslog_shutdown */ + +int +sdb_module_init(sdb_plugin_info_t *info) +{ + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "syslog"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, + "log messages to the system logger"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT, + "Copyright (C) 2013 Sebastian 'tokkee' Harl "); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); + + sdb_plugin_register_init("syslog", sdb_syslog_init, NULL); + sdb_plugin_register_log("syslog", sdb_syslog_log, NULL); + sdb_plugin_register_shutdown("syslog", sdb_syslog_shutdown, NULL); + return 0; +} /* sdb_module_init */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + -- 2.30.2