1 /*****************************************************************************\
2  *  $Id: ipmiseld.h,v 1.11 2010-02-08 22:02:30 chu11 Exp $
3  *****************************************************************************
4  *  Copyright (C) 2012-2015 Lawrence Livermore National Security, LLC.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Albert Chu <chu11@llnl.gov>
7  *  LLNL-CODE-559172
8  *
9  *  This file is part of Ipmiseld, an IPMI SEL syslog logging daemon.
10  *  For details, see http://www.llnl.gov/linux/.
11  *
12  *  Ipmiseld is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 3 of the License, or (at your
15  *  option) any later version.
16  *
17  *  Ipmiseld is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  *  for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with Ipmiseld.  If not, see <http://www.gnu.org/licenses/>.
24 \*****************************************************************************/
25 
26 #if HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #if STDC_HEADERS
33 #include <string.h>
34 #include <stdarg.h>
35 #endif /* STDC_HEADERS */
36 #include <syslog.h>
37 #include <assert.h>
38 #include <errno.h>
39 
40 #include <freeipmi/freeipmi.h>
41 
42 #include "ipmiseld.h"
43 #include "ipmiseld-common.h"
44 
45 #include "freeipmi-portability.h"
46 #include "error.h"
47 
48 #define IPMISELD_ERR_BUFLEN 1024
49 
50 int
ipmiseld_event_state_filter_parse(const char * str)51 ipmiseld_event_state_filter_parse (const char *str)
52 {
53   char *strtmp;
54   char *tok;
55   int rv = -1;
56   int filter_mask = 0;
57 
58   assert (str);
59 
60   if (!(strtmp = strdup (str)))
61     err_exit ("strdup: %s", strerror (errno));
62 
63   tok = strtok (strtmp, " ,");
64   while (tok)
65     {
66       if (!strcasecmp (tok, "NOMINAL"))
67         filter_mask |= IPMISELD_NOMINAL_FILTER;
68       else if (!strcasecmp (tok, "WARNING"))
69         filter_mask |= IPMISELD_WARNING_FILTER;
70       else if (!strcasecmp (tok, "CRITICAL"))
71         filter_mask |= IPMISELD_CRITICAL_FILTER;
72       else if (!strcasecmp (tok, "NA"))
73         filter_mask |= IPMISELD_NA_FILTER;
74       else if (!strcasecmp (tok, "none"))
75         {
76           filter_mask = 0;
77           break;
78         }
79       else
80         {
81           filter_mask = -1;
82           break;
83         }
84       tok = strtok (NULL, " ,");
85     }
86 
87   rv = filter_mask;
88 
89   free (strtmp);
90   return (rv);
91 }
92 
93 int
ipmiseld_log_facility_parse(const char * str)94 ipmiseld_log_facility_parse (const char *str)
95 {
96   assert (str);
97 
98   if (!strcasecmp (str, "LOG_DAEMON"))
99     return (LOG_DAEMON);
100   else if (!strcasecmp (str, "LOG_USER"))
101     return (LOG_USER);
102   else if (!strcasecmp (str, "LOG_LOCAL0"))
103     return (LOG_LOCAL0);
104   else if (!strcasecmp (str, "LOG_LOCAL1"))
105     return (LOG_LOCAL1);
106   else if (!strcasecmp (str, "LOG_LOCAL2"))
107     return (LOG_LOCAL2);
108   else if (!strcasecmp (str, "LOG_LOCAL3"))
109     return (LOG_LOCAL3);
110   else if (!strcasecmp (str, "LOG_LOCAL4"))
111     return (LOG_LOCAL4);
112   else if (!strcasecmp (str, "LOG_LOCAL5"))
113     return (LOG_LOCAL5);
114   else if (!strcasecmp (str, "LOG_LOCAL6"))
115     return (LOG_LOCAL6);
116   else if (!strcasecmp (str, "LOG_LOCAL7"))
117     return (LOG_LOCAL7);
118   return (-1);
119 }
120 
121 int
ipmiseld_log_priority_parse(const char * str)122 ipmiseld_log_priority_parse (const char *str)
123 {
124   assert (str);
125 
126   if (!strcasecmp (str, "LOG_EMERG"))
127     return (LOG_EMERG);
128   else if (!strcasecmp (str, "LOG_ALERT"))
129     return (LOG_ALERT);
130   else if (!strcasecmp (str, "LOG_CRIT"))
131     return (LOG_CRIT);
132   else if (!strcasecmp (str, "LOG_ERR"))
133     return (LOG_ERR);
134   else if (!strcasecmp (str, "LOG_WARNING"))
135     return (LOG_WARNING);
136   else if (!strcasecmp (str, "LOG_NOTICE"))
137     return (LOG_NOTICE);
138   else if (!strcasecmp (str, "LOG_INFO"))
139     return (LOG_INFO);
140   else if (!strcasecmp (str, "LOG_DEBUG"))
141     return (LOG_DEBUG);
142   return (-1);
143 }
144 
145 
146 static void
_ipmiseld_syslog(ipmiseld_host_data_t * host_data,const char * message,va_list ap)147 _ipmiseld_syslog (ipmiseld_host_data_t *host_data,
148                   const char *message,
149                   va_list ap)
150 {
151   char buf[IPMISELD_ERR_BUFLEN + 1];
152 
153   memset (buf, '\0', IPMISELD_ERR_BUFLEN + 1);
154   vsnprintf(buf, IPMISELD_ERR_BUFLEN, message, ap);
155 
156   if (host_data->prog_data->args->test_run
157       || host_data->prog_data->args->foreground)
158     printf ("%s\n", buf);
159   else
160     syslog (host_data->prog_data->log_priority, "%s", buf);
161 }
162 
163 void
ipmiseld_syslog(ipmiseld_host_data_t * host_data,const char * message,...)164 ipmiseld_syslog (ipmiseld_host_data_t *host_data,
165                  const char *message,
166                  ...)
167 {
168   va_list ap;
169 
170   assert (host_data);
171   assert (message);
172 
173   va_start (ap, message);
174   _ipmiseld_syslog (host_data, message, ap);
175   va_end (ap);
176 }
177 
178 void
ipmiseld_syslog_host(ipmiseld_host_data_t * host_data,const char * message,...)179 ipmiseld_syslog_host (ipmiseld_host_data_t *host_data,
180                       const char *message,
181                       ...)
182 {
183   va_list ap;
184 
185   assert (host_data);
186   assert (message);
187 
188   va_start (ap, message);
189   if (!host_data->hostname)
190     _ipmiseld_syslog (host_data, message, ap);
191   else
192     {
193       char buf[IPMISELD_ERR_BUFLEN + 1];
194       memset (buf, '\0', IPMISELD_ERR_BUFLEN + 1);
195       vsnprintf(buf, IPMISELD_ERR_BUFLEN, message, ap);
196 
197       if (host_data->prog_data->args->test_run
198           || host_data->prog_data->args->foreground)
199         printf ("%s: %s\n", host_data->hostname, buf);
200       else
201         syslog (host_data->prog_data->log_priority,
202                 "%s: %s",
203                 host_data->hostname,
204                 buf);
205     }
206   va_end (ap);
207 }
208 
209 void
ipmiseld_err_output(ipmiseld_host_data_t * host_data,const char * message,...)210 ipmiseld_err_output (ipmiseld_host_data_t *host_data,
211                      const char *message,
212                      ...)
213 {
214   char buf[IPMISELD_ERR_BUFLEN + 1];
215   va_list ap;
216 
217   assert (host_data);
218   assert (message);
219   memset (buf, '\0', IPMISELD_ERR_BUFLEN + 1);
220 
221   va_start (ap, message);
222   vsnprintf(buf, IPMISELD_ERR_BUFLEN, message, ap);
223 
224   if (!host_data->hostname)
225     err_output ("%s", buf);
226   else
227     err_output ("%s: %s", host_data->hostname, buf);
228   va_end (ap);
229 }
230