2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
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; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
20 * Florian Forster <octo at verplant.org>
29 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
32 static int log_level = LOG_DEBUG;
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
37 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
39 static char *log_file = NULL;
40 static int print_timestamp = 1;
42 static const char *config_keys[] =
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50 static int logfile_config (const char *key, const char *value)
52 if (0 == strcasecmp (key, "LogLevel")) {
53 if ((0 == strcasecmp (value, "emerg"))
54 || (0 == strcasecmp (value, "alert"))
55 || (0 == strcasecmp (value, "crit"))
56 || (0 == strcasecmp (value, "err")))
58 else if (0 == strcasecmp (value, "warning"))
59 log_level = LOG_WARNING;
60 else if (0 == strcasecmp (value, "notice"))
61 log_level = LOG_NOTICE;
62 else if (0 == strcasecmp (value, "info"))
65 else if (0 == strcasecmp (value, "debug"))
66 log_level = LOG_DEBUG;
67 #endif /* COLLECT_DEBUG */
71 else if (0 == strcasecmp (key, "File")) {
73 log_file = strdup (value);
75 else if (0 == strcasecmp (key, "Timestamp")) {
76 if ((strcasecmp (value, "false") == 0)
77 || (strcasecmp (value, "no") == 0)
78 || (strcasecmp (value, "off") == 0))
87 } /* int logfile_config (const char *, const char *) */
89 static void logfile_print (const char *msg, time_t timestamp_time)
93 struct tm timestamp_tm;
94 char timestamp_str[64];
98 localtime_r (×tamp_time, ×tamp_tm);
100 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
102 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
105 pthread_mutex_lock (&file_lock);
107 if (log_file == NULL)
109 fh = fopen (DEFAULT_LOGFILE, "a");
112 else if (strcasecmp (log_file, "stderr") == 0)
114 else if (strcasecmp (log_file, "stdout") == 0)
118 fh = fopen (log_file, "a");
125 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
126 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
127 sstrerror (errno, errbuf, sizeof (errbuf)));
132 fprintf (fh, "[%s] %s\n", timestamp_str, msg);
134 fprintf (fh, "%s\n", msg);
140 pthread_mutex_unlock (&file_lock);
143 } /* void logfile_print */
145 static void logfile_log (int severity, const char *msg)
147 if (severity > log_level)
150 logfile_print (msg, time (NULL));
151 } /* void logfile_log (int, const char *) */
153 int logfile_notification (const notification_t *n)
158 status = snprintf (msg, sizeof (msg), "Notification: %s: message = %s, "
160 (n->severity == NOTIF_FAILURE) ? "FAILURE"
161 : ((n->severity == NOTIF_WARNING) ? "WARNING"
162 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")),
163 n->message, n->host);
164 msg[sizeof (msg) - 1] = '\0';
166 logfile_print (msg, n->time);
169 } /* int logfile_notification */
171 void module_register (void)
173 plugin_register_config ("logfile", logfile_config,
174 config_keys, config_keys_num);
175 plugin_register_log ("logfile", logfile_log);
176 plugin_register_notification ("logfile", logfile_notification);
177 } /* void module_register (void) */
179 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */