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