bool proc_stat(pid_t pid, proc *pptr) { unsigned long long start_jiffies; char path[40], buffer[1024], *start; int stat_fd, num; snprintf(path, sizeof (path), "/proc/%d/stat", pid); stat_fd = open(path, O_RDONLY); if (stat_fd < 0) return false; num = read(stat_fd, buffer, sizeof (buffer)-1); close(stat_fd); if (num < 0) return false; buffer[num] = '\0'; start = strrchr(buffer, ')')+2; if (start == NULL) return false; num = sscanf(start, "%c " // state "%d %*d %*d %*d %*d " // ppid "%*u %*lu %*lu %*lu %*lu " // flags "%lu %*lu %*ld %*ld " // utime "%*ld %*ld %*ld %*ld " // priority "%llu", // starttime &(pptr->state), &(pptr->ppid), &(pptr->utime), &start_jiffies); if (num != 4) return false; pptr->utime = jiffy_to_sec(pptr->utime); pptr->start_time = boot_time() + jiffy_to_sec(start_jiffies); return true; }
/* Init kmsg input */ int in_kmsg_init(struct flb_config *config) { int fd; int ret; struct flb_in_kmsg_config *ctx; ctx = calloc(1, sizeof(struct flb_in_kmsg_config)); if (!ctx) { perror("calloc"); return -1; } /* open device */ fd = open(FLB_KMSG_DEV, O_RDONLY); if (fd == -1) { perror("open"); flb_utils_error_c("Could not open kernel log buffer on kmsg plugin"); } ctx->fd = fd; /* get the system boot time */ ret = boot_time(&ctx->boot_time); if (ret == -1) { flb_utils_error_c("Could not get system boot time for kmsg input plugin"); } /* set context */ ret = flb_input_set_context("kmsg", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for kmsg input plugin"); } /* Set our collector based on a file descriptor event */ ret = flb_input_set_collector_event("kmsg", in_kmsg_collect, ctx->fd, config); if (ret == -1) { flb_utils_error_c("Could not set collector for kmsg input plugin"); } return 0; }