|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2015-2026 The Fluent Bit Authors |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <fluent-bit/flb_info.h> |
| 21 | +#include <fluent-bit/flb_input_plugin.h> |
| 22 | +#include <sys/param.h> |
| 23 | +#include <sys/mount.h> |
| 24 | + |
| 25 | +#include "ne.h" |
| 26 | + |
| 27 | +static int filesystem_update(struct flb_ne *ctx) |
| 28 | +{ |
| 29 | + int i; |
| 30 | + int count; |
| 31 | + int skip_flag; |
| 32 | + uint64_t block_size; |
| 33 | + uint64_t blocks; |
| 34 | + uint64_t free_size; |
| 35 | + uint64_t avail_size; |
| 36 | + uint64_t size_bytes; |
| 37 | + uint64_t avail_bytes; |
| 38 | + uint64_t free_bytes; |
| 39 | + uint64_t timestamp; |
| 40 | + char *labels[3]; |
| 41 | + struct statfs *mounts; |
| 42 | + |
| 43 | + count = getmntinfo(&mounts, MNT_NOWAIT); |
| 44 | + if (count == 0) { |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + timestamp = cfl_time_now(); |
| 49 | + |
| 50 | + for (i = 0; i < count; i++) { |
| 51 | + skip_flag = flb_regex_match(ctx->fs_regex_skip_fs_types, |
| 52 | + (unsigned char *) mounts[i].f_fstypename, |
| 53 | + strlen(mounts[i].f_fstypename)); |
| 54 | + if (skip_flag) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + skip_flag = flb_regex_match(ctx->fs_regex_skip_mount, |
| 59 | + (unsigned char *) mounts[i].f_mntonname, |
| 60 | + strlen(mounts[i].f_mntonname)); |
| 61 | + if (skip_flag) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + labels[0] = mounts[i].f_mntfromname; |
| 66 | + labels[1] = mounts[i].f_fstypename; |
| 67 | + labels[2] = mounts[i].f_mntonname; |
| 68 | + |
| 69 | + block_size = (uint64_t) mounts[i].f_bsize; |
| 70 | + blocks = (uint64_t) mounts[i].f_blocks; |
| 71 | + free_size = (uint64_t) mounts[i].f_bfree; |
| 72 | + avail_size = (uint64_t) mounts[i].f_bavail; |
| 73 | + avail_bytes = block_size * avail_size; |
| 74 | + size_bytes = block_size * blocks; |
| 75 | + free_bytes = block_size * free_size; |
| 76 | + |
| 77 | + cmt_gauge_set(ctx->fs_avail_bytes, timestamp, avail_bytes, 3, labels); |
| 78 | + cmt_gauge_set(ctx->fs_device_error, timestamp, 0, 3, labels); |
| 79 | + cmt_gauge_set(ctx->fs_files, timestamp, (uint64_t) mounts[i].f_files, 3, labels); |
| 80 | + cmt_gauge_set(ctx->fs_files_free, timestamp, (uint64_t) mounts[i].f_ffree, 3, labels); |
| 81 | + cmt_gauge_set(ctx->fs_free_bytes, timestamp, free_bytes, 3, labels); |
| 82 | + cmt_gauge_set(ctx->fs_readonly, timestamp, (mounts[i].f_flags & MNT_RDONLY) ? 1 : 0, 3, labels); |
| 83 | + cmt_gauge_set(ctx->fs_size_bytes, timestamp, size_bytes, 3, labels); |
| 84 | + } |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +static int ne_filesystem_init(struct flb_ne *ctx) |
| 90 | +{ |
| 91 | + ctx->fs_regex_skip_mount = flb_regex_create(ctx->fs_regex_ingore_mount_point_text); |
| 92 | + ctx->fs_regex_skip_fs_types = flb_regex_create(ctx->fs_regex_ingore_filesystem_type_text); |
| 93 | + |
| 94 | + ctx->fs_avail_bytes = cmt_gauge_create(ctx->cmt, "node", "filesystem", "avail_bytes", |
| 95 | + "Filesystem space available to non-root users in bytes.", |
| 96 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 97 | + if (ctx->fs_avail_bytes == NULL) { |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + ctx->fs_device_error = cmt_gauge_create(ctx->cmt, "node", "filesystem", "device_error", |
| 102 | + "Whether an error occurred while getting statistics for the given device.", |
| 103 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 104 | + if (ctx->fs_device_error == NULL) { |
| 105 | + return -1; |
| 106 | + } |
| 107 | + |
| 108 | + ctx->fs_files = cmt_gauge_create(ctx->cmt, "node", "filesystem", "files", |
| 109 | + "Filesystem total file nodes.", |
| 110 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 111 | + if (ctx->fs_files == NULL) { |
| 112 | + return -1; |
| 113 | + } |
| 114 | + |
| 115 | + ctx->fs_files_free = cmt_gauge_create(ctx->cmt, "node", "filesystem", "files_free", |
| 116 | + "Filesystem total free file nodes.", |
| 117 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 118 | + if (ctx->fs_files_free == NULL) { |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + ctx->fs_free_bytes = cmt_gauge_create(ctx->cmt, "node", "filesystem", "free_bytes", |
| 123 | + "Filesystem free space in bytes.", |
| 124 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 125 | + if (ctx->fs_free_bytes == NULL) { |
| 126 | + return -1; |
| 127 | + } |
| 128 | + |
| 129 | + ctx->fs_readonly = cmt_gauge_create(ctx->cmt, "node", "filesystem", "readonly", |
| 130 | + "Filesystem read-only status.", |
| 131 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 132 | + if (ctx->fs_readonly == NULL) { |
| 133 | + return -1; |
| 134 | + } |
| 135 | + |
| 136 | + ctx->fs_size_bytes = cmt_gauge_create(ctx->cmt, "node", "filesystem", "size_bytes", |
| 137 | + "Filesystem size in bytes.", |
| 138 | + 3, (char *[]) {"device", "fstype", "mountpoint"}); |
| 139 | + if (ctx->fs_size_bytes == NULL) { |
| 140 | + return -1; |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static int ne_filesystem_update(struct flb_input_instance *ins, |
| 147 | + struct flb_config *config, void *in_context) |
| 148 | +{ |
| 149 | + struct flb_ne *ctx = (struct flb_ne *) in_context; |
| 150 | + |
| 151 | + return filesystem_update(ctx); |
| 152 | +} |
| 153 | + |
| 154 | +static int ne_filesystem_exit(struct flb_ne *ctx) |
| 155 | +{ |
| 156 | + if (ctx->fs_regex_skip_mount != NULL) { |
| 157 | + flb_regex_destroy(ctx->fs_regex_skip_mount); |
| 158 | + ctx->fs_regex_skip_mount = NULL; |
| 159 | + } |
| 160 | + |
| 161 | + if (ctx->fs_regex_skip_fs_types != NULL) { |
| 162 | + flb_regex_destroy(ctx->fs_regex_skip_fs_types); |
| 163 | + ctx->fs_regex_skip_fs_types = NULL; |
| 164 | + } |
| 165 | + |
| 166 | + return 0; |
| 167 | +} |
| 168 | + |
| 169 | +struct flb_ne_collector filesystem_collector = { |
| 170 | + .name = "filesystem", |
| 171 | + .cb_init = ne_filesystem_init, |
| 172 | + .cb_update = ne_filesystem_update, |
| 173 | + .cb_exit = ne_filesystem_exit |
| 174 | +}; |
0 commit comments