2 * collectd - src/sysevent.c
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
24 * Andrew Bays <abays at redhat.com>
31 #include "utils_complain.h"
32 #include "utils_ignorelist.h"
36 #include <netinet/in.h>
41 #include <sys/socket.h>
44 #include <yajl/yajl_common.h>
45 #include <yajl/yajl_gen.h>
47 #if HAVE_YAJL_YAJL_VERSION_H
48 #include <yajl/yajl_version.h>
50 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
51 #include <yajl/yajl_tree.h>
52 #define HAVE_YAJL_V2 1
55 #define SYSEVENT_DOMAIN_FIELD "domain"
56 #define SYSEVENT_DOMAIN_VALUE "syslog"
57 #define SYSEVENT_EVENT_ID_FIELD "eventId"
58 #define SYSEVENT_EVENT_NAME_FIELD "eventName"
59 #define SYSEVENT_EVENT_NAME_VALUE "syslog message"
60 #define SYSEVENT_LAST_EPOCH_MICROSEC_FIELD "lastEpochMicrosec"
61 #define SYSEVENT_PRIORITY_FIELD "priority"
62 #define SYSEVENT_PRIORITY_VALUE_HIGH "high"
63 #define SYSEVENT_PRIORITY_VALUE_LOW "low"
64 #define SYSEVENT_PRIORITY_VALUE_MEDIUM "medium"
65 #define SYSEVENT_PRIORITY_VALUE_NORMAL "normal"
66 #define SYSEVENT_PRIORITY_VALUE_UNKNOWN "unknown"
67 #define SYSEVENT_REPORTING_ENTITY_NAME_FIELD "reportingEntityName"
68 #define SYSEVENT_REPORTING_ENTITY_NAME_VALUE "collectd sysevent plugin"
69 #define SYSEVENT_SEQUENCE_FIELD "sequence"
70 #define SYSEVENT_SEQUENCE_VALUE "0"
71 #define SYSEVENT_SOURCE_NAME_FIELD "sourceName"
72 #define SYSEVENT_SOURCE_NAME_VALUE "syslog"
73 #define SYSEVENT_START_EPOCH_MICROSEC_FIELD "startEpochMicrosec"
74 #define SYSEVENT_VERSION_FIELD "version"
75 #define SYSEVENT_VERSION_VALUE "1.0"
77 #define SYSEVENT_EVENT_SOURCE_HOST_FIELD "eventSourceHost"
78 #define SYSEVENT_EVENT_SOURCE_TYPE_FIELD "eventSourceType"
79 #define SYSEVENT_EVENT_SOURCE_TYPE_VALUE "host"
80 #define SYSEVENT_SYSLOG_FIELDS_FIELD "syslogFields"
81 #define SYSEVENT_SYSLOG_FIELDS_VERSION_FIELD "syslogFieldsVersion"
82 #define SYSEVENT_SYSLOG_FIELDS_VERSION_VALUE "1.0"
83 #define SYSEVENT_SYSLOG_MSG_FIELD "syslogMsg"
84 #define SYSEVENT_SYSLOG_PROC_FIELD "syslogProc"
85 #define SYSEVENT_SYSLOG_SEV_FIELD "syslogSev"
86 #define SYSEVENT_SYSLOG_TAG_FIELD "syslogTag"
87 #define SYSEVENT_SYSLOG_TAG_VALUE "NILVALUE"
98 long long unsigned int *timestamp;
104 static ignorelist_t *ignorelist = NULL;
106 static int sysevent_thread_loop = 0;
107 static int sysevent_thread_error = 0;
108 static pthread_t sysevent_thread_id;
109 static pthread_mutex_t sysevent_lock = PTHREAD_MUTEX_INITIALIZER;
110 static int sock = -1;
111 static int event_id = 0;
112 static circbuf_t ring;
114 static char *listen_ip;
115 static char *listen_port;
116 static int listen_buffer_size = 4096;
117 static int buffer_length = 10;
119 static int monitor_all_messages = 1;
122 static const char *rsyslog_keys[3] = {"@timestamp", "@source_host", "@message"};
123 static const char *rsyslog_field_keys[5] = {
124 "facility", "severity", "severity-num", "program", "processid"};
131 static int gen_message_payload(const char *msg, char *sev, int sev_num,
132 char *process, char *host,
133 long long unsigned int timestamp, char **buf) {
134 const unsigned char *buf2;
136 char json_str[DATA_MAX_NAME_LEN];
138 #if !defined(HAVE_YAJL_V2)
139 yajl_gen_config conf = {};
146 g = yajl_gen_alloc(NULL);
147 yajl_gen_config(g, yajl_gen_beautify, 0);
150 g = yajl_gen_alloc(&conf, NULL);
155 // *** BEGIN common event header ***
157 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
161 if (yajl_gen_string(g, (u_char *)SYSEVENT_DOMAIN_FIELD,
162 strlen(SYSEVENT_DOMAIN_FIELD)) != yajl_gen_status_ok)
165 if (yajl_gen_string(g, (u_char *)SYSEVENT_DOMAIN_VALUE,
166 strlen(SYSEVENT_DOMAIN_VALUE)) != yajl_gen_status_ok)
170 if (yajl_gen_string(g, (u_char *)SYSEVENT_EVENT_ID_FIELD,
171 strlen(SYSEVENT_EVENT_ID_FIELD)) != yajl_gen_status_ok)
174 event_id = event_id + 1;
175 int event_id_len = sizeof(char) * sizeof(int) * 4 + 1;
176 memset(json_str, '\0', DATA_MAX_NAME_LEN);
177 snprintf(json_str, event_id_len, "%d", event_id);
179 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
184 if (yajl_gen_string(g, (u_char *)SYSEVENT_EVENT_NAME_FIELD,
185 strlen(SYSEVENT_EVENT_NAME_FIELD)) != yajl_gen_status_ok)
188 int event_name_len = 0;
189 event_name_len = event_name_len + strlen(host); // host name
192 22; // "host", "rsyslog", "message", 3 spaces and null-terminator
193 memset(json_str, '\0', DATA_MAX_NAME_LEN);
194 snprintf(json_str, event_name_len, "host %s rsyslog message", host);
196 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
197 yajl_gen_status_ok) {
202 if (yajl_gen_string(g, (u_char *)SYSEVENT_LAST_EPOCH_MICROSEC_FIELD,
203 strlen(SYSEVENT_LAST_EPOCH_MICROSEC_FIELD)) !=
207 int last_epoch_microsec_len =
208 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
209 memset(json_str, '\0', DATA_MAX_NAME_LEN);
210 snprintf(json_str, last_epoch_microsec_len, "%llu",
211 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
213 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
218 if (yajl_gen_string(g, (u_char *)SYSEVENT_PRIORITY_FIELD,
219 strlen(SYSEVENT_PRIORITY_FIELD)) != yajl_gen_status_ok)
224 if (yajl_gen_string(g, (u_char *)SYSEVENT_PRIORITY_VALUE_MEDIUM,
225 strlen(SYSEVENT_PRIORITY_VALUE_MEDIUM)) !=
230 if (yajl_gen_string(g, (u_char *)SYSEVENT_PRIORITY_VALUE_NORMAL,
231 strlen(SYSEVENT_PRIORITY_VALUE_NORMAL)) !=
237 if (yajl_gen_string(g, (u_char *)SYSEVENT_PRIORITY_VALUE_LOW,
238 strlen(SYSEVENT_PRIORITY_VALUE_LOW)) !=
243 if (yajl_gen_string(g, (u_char *)SYSEVENT_PRIORITY_VALUE_UNKNOWN,
244 strlen(SYSEVENT_PRIORITY_VALUE_UNKNOWN)) !=
250 // reportingEntityName
251 if (yajl_gen_string(g, (u_char *)SYSEVENT_REPORTING_ENTITY_NAME_FIELD,
252 strlen(SYSEVENT_REPORTING_ENTITY_NAME_FIELD)) !=
256 if (yajl_gen_string(g, (u_char *)SYSEVENT_REPORTING_ENTITY_NAME_VALUE,
257 strlen(SYSEVENT_REPORTING_ENTITY_NAME_VALUE)) !=
262 if (yajl_gen_string(g, (u_char *)SYSEVENT_SEQUENCE_FIELD,
263 strlen(SYSEVENT_SEQUENCE_FIELD)) != yajl_gen_status_ok)
266 if (yajl_gen_number(g, SYSEVENT_SEQUENCE_VALUE,
267 strlen(SYSEVENT_SEQUENCE_VALUE)) != yajl_gen_status_ok)
271 if (yajl_gen_string(g, (u_char *)SYSEVENT_SOURCE_NAME_FIELD,
272 strlen(SYSEVENT_SOURCE_NAME_FIELD)) != yajl_gen_status_ok)
275 if (yajl_gen_string(g, (u_char *)SYSEVENT_SOURCE_NAME_VALUE,
276 strlen(SYSEVENT_SOURCE_NAME_VALUE)) != yajl_gen_status_ok)
279 // startEpochMicrosec
280 if (yajl_gen_string(g, (u_char *)SYSEVENT_START_EPOCH_MICROSEC_FIELD,
281 strlen(SYSEVENT_START_EPOCH_MICROSEC_FIELD)) !=
285 int start_epoch_microsec_len =
286 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
287 memset(json_str, '\0', DATA_MAX_NAME_LEN);
288 snprintf(json_str, start_epoch_microsec_len, "%llu",
289 (long long unsigned int)timestamp);
291 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
296 if (yajl_gen_string(g, (u_char *)SYSEVENT_VERSION_FIELD,
297 strlen(SYSEVENT_VERSION_FIELD)) != yajl_gen_status_ok)
300 if (yajl_gen_number(g, SYSEVENT_VERSION_VALUE,
301 strlen(SYSEVENT_VERSION_VALUE)) != yajl_gen_status_ok)
304 // *** END common event header ***
306 // *** BEGIN syslog fields ***
308 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_FIELDS_FIELD,
309 strlen(SYSEVENT_SYSLOG_FIELDS_FIELD)) !=
313 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
317 if (yajl_gen_string(g, (u_char *)SYSEVENT_EVENT_SOURCE_HOST_FIELD,
318 strlen(SYSEVENT_EVENT_SOURCE_HOST_FIELD)) !=
322 if (yajl_gen_string(g, (u_char *)host, strlen(host)) != yajl_gen_status_ok)
326 if (yajl_gen_string(g, (u_char *)SYSEVENT_EVENT_SOURCE_TYPE_FIELD,
327 strlen(SYSEVENT_EVENT_SOURCE_TYPE_FIELD)) !=
331 if (yajl_gen_string(g, (u_char *)SYSEVENT_EVENT_SOURCE_TYPE_VALUE,
332 strlen(SYSEVENT_EVENT_SOURCE_TYPE_VALUE)) !=
336 // syslogFieldsVersion
337 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_FIELDS_VERSION_FIELD,
338 strlen(SYSEVENT_SYSLOG_FIELDS_VERSION_FIELD)) !=
342 if (yajl_gen_number(g, SYSEVENT_SYSLOG_FIELDS_VERSION_VALUE,
343 strlen(SYSEVENT_SYSLOG_FIELDS_VERSION_VALUE)) !=
349 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_MSG_FIELD,
350 strlen(SYSEVENT_SYSLOG_MSG_FIELD)) !=
354 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
359 if (process != NULL) {
360 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_PROC_FIELD,
361 strlen(SYSEVENT_SYSLOG_PROC_FIELD)) !=
365 if (yajl_gen_string(g, (u_char *)process, strlen(process)) !=
372 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_SEV_FIELD,
373 strlen(SYSEVENT_SYSLOG_SEV_FIELD)) !=
377 if (yajl_gen_string(g, (u_char *)sev, strlen(sev)) != yajl_gen_status_ok)
382 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_TAG_FIELD,
383 strlen(SYSEVENT_SYSLOG_TAG_FIELD)) != yajl_gen_status_ok)
386 if (yajl_gen_string(g, (u_char *)SYSEVENT_SYSLOG_TAG_VALUE,
387 strlen(SYSEVENT_SYSLOG_TAG_VALUE)) != yajl_gen_status_ok)
390 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
393 // *** END syslog fields ***
395 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
398 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
401 *buf = malloc(strlen((char *)buf2) + 1);
403 sstrncpy(*buf, (char *)buf2, strlen((char *)buf2) + 1);
411 ERROR("sysevent plugin: gen_message_payload failed to generate JSON");
415 static void *sysevent_thread(void *arg) /* {{{ */
417 pthread_mutex_lock(&sysevent_lock);
419 while (sysevent_thread_loop > 0) {
422 pthread_mutex_unlock(&sysevent_lock);
427 char buffer[listen_buffer_size];
428 struct sockaddr_storage src_addr;
429 socklen_t src_addr_len = sizeof(src_addr);
431 memset(buffer, '\0', listen_buffer_size);
433 ssize_t count = recvfrom(sock, buffer, sizeof(buffer), 0,
434 (struct sockaddr *)&src_addr, &src_addr_len);
437 ERROR("sysevent plugin: failed to receive data: %s", strerror(errno));
439 } else if (count >= sizeof(buffer)) {
440 WARNING("sysevent plugin: datagram too large for buffer: truncated");
443 // 2. Push to buffer if there is room, otherwise raise warning
445 pthread_mutex_lock(&sysevent_lock);
447 int next = ring.head + 1;
448 if (next >= ring.maxLen)
451 if (next == ring.tail) {
452 WARNING("sysevent plugin: ring buffer full");
454 DEBUG("sysevent plugin: writing %s", buffer);
456 strncpy(ring.buffer[ring.head], buffer, sizeof(buffer));
457 ring.timestamp[ring.head] =
458 (long long unsigned int)CDTIME_T_TO_US(cdtime());
462 pthread_mutex_unlock(&sysevent_lock);
467 pthread_mutex_lock(&sysevent_lock);
470 WARNING("sysevent plugin: problem with thread status: %d", status);
471 sysevent_thread_error = 1;
475 if (sysevent_thread_loop <= 0)
477 } /* while (sysevent_thread_loop > 0) */
479 pthread_mutex_unlock(&sysevent_lock);
481 // pthread_exit instead of return?
483 } /* }}} void *sysevent_thread */
485 static int start_thread(void) /* {{{ */
489 pthread_mutex_lock(&sysevent_lock);
491 if (sysevent_thread_loop != 0) {
492 pthread_mutex_unlock(&sysevent_lock);
496 sysevent_thread_loop = 1;
497 sysevent_thread_error = 0;
499 DEBUG("sysevent plugin: starting thread");
501 status = plugin_thread_create(&sysevent_thread_id, /* attr = */ NULL,
503 /* arg = */ (void *)0, "sysevent");
505 sysevent_thread_loop = 0;
506 ERROR("sysevent plugin: starting thread failed.");
507 pthread_mutex_unlock(&sysevent_lock);
511 pthread_mutex_unlock(&sysevent_lock);
513 } /* }}} int start_thread */
515 static int stop_thread(int shutdown) /* {{{ */
519 pthread_mutex_lock(&sysevent_lock);
521 if (sysevent_thread_loop == 0) {
522 pthread_mutex_unlock(&sysevent_lock);
526 sysevent_thread_loop = 0;
527 pthread_mutex_unlock(&sysevent_lock);
530 // Since the thread is blocking, calling pthread_join
531 // doesn't actually succeed in stopping it. It will stick around
532 // until a message is received on the socket (at which
533 // it will realize that "sysevent_thread_loop" is 0 and will
534 // break out of the read loop and be allowed to die). This is
535 // fine when the process isn't supposed to be exiting, but in
536 // the case of a process shutdown, we don't want to have an
537 // idle thread hanging around. Calling pthread_cancel here in
538 // the case of a shutdown is just assures that the thread is
539 // gone and that the process has been fully terminated.
541 DEBUG("sysevent plugin: Canceling thread for process shutdown");
543 status = pthread_cancel(sysevent_thread_id);
546 ERROR("sysevent plugin: Unable to cancel thread: %d (%s)", status,
551 status = pthread_join(sysevent_thread_id, /* return = */ NULL);
553 ERROR("sysevent plugin: Stopping thread failed.");
558 pthread_mutex_lock(&sysevent_lock);
559 memset(&sysevent_thread_id, 0, sizeof(sysevent_thread_id));
560 sysevent_thread_error = 0;
561 pthread_mutex_unlock(&sysevent_lock);
563 DEBUG("sysevent plugin: Finished requesting stop of thread");
566 } /* }}} int stop_thread */
568 static int sysevent_init(void) /* {{{ */
572 ring.maxLen = buffer_length;
573 ring.buffer = (char **)malloc(buffer_length * sizeof(char *));
575 for (int i = 0; i < buffer_length; i++) {
576 ring.buffer[i] = malloc(listen_buffer_size);
579 ring.timestamp = (long long unsigned int *)malloc(
580 buffer_length * sizeof(long long unsigned int));
583 const char *hostname = listen_ip;
584 const char *portname = listen_port;
585 struct addrinfo hints;
586 memset(&hints, 0, sizeof(hints));
587 hints.ai_family = AF_UNSPEC;
588 hints.ai_socktype = SOCK_DGRAM;
589 hints.ai_protocol = 0;
590 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
591 struct addrinfo *res = 0;
593 int err = getaddrinfo(hostname, portname, &hints, &res);
596 ERROR("sysevent plugin: failed to resolve local socket address (err=%d)",
602 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
604 ERROR("sysevent plugin: failed to open socket: %s", strerror(errno));
609 if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
610 ERROR("sysevent plugin: failed to bind socket: %s", strerror(errno));
618 DEBUG("sysevent plugin: socket created and bound");
620 return (start_thread());
621 } /* }}} int sysevent_init */
623 static int sysevent_config_add_listen(const oconfig_item_t *ci) /* {{{ */
625 if (ci->values_num != 2 || ci->values[0].type != OCONFIG_TYPE_STRING ||
626 ci->values[1].type != OCONFIG_TYPE_STRING) {
627 ERROR("sysevent plugin: The `%s' config option needs "
628 "two string arguments (ip and port).",
633 listen_ip = strdup(ci->values[0].value.string);
634 listen_port = strdup(ci->values[1].value.string);
639 static int sysevent_config_add_buffer_size(const oconfig_item_t *ci) /* {{{ */
643 if (cf_util_get_int(ci, &tmp) != 0)
645 else if ((tmp >= 1024) && (tmp <= 65535))
646 listen_buffer_size = tmp;
649 "sysevent plugin: The `BufferSize' must be between 1024 and 65535.");
656 static int sysevent_config_add_buffer_length(const oconfig_item_t *ci) /* {{{ */
660 if (cf_util_get_int(ci, &tmp) != 0)
662 else if ((tmp >= 3) && (tmp <= 4096))
665 WARNING("sysevent plugin: The `Bufferlength' must be between 3 and 4096.");
672 static int sysevent_config_add_regex_filter(const oconfig_item_t *ci) /* {{{ */
674 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
675 ERROR("sysevent plugin: The `%s' config option needs "
676 "one string argument, a regular expression.",
682 if (ignorelist == NULL)
683 ignorelist = ignorelist_create(/* invert = */ 1);
685 int status = ignorelist_add(ignorelist, ci->values[0].value.string);
688 ERROR("sysevent plugin: invalid regular expression: %s",
689 ci->values[0].value.string);
693 monitor_all_messages = 0;
695 WARNING("sysevent plugin: The plugin has been compiled without support "
696 "for the \"RegexFilter\" option.");
702 static int sysevent_config(oconfig_item_t *ci) /* {{{ */
704 for (int i = 0; i < ci->children_num; i++) {
705 oconfig_item_t *child = ci->children + i;
707 if (strcasecmp("Listen", child->key) == 0)
708 sysevent_config_add_listen(child);
709 else if (strcasecmp("BufferSize", child->key) == 0)
710 sysevent_config_add_buffer_size(child);
711 else if (strcasecmp("BufferLength", child->key) == 0)
712 sysevent_config_add_buffer_length(child);
713 else if (strcasecmp("RegexFilter", child->key) == 0)
714 sysevent_config_add_regex_filter(child);
716 WARNING("sysevent plugin: Option `%s' is not allowed here.", child->key);
721 } /* }}} int sysevent_config */
723 static void sysevent_dispatch_notification(const char *message,
727 long long unsigned int timestamp) {
729 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "sysevent",
734 // If we have a parsed-JSON node to work with, use that
736 char process[listen_buffer_size];
737 char severity[listen_buffer_size];
738 char sev_num_str[listen_buffer_size];
739 char msg[listen_buffer_size];
740 char hostname_str[listen_buffer_size];
744 const char *msg_path[] = {rsyslog_keys[2], (const char *)0};
745 yajl_val msg_v = yajl_tree_get(*node, msg_path, yajl_t_string);
748 memset(msg, '\0', listen_buffer_size);
749 snprintf(msg, listen_buffer_size, "%s%c", YAJL_GET_STRING(msg_v), '\0');
753 const char *severity_path[] = {"@fields", rsyslog_field_keys[1],
755 yajl_val severity_v = yajl_tree_get(*node, severity_path, yajl_t_string);
757 if (severity_v != NULL) {
758 memset(severity, '\0', listen_buffer_size);
759 snprintf(severity, listen_buffer_size, "%s%c",
760 YAJL_GET_STRING(severity_v), '\0');
764 const char *sev_num_str_path[] = {"@fields", rsyslog_field_keys[2],
766 yajl_val sev_num_str_v =
767 yajl_tree_get(*node, sev_num_str_path, yajl_t_string);
769 if (sev_num_str_v != NULL) {
770 memset(sev_num_str, '\0', listen_buffer_size);
771 snprintf(sev_num_str, listen_buffer_size, "%s%c",
772 YAJL_GET_STRING(sev_num_str_v), '\0');
774 sev_num = atoi(sev_num_str);
777 n.severity = NOTIF_FAILURE;
781 const char *process_path[] = {"@fields", rsyslog_field_keys[3],
783 yajl_val process_v = yajl_tree_get(*node, process_path, yajl_t_string);
785 if (process_v != NULL) {
786 memset(process, '\0', listen_buffer_size);
787 snprintf(process, listen_buffer_size, "%s%c", YAJL_GET_STRING(process_v),
792 const char *hostname_path[] = {rsyslog_keys[1], (const char *)0};
793 yajl_val hostname_v = yajl_tree_get(*node, hostname_path, yajl_t_string);
795 if (hostname_v != NULL) {
796 memset(hostname_str, '\0', listen_buffer_size);
797 snprintf(hostname_str, listen_buffer_size, "%s%c",
798 YAJL_GET_STRING(hostname_v), '\0');
802 (msg_v != NULL ? msg : NULL), (severity_v != NULL ? severity : NULL),
803 (sev_num_str_v != NULL ? sev_num : -1),
804 (process_v != NULL ? process : NULL),
805 (hostname_v != NULL ? hostname_str : hostname_g), timestamp, &buf);
807 // Data was not sent in JSON format, so just treat the whole log entry
808 // as the message (and we'll be unable to acquire certain data, so the
810 // generated below will be less informative)
812 gen_message_payload(message, NULL, -1, NULL, hostname_g, timestamp, &buf);
815 gen_message_payload(message, NULL, -1, NULL, hostname_g, timestamp, &buf);
818 sstrncpy(n.host, hostname_g, sizeof(n.host));
819 sstrncpy(n.type, "gauge", sizeof(n.type));
821 notification_meta_t *m = calloc(1, sizeof(*m));
826 ERROR("sysevent plugin: unable to allocate metadata: %s",
827 sstrerror(errno, errbuf, sizeof(errbuf)));
831 sstrncpy(m->name, "ves", sizeof(m->name));
832 m->nm_value.nm_string = sstrdup(buf);
833 m->type = NM_TYPE_STRING;
836 DEBUG("sysevent plugin: notification message: %s",
837 n.meta->nm_value.nm_string);
839 DEBUG("sysevent plugin: dispatching message");
841 plugin_dispatch_notification(&n);
842 plugin_notification_meta_free(n.meta);
844 // malloc'd in gen_message_payload
849 static int sysevent_read(void) /* {{{ */
851 if (sysevent_thread_error != 0) {
852 ERROR("sysevent plugin: The sysevent thread had a problem (%d). Restarting "
854 sysevent_thread_error);
861 } /* if (sysevent_thread_error != 0) */
863 pthread_mutex_lock(&sysevent_lock);
865 while (ring.head != ring.tail) {
866 long long unsigned int timestamp;
868 char *match_str = NULL;
869 int next = ring.tail + 1;
871 if (next >= ring.maxLen)
874 DEBUG("sysevent plugin: reading from ring buffer: %s",
875 ring.buffer[ring.tail]);
877 timestamp = ring.timestamp[ring.tail];
880 // Try to parse JSON, and if it fails, fall back to plain string
881 yajl_val node = NULL;
884 node = yajl_tree_parse((const char *)ring.buffer[ring.tail], errbuf,
890 // If we have any regex filters, we need to see if the message portion of
891 // the data matches any of them (otherwise we're not interested)
892 if (monitor_all_messages == 0) {
893 char json_val[listen_buffer_size];
894 const char *path[] = {"@message", (const char *)0};
895 yajl_val v = yajl_tree_get(node, path, yajl_t_string);
897 memset(json_val, '\0', listen_buffer_size);
899 snprintf(json_val, listen_buffer_size, "%s%c", YAJL_GET_STRING(v),
902 match_str = (char *)&json_val;
905 // non-JSON rsyslog data
907 // If we have any regex filters, we need to see if the message data
908 // matches any of them (otherwise we're not interested)
909 if (monitor_all_messages == 0)
910 match_str = ring.buffer[ring.tail];
913 // If we have any regex filters, we need to see if the message data
914 // matches any of them (otherwise we're not interested)
915 if (monitor_all_messages == 0)
916 match_str = ring.buffer[ring.tail];
919 // If we care about matching, do that comparison here
920 if (match_str != NULL) {
923 if (ignorelist_match(ignorelist, match_str) != 0)
926 DEBUG("sysevent plugin: regex filter match");
930 if (is_match == 1 && node != NULL) {
931 sysevent_dispatch_notification(NULL, &node, timestamp);
932 yajl_tree_free(node);
933 } else if (is_match == 1)
934 sysevent_dispatch_notification(ring.buffer[ring.tail], NULL, timestamp);
937 sysevent_dispatch_notification(ring.buffer[ring.tail], timestamp);
943 pthread_mutex_unlock(&sysevent_lock);
946 } /* }}} int sysevent_read */
948 static int sysevent_shutdown(void) /* {{{ */
952 DEBUG("sysevent plugin: Shutting down thread.");
953 if (stop_thread(1) < 0)
957 status = close(sock);
959 ERROR("sysevent plugin: failed to close socket %d: %d (%s)", sock, status,
969 for (int i = 0; i < buffer_length; i++) {
970 free(ring.buffer[i]);
974 free(ring.timestamp);
977 } /* }}} int sysevent_shutdown */
979 void module_register(void) {
980 plugin_register_complex_config("sysevent", sysevent_config);
981 plugin_register_init("sysevent", sysevent_init);
982 plugin_register_read("sysevent", sysevent_read);
983 plugin_register_shutdown("sysevent", sysevent_shutdown);
984 } /* void module_register */