Skip to content
5 changes: 4 additions & 1 deletion plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
REGISTER_IN_PLUGIN("in_netif")
REGISTER_IN_PLUGIN("in_docker")
REGISTER_IN_PLUGIN("in_docker_events")
REGISTER_IN_PLUGIN("in_node_exporter_metrics")
REGISTER_IN_PLUGIN("in_podman_metrics")
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
REGISTER_IN_PLUGIN("in_node_exporter_metrics")
endif()

REGISTER_IN_PLUGIN("in_kubernetes_events")
REGISTER_IN_PLUGIN("in_kafka")
REGISTER_IN_PLUGIN("in_fluentbit_metrics")
Expand Down
6 changes: 5 additions & 1 deletion plugins/in_node_exporter_metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ set(src
)
endif()

FLB_PLUGIN(in_node_exporter_metrics "${src}" "")
if (APPLE)
FLB_PLUGIN(in_node_exporter_metrics "${src}" "-framework Foundation -framework IOKit")
else()
FLB_PLUGIN(in_node_exporter_metrics "${src}" "")
endif()
2 changes: 1 addition & 1 deletion plugins/in_node_exporter_metrics/ne.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ static struct flb_config_map config_map[] = {

{
FLB_CONFIG_MAP_CLIST, "metrics",
"cpu,cpufreq,meminfo,diskstats,filesystem,uname,stat,time,loadavg,vmstat,netdev,filefd,systemd",
NE_DEFAULT_ENABLED_METRICS,
0, FLB_TRUE, offsetof(struct flb_ne, metrics),
"Comma separated list of keys to enable metrics."
},
Expand Down
39 changes: 39 additions & 0 deletions plugins/in_node_exporter_metrics/ne.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
#include <fluent-bit/flb_hash_table.h>
#include <fluent-bit/flb_metrics.h>

/* Default enabled metrics */

#ifdef __linux__
#define NE_DEFAULT_ENABLED_METRICS "cpu,cpufreq,meminfo,diskstats,filesystem,uname,stat,time,loadavg,vmstat,netdev,filefd,systemd"
#elif __APPLE__
#define NE_DEFAULT_ENABLED_METRICS "cpu,loadavg,meminfo,diskstats,uname,netdev"
#endif

/* filesystem: regex for ignoring mount points and filesystem types */

#define IGNORED_MOUNT_POINTS "^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+|var/lib/containers/storage/.+)($|/)"
Expand Down Expand Up @@ -108,6 +116,22 @@ struct flb_ne {
/* meminfo hash table */
struct flb_hash_table *meminfo_ht;

#ifdef FLB_SYSTEM_MACOS
/* meminfo_darwin */
struct cmt_gauge *darwin_free_bytes;
struct cmt_gauge *darwin_active_bytes;
struct cmt_gauge *darwin_compressed_bytes;
struct cmt_gauge *darwin_inactive_bytes;
struct cmt_gauge *darwin_wired_bytes;
struct cmt_gauge *darwin_internal_bytes;
struct cmt_gauge *darwin_purgeable_bytes;
struct cmt_counter *darwin_pageins_bytes;
struct cmt_counter *darwin_pageouts_bytes;
struct cmt_gauge *darwin_swap_used_bytes;
struct cmt_gauge *darwin_swap_total_bytes;
struct cmt_counter *darwin_total_bytes;
#endif

/* diskstats: abbreviation 'dt' */
void *dt_metrics;
struct flb_regex *dt_regex_skip_devices;
Expand All @@ -131,6 +155,21 @@ struct flb_ne {
/* netdev */
struct flb_hash_table *netdev_ht;

#ifdef FLB_SYSTEM_MACOS
/* netdev_darwin */
struct cmt_gauge *darwin_receive_packets;
struct cmt_gauge *darwin_transmit_packets;
struct cmt_gauge *darwin_receive_bytes;
struct cmt_gauge *darwin_transmit_bytes;
struct cmt_gauge *darwin_receive_errors;
struct cmt_gauge *darwin_transmit_errors;
struct cmt_gauge *darwin_receive_dropped;
struct cmt_gauge *darwin_receive_multicast;
struct cmt_gauge *darwin_transmit_multicast;
struct cmt_gauge *darwin_collisions;
struct cmt_gauge *darwin_noproto;
#endif

/* time */
struct cmt_gauge *time;

Expand Down
2 changes: 2 additions & 0 deletions plugins/in_node_exporter_metrics/ne_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
#ifdef __linux__
#include "ne_cpu_linux.c"
#include "ne_cpufreq_linux.c"
#elif __APPLE__
#include "ne_cpu_darwin.c"
#endif
34 changes: 34 additions & 0 deletions plugins/in_node_exporter_metrics/ne_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,41 @@

#include "ne.h"

#ifdef __linux__
int ne_cpu_init(struct flb_ne *ctx);
int ne_cpu_update(struct flb_ne *ctx);
#elif __APPLE__
int ne_cpu_init(struct flb_ne *ctx);
int ne_cpu_update(struct flb_ne *ctx);
static int ne_cpufreq_init(struct flb_ne *ctx)
{
return 0;
}

static int ne_cpufreq_update(struct flb_ne *ctx)
{
return 0;
}
#else
static int ne_cpu_init(struct flb_ne *ctx)
{
return 0;
}

static int ne_cpu_update(struct flb_ne *ctx)
{
return 0;
}

static int ne_cpufreq_init(struct flb_ne *ctx)
{
return 0;
}

static int ne_cpufreq_update(struct flb_ne *ctx)
{
return 0;
}
#endif

#endif
133 changes: 133 additions & 0 deletions plugins/in_node_exporter_metrics/ne_cpu_darwin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 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_plugin.h>

#include "ne.h"
#include "ne_utils.h"

#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/sysctl.h>
#include <sys/mount.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
#include <TargetConditionals.h>
#if TARGET_OS_MAC
#include <libproc.h>
#endif
#include <mach/processor_info.h>
#include <mach/vm_map.h>

/*
* CPU stats from host_processor_info()
*
* https://developer.apple.com/documentation/kernel/1502854-host_processor_info
* ---------------------------
*/
static inline int cpu_stat_init(struct flb_ne *ctx)
{
struct cmt_counter *c;

c = cmt_counter_create(ctx->cmt, "node", "cpu", "seconds_total",
"Seconds the CPUs spent in each mode.",
2, (char *[]) {"cpu", "mode"});
if (!c) {
return -1;
}
ctx->cpu_seconds = c;

return 0;
}

int ne_cpu_init(struct flb_ne *ctx)
{
int ret;

/* CPU Stats */
ret = cpu_stat_init(ctx);
if (ret == -1) {
flb_plg_error(ctx->ins, "could not initialize cpu_stat metrics");
return -1;
}

return 0;
}

static int cpu_stat_update(struct flb_ne *ctx, uint64_t ts)
{
int i;
mach_msg_type_number_t count;
processor_cpu_load_info_t pinfo;
natural_t ncpu;
kern_return_t err;
char cpu_id[8];
double clock_per_sec = CLK_TCK;

err = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO,
&ncpu,
(processor_info_array_t *)(&pinfo),
&count);

if (err != KERN_SUCCESS) {
flb_plg_error(ctx->ins, "host_processor_info() is failed with error = %d", err);
return -1;
}

for (i = 0; i < ncpu; i++) {
snprintf(cpu_id, sizeof(cpu_id), "%d", i);

/* CPU seconds */
cmt_counter_set(ctx->cpu_seconds, ts,
pinfo[i].cpu_ticks[CPU_STATE_USER]/clock_per_sec,
2, (char *[]) {cpu_id, "user"});

cmt_counter_set(ctx->cpu_seconds, ts,
pinfo[i].cpu_ticks[CPU_STATE_SYSTEM]/clock_per_sec,
2, (char *[]) {cpu_id, "system"});

cmt_counter_set(ctx->cpu_seconds, ts,
pinfo[i].cpu_ticks[CPU_STATE_NICE]/clock_per_sec,
2, (char *[]) {cpu_id, "nice"});

cmt_counter_set(ctx->cpu_seconds, ts,
pinfo[i].cpu_ticks[CPU_STATE_IDLE]/clock_per_sec,
2, (char *[]) {cpu_id, "idle"});
}

vm_deallocate(mach_task_self(), (vm_address_t)pinfo,
(vm_size_t)sizeof(*pinfo) * ncpu);

return 0;
}

int ne_cpu_update(struct flb_ne *ctx)
{
uint64_t ts;

ts = cfl_time_now();

cpu_stat_update(ctx, ts);

return 0;
}
2 changes: 2 additions & 0 deletions plugins/in_node_exporter_metrics/ne_diskstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@

#ifdef __linux__
#include "ne_diskstats_linux.c"
#elif __APPLE__
#include "ne_diskstats_darwin.c"
#endif
17 changes: 17 additions & 0 deletions plugins/in_node_exporter_metrics/ne_diskstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@

#include "ne.h"

#if defined(__linux__) || defined(__APPLE__)
int ne_diskstats_init(struct flb_ne *ctx);
int ne_diskstats_update(struct flb_ne *ctx);
int ne_diskstats_exit(struct flb_ne *ctx);
#else
static int ne_diskstats_init(struct flb_ne *ctx)
{
return 0;
}

static int ne_diskstats_update(struct flb_ne *ctx)
{
return 0;
}

static int ne_diskstats_exit(struct flb_ne *ctx)
{
return 0;
}
#endif

#endif
Loading