summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_node_exporter_metrics/ne_meminfo_linux.c
blob: 3189d53c0be4f974ad65ad36c16877c9fdcf2744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* -*- 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_hash_table.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_input_plugin.h>

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

#include <unistd.h>

static int meminfo_configure(struct flb_ne *ctx)
{
    int ret;
    int parts;
    int len;
    char *p;
    char desc[] = "Memory information field ";
    struct cmt_gauge *g;
    struct mk_list *head;
    struct mk_list list;
    struct mk_list split_list;
    struct flb_slist_entry *entry;
    struct flb_slist_entry *line;
    flb_sds_t metric_name;
    flb_sds_t metric_desc;

    /* Initialize hash table */
    ctx->meminfo_ht = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 16, 0);
    if (!ctx->meminfo_ht) {
        return -1;
    }

    mk_list_init(&list);
    mk_list_init(&split_list);

    ret = ne_utils_file_read_lines(ctx->path_procfs, "/meminfo", &list);
    if (ret == -1) {
        return -1;
    }
    metric_name = flb_sds_create_size(128);
    if (!metric_name) {
        flb_hash_table_destroy(ctx->meminfo_ht);
        flb_slist_destroy(&list);
        return -1;
    }

    metric_desc = flb_sds_create_size(256);
    if (!metric_desc) {
        flb_hash_table_destroy(ctx->meminfo_ht);
        flb_slist_destroy(&list);
        return -1;
    }

    mk_list_foreach(head, &list) {
        line = mk_list_entry(head, struct flb_slist_entry, _head);

        mk_list_init(&split_list);
        ret = flb_slist_split_string(&split_list, line->str, ' ', -1);
        if (ret == -1) {
            continue;
        }
        parts = ret;

        /* set metric name */
        entry = mk_list_entry_first(&split_list, struct flb_slist_entry, _head);

        if ((p = strstr(entry->str, "(anon)")) ||
            (p = strstr(entry->str, "(file)"))) {
            *p = '_';
            len = flb_sds_len(entry->str) - 2;
            flb_sds_len_set(entry->str, len);
        }
        else {
            len = flb_sds_len(entry->str) - 1;
            flb_sds_len_set(entry->str, len);
        }
        entry->str[len] = '\0';

        flb_sds_len_set(metric_name, 0);
        flb_sds_cat(metric_name, entry->str, flb_sds_len(entry->str));

        /* Metric description */
        flb_sds_len_set(metric_desc, 0);
        flb_sds_cat(metric_desc, desc, sizeof(desc) - 1);

        if (parts == 2) {
            /* No unit */
            flb_sds_cat(metric_desc, metric_name, flb_sds_len(metric_name));
            flb_sds_cat(metric_desc, ".", 1);

            g = cmt_gauge_create(ctx->cmt, "node", "memory", metric_name,
                                 metric_desc,
                                 0, NULL);
            if (!g) {
                flb_slist_destroy(&split_list);
                goto error;
            }
        }
        else if (parts == 3) {
            /* It has an extra 'kB' string in the line */
            flb_sds_cat(metric_name, "_bytes", 6);
            flb_sds_cat(metric_desc, metric_name, flb_sds_len(metric_name));
            flb_sds_cat(metric_desc, ".", 1);
            g = cmt_gauge_create(ctx->cmt, "node", "memory", metric_name,
                                 metric_desc,
                                 0, NULL);
            if (!g) {
                flb_slist_destroy(&split_list);
                goto error;
            }
        }
        else {
            flb_slist_destroy(&split_list);
            continue;
        }
        flb_slist_destroy(&split_list);

        /*
         * Register the gauge context into the hash table: note that depending
         * of the number of parts in the list, if it contains the extra 'kB'
         * the metric name gets appended the '_bytes' string.
         */
        ret = flb_hash_table_add(ctx->meminfo_ht,
                                 metric_name, flb_sds_len(metric_name), g, 0);
        if (ret == -1) {
            flb_plg_error(ctx->ins,
                          "could not add hash for metric: %s", metric_name);
            goto error;
        }
    }

    flb_sds_destroy(metric_name);
    flb_sds_destroy(metric_desc);
    flb_slist_destroy(&list);
    return 0;

 error:
    flb_sds_destroy(metric_name);
    flb_sds_destroy(metric_desc);
    flb_slist_destroy(&list);
    return -1;
}

static int meminfo_update(struct flb_ne *ctx)
{
    int i = 0;
    int ret;
    int len;
    int parts;
    uint64_t ts;
    double val;
    size_t out_size;
    char *p;
    flb_sds_t tmp;
    flb_sds_t metric_name = NULL;
    struct cmt_gauge *g;
    struct mk_list *head;
    struct mk_list list;
    struct mk_list split_list;
    struct flb_slist_entry *line;
    struct flb_slist_entry *entry;

    mk_list_init(&list);
    ret = ne_utils_file_read_lines(ctx->path_procfs, "/meminfo", &list);
    if (ret == -1) {
        return -1;
    }

    ts = cfl_time_now();

    mk_list_foreach(head, &list) {
        line = mk_list_entry(head, struct flb_slist_entry, _head);

        mk_list_init(&split_list);
        ret = flb_slist_split_string(&split_list, line->str, ' ', -1);
        if (ret == -1) {
            continue;
        }
        parts = ret;
        if (parts == 0) {
            flb_slist_destroy(&split_list);
            continue;
        }

        /* Metric name */
        entry = mk_list_entry_first(&split_list, struct flb_slist_entry, _head);
        metric_name = entry->str;

        if ((p = strstr(entry->str, "(anon)")) ||
            (p = strstr(entry->str, "(file)"))) {
            *p = '_';
            len = flb_sds_len(metric_name) - 1;
            flb_sds_len_set(metric_name, len);
        }

        /* Metric value */
        entry = mk_list_entry_next(&split_list, struct flb_slist_entry, _head,
                                   &entry->_head);

        ret = ne_utils_str_to_double(entry->str, &val);
        if (ret == -1) {
            i++;
            flb_slist_destroy(&split_list);
        }

        g = NULL;
        if (parts == 2) {
            /* Metric name is the same, no extra bytes */
            ret = flb_hash_table_get(ctx->meminfo_ht,
                                     metric_name, flb_sds_len(metric_name) - 1,
                                     (void *) &g, &out_size);
        }
        else if (parts == 3) {
            /* Compose new metric name */
            tmp = flb_sds_create_size(256);
            flb_sds_cat_safe(&tmp, metric_name, flb_sds_len(metric_name) - 1);
            flb_sds_cat_safe(&tmp, "_bytes", 6);

            /* Get metric context */
            ret = flb_hash_table_get(ctx->meminfo_ht,
                                     tmp, flb_sds_len(tmp),
                                     (void *) &g, &out_size);
            flb_sds_destroy(tmp);

            /* Value is in kB, convert to bytes */
            val *= 1024;
        }

        if (!g) {
            flb_plg_error(ctx->ins,
                          "gauge content for metric '%s' not found",
                          metric_name);
            flb_slist_destroy(&split_list);
            continue;
        }

        /* Update metric */
        cmt_gauge_set(g, ts, val, 0, NULL);
        flb_slist_destroy(&split_list);
    }

    flb_slist_destroy(&list);
    return 0;
}

int ne_meminfo_init(struct flb_ne *ctx)
{
    meminfo_configure(ctx);
    return 0;
}

int ne_meminfo_update(struct flb_ne *ctx)
{
    meminfo_update(ctx);
    return 0;
}

int ne_meminfo_exit(struct flb_ne *ctx)
{
    if (ctx->meminfo_ht) {
        flb_hash_table_destroy(ctx->meminfo_ht);
    }
    return 0;
}