From 032baba6b50ad02ae955e9ba4e08cd2181855bc1 Mon Sep 17 00:00:00 2001 From: Masaya YAMAMOTO Date: Tue, 2 Jun 2015 17:07:25 +0900 Subject: [PATCH] in_mem: new Memory usage Input collector. Signed-off-by: Masaya YAMAMOTO --- CMakeLists.txt | 2 + plugins/CMakeLists.txt | 1 + plugins/in_mem/CMakeLists.txt | 4 ++ plugins/in_mem/in_mem.c | 121 ++++++++++++++++++++++++++++++++++ plugins/in_mem/in_mem.h | 41 ++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 plugins/in_mem/CMakeLists.txt create mode 100644 plugins/in_mem/in_mem.c create mode 100644 plugins/in_mem/in_mem.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f962a806b45..49602324ee2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ option(WITHOUT_BIN "Do not build executable" No) # Build Plugins option(WITH_IN_XBEE "Enable XBee input plugin" No) option(WITH_IN_CPU "Enable CPU input plugin" Yes) +option(WITH_IN_MEM "Enable Memory input plugin" Yes) option(WITH_IN_KMSG "Enable Kernel log input plugin" Yes) option(WITH_OUT_FLUENTD "Enable Fluentd output plugin" Yes) option(WITH_OUT_TD "Enable Treasure Data output plugin" Yes) @@ -27,6 +28,7 @@ if(WITH_ALL) set(WITH_SSL_TLS 1) set(WITH_IN_XBEE 1) set(WITH_IN_CPU 1) + set(WITH_IN_MEM 1) set(WITH_OUT_FLUENTD 1) set(WITH_OUT_TD 1) endif() diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index c26691a4632..23c4385aec9 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -33,6 +33,7 @@ endmacro() # Try to register each plugin REGISTER_IN_PLUGIN("in_xbee") REGISTER_IN_PLUGIN("in_cpu") +REGISTER_IN_PLUGIN("in_mem") REGISTER_IN_PLUGIN("in_kmsg") REGISTER_OUT_PLUGIN("out_fluentd") REGISTER_OUT_PLUGIN("out_td") diff --git a/plugins/in_mem/CMakeLists.txt b/plugins/in_mem/CMakeLists.txt new file mode 100644 index 00000000000..9d4b87a4f29 --- /dev/null +++ b/plugins/in_mem/CMakeLists.txt @@ -0,0 +1,4 @@ +set(src + in_mem.c) + +FLB_PLUGIN(in_mem "${src}" "") diff --git a/plugins/in_mem/in_mem.c b/plugins/in_mem/in_mem.c new file mode 100644 index 00000000000..f43f3316253 --- /dev/null +++ b/plugins/in_mem/in_mem.c @@ -0,0 +1,121 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015 Treasure Data Inc. + * + * 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 +#include +#include +#include +#include + +#include "in_mem.h" + +int in_mem_collect(struct flb_config *config, void *in_context); + +int in_mem_init(struct flb_config *config) +{ + int ret; + struct flb_in_mem_config *ctx; + + ctx = malloc(sizeof(struct flb_in_mem_config)); + if (!ctx) { + return -1; + } + ctx->idx = 0; + msgpack_sbuffer_init(&ctx->sbuf); + msgpack_packer_init(&ctx->pckr, &ctx->sbuf, msgpack_sbuffer_write); + ret = flb_input_set_context("mem", ctx, config); + if (ret == -1) { + flb_utils_error_c("Could not set configuration for " + "memory input plugin"); + } + ret = flb_input_set_collector_time("mem", + in_mem_collect, + IN_MEM_COLLECT_SEC, + IN_MEM_COLLECT_NSEC, + config); + if (ret == -1) { + flb_utils_error_c("Could not set collector for memory input plugin"); + } + return 0; +} + +int in_mem_pre_run(void *in_context, struct flb_config *config) +{ + struct flb_in_mem_config *ctx = in_context; + + ctx->tag_len = snprintf(ctx->tag, sizeof(ctx->tag), "%s.mem", config->tag); + if (ctx->tag_len == -1) { + flb_utils_error_c("Could not set custom tag on memory input plugin"); + } + return 0; +} + +int in_mem_collect(struct flb_config *config, void *in_context) +{ + struct sysinfo info; + (void) config; + struct flb_in_mem_config *ctx = in_context; + uint32_t totalram, freeram; + + sysinfo(&info); + totalram = info.totalram / 1024; + freeram = info.freeram / 1024; + msgpack_pack_map(&ctx->pckr, 3); + msgpack_pack_raw(&ctx->pckr, 4); + msgpack_pack_raw_body(&ctx->pckr, "time", 4); + msgpack_pack_uint64(&ctx->pckr, time(NULL)); + msgpack_pack_raw(&ctx->pckr, 5); + msgpack_pack_raw_body(&ctx->pckr, "total", 5); + msgpack_pack_uint32(&ctx->pckr, totalram); + msgpack_pack_raw(&ctx->pckr, 4); + msgpack_pack_raw_body(&ctx->pckr, "free", 4); + msgpack_pack_uint32(&ctx->pckr, freeram); + flb_debug("[in_mem] memory total %d kb, free %d kb (buffer=%i)", + info.totalram, + info.freeram, + ctx->idx); + ++ctx->idx; + return 0; +} + +void *in_mem_flush(void *in_context, int *size) +{ + char *buf; + struct flb_in_mem_config *ctx = in_context; + + buf = malloc(ctx->sbuf.size); + if (!buf) { + return NULL; + } + memcpy(buf, ctx->sbuf.data, ctx->sbuf.size); + msgpack_sbuffer_destroy(&ctx->sbuf); + msgpack_sbuffer_init(&ctx->sbuf); + msgpack_packer_init(&ctx->pckr, &ctx->sbuf, msgpack_sbuffer_write); + ctx->idx = 0; + return buf; +} + +struct flb_input_plugin in_mem_plugin = { + .name = "mem", + .description = "Memory Usage", + .cb_init = in_mem_init, + .cb_pre_run = in_mem_pre_run, + .cb_collect = in_mem_collect, + .cb_flush_buf = in_mem_flush +}; diff --git a/plugins/in_mem/in_mem.h b/plugins/in_mem/in_mem.h new file mode 100644 index 00000000000..5f0756a6ec5 --- /dev/null +++ b/plugins/in_mem/in_mem.h @@ -0,0 +1,41 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015 Treasure Data Inc. + * + * 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 +#include +#include +#include + +#define IN_MEM_COLLECT_SEC 1 +#define IN_MEM_COLLECT_NSEC 0 + +struct flb_in_mem_config { + int tag_len; + char tag[32]; + int idx; + msgpack_packer pckr; + msgpack_sbuffer sbuf; +}; + +extern struct flb_input_plugin in_mem_plugin; + +#endif