static int connectivity_netlink_thread_error = 0;
static pthread_t connectivity_netlink_thread_id;
static int connectivity_dequeue_thread_loop = 0;
-static int connectivity_dequeue_thread_error = 0;
static pthread_t connectivity_dequeue_thread_id;
static pthread_mutex_t connectivity_threads_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t connectivity_data_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t connectivity_cond = PTHREAD_COND_INITIALIZER;
-// static struct mnl_socket *sock;
static int nl_sock = -1;
static int event_id = 0;
static int unsent_statuses = 0;
}
static int connectivity_link_state(struct nlmsghdr *msg) {
- struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg);
- struct nlattr *attr;
-
pthread_mutex_lock(&connectivity_data_lock);
- interface_list_t *il = NULL;
+ struct nlattr *attr;
+ struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg);
/* Scan attribute list for device name. */
mnl_attr_for_each(attr, msg, sizeof(*ifi)) {
break;
}
+ interface_list_t *il = NULL;
+
for (il = interface_list_head; il != NULL; il = il->next)
if (strcmp(dev, il->interface) == 0)
break;
- uint32_t prev_status;
-
if (il == NULL) {
// We haven't encountered this interface yet, so add it to the linked list
il = add_interface(dev, LINK_STATE_UNKNOWN, LINK_STATE_UNKNOWN);
}
}
+ uint32_t prev_status;
+
prev_status = il->status;
il->status =
((ifi->ifi_flags & IFF_RUNNING) ? LINK_STATE_UP : LINK_STATE_DOWN);
return ret;
while (42) {
- char buf[4096];
-
pthread_mutex_lock(&connectivity_threads_lock);
if (connectivity_netlink_thread_loop <= 0) {
pthread_mutex_unlock(&connectivity_threads_lock);
+ char buf[4096];
int status = recv(nl, buf, sizeof(buf), recv_flags);
if (status < 0) {
unsent_statuses = 0;
}
-static int read_interface_status() /* {{{ */
+static void read_interface_status() /* {{{ */
{
pthread_mutex_lock(&connectivity_data_lock);
send_interface_status();
pthread_mutex_unlock(&connectivity_data_lock);
-
- return 0;
} /* }}} int *read_interface_status */
static void *connectivity_netlink_thread(void *arg) /* {{{ */
pthread_mutex_unlock(&connectivity_threads_lock);
- return ((void *)0);
+ return (void *)0;
} /* }}} void *connectivity_netlink_thread */
static void *connectivity_dequeue_thread(void *arg) /* {{{ */
while (connectivity_dequeue_thread_loop > 0) {
pthread_mutex_unlock(&connectivity_threads_lock);
- int status = read_interface_status();
+ read_interface_status();
pthread_mutex_lock(&connectivity_threads_lock);
-
- if (status < 0) {
- connectivity_dequeue_thread_error = 1;
- break;
- }
} /* while (connectivity_dequeue_thread_loop > 0) */
pthread_mutex_unlock(&connectivity_threads_lock);
static int start_netlink_thread(void) /* {{{ */
{
- int status;
-
pthread_mutex_lock(&connectivity_threads_lock);
if (connectivity_netlink_thread_loop != 0) {
pthread_mutex_unlock(&connectivity_threads_lock);
- return (0);
+ return 0;
}
connectivity_netlink_thread_loop = 1;
connectivity_netlink_thread_error = 0;
+ int status;
+
if (nl_sock == -1) {
status = nl_connect();
} else
nl_sock = -1;
- return (-1);
+ return -1;
}
pthread_mutex_unlock(&connectivity_threads_lock);
if (connectivity_dequeue_thread_loop != 0) {
pthread_mutex_unlock(&connectivity_threads_lock);
- return (0);
+ return 0;
}
connectivity_dequeue_thread_loop = 1;
- connectivity_dequeue_thread_error = 0;
int status =
plugin_thread_create(&connectivity_dequeue_thread_id,
connectivity_dequeue_thread_loop = 0;
ERROR("connectivity plugin: Starting dequeue thread failed.");
pthread_mutex_unlock(&connectivity_threads_lock);
- return (-1);
+ return -1;
}
pthread_mutex_unlock(&connectivity_threads_lock);
static int stop_netlink_thread(int shutdown) /* {{{ */
{
- int socket_status, thread_stratus;
+ int socket_status;
if (nl_sock != -1) {
socket_status = close(nl_sock);
pthread_mutex_unlock(&connectivity_threads_lock);
// Let threads waiting on access to the interface list know to move
- // on such that they'll see the threads termination status
+ // on such that they'll see the thread's termination status
pthread_cond_broadcast(&connectivity_cond);
+ int thread_status;
+
if (shutdown == 1) {
// Since the thread is blocking, calling pthread_join
// doesn't actually succeed in stopping it. It will stick around
DEBUG("connectivity plugin: Canceling netlink thread for process shutdown");
- thread_stratus = pthread_cancel(connectivity_netlink_thread_id);
+ thread_status = pthread_cancel(connectivity_netlink_thread_id);
- if (thread_stratus != 0 && thread_stratus != ESRCH) {
+ if (thread_status != 0 && thread_status != ESRCH) {
ERROR("connectivity plugin: Unable to cancel netlink thread: %d",
- thread_stratus);
- thread_stratus = -1;
+ thread_status);
+ thread_status = -1;
} else
- thread_stratus = 0;
+ thread_status = 0;
} else {
- thread_stratus =
+ thread_status =
pthread_join(connectivity_netlink_thread_id, /* return = */ NULL);
- if (thread_stratus != 0 && thread_stratus != ESRCH) {
+ if (thread_status != 0 && thread_status != ESRCH) {
ERROR("connectivity plugin: Stopping netlink thread failed: %d",
- thread_stratus);
- thread_stratus = -1;
+ thread_status);
+ thread_status = -1;
} else
- thread_stratus = 0;
+ thread_status = 0;
}
pthread_mutex_lock(&connectivity_threads_lock);
if (socket_status != 0)
return socket_status;
else
- return thread_stratus;
+ return thread_status;
}
static int stop_dequeue_thread(int shutdown) /* {{{ */
{
- int status;
-
pthread_mutex_lock(&connectivity_threads_lock);
if (connectivity_dequeue_thread_loop == 0) {
pthread_mutex_unlock(&connectivity_threads_lock);
- return (-1);
+ return -1;
}
// Set thread termination status
// on such that they'll see the threads termination status
pthread_cond_broadcast(&connectivity_cond);
+ int status;
+
if (shutdown == 1) {
// Calling pthread_cancel here in
// the case of a shutdown just assures that the thread is
pthread_mutex_lock(&connectivity_threads_lock);
memset(&connectivity_dequeue_thread_id, 0,
sizeof(connectivity_dequeue_thread_id));
- connectivity_dequeue_thread_error = 0;
pthread_mutex_unlock(&connectivity_threads_lock);
DEBUG("connectivity plugin: Finished requesting stop of dequeue thread");
- return (status);
+ return status;
} /* }}} int stop_dequeue_thread */
static int stop_threads(int shutdown) /* {{{ */
"be monitored");
}
- return (start_threads());
+ return start_threads();
} /* }}} int connectivity_init */
static int connectivity_config(const char *key, const char *value) /* {{{ */
invert = 0;
ignorelist_set_invert(ignorelist, invert);
} else {
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
} /* }}} int connectivity_config */
static void
connectivity_dispatch_notification(const char *interface, const char *type,
gauge_t value, gauge_t old_value,
long long unsigned int timestamp) {
- char *buf = NULL;
- notification_t n = {
- NOTIF_FAILURE, cdtime(), "", "", "connectivity", "", "", "", NULL};
- if (value == LINK_STATE_UP)
- n.severity = NOTIF_OKAY;
+ notification_t n = {(value == LINK_STATE_UP ? NOTIF_OKAY : NOTIF_FAILURE),
+ cdtime(),
+ "",
+ "",
+ "connectivity",
+ "",
+ "",
+ "",
+ NULL};
sstrncpy(n.host, hostname_g, sizeof(n.host));
sstrncpy(n.plugin_instance, interface, sizeof(n.plugin_instance));
sstrncpy(n.type, "gauge", sizeof(n.type));
sstrncpy(n.type_instance, "interface_status", sizeof(n.type_instance));
+ char *buf = NULL;
+
gen_message_payload(value, old_value, interface, timestamp, &buf);
notification_meta_t *m = calloc(1, sizeof(*m));
start_netlink_thread();
- return (-1);
+ return -1;
} /* if (connectivity_netlink_thread_error != 0) */
- if (connectivity_dequeue_thread_error != 0) {
-
- pthread_mutex_unlock(&connectivity_threads_lock);
-
- ERROR("connectivity plugin: The dequeue thread had a problem. Restarting "
- "it.");
-
- stop_dequeue_thread(0);
-
- start_dequeue_thread();
-
- return (-1);
- } /* if (connectivity_dequeue_thread_error != 0) */
-
pthread_mutex_unlock(&connectivity_threads_lock);
- return (0);
+ return 0;
} /* }}} int connectivity_read */
static int connectivity_shutdown(void) /* {{{ */
{
DEBUG("connectivity plugin: Shutting down thread.");
- if (stop_threads(1) < 0)
- return (-1);
+
+ int status = stop_threads(1);
interface_list_t *il = interface_list_head;
while (il != NULL) {
ignorelist_free(ignorelist);
- return (0);
+ return status;
} /* }}} int connectivity_shutdown */
void module_register(void) {