summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/in_mem
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/plugins/in_mem')
-rw-r--r--fluent-bit/plugins/in_mem/CMakeLists.txt5
-rw-r--r--fluent-bit/plugins/in_mem/mem.c320
-rw-r--r--fluent-bit/plugins/in_mem/mem.h51
-rw-r--r--fluent-bit/plugins/in_mem/proc.c185
-rw-r--r--fluent-bit/plugins/in_mem/proc.h68
5 files changed, 629 insertions, 0 deletions
diff --git a/fluent-bit/plugins/in_mem/CMakeLists.txt b/fluent-bit/plugins/in_mem/CMakeLists.txt
new file mode 100644
index 00000000..613abd69
--- /dev/null
+++ b/fluent-bit/plugins/in_mem/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(src
+ mem.c
+ proc.c)
+
+FLB_PLUGIN(in_mem "${src}" "")
diff --git a/fluent-bit/plugins/in_mem/mem.c b/fluent-bit/plugins/in_mem/mem.c
new file mode 100644
index 00000000..391ba614
--- /dev/null
+++ b/fluent-bit/plugins/in_mem/mem.c
@@ -0,0 +1,320 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+/* Fluent Bit
+ * ==========
+ * Copyright (C) 2015-2022 The Fluent Bit Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fluent-bit/flb_info.h>
+#include <fluent-bit/flb_input.h>
+#include <fluent-bit/flb_input_plugin.h>
+#include <fluent-bit/flb_log_event.h>
+#include <fluent-bit/flb_kernel.h>
+#include <fluent-bit/flb_pack.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/sysinfo.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "mem.h"
+#include "proc.h"
+
+struct flb_input_plugin in_mem_plugin;
+
+static int in_mem_collect(struct flb_input_instance *i_ins,
+ struct flb_config *config, void *in_context);
+#if 0
+/* Locate a specific key into the buffer */
+static char *field(char *data, char *field)
+{
+ char *p;
+ char *q;
+ char *sep;
+ char *value;
+ int len = strlen(field);
+
+ p = strstr(data, field);
+ if (!p) {
+ return NULL;
+ }
+
+ sep = strchr(p, ':');
+ p = ++sep;
+ p++;
+
+ while (*p == ' ') p++;
+
+ q = strchr(p, ' ');
+ len = q - p;
+ value = flb_malloc(len + 1);
+ strncpy(value, p, len);
+ value[len] = '\0';
+
+ return value;
+}
+#endif
+
+static uint64_t calc_kb(unsigned long amount, unsigned int unit)
+{
+ unsigned long long bytes = amount;
+
+ /*
+ * Recent Linux versions return memory/swap sizes as multiples
+ * of a certain size unit. See sysinfo(2) for details.
+ */
+ if (unit > 1) {
+ bytes = bytes * unit;
+ }
+
+ bytes = bytes / 1024;
+
+ return (uint64_t) bytes;
+}
+
+static int mem_calc(struct flb_in_mem_info *m_info)
+{
+ int ret;
+ struct sysinfo info;
+
+ ret = sysinfo(&info);
+ if (ret == -1) {
+ flb_errno();
+ return -1;
+ }
+
+ /* set values in KBs */
+ m_info->mem_total = calc_kb(info.totalram, info.mem_unit);
+
+ /*
+ * This value seems to be MemAvailable if it is supported
+ * or MemFree on legacy Linux.
+ */
+ m_info->mem_free = calc_kb(info.freeram, info.mem_unit);
+
+ m_info->mem_used = m_info->mem_total - m_info->mem_free;
+
+ m_info->swap_total = calc_kb(info.totalswap, info.mem_unit);
+ m_info->swap_free = calc_kb(info.freeswap, info.mem_unit);
+ m_info->swap_used = m_info->swap_total - m_info->swap_free;
+
+ return 0;
+}
+
+static int in_mem_init(struct flb_input_instance *in,
+ struct flb_config *config, void *data)
+{
+ int ret;
+ struct flb_in_mem_config *ctx;
+ (void) data;
+
+ /* Initialize context */
+ ctx = flb_malloc(sizeof(struct flb_in_mem_config));
+ if (!ctx) {
+ return -1;
+ }
+ ctx->idx = 0;
+ ctx->pid = 0;
+ ctx->page_size = sysconf(_SC_PAGESIZE);
+ ctx->ins = in;
+
+ /* Load the config map */
+ ret = flb_input_config_map_set(in, (void *)ctx);
+ if (ret == -1) {
+ flb_free(ctx);
+ return -1;
+ }
+
+ /* Collection time setting */
+ if (ctx->interval_sec <= 0) {
+ ctx->interval_sec = atoi(DEFAULT_INTERVAL_SEC);
+ }
+ if (ctx->interval_nsec <= 0) {
+ ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC);
+ }
+
+ /* Set the context */
+ flb_input_set_context(in, ctx);
+
+ /* Set the collector */
+ ret = flb_input_set_collector_time(in,
+ in_mem_collect,
+ ctx->interval_sec,
+ ctx->interval_nsec,
+ config);
+ if (ret == -1) {
+ flb_plg_error(ctx->ins, "could not set collector for memory input plugin");
+ return -1;
+ }
+
+ ret = flb_log_event_encoder_init(&ctx->log_encoder,
+ FLB_LOG_EVENT_FORMAT_DEFAULT);
+
+ if (ret != FLB_EVENT_ENCODER_SUCCESS) {
+ flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+static int in_mem_collect(struct flb_input_instance *i_ins,
+ struct flb_config *config, void *in_context)
+{
+ int ret;
+ struct proc_task *task = NULL;
+ struct flb_in_mem_config *ctx = in_context;
+ struct flb_in_mem_info info;
+
+ if (ctx->pid) {
+ task = proc_stat(ctx->pid, ctx->page_size);
+ if (!task) {
+ flb_plg_warn(ctx->ins, "could not measure PID %i", ctx->pid);
+ ctx->pid = 0;
+ }
+ }
+
+ ret = mem_calc(&info);
+
+ if (ret == -1) {
+ if (task) {
+ proc_free(task);
+ }
+ return -1;
+ }
+
+ ret = flb_log_event_encoder_begin_record(&ctx->log_encoder);
+
+ if (ret == FLB_EVENT_ENCODER_SUCCESS) {
+ ret = flb_log_event_encoder_set_current_timestamp(
+ &ctx->log_encoder);
+ }
+
+ if (ret == FLB_EVENT_ENCODER_SUCCESS) {
+ ret = flb_log_event_encoder_append_body_values(
+ &ctx->log_encoder,
+ FLB_LOG_EVENT_CSTRING_VALUE("Mem.total"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.mem_total),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("Mem.used"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.mem_used),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("Mem.free"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.mem_free),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("Swap.total"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.swap_total),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("Swap.used"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.swap_used),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("Swap.free"),
+ FLB_LOG_EVENT_UINT64_VALUE(info.swap_free));
+ }
+
+ if (task != NULL &&
+ ret == FLB_EVENT_ENCODER_SUCCESS) {
+ /* RSS bytes */
+
+ ret = flb_log_event_encoder_append_body_values(
+ &ctx->log_encoder,
+ FLB_LOG_EVENT_CSTRING_VALUE("proc_bytes"),
+ FLB_LOG_EVENT_UINT64_VALUE(task->proc_rss),
+
+ FLB_LOG_EVENT_CSTRING_VALUE("proc_hr"),
+ FLB_LOG_EVENT_UINT64_VALUE(task->proc_rss_hr));
+
+ proc_free(task);
+ }
+
+ flb_plg_trace(ctx->ins, "memory total=%lu kb, used=%lu kb, free=%lu kb",
+ info.mem_total, info.mem_used, info.mem_free);
+ flb_plg_trace(ctx->ins, "swap total=%lu kb, used=%lu kb, free=%lu kb",
+ info.swap_total, info.swap_used, info.swap_free);
+ ++ctx->idx;
+
+ if (ret == FLB_EVENT_ENCODER_SUCCESS) {
+ ret = flb_log_event_encoder_commit_record(&ctx->log_encoder);
+ }
+
+ if (ret == FLB_EVENT_ENCODER_SUCCESS) {
+ flb_input_log_append(i_ins, NULL, 0,
+ ctx->log_encoder.output_buffer,
+ ctx->log_encoder.output_length);
+
+ ret = 0;
+ }
+ else {
+ flb_plg_error(i_ins, "Error encoding record : %d", ret);
+
+ ret = -1;
+ }
+
+ flb_log_event_encoder_reset(&ctx->log_encoder);
+
+ return 0;
+}
+
+static int in_mem_exit(void *data, struct flb_config *config)
+{
+ (void) *config;
+ struct flb_in_mem_config *ctx = data;
+
+ if (!ctx) {
+ return 0;
+ }
+
+ flb_log_event_encoder_destroy(&ctx->log_encoder);
+
+ /* done */
+ flb_free(ctx);
+
+ return 0;
+}
+
+static struct flb_config_map config_map[] = {
+ {
+ FLB_CONFIG_MAP_INT, "interval_sec", DEFAULT_INTERVAL_SEC,
+ 0, FLB_TRUE, offsetof(struct flb_in_mem_config, interval_sec),
+ "Set the collector interval"
+ },
+ {
+ FLB_CONFIG_MAP_INT, "interval_nsec", DEFAULT_INTERVAL_NSEC,
+ 0, FLB_TRUE, offsetof(struct flb_in_mem_config, interval_nsec),
+ "Set the collector interval (subseconds)"
+ },
+ {
+ FLB_CONFIG_MAP_INT, "pid", "0",
+ 0, FLB_TRUE, offsetof(struct flb_in_mem_config, pid),
+ "Set the PID of the process to measure"
+ },
+ /* EOF */
+ {0}
+};
+
+struct flb_input_plugin in_mem_plugin = {
+ .name = "mem",
+ .description = "Memory Usage",
+ .cb_init = in_mem_init,
+ .cb_pre_run = NULL,
+ .cb_collect = in_mem_collect,
+ .cb_flush_buf = NULL,
+ .cb_exit = in_mem_exit,
+ .config_map = config_map
+};
diff --git a/fluent-bit/plugins/in_mem/mem.h b/fluent-bit/plugins/in_mem/mem.h
new file mode 100644
index 00000000..3c28ff90
--- /dev/null
+++ b/fluent-bit/plugins/in_mem/mem.h
@@ -0,0 +1,51 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+/* Fluent Bit
+ * ==========
+ * Copyright (C) 2015-2022 The Fluent Bit Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FLB_IN_MEM_H
+#define FLB_IN_MEM_H
+
+#include <fluent-bit/flb_input.h>
+#include <fluent-bit/flb_config.h>
+#include <fluent-bit/flb_utils.h>
+#include <fluent-bit/flb_log_event_encoder.h>
+#include <msgpack.h>
+
+#define DEFAULT_INTERVAL_SEC "1"
+#define DEFAULT_INTERVAL_NSEC "0"
+
+struct flb_in_mem_info {
+ uint64_t mem_total;
+ uint64_t mem_used;
+ uint64_t mem_free;
+ uint64_t swap_total;
+ uint64_t swap_used;
+ uint64_t swap_free;
+};
+
+struct flb_in_mem_config {
+ int idx;
+ int page_size;
+ int interval_sec;
+ int interval_nsec;
+ pid_t pid;
+ struct flb_input_instance *ins;
+ struct flb_log_event_encoder log_encoder;
+};
+
+#endif
diff --git a/fluent-bit/plugins/in_mem/proc.c b/fluent-bit/plugins/in_mem/proc.c
new file mode 100644
index 00000000..0e70b9de
--- /dev/null
+++ b/fluent-bit/plugins/in_mem/proc.c
@@ -0,0 +1,185 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+/* Fluent Bit
+ * ==========
+ * Copyright (C) 2015-2022 The Fluent Bit Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <fluent-bit/flb_input.h>
+#include "proc.h"
+
+static char *human_readable_size(long size)
+{
+ long u = 1024, i, len = 128;
+ char *buf;
+ static const char *__units[] = { "b", "K", "M", "G",
+ "T", "P", "E", "Z", "Y", NULL
+ };
+
+ buf = flb_malloc(len);
+ if (!buf) {
+ flb_errno();
+ return NULL;
+ }
+
+ for (i = 0; __units[i] != NULL; i++) {
+ if ((size / u) == 0) {
+ break;
+ }
+ u *= 1024;
+ }
+ if (!i) {
+ snprintf(buf, len, "%ld %s", size, __units[0]);
+ }
+ else {
+ float fsize = (float) ((double) size / (u / 1024));
+ snprintf(buf, len, "%.2f%s", fsize, __units[i]);
+ }
+
+ return buf;
+}
+
+/* Read file content into a memory buffer */
+static char *file_to_buffer(const char *path)
+{
+ FILE *fp;
+ char *buffer;
+
+ if (!(fp = fopen(path, "r"))) {
+ flb_errno();
+ return NULL;
+ }
+
+ buffer = flb_calloc(1, PROC_STAT_BUF_SIZE);
+ if (!buffer) {
+ fclose(fp);
+ flb_errno();
+ return NULL;
+ }
+
+ fread(buffer, PROC_STAT_BUF_SIZE, 1, fp);
+ if (ferror(fp) || !feof(fp)) {
+ flb_free(buffer);
+ fclose(fp);
+ return NULL;
+ }
+
+ fclose(fp);
+ return buffer;
+}
+
+
+struct proc_task *proc_stat(pid_t pid, int page_size)
+{
+ int ret;
+ char *p, *q;
+ char *buf;
+ char pid_path[PROC_PID_SIZE];
+ struct proc_task *t;
+
+ t = flb_calloc(1, sizeof(struct proc_task));
+ if (!t) {
+ flb_errno();
+ return NULL;
+ }
+
+ /* Compose path for /proc/PID/stat */
+ ret = snprintf(pid_path, PROC_PID_SIZE, "/proc/%i/stat", pid);
+ if (ret < 0) {
+ flb_free(t);
+ flb_errno();
+ return NULL;
+ }
+
+ buf = file_to_buffer(pid_path);
+ if (!buf) {
+ flb_free(t);
+ return NULL;
+ }
+
+ sscanf(buf, "%d", &t->pid);
+
+ /*
+ * workaround for process with spaces in the name, so we dont screw up
+ * sscanf(3).
+ */
+ p = buf;
+ while (*p != '(') {
+ p++;
+ }
+ p++;
+
+ /* seek from tail of file. */
+ q = buf + (PROC_STAT_BUF_SIZE - 1);
+ while (*q != ')' && p < q) {
+ q--;
+ }
+ if (p >= q) {
+ flb_free(buf);
+ flb_free(t);
+ return NULL;
+ }
+
+ strncpy(t->comm, p, q - p);
+ q += 2;
+
+ /* Read pending values */
+ sscanf(q, PROC_STAT_FORMAT,
+ &t->state,
+ &t->ppid,
+ &t->pgrp,
+ &t->session,
+ &t->tty_nr,
+ &t->tpgid,
+ &t->flags,
+ &t->minflt,
+ &t->cminflt,
+ &t->majflt,
+ &t->cmajflt,
+ &t->utime,
+ &t->stime,
+ &t->cutime,
+ &t->cstime,
+ &t->priority,
+ &t->nice,
+ &t->num_threads,
+ &t->itrealvalue,
+ &t->starttime,
+ &t->vsize,
+ &t->rss);
+
+ /* Internal conversion */
+ t->proc_rss = (t->rss * page_size);
+ t->proc_rss_hr = human_readable_size(t->proc_rss);
+ if ( t->proc_rss_hr == NULL ) {
+ flb_free(buf);
+ flb_free(t);
+ return NULL;
+ }
+
+ flb_free(buf);
+ return t;
+}
+
+void proc_free(struct proc_task *t)
+{
+ flb_free(t->proc_rss_hr);
+ flb_free(t);
+}
diff --git a/fluent-bit/plugins/in_mem/proc.h b/fluent-bit/plugins/in_mem/proc.h
new file mode 100644
index 00000000..79b4c3b8
--- /dev/null
+++ b/fluent-bit/plugins/in_mem/proc.h
@@ -0,0 +1,68 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+/* Fluent Bit
+ * ==========
+ * Copyright (C) 2015-2022 The Fluent Bit Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef IN_MEM_PROC_H
+#define IN_MEM_PROC_H
+
+#define PROC_PID_SIZE 1024
+#define PROC_STAT_BUF_SIZE 1024
+
+/*
+ * This 'stat' format omits the first two fields, due to the nature
+ * of sscanf(3) and whitespaces, programs with spaces in the name can
+ * screw up when scanning the information.
+ */
+#define PROC_STAT_FORMAT "%c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld"
+
+/* Our tast struct to read the /proc/PID/stat values */
+struct proc_task {
+ int pid; /* %d */
+ char comm[256]; /* %s */
+ char state; /* %c */
+ int ppid; /* %d */
+ int pgrp; /* %d */
+ int session; /* %d */
+ int tty_nr; /* %d */
+ int tpgid; /* %d */
+ unsigned int flags; /* %u */
+ unsigned long minflt; /* %lu */
+ unsigned long cminflt; /* %lu */
+ unsigned long majflt; /* %lu */
+ unsigned long cmajflt; /* %lu */
+ unsigned long utime; /* %lu */
+ unsigned long stime; /* %lu */
+ long cutime; /* %ld */
+ long cstime; /* %ld */
+ long priority; /* %ld */
+ long nice; /* %ld */
+ long num_threads; /* %ld */
+ long itrealvalue; /* %ld */
+ unsigned long long starttime; /* %llu */
+ unsigned long vsize; /* %lu */
+ long rss; /* %ld */
+
+ /* Internal conversion */
+ long proc_rss; /* bytes = (rss * PAGESIZE) */
+ char *proc_rss_hr; /* RSS in human readable format */
+};
+
+struct proc_task *proc_stat(pid_t pid, int page_size);
+void proc_free(struct proc_task *t);
+
+#endif