2 * collectd - src/connectivity.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>
25 * Aneesh Puttur <aputtur at redhat.com>
32 #include "utils_complain.h"
33 #include "utils_ignorelist.h"
35 #include <asm/types.h>
38 #include <netinet/in.h>
42 #include <sys/socket.h>
45 #include <libmnl/libmnl.h>
46 #include <linux/netlink.h>
47 #include <linux/rtnetlink.h>
49 #include <yajl/yajl_common.h>
50 #include <yajl/yajl_gen.h>
51 #if HAVE_YAJL_YAJL_VERSION_H
52 #include <yajl/yajl_version.h>
54 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
55 #define HAVE_YAJL_V2 1
58 #define MYPROTO NETLINK_ROUTE
60 #define LINK_STATE_DOWN 0
61 #define LINK_STATE_UP 1
62 #define LINK_STATE_UNKNOWN 2
64 #define CONNECTIVITY_DOMAIN_FIELD "domain"
65 #define CONNECTIVITY_DOMAIN_VALUE "stateChange"
66 #define CONNECTIVITY_EVENT_ID_FIELD "eventId"
67 #define CONNECTIVITY_EVENT_NAME_FIELD "eventName"
68 #define CONNECTIVITY_EVENT_NAME_DOWN_VALUE "down"
69 #define CONNECTIVITY_EVENT_NAME_UP_VALUE "up"
70 #define CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD "lastEpochMicrosec"
71 #define CONNECTIVITY_PRIORITY_FIELD "priority"
72 #define CONNECTIVITY_PRIORITY_VALUE "high"
73 #define CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD "reportingEntityName"
74 #define CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE "collectd connectivity plugin"
75 #define CONNECTIVITY_SEQUENCE_FIELD "sequence"
76 #define CONNECTIVITY_SEQUENCE_VALUE "0"
77 #define CONNECTIVITY_SOURCE_NAME_FIELD "sourceName"
78 #define CONNECTIVITY_START_EPOCH_MICROSEC_FIELD "startEpochMicrosec"
79 #define CONNECTIVITY_VERSION_FIELD "version"
80 #define CONNECTIVITY_VERSION_VALUE "1.0"
82 #define CONNECTIVITY_NEW_STATE_FIELD "newState"
83 #define CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE "outOfService"
84 #define CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE "inService"
85 #define CONNECTIVITY_OLD_STATE_FIELD "oldState"
86 #define CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE "outOfService"
87 #define CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE "inService"
88 #define CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD "stateChangeFields"
89 #define CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD \
90 "stateChangeFieldsVersion"
91 #define CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE "1.0"
92 #define CONNECTIVITY_STATE_INTERFACE_FIELD "stateInterface"
98 struct interface_list_s {
102 uint32_t prev_status;
104 long long unsigned int timestamp;
106 struct interface_list_s *next;
108 typedef struct interface_list_s interface_list_t;
114 static ignorelist_t *ignorelist = NULL;
116 static interface_list_t *interface_list_head = NULL;
117 static int monitor_all_interfaces = 1;
119 static int connectivity_netlink_thread_loop = 0;
120 static int connectivity_netlink_thread_error = 0;
121 static pthread_t connectivity_netlink_thread_id;
122 static int connectivity_dequeue_thread_loop = 0;
123 static pthread_t connectivity_dequeue_thread_id;
124 static pthread_mutex_t connectivity_threads_lock = PTHREAD_MUTEX_INITIALIZER;
125 static pthread_mutex_t connectivity_data_lock = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_cond_t connectivity_cond = PTHREAD_COND_INITIALIZER;
127 static int nl_sock = -1;
128 static int event_id = 0;
129 static int unsent_statuses = 0;
131 static const char *config_keys[] = {"Interface", "IgnoreSelected"};
132 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
139 connectivity_dispatch_notification(const char *interface, const char *type,
140 gauge_t value, gauge_t old_value,
141 long long unsigned int timestamp);
147 static int gen_message_payload(int state, int old_state, const char *interface,
148 long long unsigned int timestamp, char **buf) {
149 const unsigned char *buf2;
151 char json_str[DATA_MAX_NAME_LEN];
153 #if !defined(HAVE_YAJL_V2)
154 yajl_gen_config conf = {};
161 g = yajl_gen_alloc(NULL);
162 yajl_gen_config(g, yajl_gen_beautify, 0);
165 g = yajl_gen_alloc(&conf, NULL);
170 // *** BEGIN common event header ***
172 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
176 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_FIELD,
177 strlen(CONNECTIVITY_DOMAIN_FIELD)) != yajl_gen_status_ok)
180 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_DOMAIN_VALUE,
181 strlen(CONNECTIVITY_DOMAIN_VALUE)) != yajl_gen_status_ok)
185 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_ID_FIELD,
186 strlen(CONNECTIVITY_EVENT_ID_FIELD)) !=
190 event_id = event_id + 1;
191 int event_id_len = sizeof(char) * sizeof(int) * 4 + 1;
192 memset(json_str, '\0', DATA_MAX_NAME_LEN);
193 snprintf(json_str, event_id_len, "%d", event_id);
195 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
200 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_EVENT_NAME_FIELD,
201 strlen(CONNECTIVITY_EVENT_NAME_FIELD)) !=
205 int event_name_len = 0;
206 event_name_len = event_name_len + strlen(interface); // interface name
207 event_name_len = event_name_len + (state == 0 ? 4 : 2); // "down" or "up"
209 event_name_len + 12; // "interface", 2 spaces and null-terminator
210 memset(json_str, '\0', DATA_MAX_NAME_LEN);
211 snprintf(json_str, event_name_len, "interface %s %s", interface,
212 (state == 0 ? CONNECTIVITY_EVENT_NAME_DOWN_VALUE
213 : CONNECTIVITY_EVENT_NAME_UP_VALUE));
215 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
216 yajl_gen_status_ok) {
221 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD,
222 strlen(CONNECTIVITY_LAST_EPOCH_MICROSEC_FIELD)) !=
226 int last_epoch_microsec_len =
227 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
228 memset(json_str, '\0', DATA_MAX_NAME_LEN);
229 snprintf(json_str, last_epoch_microsec_len, "%llu",
230 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
232 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
237 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_FIELD,
238 strlen(CONNECTIVITY_PRIORITY_FIELD)) !=
242 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_PRIORITY_VALUE,
243 strlen(CONNECTIVITY_PRIORITY_VALUE)) !=
247 // reportingEntityName
248 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD,
249 strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_FIELD)) !=
253 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE,
254 strlen(CONNECTIVITY_REPORTING_ENTITY_NAME_VALUE)) !=
259 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SEQUENCE_FIELD,
260 strlen(CONNECTIVITY_SEQUENCE_FIELD)) !=
264 if (yajl_gen_number(g, CONNECTIVITY_SEQUENCE_VALUE,
265 strlen(CONNECTIVITY_SEQUENCE_VALUE)) !=
270 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_SOURCE_NAME_FIELD,
271 strlen(CONNECTIVITY_SOURCE_NAME_FIELD)) !=
275 if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
279 // startEpochMicrosec
280 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_START_EPOCH_MICROSEC_FIELD,
281 strlen(CONNECTIVITY_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 *)CONNECTIVITY_VERSION_FIELD,
297 strlen(CONNECTIVITY_VERSION_FIELD)) != yajl_gen_status_ok)
300 if (yajl_gen_number(g, CONNECTIVITY_VERSION_VALUE,
301 strlen(CONNECTIVITY_VERSION_VALUE)) != yajl_gen_status_ok)
304 // *** END common event header ***
306 // *** BEGIN state change fields ***
308 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD,
309 strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_FIELD)) !=
313 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
317 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_NEW_STATE_FIELD,
318 strlen(CONNECTIVITY_NEW_STATE_FIELD)) !=
323 (state == 0 ? strlen(CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE)
324 : strlen(CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE));
327 g, (u_char *)(state == 0 ? CONNECTIVITY_NEW_STATE_FIELD_DOWN_VALUE
328 : CONNECTIVITY_NEW_STATE_FIELD_UP_VALUE),
329 new_state_len) != yajl_gen_status_ok)
333 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_OLD_STATE_FIELD,
334 strlen(CONNECTIVITY_OLD_STATE_FIELD)) !=
339 (old_state == 0 ? strlen(CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE)
340 : strlen(CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE));
343 g, (u_char *)(old_state == 0 ? CONNECTIVITY_OLD_STATE_FIELD_DOWN_VALUE
344 : CONNECTIVITY_OLD_STATE_FIELD_UP_VALUE),
345 old_state_len) != yajl_gen_status_ok)
348 // stateChangeFieldsVersion
349 if (yajl_gen_string(g,
350 (u_char *)CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD,
351 strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_FIELD)) !=
355 if (yajl_gen_number(g, CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE,
356 strlen(CONNECTIVITY_STATE_CHANGE_FIELDS_VERSION_VALUE)) !=
361 if (yajl_gen_string(g, (u_char *)CONNECTIVITY_STATE_INTERFACE_FIELD,
362 strlen(CONNECTIVITY_STATE_INTERFACE_FIELD)) !=
366 if (yajl_gen_string(g, (u_char *)interface, strlen(interface)) !=
370 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
373 // *** END state change fields ***
375 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
378 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
381 *buf = strdup((char *)buf2);
384 ERROR("connectivity plugin: strdup failed during gen_message_payload: %s",
395 ERROR("connectivity plugin: gen_message_payload failed to generate JSON");
399 static interface_list_t *add_interface(const char *interface, int status,
401 interface_list_t *il = calloc(1, sizeof(*il));
404 ERROR("connectivity plugin: calloc failed during add_interface: %s",
409 char *interface2 = strdup(interface);
410 if (interface2 == NULL) {
412 ERROR("connectivity plugin: strdup failed during add_interface: %s",
417 il->interface = interface2;
419 il->prev_status = prev_status;
420 il->timestamp = (long long unsigned int)CDTIME_T_TO_US(cdtime());
422 il->next = interface_list_head;
423 interface_list_head = il;
425 DEBUG("connectivity plugin: added interface %s", interface2);
430 static int connectivity_link_state(struct nlmsghdr *msg) {
431 pthread_mutex_lock(&connectivity_data_lock);
434 struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg);
436 /* Scan attribute list for device name. */
437 mnl_attr_for_each(attr, msg, sizeof(*ifi)) {
438 if (mnl_attr_get_type(attr) != IFLA_IFNAME)
441 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
442 ERROR("connectivity plugin: connectivity_link_state: IFLA_IFNAME "
445 pthread_mutex_unlock(&connectivity_data_lock);
449 const char *dev = mnl_attr_get_str(attr);
451 // Check the list of interfaces we should monitor, if we've chosen
452 // a subset. If we don't care about this one, abort.
453 if (ignorelist_match(ignorelist, dev) != 0) {
454 DEBUG("connectivity plugin: Ignoring link state change for unmonitored "
460 interface_list_t *il = NULL;
462 for (il = interface_list_head; il != NULL; il = il->next)
463 if (strcmp(dev, il->interface) == 0)
467 // We haven't encountered this interface yet, so add it to the linked list
468 il = add_interface(dev, LINK_STATE_UNKNOWN, LINK_STATE_UNKNOWN);
471 ERROR("connectivity plugin: unable to add interface %s during "
472 "connectivity_link_state",
478 uint32_t prev_status;
480 prev_status = il->status;
482 ((ifi->ifi_flags & IFF_RUNNING) ? LINK_STATE_UP : LINK_STATE_DOWN);
483 il->timestamp = (long long unsigned int)CDTIME_T_TO_US(cdtime());
485 // If the new status is different than the previous status,
486 // store the previous status and set sent to zero, and set the
487 // global flag to indicate there are statuses to dispatch
488 if (il->status != prev_status) {
489 il->prev_status = prev_status;
494 DEBUG("connectivity plugin (%llu): Interface %s status is now %s",
495 il->timestamp, dev, ((ifi->ifi_flags & IFF_RUNNING) ? "UP" : "DOWN"));
497 // no need to loop again, we found the interface name attr
498 // (otherwise the first if-statement in the loop would
499 // have moved us on with 'continue')
503 pthread_mutex_unlock(&connectivity_data_lock);
508 static int msg_handler(struct nlmsghdr *msg) {
509 switch (msg->nlmsg_type) {
515 // Not of interest in current version
518 connectivity_link_state(msg);
521 ERROR("connectivity plugin: msg_handler: Unknown netlink nlmsg_type %d",
528 static int read_event(int nl, int (*msg_handler)(struct nlmsghdr *)) {
530 int recv_flags = MSG_DONTWAIT;
536 pthread_mutex_lock(&connectivity_threads_lock);
538 if (connectivity_netlink_thread_loop <= 0) {
539 pthread_mutex_unlock(&connectivity_threads_lock);
543 pthread_mutex_unlock(&connectivity_threads_lock);
546 int status = recv(nl, buf, sizeof(buf), recv_flags);
550 // If there were no more messages to drain from the socket,
551 // then signal the dequeue thread and allow it to dispatch
552 // any saved interface status changes. Then continue, but
553 // block and wait for new messages
554 if (errno == EWOULDBLOCK || errno == EAGAIN) {
555 pthread_cond_signal(&connectivity_cond);
563 // Interrupt, so just return
567 /* Anything else is an error */
568 ERROR("connectivity plugin: read_event: Error recv: %d", status);
572 // Message received successfully, so we'll stop blocking on the
573 // receive call for now (until we get a "would block" error, which
574 // will be handled above)
575 recv_flags = MSG_DONTWAIT;
578 DEBUG("connectivity plugin: read_event: EOF");
581 /* We need to handle more than one message per 'recvmsg' */
582 for (struct nlmsghdr *h = (struct nlmsghdr *)buf;
583 NLMSG_OK(h, (unsigned int)status); h = NLMSG_NEXT(h, status)) {
585 if (h->nlmsg_type == NLMSG_DONE)
588 /* Message is some kind of error */
589 if (h->nlmsg_type == NLMSG_ERROR) {
590 ERROR("connectivity plugin: read_event: Message is an error");
594 /* Call message handler */
596 ret = (*msg_handler)(h);
598 ERROR("connectivity plugin: read_event: Message handler error %d",
603 ERROR("connectivity plugin: read_event: Error NULL message handler");
612 // NOTE: Caller MUST hold connectivity_data_lock when calling this function
613 static void send_interface_status() {
614 for (interface_list_t *il = interface_list_head; il != NULL;
615 il = il->next) /* {{{ */
618 uint32_t prev_status;
622 prev_status = il->prev_status;
625 if (status != prev_status && sent == 0) {
626 connectivity_dispatch_notification(il->interface, "gauge", status,
627 prev_status, il->timestamp);
630 } /* }}} for (il = interface_list_head; il != NULL; il = il->next) */
635 static void read_interface_status() /* {{{ */
637 pthread_mutex_lock(&connectivity_data_lock);
639 if (!unsent_statuses)
640 pthread_cond_wait(&connectivity_cond, &connectivity_data_lock);
642 send_interface_status();
644 pthread_mutex_unlock(&connectivity_data_lock);
645 } /* }}} int *read_interface_status */
647 static void *connectivity_netlink_thread(void *arg) /* {{{ */
649 pthread_mutex_lock(&connectivity_threads_lock);
651 while (connectivity_netlink_thread_loop > 0) {
652 pthread_mutex_unlock(&connectivity_threads_lock);
654 int status = read_event(nl_sock, msg_handler);
656 pthread_mutex_lock(&connectivity_threads_lock);
659 connectivity_netlink_thread_error = 1;
662 } /* while (connectivity_netlink_thread_loop > 0) */
664 pthread_mutex_unlock(&connectivity_threads_lock);
667 } /* }}} void *connectivity_netlink_thread */
669 static void *connectivity_dequeue_thread(void *arg) /* {{{ */
671 pthread_mutex_lock(&connectivity_threads_lock);
673 while (connectivity_dequeue_thread_loop > 0) {
674 pthread_mutex_unlock(&connectivity_threads_lock);
676 read_interface_status();
678 pthread_mutex_lock(&connectivity_threads_lock);
679 } /* while (connectivity_dequeue_thread_loop > 0) */
681 pthread_mutex_unlock(&connectivity_threads_lock);
684 } /* }}} void *connectivity_dequeue_thread */
686 static int nl_connect() {
687 struct sockaddr_nl sa_nl = {
688 .nl_family = AF_NETLINK, .nl_groups = RTMGRP_LINK, .nl_pid = getpid(),
691 nl_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
693 ERROR("connectivity plugin: socket open failed: %s", STRERRNO);
697 int rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
699 ERROR("connectivity plugin: socket bind failed: %s", STRERRNO);
707 static int start_netlink_thread(void) /* {{{ */
709 pthread_mutex_lock(&connectivity_threads_lock);
711 if (connectivity_netlink_thread_loop != 0) {
712 pthread_mutex_unlock(&connectivity_threads_lock);
716 connectivity_netlink_thread_loop = 1;
717 connectivity_netlink_thread_error = 0;
722 status = nl_connect();
725 pthread_mutex_unlock(&connectivity_threads_lock);
730 status = plugin_thread_create(&connectivity_netlink_thread_id,
731 /* attr = */ NULL, connectivity_netlink_thread,
732 /* arg = */ (void *)0, "connectivity");
734 connectivity_netlink_thread_loop = 0;
735 ERROR("connectivity plugin: Starting thread failed.");
736 pthread_mutex_unlock(&connectivity_threads_lock);
738 int status2 = close(nl_sock);
741 ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
749 pthread_mutex_unlock(&connectivity_threads_lock);
754 static int start_dequeue_thread(void) /* {{{ */
756 pthread_mutex_lock(&connectivity_threads_lock);
758 if (connectivity_dequeue_thread_loop != 0) {
759 pthread_mutex_unlock(&connectivity_threads_lock);
763 connectivity_dequeue_thread_loop = 1;
766 plugin_thread_create(&connectivity_dequeue_thread_id,
767 /* attr = */ NULL, connectivity_dequeue_thread,
768 /* arg = */ (void *)0, "connectivity");
770 connectivity_dequeue_thread_loop = 0;
771 ERROR("connectivity plugin: Starting dequeue thread failed.");
772 pthread_mutex_unlock(&connectivity_threads_lock);
776 pthread_mutex_unlock(&connectivity_threads_lock);
779 } /* }}} int start_dequeue_thread */
781 static int start_threads(void) /* {{{ */
783 int status = start_netlink_thread();
784 int status2 = start_dequeue_thread();
790 } /* }}} int start_threads */
792 static int stop_netlink_thread(int shutdown) /* {{{ */
797 socket_status = close(nl_sock);
798 if (socket_status != 0) {
799 ERROR("connectivity plugin: failed to close socket %d: %d (%s)", nl_sock,
800 socket_status, STRERRNO);
806 pthread_mutex_lock(&connectivity_threads_lock);
808 if (connectivity_netlink_thread_loop == 0) {
809 pthread_mutex_unlock(&connectivity_threads_lock);
810 // Thread has already been terminated, nothing more to attempt
811 return socket_status;
814 // Set thread termination status
815 connectivity_netlink_thread_loop = 0;
816 pthread_mutex_unlock(&connectivity_threads_lock);
818 // Let threads waiting on access to the interface list know to move
819 // on such that they'll see the thread's termination status
820 pthread_cond_broadcast(&connectivity_cond);
825 // Since the thread is blocking, calling pthread_join
826 // doesn't actually succeed in stopping it. It will stick around
827 // until a NETLINK message is received on the socket (at which
828 // it will realize that "connectivity_netlink_thread_loop" is 0 and will
829 // break out of the read loop and be allowed to die). This is
830 // fine when the process isn't supposed to be exiting, but in
831 // the case of a process shutdown, we don't want to have an
832 // idle thread hanging around. Calling pthread_cancel here in
833 // the case of a shutdown is just assures that the thread is
834 // gone and that the process has been fully terminated.
836 DEBUG("connectivity plugin: Canceling netlink thread for process shutdown");
838 thread_status = pthread_cancel(connectivity_netlink_thread_id);
840 if (thread_status != 0 && thread_status != ESRCH) {
841 ERROR("connectivity plugin: Unable to cancel netlink thread: %d",
848 pthread_join(connectivity_netlink_thread_id, /* return = */ NULL);
849 if (thread_status != 0 && thread_status != ESRCH) {
850 ERROR("connectivity plugin: Stopping netlink thread failed: %d",
857 pthread_mutex_lock(&connectivity_threads_lock);
858 memset(&connectivity_netlink_thread_id, 0,
859 sizeof(connectivity_netlink_thread_id));
860 connectivity_netlink_thread_error = 0;
861 pthread_mutex_unlock(&connectivity_threads_lock);
863 DEBUG("connectivity plugin: Finished requesting stop of netlink thread");
865 if (socket_status != 0)
866 return socket_status;
868 return thread_status;
871 static int stop_dequeue_thread(int shutdown) /* {{{ */
873 pthread_mutex_lock(&connectivity_threads_lock);
875 if (connectivity_dequeue_thread_loop == 0) {
876 pthread_mutex_unlock(&connectivity_threads_lock);
880 // Set thread termination status
881 connectivity_dequeue_thread_loop = 0;
882 pthread_mutex_unlock(&connectivity_threads_lock);
884 // Let threads waiting on access to the interface list know to move
885 // on such that they'll see the threads termination status
886 pthread_cond_broadcast(&connectivity_cond);
891 // Calling pthread_cancel here in
892 // the case of a shutdown just assures that the thread is
893 // gone and that the process has been fully terminated.
895 DEBUG("connectivity plugin: Canceling dequeue thread for process shutdown");
897 status = pthread_cancel(connectivity_dequeue_thread_id);
899 if (status != 0 && status != ESRCH) {
900 ERROR("connectivity plugin: Unable to cancel dequeue thread: %d", status);
905 status = pthread_join(connectivity_dequeue_thread_id, /* return = */ NULL);
906 if (status != 0 && status != ESRCH) {
907 ERROR("connectivity plugin: Stopping dequeue thread failed.");
913 pthread_mutex_lock(&connectivity_threads_lock);
914 memset(&connectivity_dequeue_thread_id, 0,
915 sizeof(connectivity_dequeue_thread_id));
916 pthread_mutex_unlock(&connectivity_threads_lock);
918 DEBUG("connectivity plugin: Finished requesting stop of dequeue thread");
921 } /* }}} int stop_dequeue_thread */
923 static int stop_threads(int shutdown) /* {{{ */
925 int status = stop_netlink_thread(shutdown);
926 int status2 = stop_dequeue_thread(shutdown);
932 } /* }}} int stop_threads */
934 static int connectivity_init(void) /* {{{ */
936 if (monitor_all_interfaces) {
937 NOTICE("connectivity plugin: No interfaces have been selected, so all will "
941 return start_threads();
942 } /* }}} int connectivity_init */
944 static int connectivity_config(const char *key, const char *value) /* {{{ */
946 if (ignorelist == NULL) {
947 ignorelist = ignorelist_create(/* invert = */ 1);
950 if (strcasecmp(key, "Interface") == 0) {
951 ignorelist_add(ignorelist, value);
952 monitor_all_interfaces = 0;
953 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
957 ignorelist_set_invert(ignorelist, invert);
963 } /* }}} int connectivity_config */
966 connectivity_dispatch_notification(const char *interface, const char *type,
967 gauge_t value, gauge_t old_value,
968 long long unsigned int timestamp) {
970 notification_t n = {(value == LINK_STATE_UP ? NOTIF_OKAY : NOTIF_FAILURE),
980 sstrncpy(n.host, hostname_g, sizeof(n.host));
981 sstrncpy(n.plugin_instance, interface, sizeof(n.plugin_instance));
982 sstrncpy(n.type, "gauge", sizeof(n.type));
983 sstrncpy(n.type_instance, "interface_status", sizeof(n.type_instance));
987 gen_message_payload(value, old_value, interface, timestamp, &buf);
989 notification_meta_t *m = calloc(1, sizeof(*m));
993 ERROR("connectivity plugin: unable to allocate metadata: %s", STRERRNO);
997 sstrncpy(m->name, "ves", sizeof(m->name));
998 m->nm_value.nm_string = sstrdup(buf);
999 m->type = NM_TYPE_STRING;
1002 DEBUG("connectivity plugin: notification message: %s",
1003 n.meta->nm_value.nm_string);
1005 DEBUG("connectivity plugin: dispatching state %d for interface %s",
1006 (int)value, interface);
1008 plugin_dispatch_notification(&n);
1009 plugin_notification_meta_free(n.meta);
1011 // strdup'd in gen_message_payload
1016 static int connectivity_read(void) /* {{{ */
1018 pthread_mutex_lock(&connectivity_threads_lock);
1020 if (connectivity_netlink_thread_error != 0) {
1022 pthread_mutex_unlock(&connectivity_threads_lock);
1024 ERROR("connectivity plugin: The netlink thread had a problem. Restarting "
1027 stop_netlink_thread(0);
1029 for (interface_list_t *il = interface_list_head; il != NULL;
1031 il->status = LINK_STATE_UNKNOWN;
1032 il->prev_status = LINK_STATE_UNKNOWN;
1036 start_netlink_thread();
1039 } /* if (connectivity_netlink_thread_error != 0) */
1041 pthread_mutex_unlock(&connectivity_threads_lock);
1044 } /* }}} int connectivity_read */
1046 static int connectivity_shutdown(void) /* {{{ */
1048 DEBUG("connectivity plugin: Shutting down thread.");
1050 int status = stop_threads(1);
1052 interface_list_t *il = interface_list_head;
1053 while (il != NULL) {
1054 interface_list_t *il_next;
1058 sfree(il->interface);
1064 ignorelist_free(ignorelist);
1067 } /* }}} int connectivity_shutdown */
1069 void module_register(void) {
1070 plugin_register_config("connectivity", connectivity_config, config_keys,
1072 plugin_register_init("connectivity", connectivity_init);
1073 plugin_register_read("connectivity", connectivity_read);
1074 plugin_register_shutdown("connectivity", connectivity_shutdown);
1075 } /* void module_register */