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_thread_loop = 0;
128 static int procevent_thread_error = 0;
129 static pthread_t procevent_thread_id;
130 static pthread_mutex_t procevent_lock = PTHREAD_MUTEX_INITIALIZER;
131 static pthread_cond_t procevent_cond = PTHREAD_COND_INITIALIZER;
132 static pthread_mutex_t procevent_list_lock = PTHREAD_MUTEX_INITIALIZER;
133 static int nl_sock = -1;
134 static int buffer_length;
135 static circbuf_t ring;
136 static processlist_t *processlist_head = NULL;
137 static int event_id = 0;
139 static const char *config_keys[] = {"BufferLength", "Process", "ProcessRegex"};
140 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
146 static int gen_message_payload(int state, long pid, char *process,
147 long long unsigned int timestamp, char **buf) {
148 const unsigned char *buf2;
150 char json_str[DATA_MAX_NAME_LEN];
152 #if !defined(HAVE_YAJL_V2)
153 yajl_gen_config conf = {};
160 g = yajl_gen_alloc(NULL);
161 yajl_gen_config(g, yajl_gen_beautify, 0);
164 g = yajl_gen_alloc(&conf, NULL);
169 // *** BEGIN common event header ***
171 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
175 if (yajl_gen_string(g, (u_char *)PROCEVENT_DOMAIN_FIELD,
176 strlen(PROCEVENT_DOMAIN_FIELD)) != yajl_gen_status_ok)
179 if (yajl_gen_string(g, (u_char *)PROCEVENT_DOMAIN_VALUE,
180 strlen(PROCEVENT_DOMAIN_VALUE)) != yajl_gen_status_ok)
184 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_ID_FIELD,
185 strlen(PROCEVENT_EVENT_ID_FIELD)) != yajl_gen_status_ok)
188 event_id = event_id + 1;
189 int event_id_len = sizeof(char) * sizeof(int) * 4 + 1;
190 memset(json_str, '\0', DATA_MAX_NAME_LEN);
191 snprintf(json_str, event_id_len, "%d", event_id);
193 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
198 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_NAME_FIELD,
199 strlen(PROCEVENT_EVENT_NAME_FIELD)) != yajl_gen_status_ok)
202 int event_name_len = 0;
203 event_name_len = event_name_len + (sizeof(char) * sizeof(int) * 4); // pid
204 event_name_len = event_name_len + strlen(process); // process name
205 event_name_len = event_name_len + (state == 0 ? 4 : 2); // "down" or "up"
206 event_name_len = event_name_len +
207 13; // "process", 3 spaces, 2 parentheses and null-terminator
208 memset(json_str, '\0', DATA_MAX_NAME_LEN);
209 snprintf(json_str, event_name_len, "process %s (%ld) %s", process, pid,
210 (state == 0 ? PROCEVENT_EVENT_NAME_DOWN_VALUE
211 : PROCEVENT_EVENT_NAME_UP_VALUE));
213 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
214 yajl_gen_status_ok) {
219 if (yajl_gen_string(g, (u_char *)PROCEVENT_LAST_EPOCH_MICROSEC_FIELD,
220 strlen(PROCEVENT_LAST_EPOCH_MICROSEC_FIELD)) !=
224 int last_epoch_microsec_len =
225 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
226 memset(json_str, '\0', DATA_MAX_NAME_LEN);
227 snprintf(json_str, last_epoch_microsec_len, "%llu",
228 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
230 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
235 if (yajl_gen_string(g, (u_char *)PROCEVENT_PRIORITY_FIELD,
236 strlen(PROCEVENT_PRIORITY_FIELD)) != yajl_gen_status_ok)
239 if (yajl_gen_string(g, (u_char *)PROCEVENT_PRIORITY_VALUE,
240 strlen(PROCEVENT_PRIORITY_VALUE)) != yajl_gen_status_ok)
243 // reportingEntityName
244 if (yajl_gen_string(g, (u_char *)PROCEVENT_REPORTING_ENTITY_NAME_FIELD,
245 strlen(PROCEVENT_REPORTING_ENTITY_NAME_FIELD)) !=
249 if (yajl_gen_string(g, (u_char *)PROCEVENT_REPORTING_ENTITY_NAME_VALUE,
250 strlen(PROCEVENT_REPORTING_ENTITY_NAME_VALUE)) !=
255 if (yajl_gen_string(g, (u_char *)PROCEVENT_SEQUENCE_FIELD,
256 strlen(PROCEVENT_SEQUENCE_FIELD)) != yajl_gen_status_ok)
259 if (yajl_gen_number(g, PROCEVENT_SEQUENCE_VALUE,
260 strlen(PROCEVENT_SEQUENCE_VALUE)) != yajl_gen_status_ok)
264 if (yajl_gen_string(g, (u_char *)PROCEVENT_SOURCE_NAME_FIELD,
265 strlen(PROCEVENT_SOURCE_NAME_FIELD)) !=
269 if (yajl_gen_string(g, (u_char *)process, strlen(process)) !=
273 // startEpochMicrosec
274 if (yajl_gen_string(g, (u_char *)PROCEVENT_START_EPOCH_MICROSEC_FIELD,
275 strlen(PROCEVENT_START_EPOCH_MICROSEC_FIELD)) !=
279 int start_epoch_microsec_len =
280 sizeof(char) * sizeof(long long unsigned int) * 4 + 1;
281 memset(json_str, '\0', DATA_MAX_NAME_LEN);
282 snprintf(json_str, start_epoch_microsec_len, "%llu",
283 (long long unsigned int)timestamp);
285 if (yajl_gen_number(g, json_str, strlen(json_str)) != yajl_gen_status_ok) {
290 if (yajl_gen_string(g, (u_char *)PROCEVENT_VERSION_FIELD,
291 strlen(PROCEVENT_VERSION_FIELD)) != yajl_gen_status_ok)
294 if (yajl_gen_number(g, PROCEVENT_VERSION_VALUE,
295 strlen(PROCEVENT_VERSION_VALUE)) != yajl_gen_status_ok)
298 // *** END common event header ***
300 // *** BEGIN fault fields ***
302 if (yajl_gen_string(g, (u_char *)PROCEVENT_FAULT_FIELDS_FIELD,
303 strlen(PROCEVENT_FAULT_FIELDS_FIELD)) !=
307 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
311 if (yajl_gen_string(g, (u_char *)PROCEVENT_ALARM_CONDITION_FIELD,
312 strlen(PROCEVENT_ALARM_CONDITION_FIELD)) !=
316 int alarm_condition_len = 0;
317 alarm_condition_len =
318 alarm_condition_len + (sizeof(char) * sizeof(int) * 4); // pid
319 alarm_condition_len = alarm_condition_len + strlen(process); // process name
320 alarm_condition_len =
321 alarm_condition_len + 25; // "process", "state", "change", 4 spaces, 2
322 // parentheses and null-terminator
323 memset(json_str, '\0', DATA_MAX_NAME_LEN);
324 snprintf(json_str, alarm_condition_len, "process %s (%ld) state change",
327 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
328 yajl_gen_status_ok) {
333 if (yajl_gen_string(g, (u_char *)PROCEVENT_ALARM_INTERFACE_A_FIELD,
334 strlen(PROCEVENT_ALARM_INTERFACE_A_FIELD)) !=
338 if (yajl_gen_string(g, (u_char *)process, strlen(process)) !=
343 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SEVERITY_FIELD,
344 strlen(PROCEVENT_EVENT_SEVERITY_FIELD)) !=
349 g, (u_char *)(state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
350 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE),
351 strlen((state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
352 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE))) !=
357 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_FIELD,
358 strlen(PROCEVENT_EVENT_SOURCE_TYPE_FIELD)) !=
362 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_VALUE,
363 strlen(PROCEVENT_EVENT_SOURCE_TYPE_VALUE)) !=
367 // faultFieldsVersion
368 if (yajl_gen_string(g, (u_char *)PROCEVENT_FAULT_FIELDS_VERSION_FIELD,
369 strlen(PROCEVENT_FAULT_FIELDS_VERSION_FIELD)) !=
373 if (yajl_gen_number(g, PROCEVENT_FAULT_FIELDS_VERSION_VALUE,
374 strlen(PROCEVENT_FAULT_FIELDS_VERSION_VALUE)) !=
379 if (yajl_gen_string(g, (u_char *)PROCEVENT_SPECIFIC_PROBLEM_FIELD,
380 strlen(PROCEVENT_SPECIFIC_PROBLEM_FIELD)) !=
384 int specific_problem_len = 0;
385 specific_problem_len =
386 specific_problem_len + (sizeof(char) * sizeof(int) * 4); // pid
387 specific_problem_len = specific_problem_len + strlen(process); // process name
388 specific_problem_len =
389 specific_problem_len + (state == 0 ? 4 : 2); // "down" or "up"
390 specific_problem_len =
391 specific_problem_len +
392 13; // "process", 3 spaces, 2 parentheses and null-terminator
393 memset(json_str, '\0', DATA_MAX_NAME_LEN);
394 snprintf(json_str, specific_problem_len, "process %s (%ld) %s", process, pid,
395 (state == 0 ? PROCEVENT_SPECIFIC_PROBLEM_DOWN_VALUE
396 : PROCEVENT_SPECIFIC_PROBLEM_UP_VALUE));
398 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
399 yajl_gen_status_ok) {
404 if (yajl_gen_string(g, (u_char *)PROCEVENT_VF_STATUS_FIELD,
405 strlen(PROCEVENT_VF_STATUS_FIELD)) != yajl_gen_status_ok)
409 g, (u_char *)(state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
410 : PROCEVENT_VF_STATUS_NORMAL_VALUE),
411 strlen((state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
412 : PROCEVENT_VF_STATUS_NORMAL_VALUE))) !=
416 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
419 // *** END fault fields ***
421 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
424 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
427 *buf = malloc(strlen((char *)buf2) + 1);
431 ERROR("procevent plugin: malloc failed during gen_message_payload: %s",
432 sstrerror(errno, errbuf, sizeof(errbuf)));
436 sstrncpy(*buf, (char *)buf2, strlen((char *)buf2) + 1);
444 ERROR("procevent plugin: gen_message_payload failed to generate JSON");
448 // Does /proc/<pid>/comm contain a process name we are interested in?
449 static processlist_t *process_check(long pid) {
450 int len, is_match, retval;
453 char buffer[BUFSIZE];
455 len = snprintf(file, sizeof(file), PROCDIR "/%ld/comm", pid);
457 if ((len < 0) || (len >= BUFSIZE)) {
458 WARNING("procevent process_check: process name too large");
462 if (NULL == (fh = fopen(file, "r"))) {
463 // No /proc/<pid>/comm for this pid, just ignore
464 DEBUG("procevent plugin: no comm file available for pid %ld", pid);
468 retval = fscanf(fh, "%[^\n]", buffer);
471 WARNING("procevent process_check: unable to read comm file for pid %ld",
477 // Now that we have the process name in the buffer, check if we are
478 // even interested in it
479 if (ignorelist_match(ignorelist, buffer) != 0) {
480 DEBUG("procevent process_check: ignoring process %s (%ld)", buffer, pid);
491 // Go through the processlist linked list and look for the process name
492 // in /proc/<pid>/comm. If found:
493 // 1. If pl->pid is -1, then set pl->pid to <pid> (and return that object)
494 // 2. If pl->pid is not -1, then another <process name> process was already
495 // found. If <pid> == pl->pid, this is an old match, so do nothing.
496 // If the <pid> is different, however, make a new processlist_t and
497 // associate <pid> with it (with the same process name as the existing).
500 pthread_mutex_lock(&procevent_list_lock);
503 processlist_t *match = NULL;
505 for (pl = processlist_head; pl != NULL; pl = pl->next) {
507 is_match = (strcmp(buffer, pl->process) == 0 ? 1 : 0);
510 DEBUG("procevent plugin: process %ld name match for %s", pid, buffer);
512 if (pl->pid == pid) {
513 // this is a match, and we've already stored the exact pid/name combo
514 DEBUG("procevent plugin: found exact match with name %s, PID %ld for "
516 pl->process, pl->pid, pid);
519 } else if (pl->pid == -1) {
520 // this is a match, and we've found a candidate processlist_t to store
521 // this new pid/name combo
522 DEBUG("procevent plugin: reusing pl object with PID %ld for incoming "
528 } else if (pl->pid != -1) {
529 // this is a match, but another instance of this process has already
530 // claimed this pid/name combo,
532 DEBUG("procevent plugin: found pl object with matching name for "
533 "incoming PID %ld, but object is in use by PID %ld",
542 (match != NULL && match->pid != -1 && match->pid != pid)) {
543 // if there wasn't an existing match, OR
544 // if there was a match but the associated processlist_t object already
545 // contained a pid/name combo,
546 // then make a new one and add it to the linked list
548 DEBUG("procevent plugin: allocating new processlist_t object for PID %ld "
555 pl2 = malloc(sizeof(*pl2));
558 ERROR("procevent plugin: malloc failed during process_check: %s",
559 sstrerror(errno, errbuf, sizeof(errbuf)));
560 pthread_mutex_unlock(&procevent_list_lock);
564 process = strdup(buffer);
565 if (process == NULL) {
568 ERROR("procevent plugin: strdup failed during process_check: %s",
569 sstrerror(errno, errbuf, sizeof(errbuf)));
570 pthread_mutex_unlock(&procevent_list_lock);
574 pl2->process = process;
576 pl2->next = processlist_head;
577 processlist_head = pl2;
582 pthread_mutex_unlock(&procevent_list_lock);
587 // Does our map have this PID or name?
588 static processlist_t *process_map_check(long pid, char *process) {
591 pthread_mutex_lock(&procevent_list_lock);
593 for (pl = processlist_head; pl != NULL; pl = pl->next) {
595 int match_process = 0;
603 if (process != NULL) {
604 if (strcmp(pl->process, process) == 0)
608 if (pid > 0 && process == NULL && match_pid == 1)
610 else if (pid < 0 && process != NULL && match_process == 1)
612 else if (pid > 0 && process != NULL && match_pid == 1 && match_process == 1)
616 pthread_mutex_unlock(&procevent_list_lock);
621 pthread_mutex_unlock(&procevent_list_lock);
626 static int process_map_refresh(void) {
630 proc = opendir(PROCDIR);
633 ERROR("procevent plugin: fopen (%s): %s", PROCDIR,
634 sstrerror(errno, errbuf, sizeof(errbuf)));
648 dent = readdir(proc);
652 if (errno == 0) /* end of directory */
655 ERROR("procevent plugin: failed to read directory %s: %s", PROCDIR,
656 sstrerror(errno, errbuf, sizeof(errbuf)));
661 if (dent->d_name[0] == '.')
664 len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
665 if ((len < 0) || (len >= BUFSIZE))
668 status = stat(file, &statbuf);
671 WARNING("procevent plugin: stat (%s) failed: %s", file,
672 sstrerror(errno, errbuf, sizeof(errbuf)));
676 if (!S_ISDIR(statbuf.st_mode))
679 len = snprintf(file, sizeof(file), PROCDIR "/%s/comm", dent->d_name);
680 if ((len < 0) || (len >= BUFSIZE))
685 for (int i = 0; i < strlen(dent->d_name); i++) {
686 if (!isdigit(dent->d_name[i])) {
695 // Check if we need to store this pid/name combo in our processlist_t linked
697 int this_pid = atoi(dent->d_name);
698 processlist_t *pl = process_check(this_pid);
701 DEBUG("procevent plugin: process map refreshed for PID %d and name %s",
702 this_pid, pl->process);
710 static int nl_connect() {
712 struct sockaddr_nl sa_nl;
714 nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
716 ERROR("procevent plugin: socket open failed: %d", errno);
720 sa_nl.nl_family = AF_NETLINK;
721 sa_nl.nl_groups = CN_IDX_PROC;
722 sa_nl.nl_pid = getpid();
724 rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
726 ERROR("procevent plugin: socket bind failed: %d", errno);
734 static int set_proc_ev_listen(bool enable) {
736 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
737 struct nlmsghdr nl_hdr;
738 struct __attribute__((__packed__)) {
739 struct cn_msg cn_msg;
740 enum proc_cn_mcast_op cn_mcast;
744 memset(&nlcn_msg, 0, sizeof(nlcn_msg));
745 nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg);
746 nlcn_msg.nl_hdr.nlmsg_pid = getpid();
747 nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE;
749 nlcn_msg.cn_msg.id.idx = CN_IDX_PROC;
750 nlcn_msg.cn_msg.id.val = CN_VAL_PROC;
751 nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
753 nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
755 rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
757 ERROR("procevent plugin: subscribing to netlink process events failed: %d",
765 static int read_event() {
769 int proc_status = -1;
771 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
772 struct nlmsghdr nl_hdr;
773 struct __attribute__((__packed__)) {
774 struct cn_msg cn_msg;
775 struct proc_event proc_ev;
784 pthread_mutex_lock(&procevent_lock);
786 if (procevent_thread_loop <= 0)
789 pthread_mutex_unlock(&procevent_lock);
791 status = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
795 } else if (status == -1) {
796 if (errno != EINTR) {
797 ERROR("procevent plugin: socket receive error: %d", errno);
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_lock);
828 int next = ring.head + 1;
829 if (next >= ring.maxLen)
832 if (next == ring.tail) {
833 WARNING("procevent plugin: ring buffer full");
835 DEBUG("procevent plugin: Process %d status is now %s at %llu", proc_id,
836 (proc_status == PROCEVENT_EXITED ? "EXITED" : "STARTED"),
837 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
839 if (proc_status == PROCEVENT_EXITED) {
840 ring.buffer[ring.head][0] = proc_id;
841 ring.buffer[ring.head][1] = proc_status;
842 ring.buffer[ring.head][2] = proc_extra;
843 ring.buffer[ring.head][3] =
844 (long long unsigned int)CDTIME_T_TO_US(cdtime());
846 ring.buffer[ring.head][0] = proc_id;
847 ring.buffer[ring.head][1] = proc_status;
848 ring.buffer[ring.head][2] = 0;
849 ring.buffer[ring.head][3] =
850 (long long unsigned int)CDTIME_T_TO_US(cdtime());
856 pthread_mutex_unlock(&procevent_lock);
863 static void *procevent_thread(void *arg) /* {{{ */
865 pthread_mutex_lock(&procevent_lock);
867 while (procevent_thread_loop > 0) {
870 pthread_mutex_unlock(&procevent_lock);
874 status = read_event();
876 pthread_mutex_lock(&procevent_lock);
879 procevent_thread_error = 1;
883 if (procevent_thread_loop <= 0)
885 } /* while (procevent_thread_loop > 0) */
887 pthread_mutex_unlock(&procevent_lock);
890 } /* }}} void *procevent_thread */
892 static int start_thread(void) /* {{{ */
896 pthread_mutex_lock(&procevent_lock);
898 if (procevent_thread_loop != 0) {
899 pthread_mutex_unlock(&procevent_lock);
904 status = nl_connect();
909 status = set_proc_ev_listen(true);
914 DEBUG("procevent plugin: socket created and bound");
916 procevent_thread_loop = 1;
917 procevent_thread_error = 0;
919 status = plugin_thread_create(&procevent_thread_id, /* attr = */ NULL,
921 /* arg = */ (void *)0, "procevent");
923 procevent_thread_loop = 0;
924 ERROR("procevent plugin: Starting thread failed.");
925 pthread_mutex_unlock(&procevent_lock);
929 pthread_mutex_unlock(&procevent_lock);
931 } /* }}} int start_thread */
933 static int stop_thread(int shutdown) /* {{{ */
938 status = close(nl_sock);
940 ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
941 status, strerror(errno));
947 pthread_mutex_lock(&procevent_lock);
949 if (procevent_thread_loop == 0) {
950 pthread_mutex_unlock(&procevent_lock);
954 procevent_thread_loop = 0;
955 pthread_cond_broadcast(&procevent_cond);
956 pthread_mutex_unlock(&procevent_lock);
959 // Calling pthread_cancel here in
960 // the case of a shutdown just assures that the thread is
961 // gone and that the process has been fully terminated.
963 DEBUG("procevent plugin: Canceling thread for process shutdown");
965 status = pthread_cancel(procevent_thread_id);
968 ERROR("procevent plugin: Unable to cancel thread: %d", status);
972 status = pthread_join(procevent_thread_id, /* return = */ NULL);
974 ERROR("procevent plugin: Stopping thread failed.");
979 pthread_mutex_lock(&procevent_lock);
980 memset(&procevent_thread_id, 0, sizeof(procevent_thread_id));
981 procevent_thread_error = 0;
982 pthread_mutex_unlock(&procevent_lock);
984 DEBUG("procevent plugin: Finished requesting stop of thread");
987 } /* }}} int stop_thread */
989 static int procevent_init(void) /* {{{ */
995 ring.maxLen = buffer_length;
996 ring.buffer = (long long unsigned int **)malloc(
997 buffer_length * sizeof(long long unsigned int *));
999 for (int i = 0; i < buffer_length; i++) {
1000 ring.buffer[i] = (long long unsigned int *)malloc(
1001 PROCEVENT_FIELDS * sizeof(long long unsigned int));
1004 status = process_map_refresh();
1007 ERROR("procevent plugin: Initial process mapping failed.");
1011 if (ignorelist == NULL) {
1012 NOTICE("procevent plugin: No processes have been configured.");
1016 return (start_thread());
1017 } /* }}} int procevent_init */
1019 static int procevent_config(const char *key, const char *value) /* {{{ */
1023 if (ignorelist == NULL)
1024 ignorelist = ignorelist_create(/* invert = */ 1);
1026 if (strcasecmp(key, "BufferLength") == 0) {
1027 buffer_length = atoi(value);
1028 } else if (strcasecmp(key, "Process") == 0) {
1029 ignorelist_add(ignorelist, value);
1030 } else if (strcasecmp(key, "ProcessRegex") == 0) {
1032 status = ignorelist_add(ignorelist, value);
1035 ERROR("procevent plugin: invalid regular expression: %s", value);
1039 WARNING("procevent plugin: The plugin has been compiled without support "
1040 "for the \"ProcessRegex\" option.");
1047 } /* }}} int procevent_config */
1049 static void procevent_dispatch_notification(long pid,
1050 const char *type, /* {{{ */
1051 gauge_t value, char *process,
1052 long long unsigned int timestamp) {
1054 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", "procevent", "", "", "",
1058 n.severity = NOTIF_OKAY;
1060 sstrncpy(n.host, hostname_g, sizeof(n.host));
1061 sstrncpy(n.plugin_instance, process, sizeof(n.plugin_instance));
1062 sstrncpy(n.type, "gauge", sizeof(n.type));
1063 sstrncpy(n.type_instance, "process_status", sizeof(n.type_instance));
1065 gen_message_payload(value, pid, process, timestamp, &buf);
1067 notification_meta_t *m = calloc(1, sizeof(*m));
1072 ERROR("procevent plugin: unable to allocate metadata: %s",
1073 sstrerror(errno, errbuf, sizeof(errbuf)));
1077 sstrncpy(m->name, "ves", sizeof(m->name));
1078 m->nm_value.nm_string = sstrdup(buf);
1079 m->type = NM_TYPE_STRING;
1082 DEBUG("procevent plugin: notification message: %s",
1083 n.meta->nm_value.nm_string);
1085 DEBUG("procevent plugin: dispatching state %d for PID %ld (%s)", (int)value,
1088 plugin_dispatch_notification(&n);
1089 plugin_notification_meta_free(n.meta);
1091 // malloc'd in gen_message_payload
1096 static int procevent_read(void) /* {{{ */
1098 if (procevent_thread_error != 0) {
1100 "procevent plugin: The interface thread had a problem. Restarting it.");
1107 } /* if (procevent_thread_error != 0) */
1109 pthread_mutex_lock(&procevent_lock);
1111 while (ring.head != ring.tail) {
1112 int next = ring.tail + 1;
1114 if (next >= ring.maxLen)
1117 if (ring.buffer[ring.tail][1] == PROCEVENT_EXITED) {
1118 processlist_t *pl = process_map_check(ring.buffer[ring.tail][0], NULL);
1121 // This process is of interest to us, so publish its EXITED status
1122 procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge",
1123 ring.buffer[ring.tail][1], pl->process,
1124 ring.buffer[ring.tail][3]);
1126 "procevent plugin: PID %ld (%s) EXITED, removing PID from process "
1128 pl->pid, pl->process);
1130 pl->last_status = -1;
1132 } else if (ring.buffer[ring.tail][1] == PROCEVENT_STARTED) {
1133 // a new process has started, so check if we should monitor it
1134 processlist_t *pl = process_check(ring.buffer[ring.tail][0]);
1136 // If we had already seen this process name and pid combo before,
1137 // and the last message was a "process started" message, don't send
1138 // the notfication again
1140 if (pl != NULL && pl->last_status != PROCEVENT_STARTED) {
1141 // This process is of interest to us, so publish its STARTED status
1142 procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge",
1143 ring.buffer[ring.tail][1], pl->process,
1144 ring.buffer[ring.tail][3]);
1146 pl->last_status = PROCEVENT_STARTED;
1148 DEBUG("procevent plugin: PID %ld (%s) STARTED, adding PID to process "
1150 pl->pid, pl->process);
1157 pthread_mutex_unlock(&procevent_lock);
1160 } /* }}} int procevent_read */
1162 static int procevent_shutdown(void) /* {{{ */
1166 DEBUG("procevent plugin: Shutting down thread.");
1168 if (stop_thread(1) < 0)
1171 for (int i = 0; i < buffer_length; i++) {
1172 free(ring.buffer[i]);
1177 pl = processlist_head;
1178 while (pl != NULL) {
1179 processlist_t *pl_next;
1190 } /* }}} int procevent_shutdown */
1192 void module_register(void) {
1193 plugin_register_config("procevent", procevent_config, config_keys,
1195 plugin_register_init("procevent", procevent_init);
1196 plugin_register_read("procevent", procevent_read);
1197 plugin_register_shutdown("procevent", procevent_shutdown);
1198 } /* void module_register */