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", "RegexProcess"};
140 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
146 static int gen_message_payload(int state, int 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 (%d) %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 (%d) 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)) !=
350 (u_char *)(state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
351 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE),
352 strlen((state == 0 ? PROCEVENT_EVENT_SEVERITY_CRITICAL_VALUE
353 : PROCEVENT_EVENT_SEVERITY_NORMAL_VALUE))) !=
358 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_FIELD,
359 strlen(PROCEVENT_EVENT_SOURCE_TYPE_FIELD)) !=
363 if (yajl_gen_string(g, (u_char *)PROCEVENT_EVENT_SOURCE_TYPE_VALUE,
364 strlen(PROCEVENT_EVENT_SOURCE_TYPE_VALUE)) !=
368 // faultFieldsVersion
369 if (yajl_gen_string(g, (u_char *)PROCEVENT_FAULT_FIELDS_VERSION_FIELD,
370 strlen(PROCEVENT_FAULT_FIELDS_VERSION_FIELD)) !=
374 if (yajl_gen_number(g, PROCEVENT_FAULT_FIELDS_VERSION_VALUE,
375 strlen(PROCEVENT_FAULT_FIELDS_VERSION_VALUE)) !=
380 if (yajl_gen_string(g, (u_char *)PROCEVENT_SPECIFIC_PROBLEM_FIELD,
381 strlen(PROCEVENT_SPECIFIC_PROBLEM_FIELD)) !=
385 int specific_problem_len = 0;
386 specific_problem_len =
387 specific_problem_len + (sizeof(char) * sizeof(int) * 4); // pid
388 specific_problem_len = specific_problem_len + strlen(process); // process name
389 specific_problem_len =
390 specific_problem_len + (state == 0 ? 4 : 2); // "down" or "up"
391 specific_problem_len =
392 specific_problem_len +
393 13; // "process", 3 spaces, 2 parentheses and null-terminator
394 memset(json_str, '\0', DATA_MAX_NAME_LEN);
395 snprintf(json_str, specific_problem_len, "process %s (%d) %s", process, pid,
396 (state == 0 ? PROCEVENT_SPECIFIC_PROBLEM_DOWN_VALUE
397 : PROCEVENT_SPECIFIC_PROBLEM_UP_VALUE));
399 if (yajl_gen_string(g, (u_char *)json_str, strlen(json_str)) !=
400 yajl_gen_status_ok) {
405 if (yajl_gen_string(g, (u_char *)PROCEVENT_VF_STATUS_FIELD,
406 strlen(PROCEVENT_VF_STATUS_FIELD)) != yajl_gen_status_ok)
411 (u_char *)(state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
412 : PROCEVENT_VF_STATUS_NORMAL_VALUE),
413 strlen((state == 0 ? PROCEVENT_VF_STATUS_CRITICAL_VALUE
414 : PROCEVENT_VF_STATUS_NORMAL_VALUE))) !=
418 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
421 // *** END fault fields ***
423 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
426 if (yajl_gen_get_buf(g, &buf2, &len) != yajl_gen_status_ok)
429 *buf = malloc(strlen((char *)buf2) + 1);
433 ERROR("procevent plugin: malloc failed during gen_message_payload: %s",
434 sstrerror(errno, errbuf, sizeof(errbuf)));
438 sstrncpy(*buf, (char *)buf2, strlen((char *)buf2) + 1);
446 ERROR("procevent plugin: gen_message_payload failed to generate JSON");
450 // Does /proc/<pid>/comm contain a process name we are interested in?
451 static processlist_t *process_check(int pid) {
452 int len, is_match, retval;
455 char buffer[BUFSIZE];
457 len = snprintf(file, sizeof(file), PROCDIR "/%d/comm", pid);
459 if ((len < 0) || (len >= BUFSIZE)) {
460 WARNING("procevent process_check: process name too large");
464 if (NULL == (fh = fopen(file, "r"))) {
465 // No /proc/<pid>/comm for this pid, just ignore
466 DEBUG("procevent plugin: no comm file available for pid %d", pid);
470 retval = fscanf(fh, "%[^\n]", buffer);
473 WARNING("procevent process_check: unable to read comm file for pid %d",
479 // Now that we have the process name in the buffer, check if we are
480 // even interested in it
481 if (ignorelist_match(ignorelist, buffer) != 0) {
482 DEBUG("procevent process_check: ignoring process %s (%d)", buffer, pid);
493 // Go through the processlist linked list and look for the process name
494 // in /proc/<pid>/comm. If found:
495 // 1. If pl->pid is -1, then set pl->pid to <pid> (and return that object)
496 // 2. If pl->pid is not -1, then another <process name> process was already
497 // found. If <pid> == pl->pid, this is an old match, so do nothing.
498 // If the <pid> is different, however, make a new processlist_t and
499 // associate <pid> with it (with the same process name as the existing).
502 pthread_mutex_lock(&procevent_list_lock);
505 processlist_t *match = NULL;
507 for (pl = processlist_head; pl != NULL; pl = pl->next) {
509 is_match = (strcmp(buffer, pl->process) == 0 ? 1 : 0);
512 DEBUG("procevent plugin: process %d name match for %s", pid, buffer);
514 if (pl->pid == pid) {
515 // this is a match, and we've already stored the exact pid/name combo
516 DEBUG("procevent plugin: found exact match with name %s, PID %ld for "
518 pl->process, pl->pid, pid);
521 } else if (pl->pid == -1) {
522 // this is a match, and we've found a candidate processlist_t to store
523 // this new pid/name combo
524 DEBUG("procevent plugin: reusing pl object with PID %ld for incoming "
530 } else if (pl->pid != -1) {
531 // this is a match, but another instance of this process has already
532 // claimed this pid/name combo,
534 DEBUG("procevent plugin: found pl object with matching name for "
535 "incoming PID %d, but object is in use by PID %ld",
544 (match != NULL && match->pid != -1 && match->pid != pid)) {
545 // if there wasn't an existing match, OR
546 // if there was a match but the associated processlist_t object already
547 // contained a pid/name combo,
548 // then make a new one and add it to the linked list
551 "procevent plugin: allocating new processlist_t object for PID %d (%s)",
557 pl2 = malloc(sizeof(*pl2));
560 ERROR("procevent plugin: malloc failed during process_check: %s",
561 sstrerror(errno, errbuf, sizeof(errbuf)));
562 pthread_mutex_unlock(&procevent_list_lock);
566 process = strdup(buffer);
567 if (process == NULL) {
570 ERROR("procevent plugin: strdup failed during process_check: %s",
571 sstrerror(errno, errbuf, sizeof(errbuf)));
572 pthread_mutex_unlock(&procevent_list_lock);
576 pl2->process = process;
578 pl2->next = processlist_head;
579 processlist_head = pl2;
584 pthread_mutex_unlock(&procevent_list_lock);
589 // Does our map have this PID or name?
590 static processlist_t *process_map_check(int pid, char *process) {
593 pthread_mutex_lock(&procevent_list_lock);
595 for (pl = processlist_head; pl != NULL; pl = pl->next) {
597 int match_process = 0;
605 if (process != NULL) {
606 if (strcmp(pl->process, process) == 0)
610 if (pid > 0 && process == NULL && match_pid == 1)
612 else if (pid < 0 && process != NULL && match_process == 1)
614 else if (pid > 0 && process != NULL && match_pid == 1 && match_process == 1)
618 pthread_mutex_unlock(&procevent_list_lock);
623 pthread_mutex_unlock(&procevent_list_lock);
628 static int process_map_refresh(void) {
632 proc = opendir(PROCDIR);
635 ERROR("procevent plugin: fopen (%s): %s", PROCDIR,
636 sstrerror(errno, errbuf, sizeof(errbuf)));
650 dent = readdir(proc);
654 if (errno == 0) /* end of directory */
657 ERROR("procevent plugin: failed to read directory %s: %s", PROCDIR,
658 sstrerror(errno, errbuf, sizeof(errbuf)));
663 if (dent->d_name[0] == '.')
666 len = snprintf(file, sizeof(file), PROCDIR "/%s", dent->d_name);
667 if ((len < 0) || (len >= BUFSIZE))
670 status = stat(file, &statbuf);
673 WARNING("procevent plugin: stat (%s) failed: %s", file,
674 sstrerror(errno, errbuf, sizeof(errbuf)));
678 if (!S_ISDIR(statbuf.st_mode))
681 len = snprintf(file, sizeof(file), PROCDIR "/%s/comm", dent->d_name);
682 if ((len < 0) || (len >= BUFSIZE))
687 for (int i = 0; i < strlen(dent->d_name); i++) {
688 if (!isdigit(dent->d_name[i])) {
697 // Check if we need to store this pid/name combo in our processlist_t linked
699 int this_pid = atoi(dent->d_name);
700 processlist_t *pl = process_check(this_pid);
703 DEBUG("procevent plugin: process map refreshed for PID %d and name %s",
704 this_pid, pl->process);
712 static int nl_connect() {
714 struct sockaddr_nl sa_nl;
716 nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
718 ERROR("procevent plugin: socket open failed: %d", errno);
722 sa_nl.nl_family = AF_NETLINK;
723 sa_nl.nl_groups = CN_IDX_PROC;
724 sa_nl.nl_pid = getpid();
726 rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
728 ERROR("procevent plugin: socket bind failed: %d", errno);
736 static int set_proc_ev_listen(bool enable) {
738 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
739 struct nlmsghdr nl_hdr;
740 struct __attribute__((__packed__)) {
741 struct cn_msg cn_msg;
742 enum proc_cn_mcast_op cn_mcast;
746 memset(&nlcn_msg, 0, sizeof(nlcn_msg));
747 nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg);
748 nlcn_msg.nl_hdr.nlmsg_pid = getpid();
749 nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE;
751 nlcn_msg.cn_msg.id.idx = CN_IDX_PROC;
752 nlcn_msg.cn_msg.id.val = CN_VAL_PROC;
753 nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
755 nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
757 rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
759 ERROR("procevent plugin: subscribing to netlink process events failed: %d",
767 static int read_event() {
771 int proc_status = -1;
773 struct __attribute__((aligned(NLMSG_ALIGNTO))) {
774 struct nlmsghdr nl_hdr;
775 struct __attribute__((__packed__)) {
776 struct cn_msg cn_msg;
777 struct proc_event proc_ev;
786 pthread_mutex_lock(&procevent_lock);
788 if (procevent_thread_loop <= 0)
791 pthread_mutex_unlock(&procevent_lock);
793 status = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
797 } else if (status == -1) {
798 if (errno != EINTR) {
799 ERROR("procevent plugin: socket receive error: %d", errno);
804 switch (nlcn_msg.proc_ev.what) {
805 case PROC_EVENT_NONE:
806 case PROC_EVENT_FORK:
809 // Not of interest in current version
811 case PROC_EVENT_EXEC:
812 proc_status = PROCEVENT_STARTED;
813 proc_id = nlcn_msg.proc_ev.event_data.exec.process_pid;
815 case PROC_EVENT_EXIT:
816 proc_id = nlcn_msg.proc_ev.event_data.exit.process_pid;
817 proc_status = PROCEVENT_EXITED;
818 proc_extra = nlcn_msg.proc_ev.event_data.exit.exit_code;
824 // If we're interested in this process status event, place the event
825 // in the ring buffer for consumption by the main polling thread.
827 if (proc_status != -1) {
828 pthread_mutex_lock(&procevent_lock);
830 int next = ring.head + 1;
831 if (next >= ring.maxLen)
834 if (next == ring.tail) {
835 WARNING("procevent plugin: ring buffer full");
837 DEBUG("procevent plugin: Process %d status is now %s at %llu", proc_id,
838 (proc_status == PROCEVENT_EXITED ? "EXITED" : "STARTED"),
839 (long long unsigned int)CDTIME_T_TO_US(cdtime()));
841 if (proc_status == PROCEVENT_EXITED) {
842 ring.buffer[ring.head][0] = proc_id;
843 ring.buffer[ring.head][1] = proc_status;
844 ring.buffer[ring.head][2] = proc_extra;
845 ring.buffer[ring.head][3] =
846 (long long unsigned int)CDTIME_T_TO_US(cdtime());
848 ring.buffer[ring.head][0] = proc_id;
849 ring.buffer[ring.head][1] = proc_status;
850 ring.buffer[ring.head][2] = 0;
851 ring.buffer[ring.head][3] =
852 (long long unsigned int)CDTIME_T_TO_US(cdtime());
858 pthread_mutex_unlock(&procevent_lock);
865 static void *procevent_thread(void *arg) /* {{{ */
867 pthread_mutex_lock(&procevent_lock);
869 while (procevent_thread_loop > 0) {
872 pthread_mutex_unlock(&procevent_lock);
876 status = read_event();
878 pthread_mutex_lock(&procevent_lock);
881 procevent_thread_error = 1;
885 if (procevent_thread_loop <= 0)
887 } /* while (procevent_thread_loop > 0) */
889 pthread_mutex_unlock(&procevent_lock);
892 } /* }}} void *procevent_thread */
894 static int start_thread(void) /* {{{ */
898 pthread_mutex_lock(&procevent_lock);
900 if (procevent_thread_loop != 0) {
901 pthread_mutex_unlock(&procevent_lock);
906 status = nl_connect();
911 status = set_proc_ev_listen(true);
916 DEBUG("procevent plugin: socket created and bound");
918 procevent_thread_loop = 1;
919 procevent_thread_error = 0;
921 status = plugin_thread_create(&procevent_thread_id, /* attr = */ NULL,
923 /* arg = */ (void *)0, "procevent");
925 procevent_thread_loop = 0;
926 ERROR("procevent plugin: Starting thread failed.");
927 pthread_mutex_unlock(&procevent_lock);
931 pthread_mutex_unlock(&procevent_lock);
933 } /* }}} int start_thread */
935 static int stop_thread(int shutdown) /* {{{ */
940 status = close(nl_sock);
942 ERROR("procevent plugin: failed to close socket %d: %d (%s)", nl_sock,
943 status, strerror(errno));
949 pthread_mutex_lock(&procevent_lock);
951 if (procevent_thread_loop == 0) {
952 pthread_mutex_unlock(&procevent_lock);
956 procevent_thread_loop = 0;
957 pthread_cond_broadcast(&procevent_cond);
958 pthread_mutex_unlock(&procevent_lock);
961 // Calling pthread_cancel here in
962 // the case of a shutdown just assures that the thread is
963 // gone and that the process has been fully terminated.
965 DEBUG("procevent plugin: Canceling thread for process shutdown");
967 status = pthread_cancel(procevent_thread_id);
970 ERROR("procevent plugin: Unable to cancel thread: %d", status);
974 status = pthread_join(procevent_thread_id, /* return = */ NULL);
976 ERROR("procevent plugin: Stopping thread failed.");
981 pthread_mutex_lock(&procevent_lock);
982 memset(&procevent_thread_id, 0, sizeof(procevent_thread_id));
983 procevent_thread_error = 0;
984 pthread_mutex_unlock(&procevent_lock);
986 DEBUG("procevent plugin: Finished requesting stop of thread");
989 } /* }}} int stop_thread */
991 static int procevent_init(void) /* {{{ */
997 ring.maxLen = buffer_length;
998 ring.buffer = (long long unsigned int **)malloc(
999 buffer_length * sizeof(long long unsigned int *));
1001 for (int i = 0; i < buffer_length; i++) {
1002 ring.buffer[i] = (long long unsigned int *)malloc(
1003 PROCEVENT_FIELDS * sizeof(long long unsigned int));
1006 status = process_map_refresh();
1009 ERROR("procevent plugin: Initial process mapping failed.");
1013 if (ignorelist == NULL) {
1014 NOTICE("procevent plugin: No processes have been configured.");
1018 return (start_thread());
1019 } /* }}} int procevent_init */
1021 static int procevent_config(const char *key, const char *value) /* {{{ */
1025 if (ignorelist == NULL)
1026 ignorelist = ignorelist_create(/* invert = */ 1);
1028 if (strcasecmp(key, "BufferLength") == 0) {
1029 buffer_length = atoi(value);
1030 } else if (strcasecmp(key, "Process") == 0) {
1031 ignorelist_add(ignorelist, value);
1032 } else if (strcasecmp(key, "RegexProcess") == 0) {
1034 status = ignorelist_add(ignorelist, value);
1037 ERROR("procevent plugin: invalid regular expression: %s", value);
1041 WARNING("procevent plugin: The plugin has been compiled without support "
1042 "for the \"RegexProcess\" option.");
1049 } /* }}} int procevent_config */
1051 static void procevent_dispatch_notification(int pid, const char *type, /* {{{ */
1052 gauge_t value, char *process,
1053 long long unsigned int timestamp) {
1055 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", "procevent", "", "", "",
1059 n.severity = NOTIF_OKAY;
1061 sstrncpy(n.host, hostname_g, sizeof(n.host));
1062 sstrncpy(n.plugin_instance, process, sizeof(n.plugin_instance));
1063 sstrncpy(n.type, "gauge", sizeof(n.type));
1064 sstrncpy(n.type_instance, "process_status", sizeof(n.type_instance));
1066 gen_message_payload(value, pid, process, timestamp, &buf);
1068 notification_meta_t *m = calloc(1, sizeof(*m));
1073 ERROR("procevent plugin: unable to allocate metadata: %s",
1074 sstrerror(errno, errbuf, sizeof(errbuf)));
1078 sstrncpy(m->name, "ves", sizeof(m->name));
1079 m->nm_value.nm_string = sstrdup(buf);
1080 m->type = NM_TYPE_STRING;
1083 DEBUG("procevent plugin: notification message: %s",
1084 n.meta->nm_value.nm_string);
1086 DEBUG("procevent plugin: dispatching state %d for PID %d (%s)", (int)value,
1089 plugin_dispatch_notification(&n);
1090 plugin_notification_meta_free(n.meta);
1092 // malloc'd in gen_message_payload
1097 static int procevent_read(void) /* {{{ */
1099 if (procevent_thread_error != 0) {
1101 "procevent plugin: The interface thread had a problem. Restarting it.");
1108 } /* if (procevent_thread_error != 0) */
1110 pthread_mutex_lock(&procevent_lock);
1112 while (ring.head != ring.tail) {
1113 int next = ring.tail + 1;
1115 if (next >= ring.maxLen)
1118 if (ring.buffer[ring.tail][1] == PROCEVENT_EXITED) {
1119 processlist_t *pl = process_map_check(ring.buffer[ring.tail][0], NULL);
1122 // This process is of interest to us, so publish its EXITED status
1123 procevent_dispatch_notification(ring.buffer[ring.tail][0], "gauge",
1124 ring.buffer[ring.tail][1], pl->process,
1125 ring.buffer[ring.tail][3]);
1126 DEBUG("procevent plugin: PID %d (%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 */