X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsyslog.c;h=3f73178fa5e09283dec5efee71f57a3d941b0735;hb=7cea19815ba24735e91dde1c08a889960b299b62;hp=d584eec905a03560228e05afac03e41769c42edd;hpb=c888188e5aabb3600d30ef48202b6f1c53349024;p=collectd.git diff --git a/src/syslog.c b/src/syslog.c index d584eec9..3f73178f 100644 --- a/src/syslog.c +++ b/src/syslog.c @@ -1,23 +1,27 @@ /** * collectd - src/syslog.c - * Copyright (C) 2007 Florian Forster + * Copyright (C) 2007 Florian Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian Forster + * Florian Forster **/ #include "collectd.h" @@ -48,7 +52,11 @@ static int sl_config (const char *key, const char *value) { log_level = parse_log_severity (value); if (log_level < 0) + { + log_level = LOG_INFO; + ERROR ("syslog: invalid loglevel [%s] defaulting to 'info'", value); return (1); + } } else if (strcasecmp (key, "NotifyLevel") == 0) { @@ -80,60 +88,63 @@ static int sl_notification (const notification_t *n, user_data_t __attribute__((unused)) *user_data) { char buf[1024] = ""; - char *buf_ptr = buf; - int buf_len = sizeof (buf); + size_t offset = 0; + int log_severity; + char *severity_string; int status; - int severity; - - /* do nothing if parsing of NotifSeverity failed */ - if (notif_severity == -1) - return 0; - /* do nothing if NotifSeverity is higer than notification - * note that OKAY notifs will always be displayed */ - if ((notif_severity == NOTIF_FAILURE) && (n -> severity == NOTIF_WARNING)) - return 0; - - status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s", - (n->severity == NOTIF_FAILURE) ? "FAILURE" - : ((n->severity == NOTIF_WARNING) ? "WARNING" - : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); - if (status > 0) - { - buf_ptr += status; - buf_len -= status; - } -#define APPEND(bufptr, buflen, key, value) \ - if ((buflen > 0) && (strlen (value) > 0)) { \ - int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \ - if (status > 0) { \ - bufptr += status; \ - buflen -= status; \ - } \ - } - APPEND (buf_ptr, buf_len, "host", n->host); - APPEND (buf_ptr, buf_len, "plugin", n->plugin); - APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance); - APPEND (buf_ptr, buf_len, "type", n->type); - APPEND (buf_ptr, buf_len, "type_instance", n->type_instance); - APPEND (buf_ptr, buf_len, "message", n->message); - - buf[sizeof (buf) - 1] = '\0'; + if (n->severity > notif_severity) + return (0); switch (n->severity) { case NOTIF_FAILURE: - severity = LOG_ERR; + severity_string = "FAILURE"; + log_severity = LOG_ERR; break; case NOTIF_WARNING: - severity = LOG_WARNING; + severity_string = "WARNING"; + log_severity = LOG_WARNING; break; case NOTIF_OKAY: - severity = LOG_WARNING; + severity_string = "OKAY"; + log_severity = LOG_NOTICE; break; - default: severity = LOG_INFO; + default: + severity_string = "UNKNOWN"; + log_severity = LOG_ERR; } - sl_log (severity, buf, NULL); + +#define BUFFER_ADD(...) do { \ + status = ssnprintf (&buf[offset], sizeof (buf) - offset, \ + __VA_ARGS__); \ + if (status < 1) \ + return (-1); \ + else if (((size_t) status) >= (sizeof (buf) - offset)) \ + return (-ENOMEM); \ + else \ + offset += ((size_t) status); \ +} while (0) + +#define BUFFER_ADD_FIELD(field) do { \ + if (n->field[0]) \ + BUFFER_ADD (", " #field " = %s", n->field); \ +} while (0) + + BUFFER_ADD ("Notification: severity = %s", severity_string); + BUFFER_ADD_FIELD (host); + BUFFER_ADD_FIELD (plugin); + BUFFER_ADD_FIELD (plugin_instance); + BUFFER_ADD_FIELD (type); + BUFFER_ADD_FIELD (type_instance); + BUFFER_ADD_FIELD (message); + +#undef BUFFER_ADD_FIELD +#undef BUFFER_ADD + + buf[sizeof (buf) - 1] = '\0'; + + sl_log (log_severity, buf, NULL); return (0); } /* int sl_notification */