Modified hardcoded eventname id to user defined
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / evel_syslog.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17 /**************************************************************************//**
18  * @file
19  * Implementation of EVEL functions relating to the Syslog.
20  *
21  ****************************************************************************/
22
23 #include <string.h>
24 #include <assert.h>
25 #include <stdlib.h>
26
27 #include "evel_throttle.h"
28
29 /**************************************************************************//**
30  * Create a new Syslog event.
31  *
32  * @note    The mandatory fields on the Syslog must be supplied to this factory
33  *          function and are immutable once set.  Optional fields have explicit
34  *          setter functions, but again values may only be set once so that the
35  *          Syslog has immutable properties.
36  * @param event_name  Unique Event Name confirming Domain AsdcModel Description
37  * @param event_id    A universal identifier of the event for: troubleshooting correlation, analysis, etc
38  * @param   event_source_type  The type of Syslog event source.
39  * @param   syslog_msg         The Syslog event message.
40  * @param   syslog_tag         The messgaeId identifying the type of message.
41  * @returns pointer to the newly manufactured ::EVENT_SYSLOG.  If the event is
42  *          not used (i.e. posted) it must be released using
43  *          ::evel_free_syslog.
44  * @retval  NULL  Failed to create the event.
45  *****************************************************************************/
46 EVENT_SYSLOG * evel_new_syslog(const char* ev_name, const char *ev_id,
47                                EVEL_SOURCE_TYPES event_source_type,
48                                const char * const syslog_msg,
49                                const char * const syslog_tag)
50 {
51   EVENT_SYSLOG * syslog = NULL;
52   EVEL_ENTER();
53
54   /***************************************************************************/
55   /* Check preconditions.                                                    */
56   /***************************************************************************/
57   assert(event_source_type < EVEL_MAX_SOURCE_TYPES);
58   assert(syslog_msg != NULL);
59   assert(syslog_tag != NULL);
60
61   /***************************************************************************/
62   /* Allocate the Syslog.                                                    */
63   /***************************************************************************/
64   syslog = malloc(sizeof(EVENT_SYSLOG));
65   if (syslog == NULL)
66   {
67     log_error_state("Out of memory");
68     goto exit_label;
69   }
70   memset(syslog, 0, sizeof(EVENT_SYSLOG));
71   EVEL_DEBUG("New Syslog is at %lp", syslog);
72
73   /***************************************************************************/
74   /* Initialize the header & the Syslog fields.  Optional string values are  */
75   /* uninitialized (NULL).                                                   */
76   /***************************************************************************/
77   evel_init_header_nameid(&syslog->header,ev_name,ev_id);
78   syslog->header.event_domain = EVEL_DOMAIN_SYSLOG;
79   syslog->major_version = EVEL_SYSLOG_MAJOR_VERSION;
80   syslog->minor_version = EVEL_SYSLOG_MINOR_VERSION;
81   syslog->event_source_type = event_source_type;
82   syslog->syslog_msg = strdup(syslog_msg);
83   syslog->syslog_tag = strdup(syslog_tag);
84   evel_init_option_int(&syslog->syslog_facility);
85   evel_init_option_int(&syslog->syslog_proc_id);
86   evel_init_option_int(&syslog->syslog_ver);
87   evel_init_option_string(&syslog->additional_filters);
88   evel_init_option_string(&syslog->event_source_host);
89   evel_init_option_string(&syslog->syslog_proc);
90   evel_init_option_string(&syslog->syslog_s_data);
91   evel_init_option_string(&syslog->syslog_sdid);
92   evel_init_option_string(&syslog->syslog_severity);
93
94 exit_label:
95   EVEL_EXIT();
96   return syslog;
97 }
98
99 /**************************************************************************//**
100  * Set the Event Type property of the Syslog.
101  *
102  * @note  The property is treated as immutable: it is only valid to call
103  *        the setter once.  However, we don't assert if the caller tries to
104  *        overwrite, just ignoring the update instead.
105  *
106  * @param syslog      Pointer to the syslog.
107  * @param type        The Event Type to be set. ASCIIZ string. The caller
108  *                    does not need to preserve the value once the function
109  *                    returns.
110  *****************************************************************************/
111 void evel_syslog_type_set(EVENT_SYSLOG * syslog,
112                           const char * const type)
113 {
114   EVEL_ENTER();
115
116   /***************************************************************************/
117   /* Check preconditions and call evel_header_type_set.                      */
118   /***************************************************************************/
119   assert(syslog != NULL);
120   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
121   evel_header_type_set(&syslog->header, type);
122
123   EVEL_EXIT();
124 }
125
126 /**************************************************************************//**
127  * Add an additional value name/value pair to the Syslog.
128  *
129  * The name and value are null delimited ASCII strings.  The library takes
130  * a copy so the caller does not have to preserve values after the function
131  * returns.
132  *
133  * @param syslog    Pointer to the syslog.
134  * @param name      ASCIIZ string with the attribute's name.  The caller
135  *                  does not need to preserve the value once the function
136  *                  returns.
137  * @param value     ASCIIZ string with the attribute's value.  The caller
138  *                  does not need to preserve the value once the function
139  *                  returns.
140  *****************************************************************************/
141 void evel_syslog_addl_filter_set(EVENT_SYSLOG * syslog,
142                                 char * filter)
143 {
144   EVEL_ENTER();
145
146   /***************************************************************************/
147   /* Check preconditions.                                                    */
148   /***************************************************************************/
149   assert(syslog != NULL);
150   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
151   assert(filter != NULL);
152
153   evel_set_option_string(&syslog->additional_filters,
154                          filter,
155                          "Syslog filter string");
156
157   EVEL_EXIT();
158 }
159
160 /**************************************************************************//**
161  * Set the Event Source Host property of the Syslog.
162  *
163  * @note  The property is treated as immutable: it is only valid to call
164  *        the setter once.  However, we don't assert if the caller tries to
165  *        overwrite, just ignoring the update instead.
166  *
167  * @param syslog     Pointer to the Syslog.
168  * @param host       The Event Source Host to be set. ASCIIZ string. The caller
169  *                   does not need to preserve the value once the function
170  *                   returns.
171  *****************************************************************************/
172 void evel_syslog_event_source_host_set(EVENT_SYSLOG * syslog,
173                                        const char * const host)
174 {
175   EVEL_ENTER();
176
177   /***************************************************************************/
178   /* Check preconditions.                                                    */
179   /***************************************************************************/
180   assert(syslog != NULL);
181   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
182   assert(host != NULL);
183
184   evel_set_option_string(&syslog->event_source_host,
185                          host,
186                          "Event Source Host");
187   EVEL_EXIT();
188 }
189
190 /**************************************************************************//**
191  * Set the Facility property of the Syslog.
192  *
193  * @note  The property is treated as immutable: it is only valid to call
194  *        the setter once.  However, we don't assert if the caller tries to
195  *        overwrite, just ignoring the update instead.
196  *
197  * @param syslog      Pointer to the Syslog.
198  * @param facility    The Syslog Facility to be set.  ASCIIZ string. The caller
199  *                    does not need to preserve the value once the function
200  *                    returns.
201  *****************************************************************************/
202 void evel_syslog_facility_set(EVENT_SYSLOG * syslog,
203                               EVEL_SYSLOG_FACILITIES facility)
204 {
205   EVEL_ENTER();
206
207   /***************************************************************************/
208   /* Check preconditions.                                                    */
209   /***************************************************************************/
210   assert(syslog != NULL);
211   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
212   assert(facility < EVEL_MAX_SYSLOG_FACILITIES);
213
214   evel_set_option_int(&syslog->syslog_facility,
215                       facility,
216                       "Facility");
217   EVEL_EXIT();
218 }
219
220 /**************************************************************************//**
221  * Set the Process property of the Syslog.
222  *
223  * @note  The property is treated as immutable: it is only valid to call
224  *        the setter once.  However, we don't assert if the caller tries to
225  *        overwrite, just ignoring the update instead.
226  *
227  * @param syslog     Pointer to the Syslog.
228  * @param proc       The Process to be set. ASCIIZ string. The caller does not
229  *                   need to preserve the value once the function returns.
230  *****************************************************************************/
231 void evel_syslog_proc_set(EVENT_SYSLOG * syslog, const char * const proc)
232 {
233   EVEL_ENTER();
234
235   /***************************************************************************/
236   /* Check preconditions.                                                    */
237   /***************************************************************************/
238   assert(syslog != NULL);
239   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
240   assert(proc != NULL);
241
242   evel_set_option_string(&syslog->syslog_proc, proc, "Process");
243   EVEL_EXIT();
244 }
245
246 /**************************************************************************//**
247  * Set the Process ID property of the Syslog.
248  *
249  * @note  The property is treated as immutable: it is only valid to call
250  *        the setter once.  However, we don't assert if the caller tries to
251  *        overwrite, just ignoring the update instead.
252  *
253  * @param syslog     Pointer to the Syslog.
254  * @param proc_id    The Process ID to be set. ASCIIZ string. The caller does
255  *                   not need to preserve the value once the function returns.
256  *****************************************************************************/
257 void evel_syslog_proc_id_set(EVENT_SYSLOG * syslog, int proc_id)
258 {
259   EVEL_ENTER();
260
261   /***************************************************************************/
262   /* Check preconditions.                                                    */
263   /***************************************************************************/
264   assert(syslog != NULL);
265   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
266   assert(proc_id > 0);
267
268   evel_set_option_int(&syslog->syslog_proc_id,
269                       proc_id,
270                       "Process ID");
271   EVEL_EXIT();
272 }
273
274 /**************************************************************************//**
275  * Set the Version property of the Syslog.
276  *
277  * @note  The property is treated as immutable: it is only valid to call
278  *        the setter once.  However, we don't assert if the caller tries to
279  *        overwrite, just ignoring the update instead.
280  *
281  * @param syslog     Pointer to the Syslog.
282  * @param version    The Version to be set. ASCIIZ string. The caller does not
283  *                   need to preserve the value once the function returns.
284  *****************************************************************************/
285 void evel_syslog_version_set(EVENT_SYSLOG * syslog, int version)
286 {
287   EVEL_ENTER();
288
289   /***************************************************************************/
290   /* Check preconditions.                                                    */
291   /***************************************************************************/
292   assert(syslog != NULL);
293   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
294   assert(version >= 0);
295
296   evel_set_option_int(&syslog->syslog_ver,
297                       version,
298                       "Version");
299   EVEL_EXIT();
300 }
301
302 /**************************************************************************//**
303  * Set the Structured Data property of the Syslog.
304  *
305  * @note  The property is treated as immutable: it is only valid to call
306  *        the setter once.  However, we don't assert if the caller tries to
307  *        overwrite, just ignoring the update instead.
308  *
309  * @param syslog     Pointer to the Syslog.
310  * @param s_data     The Structured Data to be set. ASCIIZ string. The caller
311  *                   does not need to preserve the value once the function
312  *                   returns.
313  *****************************************************************************/
314 void evel_syslog_s_data_set(EVENT_SYSLOG * syslog, const char * const s_data)
315 {
316   EVEL_ENTER();
317
318   /***************************************************************************/
319   /* Check preconditions.                                                    */
320   /***************************************************************************/
321   assert(syslog != NULL);
322   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
323   assert(s_data != NULL);
324
325   evel_set_option_string(&syslog->syslog_s_data,
326                          s_data,
327                          "Structured Data");
328   EVEL_EXIT();
329 }
330
331 /**************************************************************************//**
332  * Set the Structured SDID property of the Syslog.
333  *
334  * @note  The property is treated as immutable: it is only valid to call
335  *        the setter once.  However, we don't assert if the caller tries to
336  *        overwrite, just ignoring the update instead.
337  *
338  * @param syslog     Pointer to the Syslog.
339  * @param sdid     The Structured Data to be set. ASCIIZ string. name@number
340  *                 Caller does not need to preserve the value once the function
341  *                   returns.
342  *****************************************************************************/
343 void evel_syslog_sdid_set(EVENT_SYSLOG * syslog, const char * const sdid)
344 {
345   EVEL_ENTER();
346
347   /***************************************************************************/
348   /* Check preconditions.                                                    */
349   /***************************************************************************/
350   assert(syslog != NULL);
351   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
352   assert(sdid != NULL);
353
354   evel_set_option_string(&syslog->syslog_sdid,
355                          sdid,
356                          "SdId set");
357   EVEL_EXIT();
358 }
359
360 /**************************************************************************//**
361  * Set the Structured Severity property of the Syslog.
362  *
363  * @note  The property is treated as immutable: it is only valid to call
364  *        the setter once.  However, we don't assert if the caller tries to
365  *        overwrite, just ignoring the update instead.
366  *
367  * @param syslog     Pointer to the Syslog.
368  * @param sdid     The Structured Data to be set. ASCIIZ string. 
369  *                 Caller does not need to preserve the value once the function
370  *                   returns.
371  *****************************************************************************/
372 void evel_syslog_severity_set(EVENT_SYSLOG * syslog, const char * const severty)
373 {
374   EVEL_ENTER();
375
376   /***************************************************************************/
377   /* Check preconditions.                                                    */
378   /***************************************************************************/
379   assert(syslog != NULL);
380   assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
381   assert(severty != NULL);
382
383   if( !strcmp(severty,"Alert") || !strcmp(severty,"Critical") || !strcmp(severty,"Debug") ||
384       !strcmp(severty,"Emergency") || !strcmp(severty,"Error") || !strcmp(severty,"Info") ||
385       !strcmp(severty,"Notice") || !strcmp(severty,"Warning") )
386   {
387      evel_set_option_string(&syslog->syslog_severity,
388                          severty,
389                          "Severity set");
390   }
391   EVEL_EXIT();
392 }
393
394 /**************************************************************************//**
395  * Encode the Syslog in JSON according to AT&T's schema for the event type.
396  *
397  * @param jbuf          Pointer to the ::EVEL_JSON_BUFFER to encode into.
398  * @param event         Pointer to the ::EVENT_HEADER to encode.
399  *****************************************************************************/
400 void evel_json_encode_syslog(EVEL_JSON_BUFFER * jbuf,
401                              EVENT_SYSLOG * event)
402 {
403   char * event_source_type;
404
405   EVEL_ENTER();
406
407   /***************************************************************************/
408   /* Check preconditions.                                                    */
409   /***************************************************************************/
410   assert(event != NULL);
411   assert(event->header.event_domain == EVEL_DOMAIN_SYSLOG);
412
413   event_source_type = evel_source_type(event->event_source_type);
414
415   evel_json_encode_header(jbuf, &event->header);
416   evel_json_open_named_object(jbuf, "syslogFields");
417
418   evel_enc_kv_opt_string(jbuf, "additionalFields", &event->additional_filters);
419   /***************************************************************************/
420   /* Mandatory fields                                                        */
421   /***************************************************************************/
422   evel_enc_kv_string(jbuf, "eventSourceType", event_source_type);
423   evel_enc_kv_string(jbuf, "syslogMsg", event->syslog_msg);
424   evel_enc_kv_string(jbuf, "syslogTag", event->syslog_tag);
425   evel_enc_version(
426     jbuf, "syslogFieldsVersion", event->major_version, event->minor_version);
427
428   /***************************************************************************/
429   /* Optional fields                                                         */
430   /***************************************************************************/
431   evel_enc_kv_opt_string(jbuf, "eventSourceHost", &event->event_source_host);
432   evel_enc_kv_opt_int(jbuf, "syslogFacility", &event->syslog_facility);
433   evel_enc_kv_opt_int(jbuf, "syslogPri", &event->syslog_priority);
434   evel_enc_kv_opt_string(jbuf, "syslogProc", &event->syslog_proc);
435   evel_enc_kv_opt_int(jbuf, "syslogProcId", &event->syslog_proc_id);
436   evel_enc_kv_opt_string(jbuf, "syslogSData", &event->syslog_s_data);
437   evel_enc_kv_opt_string(jbuf, "syslogSdId", &event->syslog_sdid);
438   evel_enc_kv_opt_string(jbuf, "syslogSev", &event->syslog_severity);
439   evel_enc_kv_opt_int(jbuf, "syslogVer", &event->syslog_ver);
440   evel_json_close_object(jbuf);
441
442   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_KERNEL == 0);
443   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_USER == 1);
444   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_MAIL == 2);
445   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_SYSTEM_DAEMON == 3);
446   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_SECURITY_AUTH == 4);
447   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_INTERNAL == 5);
448   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LINE_PRINTER == 6);
449   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_NETWORK_NEWS == 7);
450   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_UUCP == 8);
451   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_CLOCK_DAEMON == 9);
452   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_SECURITY_AUTH2 == 10);
453   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_FTP_DAEMON == 11);
454   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_NTP == 12);
455   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOG_AUDIT == 13);
456   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOG_ALERT == 14);
457   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_CLOCK_DAEMON2 == 15);
458   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL0 == 16);
459   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL1 == 17);
460   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL2 == 18);
461   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL3 == 19);
462   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL4 == 20);
463   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL5 == 21);
464   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL6 == 22);
465   EVEL_CT_ASSERT(EVEL_SYSLOG_FACILITY_LOCAL7 == 23);
466
467   EVEL_EXIT();
468 }
469
470 /**************************************************************************//**
471  * Free a Syslog.
472  *
473  * Free off the Syslog supplied.  Will free all the contained allocated memory.
474  *
475  * @note It does not free the Syslog itself, since that may be part of a
476  * larger structure.
477  *****************************************************************************/
478 void evel_free_syslog(EVENT_SYSLOG * event)
479 {
480
481   EVEL_ENTER();
482
483   /***************************************************************************/
484   /* Check preconditions.  As an internal API we don't allow freeing NULL    */
485   /* events as we do on the public API.                                      */
486   /***************************************************************************/
487   assert(event != NULL);
488   assert(event->header.event_domain == EVEL_DOMAIN_SYSLOG);
489
490   /***************************************************************************/
491   /* Free all internal strings then the header itself.                       */
492   /***************************************************************************/
493
494   evel_free_option_string(&event->additional_filters);
495   evel_free_option_string(&event->event_source_host);
496   free(event->syslog_msg);
497   evel_free_option_string(&event->syslog_proc);
498   evel_free_option_string(&event->syslog_s_data);
499   evel_free_option_string(&event->syslog_sdid);
500   evel_free_option_string(&event->syslog_severity);
501   free(event->syslog_tag);
502   evel_free_header(&event->header);
503
504   EVEL_EXIT();
505 }