src/plugin.c: Fix use of strcmp().
[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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #if HAVE_SYSLOG_H
28 # include <syslog.h>
29 #endif
30
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 = -1;
37
38 static const char *config_keys[] =
39 {
40         "LogLevel",
41         "NotifyLevel",
42 };
43 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
44
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 == -1) return (1);
51         }
52         else if (strcasecmp (key, "NotifyLevel") == 0)
53         {
54                 notif_severity = parse_notif_severity(key);
55         }
56
57         return (0);
58 } /* int sl_config */
59
60 static void sl_log (int severity, const char *msg,
61                 user_data_t __attribute__((unused)) *user_data)
62 {
63         if (severity > log_level)
64                 return;
65
66         syslog (severity, "%s", msg);
67 } /* void sl_log */
68
69 static int sl_shutdown (void)
70 {
71         closelog ();
72
73         return (0);
74 }
75
76 static int sl_notification (const notification_t *n,
77                 user_data_t __attribute__((unused)) *user_data)
78 {
79         char  buf[1024] = "";
80         char *buf_ptr = buf;
81         int   buf_len = sizeof (buf);
82         int status;
83         int severity;
84
85         /* do nothing if parsing of NotifSeverity failed */
86         if (notif_severity == -1)
87                 return 0;
88         /* do nothing if NotifSeverity is higer than notification
89          * note that OKAY notifs will always be displayed */
90         if ((notif_severity == NOTIF_FAILURE) && (n -> severity == NOTIF_WARNING))
91                 return 0;
92
93         status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
94                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
95                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
96                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
97         if (status > 0)
98         {
99                 buf_ptr += status;
100                 buf_len -= status;
101         }
102
103 #define APPEND(bufptr, buflen, key, value) \
104         if ((buflen > 0) && (strlen (value) > 0)) { \
105                 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
106                 if (status > 0) { \
107                         bufptr += status; \
108                         buflen -= status; \
109                 } \
110         }
111         APPEND (buf_ptr, buf_len, "host", n->host);
112         APPEND (buf_ptr, buf_len, "plugin", n->plugin);
113         APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
114         APPEND (buf_ptr, buf_len, "type", n->type);
115         APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
116         APPEND (buf_ptr, buf_len, "message", n->message);
117
118         buf[sizeof (buf) - 1] = '\0';
119
120         switch (n->severity)
121         {
122                 case NOTIF_FAILURE:
123                         severity = LOG_ERR;
124                         break;
125                 case NOTIF_WARNING:
126                         severity = LOG_WARNING;
127                         break;
128                 case NOTIF_OKAY:
129                         severity = LOG_WARNING;
130                         break;
131                 default: severity = LOG_INFO;
132         }
133         sl_log (severity, buf, NULL);
134
135         return (0);
136 } /* int sl_notification */
137
138 void module_register (void)
139 {
140         openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
141
142         plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
143         plugin_register_log ("syslog", sl_log, /* user_data = */ NULL);
144         plugin_register_notification ("syslog", sl_notification, NULL);
145         plugin_register_shutdown ("syslog", sl_shutdown);
146 } /* void module_register(void) */