2 * collectd - src/procevent.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"
38 #include <sys/socket.h>
42 #include <linux/cn_proc.h>
43 #include <linux/connector.h>
44 #include <linux/netlink.h>
45 #include <linux/rtnetlink.h>
51 #include <yajl/yajl_common.h>
52 #include <yajl/yajl_gen.h>
53 #if HAVE_YAJL_YAJL_VERSION_H
54 #include <yajl/yajl_version.h>
56 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
57 #define HAVE_YAJL_V2 1
60 #define PROCEVENT_EXITED 0
61 #define PROCEVENT_STARTED 1
62 #define PROCEVENT_FIELDS 4 // pid, status, extra, timestamp
64 #define PROCDIR "/proc"
66 #define PROCEVENT_DOMAIN_FIELD "domain"
67 #define PROCEVENT_DOMAIN_VALUE "fault"
68 #define PROCEVENT_EVENT_ID_FIELD "eventId"
69 #define PROCEVENT_EVENT_NAME_FIELD "eventName"
70 #define PROCEVENT_EVENT_NAME_DOWN_VALUE "down"
71 #define PROCEVENT_EVENT_NAME_UP_VALUE "up"
72 #define PROCEVENT_LAST_EPOCH_MICROSEC_FIELD "lastEpochMicrosec"
73 #define PROCEVENT_PRIORITY_FIELD "priority"
74 #define PROCEVENT_PRIORITY_VALUE "high"
75 #define PROCEVENT_REPORTING_ENTITY_NAME_FIELD "reportingEntityName"
76 #define PROCEVENT_REPORTING_ENTITY_NAME_VALUE "collectd procevent plugin"
77 #define PROCEVENT_SEQUENCE_FIELD "sequence"
78 #define PROCEVENT_SEQUENCE_VALUE "0"
79 #define PROCEVENT_SOURCE_NAME_FIELD "sourceName"
80 #define PROCEVENT_START_EPOCH_MICROSEC_FIELD "startEpochMicrosec"
81 #define PROCEVENT_VERSION_FIELD "version"
82 #define PROCEVENT_VERSION_VALUE "1.0"
84 #define PROCEVENT_ALARM_CONDITION_FIELD "alarmCondition"
85 #define PROCEVENT_ALARM_INTERFACE_A_FIELD "alarmInterfaceA"
86 #define PROCEVENT_EVENT_SEVERITY_FIELD "eventSeverity"
87 #define PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE "CRITICAL"
88 #define PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE "NORMAL"
89 #define PROCEVENT_EVENT_SOURCE_TYPE_FIELD "eventSourceType"
90 #define PROCEVENT_EVENT_SOURCE_TYPE_VALUE "process"
91 #define PROCEVENT_FAULT_FIELDS_FIELD "faultFields"
92 #define PROCEVENT_FAULT_FIELDS_VERSION_FIELD "faultFieldsVersion"
93 #define PROCEVENT_FAULT_FIELDS_VERSION_VALUE "1.0"
94 #define PROCEVENT_SPECIFIC_PROBLEM_FIELD "specificProblem"
95 #define PROCEVENT_SPECIFIC_PROBLEM_DOWN_VALUE "down"
96 #define PROCEVENT_SPECIFIC_PROBLEM_UP_VALUE "up"
97 #define PROCEVENT_VF_STATUS_FIELD "vfStatus"
98 #define PROCEVENT_VF_STATUS_CRITICAL_VALUE "Ready to terminate"
99 #define PROCEVENT_VF_STATUS_NORMAL_VALUE "Active"
109 long long unsigned int **buffer;
112 struct processlist_s {
118 struct processlist_s *next;
120 typedef struct processlist_s processlist_t;
125 static ignorelist_t *ignorelist = NULL;
127 static int procevent_netlink_thread_loop = 0;
128 static int procevent_netlink_thread_error = 0;
129 static pthread_t procevent_netlink_thread_id;
130 static int procevent_dequeue_thread_loop = 0;
131 static int procevent_dequeue_thread_error = 0;
132 static pthread_t procevent_dequeue_thread_id;
133 static pthread_mutex_t procevent_thread_lock = PTHREAD_MUTEX_INITIALIZER;
134 static pthread_mutex_t procevent_data_lock = PTHREAD_MUTEX_INITIALIZER;
135 static pthread_cond_t procevent_cond = PTHREAD_COND_INITIALIZER;
136 static int nl_sock = -1;
137 static int buffer_length;
138 static circbuf_t ring;
139 static processlist_t *processlist_head = NULL;
140 static int event_id = 0;
142 static const char *config_keys[] = {"BufferLength", "Process", "ProcessRegex"};
143 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
149 static void procevent_dispatch_notification(long pid, const char *type,
150 gauge_t value, char *process,
151 long long unsigned int timestamp);
157 static int gen_message_payload(int state, long pid, char *process,
158 long long unsigned int timestamp, char **buf) {
159 const unsigned char *buf2;
161 char json_str[DATA_MAX_NAME_LEN];
163 #if !defined(HAVE_YAJL_V2)
164 yajl_gen_config conf = {};
171 g = yajl_gen_alloc(NULL);
172 yajl_gen_config(g, yajl_gen_beautify, 0);
175 g = yajl_gen_alloc(&conf, NULL);
180 // *** BEGIN common event header ***
182 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
186 if (yajl_gen_string(g, (u_char *)PROCEVENT_DOMAIN_FIELD,
187 strlen(PROCEVENT_DOMAIN_FIELD)) != yajl_gen_status_ok)
190 if (yajl_gen_string(g, (u_char *)PROCEVENT_DOMAIN_VALUE,
191 strlen(PROCEVENT_DOMAIN_VALUE)) != yajl_gen_status_ok)
195 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_ID_FIELD,
196 strlen(PROCEVENT_EVENT_ID_FIELD)) != yajl_gen_status_ok)
199 event_id = event_id + 1;
200 int event_id_len = sizeof(char) * sizeof(int) * 4 + 1;
201 memset(json_str, '\0', DATA_MAX_NAME_LEN);
202 snprintf(json_str, event_id_len, "%d", event_id);
204 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
209 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_NAME_FIELD,
210 strlen(PROCEVENT_EVENT_NAME_FIELD)) != yajl_gen_status_ok)
213 int event_name_len = 0;
214 event_name_len = event_name_len + (sizeof(char) * sizeof(int) * 4); // pid
215 event_name_len = event_name_len + strlen(process); // process name
216 event_name_len = event_name_len + (state == 0 ? 4 : 2); // "down" or "up"
217 event_name_len = event_name_len +
218 13; // "process", 3 spaces, 2 parentheses and null-terminator
219 memset(json_str, '\0', DATA_MAX_NAME_LEN);
220 snprintf(json_str, event_name_len, "process %s (%ld) %s", process, pid,
221 (state == 0 ? PROCEVENT_EVENT_NAME_DOWN_VALUE
222 : PROCEVENT_EVENT_NAME_UP_VALUE));
224 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
225 yajl_gen_status_ok) {
230 if (yajl_gen_string(g, (u_char *)PROCEVENT_LAST_EPOCH_MICROSEC_FIELD,
231 strlen(PROCEVENT_LAST_EPOCH_MICROSEC_FIELD)) !=
235 int last_epoch_microsec_len =
236 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
237 memset(json_str, '\0', DATA_MAX_NAME_LEN);
238 snprintf(json_str, last_epoch_microsec_len, "%llu",
239 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
241 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
246 if (yajl_gen_string(g, (u_char *)PROCEVENT_PRIORITY_FIELD,
247 strlen(PROCEVENT_PRIORITY_FIELD)) != yajl_gen_status_ok)
250 if (yajl_gen_string(g, (u_char *)PROCEVENT_PRIORITY_VALUE,
251 strlen(PROCEVENT_PRIORITY_VALUE)) != yajl_gen_status_ok)
254 // reportingEntityName
255 if (yajl_gen_string(g, (u_char *)PROCEVENT_REPORTING_ENTITY_NAME_FIELD,
256 strlen(PROCEVENT_REPORTING_ENTITY_NAME_FIELD)) !=
260 if (yajl_gen_string(g, (u_char *)PROCEVENT_REPORTING_ENTITY_NAME_VALUE,
261 strlen(PROCEVENT_REPORTING_ENTITY_NAME_VALUE)) !=
266 if (yajl_gen_string(g, (u_char *)PROCEVENT_SEQUENCE_FIELD,
267 strlen(PROCEVENT_SEQUENCE_FIELD)) != yajl_gen_status_ok)
270 if (yajl_gen_number(g, PROCEVENT_SEQUENCE_VALUE,
271 strlen(PROCEVENT_SEQUENCE_VALUE)) != yajl_gen_status_ok)
275 if (yajl_gen_string(g, (u_char *)PROCEVENT_SOURCE_NAME_FIELD,
276 strlen(PROCEVENT_SOURCE_NAME_FIELD)) !=
280 if (yajl_gen_string(g, (u_char *)process, strlen(process)) !=
284 // startEpochMicrosec
285 if (yajl_gen_string(g, (u_char *)PROCEVENT_START_EPOCH_MICROSEC_FIELD,
286 strlen(PROCEVENT_START_EPOCH_MICROSEC_FIELD)) !=
290 int start_epoch_microsec_len =
291 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
292 memset(json_str, '\0', DATA_MAX_NAME_LEN);
293 snprintf(json_str, start_epoch_microsec_len, "%llu",
294 (long long unsigned int)timestamp);
296 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
301 if (yajl_gen_string(g, (u_char *)PROCEVENT_VERSION_FIELD,
302 strlen(PROCEVENT_VERSION_FIELD)) != yajl_gen_status_ok)
305 if (yajl_gen_number(g, PROCEVENT_VERSION_VALUE,
306 strlen(PROCEVENT_VERSION_VALUE)) != yajl_gen_status_ok)
309 // *** END common event header ***
311 // *** BEGIN fault fields ***
313 if (yajl_gen_string(g, (u_char *)PROCEVENT_FAULT_FIELDS_FIELD,
314 strlen(PROCEVENT_FAULT_FIELDS_FIELD)) !=
318 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
322 if (yajl_gen_string(g, (u_char *)PROCEVENT_ALARM_CONDITION_FIELD,
323 strlen(PROCEVENT_ALARM_CONDITION_FIELD)) !=
327 int alarm_condition_len = 0;
328 alarm_condition_len =
329 alarm_condition_len + (sizeof(char) * sizeof(int) * 4); // pid
330 alarm_condition_len = alarm_condition_len + strlen(process); // process name
331 alarm_condition_len =
332 alarm_condition_len + 25; // "process", "state", "change", 4 spaces, 2
333 // parentheses and null-terminator
334 memset(json_str, '\0', DATA_MAX_NAME_LEN);
335 snprintf(json_str, alarm_condition_len, "process %s (%ld) state change",
338 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
339 yajl_gen_status_ok) {
344 if (yajl_gen_string(g, (u_char *)PROCEVENT_ALARM_INTERFACE_A_FIELD,
345 strlen(PROCEVENT_ALARM_INTERFACE_A_FIELD)) !=
349 if (yajl_gen_string(g, (u_char *)process, strlen(process)) !=
354 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SEVERITY_FIELD,
355 strlen(PROCEVENT_EVENT_SEVERITY_FIELD)) !=
360 g, (u_char *)(state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
361 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE),
362 strlen((state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
363 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE))) !=
368 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_FIELD,
369 strlen(PROCEVENT_EVENT_SOURCE_TYPE_FIELD)) !=
373 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_VALUE,
374 strlen(PROCEVENT_EVENT_SOURCE_TYPE_VALUE)) !=
378 // faultFieldsVersion
379 if (yajl_gen_string(g, (u_char *)PROCEVENT_FAULT_FIELDS_VERSION_FIELD,
380 strlen(PROCEVENT_FAULT_FIELDS_VERSION_FIELD)) !=
384 if (yajl_gen_number(g, PROCEVENT_FAULT_FIELDS_VERSION_VALUE,
385 strlen(PROCEVENT_FAULT_FIELDS_VERSION_VALUE)) !=
390 if (yajl_gen_string(g, (u_char *)PROCEVENT_SPECIFIC_PROBLEM_FIELD,
391 strlen(PROCEVENT_SPECIFIC_PROBLEM_FIELD)) !=
395 int specific_problem_len = 0;
396 specific_problem_len =
397 specific_problem_len + (sizeof(char) * sizeof(int) * 4); // pid
398 specific_problem_len = specific_problem_len + strlen(process); // process name
399 specific_problem_len =
400 specific_problem_len + (state == 0 ? 4 : 2); // "down" or "up"
401 specific_problem_len =
402 specific_problem_len +
403 13; // "process", 3 spaces, 2 parentheses and null-terminator
404 memset(json_str, '\0', DATA_MAX_NAME_LEN);
405 snprintf(json_str, specific_problem_len, "process %s (%ld) %s", process, pid,
406 (state == 0 ? PROCEVENT_SPECIFIC_PROBLEM_DOWN_VALUE
407 : PROCEVENT_SPECIFIC_PROBLEM_UP_VALUE));
409 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
410 yajl_gen_status_ok) {
415 if (yajl_gen_string(g, (u_char *)PROCEVENT_VF_STATUS_FIELD,
416 strlen(PROCEVENT_VF_STATUS_FIELD)) != yajl_gen_status_ok)
420 g, (u_char *)(state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
421 : PROCEVENT_VF_STATUS_NORMAL_VALUE),
422 strlen((state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
423 : PROCEVENT_VF_STATUS_NORMAL_VALUE))) !=
427 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
430 // *** END fault fields ***
432 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
435 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
438 *buf = strdup((char *)buf2);
441 ERROR("procevent plugin: strdup failed during gen_message_payload: %s",
452 ERROR("procevent plugin: gen_message_payload failed to generate JSON");
456 // Does /proc/<pid>/comm contain a process name we are interested in?
457 // NOTE: Caller MUST hold procevent_data_lock when calling this function
458 static processlist_t *process_check(long pid) {
461 char buffer[BUFSIZE];
463 int len = snprintf(file, sizeof(file), PROCDIR "/%ld/comm", pid);
465 if ((len < 0) || (len >= BUFSIZE)) {
466 WARNING("procevent process_check: process name too large");
470 if (NULL == (fh = fopen(file, "r"))) {
471 // No /proc/<pid>/comm for this pid, just ignore
472 DEBUG("procevent plugin: no comm file available for pid %ld", pid);
476 int retval = fscanf(fh, "%[^\n]", buffer);
479 WARNING("procevent process_check: unable to read comm file for pid %ld",
485 // Now that we have the process name in the buffer, check if we are
486 // even interested in it
487 if (ignorelist_match(ignorelist, buffer) != 0) {
488 DEBUG("procevent process_check: ignoring process %s (%ld)", buffer, pid);
499 // Go through the processlist linked list and look for the process name
500 // in /proc/<pid>/comm. If found:
501 // 1. If pl->pid is -1, then set pl->pid to <pid> (and return that object)
502 // 2. If pl->pid is not -1, then another <process name> process was already
503 // found. If <pid> == pl->pid, this is an old match, so do nothing.
504 // If the <pid> is different, however, make a new processlist_t and
505 // associate <pid> with it (with the same process name as the existing).
508 processlist_t *match = NULL;
510 for (processlist_t *pl = processlist_head; pl != NULL; pl = pl->next) {
512 int is_match = (strcmp(buffer, pl->process) == 0 ? 1 : 0);
515 DEBUG("procevent plugin: process %ld name match for %s", pid, buffer);
517 if (pl->pid == pid) {
518 // this is a match, and we've already stored the exact pid/name combo
519 DEBUG("procevent plugin: found exact match with name %s, PID %ld for "
521 pl->process, pl->pid, pid);
524 } else if (pl->pid == -1) {
525 // this is a match, and we've found a candidate processlist_t to store
526 // this new pid/name combo
527 DEBUG("procevent plugin: reusing pl object with PID %ld for incoming "
533 } else if (pl->pid != -1) {
534 // this is a match, but another instance of this process has already
535 // claimed this pid/name combo,
537 DEBUG("procevent plugin: found pl object with matching name for "
538 "incoming PID %ld, but object is in use by PID %ld",
547 (match != NULL && match->pid != -1 && match->pid != pid)) {
548 // if there wasn't an existing match, OR
549 // if there was a match but the associated processlist_t object already
550 // contained a pid/name combo,
551 // then make a new one and add it to the linked list
553 DEBUG("procevent plugin: allocating new processlist_t object for PID %ld "
557 processlist_t *pl2 = calloc(1, sizeof(*pl2));
559 ERROR("procevent plugin: calloc failed during process_check: %s",
564 char *process = strdup(buffer);
565 if (process == NULL) {
567 ERROR("procevent plugin: strdup failed during process_check: %s",
572 pl2->process = process;
574 pl2->next = processlist_head;
575 processlist_head = pl2;
583 // Does our map have this PID or name?
584 // NOTE: Caller MUST hold procevent_data_lock when calling this function
585 static processlist_t *process_map_check(long pid, char *process) {
586 for (processlist_t *pl = processlist_head; pl != NULL; pl = pl->next) {
588 int match_process = 0;
596 if (process != NULL) {
597 if (strcmp(pl->process, process) == 0)
601 if (pid > 0 && process == NULL && match_pid == 1)
603 else if (pid < 0 && process != NULL && match_process == 1)
605 else if (pid > 0 && process != NULL && match_pid == 1 && match_process == 1)
616 static int process_map_refresh(void) {
618 DIR *proc = opendir(PROCDIR);
621 ERROR("procevent plugin: fopen (%s): %s", PROCDIR, STRERRNO);
631 struct dirent *dent = readdir(proc);
633 if (errno == 0) /* end of directory */
636 ERROR("procevent plugin: failed to read directory %s: %s", PROCDIR,
642 if (dent->d_name[0] == '.')
645 int len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
646 if ((len < 0) || (len >= BUFSIZE))
649 int status = stat(file, &statbuf);
651 WARNING("procevent plugin: stat (%s) failed: %s", file, STRERRNO);
655 if (!S_ISDIR(statbuf.st_mode))
658 len = snprintf(file, sizeof(file), PROCDIR "/%s/comm", dent->d_name);
659 if ((len < 0) || (len >= BUFSIZE))
664 for (int i = 0; i < strlen(dent->d_name); i++) {
665 if (!isdigit(dent->d_name[i])) {
674 // Check if we need to store this pid/name combo in our processlist_t linked
676 int this_pid = atoi(dent->d_name);
677 pthread_mutex_lock(&procevent_data_lock);
678 processlist_t *pl = process_check(this_pid);
679 pthread_mutex_unlock(&procevent_data_lock);
682 DEBUG("procevent plugin: process map refreshed for PID %d and name %s",
683 this_pid, pl->process);
691 static int nl_connect() {
692 struct sockaddr_nl sa_nl = {
693 .nl_family = AF_NETLINK, .nl_groups = CN_IDX_PROC, .nl_pid = getpid(),
696 nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
698 ERROR("procevent plugin: socket open failed: %d", errno);
702 int rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
704 ERROR("procevent plugin: socket bind failed: %d", errno);
712 static int set_proc_ev_listen(bool enable) {
713 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
714 struct nlmsghdr nl_hdr;
715 struct __attribute__((__packed__)) {
716 struct cn_msg cn_msg;
717 enum proc_cn_mcast_op cn_mcast;
721 memset(&nlcn_msg, 0, sizeof(nlcn_msg));
722 nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg);
723 nlcn_msg.nl_hdr.nlmsg_pid = getpid();
724 nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE;
726 nlcn_msg.cn_msg.id.idx = CN_IDX_PROC;
727 nlcn_msg.cn_msg.id.val = CN_VAL_PROC;
728 nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
730 nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
732 int rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
734 ERROR("procevent plugin: subscribing to netlink process events failed: %d",
742 // Read from netlink socket and write to ring buffer
743 static int read_event() {
744 int recv_flags = MSG_DONTWAIT;
745 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
746 struct nlmsghdr nl_hdr;
747 struct __attribute__((__packed__)) {
748 struct cn_msg cn_msg;
749 struct proc_event proc_ev;
758 int proc_status = -1;
761 pthread_mutex_lock(&procevent_thread_lock);
763 if (procevent_netlink_thread_loop <= 0) {
764 pthread_mutex_unlock(&procevent_thread_lock);
768 pthread_mutex_unlock(&procevent_thread_lock);
770 int status = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), recv_flags);
774 } else if (status < 0) {
775 if (errno == EAGAIN || errno == EWOULDBLOCK) {
776 pthread_mutex_lock(&procevent_data_lock);
778 // There was nothing more to receive for now, so...
779 // If ring head does not equal ring tail, there is data
780 // in the ring buffer for the dequeue thread to read, so
782 if (ring.head != ring.tail)
783 pthread_cond_signal(&procevent_cond);
785 pthread_mutex_unlock(&procevent_data_lock);
787 // Since there was nothing to receive, set recv to block and
791 } else if (errno != EINTR) {
792 ERROR("procevent plugin: socket receive error: %d", errno);
797 // We successfully received a message, so don't block on the next
798 // read in case there are more (and if there aren't, it will be
799 // handled above in the error-checking)
800 recv_flags = MSG_DONTWAIT;
802 switch (nlcn_msg.proc_ev.what) {
803 case PROC_EVENT_NONE:
804 case PROC_EVENT_FORK:
807 // Not of interest in current version
809 case PROC_EVENT_EXEC:
810 proc_status = PROCEVENT_STARTED;
811 proc_id = nlcn_msg.proc_ev.event_data.exec.process_pid;
813 case PROC_EVENT_EXIT:
814 proc_id = nlcn_msg.proc_ev.event_data.exit.process_pid;
815 proc_status = PROCEVENT_EXITED;
816 proc_extra = nlcn_msg.proc_ev.event_data.exit.exit_code;
822 // If we're interested in this process status event, place the event
823 // in the ring buffer for consumption by the main polling thread.
825 if (proc_status != -1) {
826 pthread_mutex_lock(&procevent_data_lock);
828 int next = ring.head + 1;
829 if (next >= ring.maxLen)
832 if (next == ring.tail) {
833 // Buffer is full, signal the dequeue thread to process the buffer
834 // and clean it out, and then sleep
835 WARNING("procevent plugin: ring buffer full");
837 pthread_cond_signal(&procevent_cond);
838 pthread_mutex_unlock(&procevent_data_lock);
843 DEBUG("procevent plugin: Process %d status is now %s at %llu", proc_id,
844 (proc_status == PROCEVENT_EXITED ? "EXITED" : "STARTED"),
845 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
847 if (proc_status == PROCEVENT_EXITED) {
848 ring.buffer[ring.head][0] = proc_id;
849 ring.buffer[ring.head][1] = proc_status;
850 ring.buffer[ring.head][2] = proc_extra;
851 ring.buffer[ring.head][3] =
852 (long long unsigned int)CDTIME_T_TO_US(cdtime());
854 ring.buffer[ring.head][0] = proc_id;
855 ring.buffer[ring.head][1] = proc_status;
856 ring.buffer[ring.head][2] = 0;
857 ring.buffer[ring.head][3] =
858 (long long unsigned int)CDTIME_T_TO_US(cdtime());
864 pthread_mutex_unlock(&procevent_data_lock);
871 // Read from ring buffer and dispatch to write plugins
872 static int read_ring_buffer() {
873 pthread_mutex_lock(&procevent_data_lock);
875 // If there's currently nothing to read from the buffer,
877 if (ring.head == ring.tail)
878 pthread_cond_wait(&procevent_cond, &procevent_data_lock);
880 while (ring.head != ring.tail) {
881 int next = ring.tail + 1;
883 if (next >= ring.maxLen)
886 if (ring.buffer[ring.tail][1] == PROCEVENT_EXITED) {
887 processlist_t *pl = process_map_check(ring.buffer[ring.tail][0], NULL);
890 // This process is of interest to us, so publish its EXITED status
891 procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge",
892 ring.buffer[ring.tail][1], pl->process,
893 ring.buffer[ring.tail][3]);
895 "procevent plugin: PID %ld (%s) EXITED, removing PID from process "
897 pl->pid, pl->process);
899 pl->last_status = -1;
901 } else if (ring.buffer[ring.tail][1] == PROCEVENT_STARTED) {
902 // a new process has started, so check if we should monitor it
903 processlist_t *pl = process_check(ring.buffer[ring.tail][0]);
905 // If we had already seen this process name and pid combo before,
906 // and the last message was a "process started" message, don't send
907 // the notfication again
909 if (pl != NULL && pl->last_status != PROCEVENT_STARTED) {
910 // This process is of interest to us, so publish its STARTED status
911 procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge",
912 ring.buffer[ring.tail][1], pl->process,
913 ring.buffer[ring.tail][3]);
915 pl->last_status = PROCEVENT_STARTED;
917 DEBUG("procevent plugin: PID %ld (%s) STARTED, adding PID to process "
919 pl->pid, pl->process);
926 pthread_mutex_unlock(&procevent_data_lock);
931 // Entry point for thread responsible for listening
932 // to netlink socket and writing data to ring buffer
933 static void *procevent_netlink_thread(void *arg) /* {{{ */
935 pthread_mutex_lock(&procevent_thread_lock);
937 while (procevent_netlink_thread_loop > 0) {
938 pthread_mutex_unlock(&procevent_thread_lock);
940 int status = read_event();
942 pthread_mutex_lock(&procevent_thread_lock);
945 procevent_netlink_thread_error = 1;
948 } /* while (procevent_netlink_thread_loop > 0) */
950 pthread_mutex_unlock(&procevent_thread_lock);
953 } /* }}} void *procevent_netlink_thread */
955 // Entry point for thread responsible for reading from
956 // ring buffer and dispatching notifications
957 static void *procevent_dequeue_thread(void *arg) /* {{{ */
959 pthread_mutex_lock(&procevent_thread_lock);
961 while (procevent_dequeue_thread_loop > 0) {
962 pthread_mutex_unlock(&procevent_thread_lock);
964 int status = read_ring_buffer();
966 pthread_mutex_lock(&procevent_thread_lock);
969 procevent_dequeue_thread_error = 1;
972 } /* while (procevent_dequeue_thread_loop > 0) */
974 pthread_mutex_unlock(&procevent_thread_lock);
977 } /* }}} void *procevent_dequeue_thread */
979 static int start_netlink_thread(void) /* {{{ */
983 pthread_mutex_lock(&procevent_thread_lock);
985 if (procevent_netlink_thread_loop != 0) {
986 pthread_mutex_unlock(&procevent_thread_lock);
991 status = nl_connect();
994 pthread_mutex_unlock(&procevent_thread_lock);
998 status = set_proc_ev_listen(true);
1000 pthread_mutex_unlock(&procevent_thread_lock);
1005 DEBUG("procevent plugin: socket created and bound");
1007 procevent_netlink_thread_loop = 1;
1008 procevent_netlink_thread_error = 0;
1010 status = plugin_thread_create(&procevent_netlink_thread_id, /* attr = */ NULL,
1011 procevent_netlink_thread,
1012 /* arg = */ (void *)0, "procevent");
1014 procevent_netlink_thread_loop = 0;
1015 ERROR("procevent plugin: Starting netlink thread failed.");
1016 pthread_mutex_unlock(&procevent_thread_lock);
1018 int status2 = close(nl_sock);
1021 ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
1029 pthread_mutex_unlock(&procevent_thread_lock);
1032 } /* }}} int start_netlink_thread */
1034 static int start_dequeue_thread(void) /* {{{ */
1038 pthread_mutex_lock(&procevent_thread_lock);
1040 if (procevent_dequeue_thread_loop != 0) {
1041 pthread_mutex_unlock(&procevent_thread_lock);
1045 procevent_dequeue_thread_loop = 1;
1046 procevent_dequeue_thread_error = 0;
1048 status = plugin_thread_create(&procevent_dequeue_thread_id, /* attr = */ NULL,
1049 procevent_dequeue_thread,
1050 /* arg = */ (void *)0, "procevent");
1052 procevent_dequeue_thread_loop = 0;
1053 ERROR("procevent plugin: Starting dequeue thread failed.");
1054 pthread_mutex_unlock(&procevent_thread_lock);
1058 pthread_mutex_unlock(&procevent_thread_lock);
1061 } /* }}} int start_dequeue_thread */
1063 static int start_threads(void) /* {{{ */
1065 int status = start_netlink_thread();
1066 int status2 = start_dequeue_thread();
1072 } /* }}} int start_threads */
1074 static int stop_netlink_thread(int shutdown) /* {{{ */
1076 int socket_status, thread_status;
1078 if (nl_sock != -1) {
1079 socket_status = close(nl_sock);
1080 if (socket_status != 0) {
1081 ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
1082 socket_status, strerror(errno));
1089 pthread_mutex_lock(&procevent_thread_lock);
1091 if (procevent_netlink_thread_loop == 0) {
1092 pthread_mutex_unlock(&procevent_thread_lock);
1096 procevent_netlink_thread_loop = 0;
1097 pthread_mutex_unlock(&procevent_thread_lock);
1099 pthread_cond_broadcast(&procevent_cond);
1101 if (shutdown == 1) {
1102 // Calling pthread_cancel here in
1103 // the case of a shutdown just assures that the thread is
1104 // gone and that the process has been fully terminated.
1106 DEBUG("procevent plugin: Canceling netlink thread for process shutdown");
1108 thread_status = pthread_cancel(procevent_netlink_thread_id);
1110 if (thread_status != 0 && thread_status != ESRCH) {
1111 ERROR("procevent plugin: Unable to cancel netlink thread: %d",
1118 pthread_join(procevent_netlink_thread_id, /* return = */ NULL);
1119 if (thread_status != 0 && thread_status != ESRCH) {
1120 ERROR("procevent plugin: Stopping netlink thread failed.");
1126 pthread_mutex_lock(&procevent_thread_lock);
1127 memset(&procevent_netlink_thread_id, 0, sizeof(procevent_netlink_thread_id));
1128 procevent_netlink_thread_error = 0;
1129 pthread_mutex_unlock(&procevent_thread_lock);
1131 DEBUG("procevent plugin: Finished requesting stop of netlink thread");
1133 if (socket_status != 0)
1134 return socket_status;
1136 return thread_status;
1137 } /* }}} int stop_netlink_thread */
1139 static int stop_dequeue_thread(int shutdown) /* {{{ */
1143 pthread_mutex_lock(&procevent_thread_lock);
1145 if (procevent_dequeue_thread_loop == 0) {
1146 pthread_mutex_unlock(&procevent_thread_lock);
1150 procevent_dequeue_thread_loop = 0;
1151 pthread_mutex_unlock(&procevent_thread_lock);
1153 pthread_cond_broadcast(&procevent_cond);
1155 if (shutdown == 1) {
1156 // Calling pthread_cancel here in
1157 // the case of a shutdown just assures that the thread is
1158 // gone and that the process has been fully terminated.
1160 DEBUG("procevent plugin: Canceling dequeue thread for process shutdown");
1162 status = pthread_cancel(procevent_dequeue_thread_id);
1164 if (status != 0 && status != ESRCH) {
1165 ERROR("procevent plugin: Unable to cancel dequeue thread: %d", status);
1170 status = pthread_join(procevent_dequeue_thread_id, /* return = */ NULL);
1171 if (status != 0 && status != ESRCH) {
1172 ERROR("procevent plugin: Stopping dequeue thread failed.");
1178 pthread_mutex_lock(&procevent_thread_lock);
1179 memset(&procevent_dequeue_thread_id, 0, sizeof(procevent_dequeue_thread_id));
1180 procevent_dequeue_thread_error = 0;
1181 pthread_mutex_unlock(&procevent_thread_lock);
1183 DEBUG("procevent plugin: Finished requesting stop of dequeue thread");
1186 } /* }}} int stop_dequeue_thread */
1188 static int stop_threads(int shutdown) /* {{{ */
1190 int status = stop_netlink_thread(shutdown);
1191 int status2 = stop_dequeue_thread(shutdown);
1197 } /* }}} int stop_threads */
1199 static int procevent_init(void) /* {{{ */
1203 ring.maxLen = buffer_length;
1204 ring.buffer = (long long unsigned int **)calloc(
1205 buffer_length, sizeof(long long unsigned int *));
1207 for (int i = 0; i < buffer_length; i++) {
1208 ring.buffer[i] = (long long unsigned int *)calloc(
1209 PROCEVENT_FIELDS, sizeof(long long unsigned int));
1212 int status = process_map_refresh();
1215 ERROR("procevent plugin: Initial process mapping failed.");
1219 if (ignorelist == NULL) {
1220 NOTICE("procevent plugin: No processes have been configured.");
1224 return (start_threads());
1225 } /* }}} int procevent_init */
1227 static int procevent_config(const char *key, const char *value) /* {{{ */
1229 if (ignorelist == NULL)
1230 ignorelist = ignorelist_create(/* invert = */ 1);
1232 if (strcasecmp(key, "BufferLength") == 0) {
1233 buffer_length = atoi(value);
1234 } else if (strcasecmp(key, "Process") == 0) {
1235 ignorelist_add(ignorelist, value);
1236 } else if (strcasecmp(key, "ProcessRegex") == 0) {
1238 int status = ignorelist_add(ignorelist, value);
1241 ERROR("procevent plugin: invalid regular expression: %s", value);
1245 WARNING("procevent plugin: The plugin has been compiled without support "
1246 "for the \"ProcessRegex\" option.");
1253 } /* }}} int procevent_config */
1255 static void procevent_dispatch_notification(long pid,
1256 const char *type, /* {{{ */
1257 gauge_t value, char *process,
1258 long long unsigned int timestamp) {
1260 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", "procevent", "", "", "",
1264 n.severity = NOTIF_OKAY;
1266 sstrncpy(n.host, hostname_g, sizeof(n.host));
1267 sstrncpy(n.plugin_instance, process, sizeof(n.plugin_instance));
1268 sstrncpy(n.type, "gauge", sizeof(n.type));
1269 sstrncpy(n.type_instance, "process_status", sizeof(n.type_instance));
1271 gen_message_payload(value, pid, process, timestamp, &buf);
1273 notification_meta_t *m = calloc(1, sizeof(*m));
1277 ERROR("procevent plugin: unable to allocate metadata: %s", STRERRNO);
1281 sstrncpy(m->name, "ves", sizeof(m->name));
1282 m->nm_value.nm_string = sstrdup(buf);
1283 m->type = NM_TYPE_STRING;
1286 DEBUG("procevent plugin: notification message: %s",
1287 n.meta->nm_value.nm_string);
1289 DEBUG("procevent plugin: dispatching state %d for PID %ld (%s)", (int)value,
1292 plugin_dispatch_notification(&n);
1293 plugin_notification_meta_free(n.meta);
1295 // strdup'd in gen_message_payload
1300 static int procevent_read(void) /* {{{ */
1302 pthread_mutex_lock(&procevent_thread_lock);
1304 if (procevent_netlink_thread_error != 0) {
1306 pthread_mutex_unlock(&procevent_thread_lock);
1308 ERROR("procevent plugin: The netlink thread had a problem. Restarting it.");
1310 stop_netlink_thread(0);
1312 start_netlink_thread();
1315 } /* if (procevent_netlink_thread_error != 0) */
1317 if (procevent_dequeue_thread_error != 0) {
1319 pthread_mutex_unlock(&procevent_thread_lock);
1321 ERROR("procevent plugin: The dequeue thread had a problem. Restarting it.");
1323 stop_dequeue_thread(0);
1325 start_dequeue_thread();
1328 } /* if (procevent_dequeue_thread_error != 0) */
1330 pthread_mutex_unlock(&procevent_thread_lock);
1333 } /* }}} int procevent_read */
1335 static int procevent_shutdown(void) /* {{{ */
1337 DEBUG("procevent plugin: Shutting down threads.");
1339 int status = stop_threads(1);
1341 for (int i = 0; i < buffer_length; i++) {
1342 free(ring.buffer[i]);
1347 processlist_t *pl = processlist_head;
1348 while (pl != NULL) {
1349 processlist_t *pl_next;
1360 } /* }}} int procevent_shutdown */
1362 void module_register(void) {
1363 plugin_register_config("procevent", procevent_config, config_keys,
1365 plugin_register_init("procevent", procevent_init);
1366 plugin_register_read("procevent", procevent_read);
1367 plugin_register_shutdown("procevent", procevent_shutdown);
1368 } /* void module_register */