Code

Let the JSON formatter include a metric's data_names.
[sysdb.git] / src / plugins / syslog.c
1 /*
2  * SysDB - src/plugins/syslog.c
3  * Copyright (C) 2013, 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "utils/error.h"
32 #include "liboconfig/utils.h"
34 #include <assert.h>
35 #include <strings.h>
36 #include <syslog.h>
38 SDB_PLUGIN_MAGIC;
40 #if (SDB_LOG_EMERG != LOG_EMERG) \
41                 || (SDB_LOG_ERR != LOG_ERR) \
42                 || (SDB_LOG_WARNING != LOG_WARNING) \
43                 || (SDB_LOG_NOTICE != LOG_NOTICE) \
44                 || (SDB_LOG_INFO != LOG_INFO) \
45                 || (SDB_LOG_DEBUG != LOG_DEBUG)
46 #       define SDB_LOG_PRIO_TO_SYSLOG(prio) \
47                 (((prio) == SDB_LOG_EMERG) ? LOG_EMERG \
48                         : ((prio) == SDB_LOG_ERR) ? LOG_ERR \
49                         : ((prio) == SDB_LOG_WARNING) ? LOG_WARNING \
50                         : ((prio) == SDB_LOG_NOTICE) ? LOG_NOTICE \
51                         : ((prio) == SDB_LOG_INFO) ? LOG_INFO \
52                         : ((prio) == SDB_LOG_DEBUG) ? LOG_DEBUG : LOG_ERR)
53 #else
54 #       define SDB_LOG_PRIO_TO_SYSLOG(prio) (prio)
55 #endif
57 static int loglevel = SDB_DEFAULT_LOGLEVEL;
59 /*
60  * plugin API
61  */
63 static int
64 syslog_log(int prio, const char *msg,
65                 sdb_object_t __attribute__((unused)) *user_data)
66 {
67         if (prio > loglevel)
68                 return 0;
69         syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg);
70         return 0;
71 } /* syslog_log */
73 static int
74 syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data)
75 {
76         closelog();
77         return 0;
78 } /* syslog_shutdown */
80 static int
81 syslog_config(oconfig_item_t *ci)
82 {
83         int i;
85         if (! ci) {
86                 /* reset loglevel on deconfigure */
87                 loglevel = SDB_DEFAULT_LOGLEVEL;
88                 return 0;
89         }
91         for (i = 0; i < ci->children_num; ++i) {
92                 oconfig_item_t *child = ci->children + i;
94                 if (! strcasecmp(child->key, "LogLevel")) {
95                         char *level = NULL;
96                         if (oconfig_get_string(child, &level)) {
97                                 sdb_log(SDB_LOG_ERR, "syslog plugin: LogLevel requires "
98                                                 "a single string argument\n\tUsage: Loglevel LEVEL");
99                                 return -1;
100                         }
101                         loglevel = sdb_error_parse_priority(level);
102                         if (loglevel < 0) {
103                                 loglevel = SDB_DEFAULT_LOGLEVEL;
104                                 sdb_log(SDB_LOG_ERR,
105                                                 "syslog plugin: Invalid loglevel: '%s'", level);
106                                 return -1;
107                         }
108                         sdb_log(SDB_LOG_INFO, "syslog plugin: Log-level set to %s",
109                                         SDB_LOG_PRIO_TO_STRING(loglevel));
110                 }
111                 else
112                         sdb_log(SDB_LOG_WARNING, "syslog plugin: Ignoring unknown config "
113                                         "option '%s'.", child->key);
114         }
115         return 0;
116 } /* syslog_config */
118 int
119 sdb_module_init(sdb_plugin_info_t *info)
121         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
122                         "log messages to the system logger");
123         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
124                         "Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>");
125         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
126         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
127         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
129         if (info)
130                 openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON);
132         sdb_plugin_register_log("main", syslog_log, NULL);
133         sdb_plugin_register_config(syslog_config);
134         sdb_plugin_register_shutdown("main", syslog_shutdown, NULL);
135         return 0;
136 } /* sdb_module_init */
138 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */