1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
17 /**************************************************************************//**
19 * Implementation of EVEL functions relating to the Syslog.
21 ****************************************************************************/
27 #include "evel_throttle.h"
29 /**************************************************************************//**
30 * Create a new Syslog event.
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
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)
51 EVENT_SYSLOG * syslog = NULL;
54 /***************************************************************************/
55 /* Check preconditions. */
56 /***************************************************************************/
57 assert(event_source_type < EVEL_MAX_SOURCE_TYPES);
58 assert(syslog_msg != NULL);
59 assert(syslog_tag != NULL);
61 /***************************************************************************/
62 /* Allocate the Syslog. */
63 /***************************************************************************/
64 syslog = malloc(sizeof(EVENT_SYSLOG));
67 log_error_state("Out of memory");
70 memset(syslog, 0, sizeof(EVENT_SYSLOG));
71 EVEL_DEBUG("New Syslog is at %lp", syslog);
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);
99 /**************************************************************************//**
100 * Set the Event Type property of the Syslog.
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.
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
110 *****************************************************************************/
111 void evel_syslog_type_set(EVENT_SYSLOG * syslog,
112 const char * const type)
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);
126 /**************************************************************************//**
127 * Add an additional value name/value pair to the Syslog.
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
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
137 * @param value ASCIIZ string with the attribute's value. The caller
138 * does not need to preserve the value once the function
140 *****************************************************************************/
141 void evel_syslog_addl_filter_set(EVENT_SYSLOG * syslog,
146 /***************************************************************************/
147 /* Check preconditions. */
148 /***************************************************************************/
149 assert(syslog != NULL);
150 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
151 assert(filter != NULL);
153 evel_set_option_string(&syslog->additional_filters,
155 "Syslog filter string");
160 /**************************************************************************//**
161 * Set the Event Source Host property of the Syslog.
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.
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
171 *****************************************************************************/
172 void evel_syslog_event_source_host_set(EVENT_SYSLOG * syslog,
173 const char * const host)
177 /***************************************************************************/
178 /* Check preconditions. */
179 /***************************************************************************/
180 assert(syslog != NULL);
181 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
182 assert(host != NULL);
184 evel_set_option_string(&syslog->event_source_host,
186 "Event Source Host");
190 /**************************************************************************//**
191 * Set the Facility property of the Syslog.
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.
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
201 *****************************************************************************/
202 void evel_syslog_facility_set(EVENT_SYSLOG * syslog,
203 EVEL_SYSLOG_FACILITIES facility)
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);
214 evel_set_option_int(&syslog->syslog_facility,
220 /**************************************************************************//**
221 * Set the Process property of the Syslog.
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.
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)
235 /***************************************************************************/
236 /* Check preconditions. */
237 /***************************************************************************/
238 assert(syslog != NULL);
239 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
240 assert(proc != NULL);
242 evel_set_option_string(&syslog->syslog_proc, proc, "Process");
246 /**************************************************************************//**
247 * Set the Process ID property of the Syslog.
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.
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)
261 /***************************************************************************/
262 /* Check preconditions. */
263 /***************************************************************************/
264 assert(syslog != NULL);
265 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
268 evel_set_option_int(&syslog->syslog_proc_id,
274 /**************************************************************************//**
275 * Set the Version property of the Syslog.
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.
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)
289 /***************************************************************************/
290 /* Check preconditions. */
291 /***************************************************************************/
292 assert(syslog != NULL);
293 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
294 assert(version >= 0);
296 evel_set_option_int(&syslog->syslog_ver,
302 /**************************************************************************//**
303 * Set the Structured Data property of the Syslog.
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.
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
313 *****************************************************************************/
314 void evel_syslog_s_data_set(EVENT_SYSLOG * syslog, const char * const s_data)
318 /***************************************************************************/
319 /* Check preconditions. */
320 /***************************************************************************/
321 assert(syslog != NULL);
322 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
323 assert(s_data != NULL);
325 evel_set_option_string(&syslog->syslog_s_data,
331 /**************************************************************************//**
332 * Set the Structured SDID property of the Syslog.
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.
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
342 *****************************************************************************/
343 void evel_syslog_sdid_set(EVENT_SYSLOG * syslog, const char * const sdid)
347 /***************************************************************************/
348 /* Check preconditions. */
349 /***************************************************************************/
350 assert(syslog != NULL);
351 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
352 assert(sdid != NULL);
354 evel_set_option_string(&syslog->syslog_sdid,
360 /**************************************************************************//**
361 * Set the Structured Severity property of the Syslog.
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.
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
371 *****************************************************************************/
372 void evel_syslog_severity_set(EVENT_SYSLOG * syslog, const char * const severty)
376 /***************************************************************************/
377 /* Check preconditions. */
378 /***************************************************************************/
379 assert(syslog != NULL);
380 assert(syslog->header.event_domain == EVEL_DOMAIN_SYSLOG);
381 assert(severty != NULL);
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") )
387 evel_set_option_string(&syslog->syslog_severity,
394 /**************************************************************************//**
395 * Encode the Syslog in JSON according to AT&T's schema for the event type.
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)
403 char * event_source_type;
407 /***************************************************************************/
408 /* Check preconditions. */
409 /***************************************************************************/
410 assert(event != NULL);
411 assert(event->header.event_domain == EVEL_DOMAIN_SYSLOG);
413 event_source_type = evel_source_type(event->event_source_type);
415 evel_json_encode_header(jbuf, &event->header);
416 evel_json_open_named_object(jbuf, "syslogFields");
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);
426 jbuf, "syslogFieldsVersion", event->major_version, event->minor_version);
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);
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);
470 /**************************************************************************//**
473 * Free off the Syslog supplied. Will free all the contained allocated memory.
475 * @note It does not free the Syslog itself, since that may be part of a
477 *****************************************************************************/
478 void evel_free_syslog(EVENT_SYSLOG * event)
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);
490 /***************************************************************************/
491 /* Free all internal strings then the header itself. */
492 /***************************************************************************/
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);