1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 /**************************************************************************//**
20 * Implementation of EVEL functions relating to the Syslog.
22 ****************************************************************************/
28 #include "evel_throttle.h"
30 /**************************************************************************//**
31 * Create a new Syslog event.
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
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)
52 EVENT_SYSLOG * syslog = NULL;
55 /***************************************************************************/
56 /* Check preconditions. */
57 /***************************************************************************/
58 assert(event_source_type < EVEL_MAX_SOURCE_TYPES);
59 assert(syslog_msg != NULL);
60 assert(syslog_tag != NULL);
62 /***************************************************************************/
63 /* Allocate the Syslog. */
64 /***************************************************************************/
65 syslog = malloc(sizeof(EVENT_SYSLOG));
68 log_error_state("Out of memory");
71 memset(syslog, 0, sizeof(EVENT_SYSLOG));
72 EVEL_DEBUG("New Syslog is at %lp", syslog);
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);
100 /**************************************************************************//**
101 * Set the Event Type property of the Syslog.
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.
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
111 *****************************************************************************/
112 void evel_syslog_type_set(EVENT_SYSLOG * syslog,
113 const char * const type)
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);
127 /**************************************************************************//**
128 * Add an additional value name/value pair to the Syslog.
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
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
138 * @param value ASCIIZ string with the attribute's value. The caller
139 * does not need to preserve the value once the function
141 *****************************************************************************/
142 void evel_syslog_addl_filter_set(EVENT_SYSLOG * syslog,
147 /***************************************************************************/
148 /* Check preconditions. */
149 /***************************************************************************/
150 assert(syslog != NULL);
151 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
152 assert(filter != NULL);
154 evel_set_option_string(&syslog->additional_filters,
156 "Syslog filter string");
161 /**************************************************************************//**
162 * Set the Event Source Host property of the Syslog.
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.
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
172 *****************************************************************************/
173 void evel_syslog_event_source_host_set(EVENT_SYSLOG * syslog,
174 const char * const host)
178 /***************************************************************************/
179 /* Check preconditions. */
180 /***************************************************************************/
181 assert(syslog != NULL);
182 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
183 assert(host != NULL);
185 evel_set_option_string(&syslog->event_source_host,
187 "Event Source Host");
191 /**************************************************************************//**
192 * Set the Facility property of the Syslog.
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.
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
202 *****************************************************************************/
203 void evel_syslog_facility_set(EVENT_SYSLOG * syslog,
204 EVEL_SYSLOG_FACILITIES facility)
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);
215 evel_set_option_int(&syslog->syslog_facility,
221 /**************************************************************************//**
222 * Set the Process property of the Syslog.
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.
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)
236 /***************************************************************************/
237 /* Check preconditions. */
238 /***************************************************************************/
239 assert(syslog != NULL);
240 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
241 assert(proc != NULL);
243 evel_set_option_string(&syslog->syslog_proc, proc, "Process");
247 /**************************************************************************//**
248 * Set the Process ID property of the Syslog.
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.
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)
262 /***************************************************************************/
263 /* Check preconditions. */
264 /***************************************************************************/
265 assert(syslog != NULL);
266 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
269 evel_set_option_int(&syslog->syslog_proc_id,
275 /**************************************************************************//**
276 * Set the Version property of the Syslog.
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.
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)
290 /***************************************************************************/
291 /* Check preconditions. */
292 /***************************************************************************/
293 assert(syslog != NULL);
294 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
295 assert(version >= 0);
297 evel_set_option_int(&syslog->syslog_ver,
303 /**************************************************************************//**
304 * Set the Structured Data property of the Syslog.
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.
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
314 *****************************************************************************/
315 void evel_syslog_s_data_set(EVENT_SYSLOG * syslog, const char * const s_data)
319 /***************************************************************************/
320 /* Check preconditions. */
321 /***************************************************************************/
322 assert(syslog != NULL);
323 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
324 assert(s_data != NULL);
326 evel_set_option_string(&syslog->syslog_s_data,
332 /**************************************************************************//**
333 * Set the Structured SDID property of the Syslog.
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.
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
343 *****************************************************************************/
344 void evel_syslog_sdid_set(EVENT_SYSLOG * syslog, const char * const sdid)
348 /***************************************************************************/
349 /* Check preconditions. */
350 /***************************************************************************/
351 assert(syslog != NULL);
352 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
353 assert(sdid != NULL);
355 evel_set_option_string(&syslog->syslog_sdid,
361 /**************************************************************************//**
362 * Set the Structured Severity property of the Syslog.
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.
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
372 *****************************************************************************/
373 void evel_syslog_severity_set(EVENT_SYSLOG * syslog, const char * const severty)
377 /***************************************************************************/
378 /* Check preconditions. */
379 /***************************************************************************/
380 assert(syslog != NULL);
381 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
382 assert(severty != NULL);
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") )
388 evel_set_option_string(&syslog->syslog_severity,
395 /**************************************************************************//**
396 * Encode the Syslog in JSON according to AT&T's schema for the event type.
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)
404 char * event_source_type;
408 /***************************************************************************/
409 /* Check preconditions. */
410 /***************************************************************************/
411 assert(event != NULL);
412 assert(event->header.event_domain == EVEL_DOMAIN_SYSLOG);
414 event_source_type = evel_source_type(event->event_source_type);
416 evel_json_encode_header(jbuf, &event->header);
417 evel_json_open_named_object(jbuf, "syslogFields");
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);
427 jbuf, "syslogFieldsVersion", event->major_version, event->minor_version);
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);
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);
471 /**************************************************************************//**
474 * Free off the Syslog supplied. Will free all the contained allocated memory.
476 * @note It does not free the Syslog itself, since that may be part of a
478 *****************************************************************************/
479 void evel_free_syslog(EVENT_SYSLOG * event)
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);
491 /***************************************************************************/
492 /* Free all internal strings then the header itself. */
493 /***************************************************************************/
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);