Code

syslog plugin: Log "OKAY" notifications with severity "NOTICE".
[collectd.git] / src / syslog.c
1 /**
2  * collectd - src/syslog.c
3  * Copyright (C) 2007  Florian Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian Forster <octo at verplant.org>
21  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #if HAVE_SYSLOG_H
28 # include <syslog.h>
29 #endif
31 #if COLLECT_DEBUG
32 static int log_level = LOG_DEBUG;
33 #else
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
36 static int notif_severity = 0;
38 static const char *config_keys[] =
39 {
40         "LogLevel",
41         "NotifyLevel",
42 };
43 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
45 static int sl_config (const char *key, const char *value)
46 {
47         if (strcasecmp (key, "LogLevel") == 0)
48         {
49                 log_level = parse_log_severity (value);
50                 if (log_level < 0)
51                         return (1);
52         }
53         else if (strcasecmp (key, "NotifyLevel") == 0)
54         {
55                 notif_severity = parse_notif_severity (value);
56                 if (notif_severity < 0)
57                         return (1);
58         }
60         return (0);
61 } /* int sl_config */
63 static void sl_log (int severity, const char *msg,
64                 user_data_t __attribute__((unused)) *user_data)
65 {
66         if (severity > log_level)
67                 return;
69         syslog (severity, "%s", msg);
70 } /* void sl_log */
72 static int sl_shutdown (void)
73 {
74         closelog ();
76         return (0);
77 }
79 static int sl_notification (const notification_t *n,
80                 user_data_t __attribute__((unused)) *user_data)
81 {
82         char  buf[1024] = "";
83         char *buf_ptr = buf;
84         int   buf_len = sizeof (buf);
85         int status;
86         int log_severity;
88         if (n->severity > notif_severity)
89                 return (0);
91         status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
92                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
93                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
94                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
95         if (status > 0)
96         {
97                 buf_ptr += status;
98                 buf_len -= status;
99         }
101 #define APPEND(bufptr, buflen, key, value) \
102         if ((buflen > 0) && (strlen (value) > 0)) { \
103                 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
104                 if (status > 0) { \
105                         bufptr += status; \
106                         buflen -= status; \
107                 } \
108         }
109         APPEND (buf_ptr, buf_len, "host", n->host);
110         APPEND (buf_ptr, buf_len, "plugin", n->plugin);
111         APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
112         APPEND (buf_ptr, buf_len, "type", n->type);
113         APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
114         APPEND (buf_ptr, buf_len, "message", n->message);
116 #undef APPEND
118         buf[sizeof (buf) - 1] = '\0';
120         switch (n->severity)
121         {
122                 case NOTIF_FAILURE:
123                         log_severity = LOG_ERR;
124                         break;
125                 case NOTIF_WARNING:
126                         log_severity = LOG_WARNING;
127                         break;
128                 case NOTIF_OKAY:
129                         log_severity = LOG_NOTICE;
130                         break;
131                 default:
132                         log_severity = LOG_ERR;
133         }
134         sl_log (log_severity, buf, NULL);
136         return (0);
137 } /* int sl_notification */
139 void module_register (void)
141         openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
143         plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
144         plugin_register_log ("syslog", sl_log, /* user_data = */ NULL);
145         plugin_register_notification ("syslog", sl_notification, NULL);
146         plugin_register_shutdown ("syslog", sl_shutdown);
147 } /* void module_register(void) */