summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_node_exporter_metrics/ne_textfile_linux.c
blob: d65e01441da539bcc085dd427ab763088d912105 (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
/* -*- 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_sds.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_file.h>

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

#include <unistd.h>
#include <float.h>
#include <sys/types.h>
#include <sys/stat.h>

/* Prometheus decoder */
#include <cmetrics/cmt_decode_prometheus.h>
#include "cmt_decode_prometheus_parser.h"

static char *error_reason(int cmt_error)
{
    static char *reason = NULL;

    switch(cmt_error) {
    case CMT_DECODE_PROMETHEUS_SYNTAX_ERROR:
        reason = "syntax error";
        break;
    case CMT_DECODE_PROMETHEUS_ALLOCATION_ERROR:
        reason = "allocation error";
        break;
    case CMT_DECODE_PROMETHEUS_MAX_LABEL_COUNT_EXCEEDED:
        reason = "max label count exceeded";
        break;
    case CMT_DECODE_PROMETHEUS_CMT_SET_ERROR:
        reason = "cmt set error";
        break;
    case CMT_DECODE_PROMETHEUS_CMT_CREATE_ERROR:
        reason = "cmt create error";
        break;
    case CMT_DECODE_PROMETHEUS_PARSE_VALUE_FAILED:
        reason = "parse value failed";
        break;
    case CMT_DECODE_PROMETHEUS_PARSE_TIMESTAMP_FAILED:
        reason = "parse timestamp failed";
        break;
    default:
        reason = "unknown reason";
    }

    return reason;
}

static int textfile_update(struct flb_ne *ctx)
{
    int ret;
    char errbuf[256];
    flb_sds_t contents;
    struct cmt_decode_prometheus_parse_opts opts;
    uint64_t timestamp;
    struct cmt *cmt;
    struct mk_list *head;
    struct mk_list list;
    struct flb_slist_entry *entry;
    const char *nop_pattern = "";
    const char *dir_pattern = "/*.prom";
    char *ext;
    struct stat st;
    int use_directory_pattern = FLB_FALSE;

    timestamp = cfl_time_now();

    memset(&opts, 0, sizeof(opts));
    opts.errbuf = errbuf;
    opts.errbuf_size = sizeof(errbuf);
    opts.default_timestamp = timestamp;

    flb_plg_debug(ctx->ins, "scanning path %s", ctx->path_textfile);

    if (ctx->path_textfile == NULL) {
        flb_plg_warn(ctx->ins, "No valid path for textfile metric is registered");
        return -1;
    }

    ext = strrchr(ctx->path_textfile, '.');
    if (ext != NULL) {
        if (strncmp(ext, ".prom", 5) == 0) {
            flb_plg_debug(ctx->ins, "specified path %s has \".prom\" extension",
                          ctx->path_textfile);
            use_directory_pattern = FLB_FALSE;
        }
        else {
            ret = stat(ctx->path_textfile, &st);
            if (ret != 0) {
                flb_plg_warn(ctx->ins, "specified path %s is not accesible",
                             ctx->path_textfile);
            }
            if (S_ISREG(st.st_mode)) {
                flb_plg_warn(ctx->ins, "specified path %s does not have \".prom\" extension. Assuming directory",
                             ctx->path_textfile);
                use_directory_pattern = FLB_TRUE;
            }
        }
    }
    else {
        flb_plg_debug(ctx->ins, "specified file path %s does not have extension part. Globbing directory with \"%s\" suffix",
                      ctx->path_textfile, dir_pattern);
        use_directory_pattern = FLB_TRUE;
    }

    if (use_directory_pattern == FLB_TRUE) {
        /* Scan the given directory path */
        ret = ne_utils_path_scan(ctx, ctx->path_textfile, dir_pattern, NE_SCAN_FILE, &list);
        if (ret != 0) {
            return -1;
        }
    }
    else {
        /* Scan the given file path */
        ret = ne_utils_path_scan(ctx, ctx->path_textfile, nop_pattern, NE_SCAN_FILE, &list);
        if (ret != 0) {
            return -1;
        }
    }

    /* Process entries */
    mk_list_foreach(head, &list) {
        entry = mk_list_entry(head, struct flb_slist_entry, _head);
        /* Update metrics from text file */
        contents = flb_file_read(entry->str);
        if (contents == NULL) {
            flb_plg_debug(ctx->ins, "skip invalid file of prometheus: %s",
                          entry->str);
            continue;
        }

        if (flb_sds_len(contents) == 0) {
            flb_plg_debug(ctx->ins, "skip empty payload of prometheus: %s",
                          entry->str);
            continue;
        }

        ret = cmt_decode_prometheus_create(&cmt, contents, flb_sds_len(contents), &opts);
        if (ret == 0) {
            flb_plg_debug(ctx->ins, "parse a payload of prometheus: %s",
                          entry->str);
            cmt_cat(ctx->cmt, cmt);
            cmt_decode_prometheus_destroy(cmt);
        }
        else {
            flb_plg_debug(ctx->ins, "parse a payload of prometheus: dismissed: %s, error: %d",
                          entry->str, ret);
            cmt_counter_set(ctx->load_errors, timestamp, 1.0, 1,  (char*[]){error_reason(ret)});
        }
        flb_sds_destroy(contents);
    }
    flb_slist_destroy(&list);

    return 0;
}

int ne_textfile_init(struct flb_ne *ctx)
{
    ctx->load_errors = cmt_counter_create(ctx->cmt,
                                          "node",
                                          "textfile",
                                          "node_textfile_scrape_error",
                                          "Greater equal than 1 if there was an error opening, reading, or parsing a file, 0 otherwise.",
                                          1, (char *[]) {"reason"});

    if (ctx->load_errors == NULL) {
        return -1;
    }

    return 0;
}

int ne_textfile_update(struct flb_ne *ctx)
{
    textfile_update(ctx);

    return 0;
}

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