summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/in_podman_metrics/podman_metrics_config.h
blob: fabdc0a8ddb59ebdfb0e8b2d217deaceb723501d (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
/* -*- 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.
 */

#ifndef FLB_IN_PODMAN_METRICS_CONFIG_H
#define FLB_IN_PODMAN_METRICS_CONFIG_H

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_jsmn.h>

#include <monkey/mk_core/mk_list.h>

/* Buffers and sizes */
#define JSON_TOKENS                     2048
#define CONTAINER_NAME_SIZE             50
#define CONTAINER_ID_SIZE               80
#define CONTAINER_METADATA_SIZE         512
#define IMAGE_NAME_SIZE                 512
#define PID_BUFFER_SIZE                 21
#define SYSFS_FILE_PATH_SIZE            512
#define PROCFS_FILE_PATH_SIZE           512
#define CGROUP_PATH_SIZE                25

/* Special paths for sysfs traversal */
#define CURRENT_DIR                     "."
#define PREV_DIR                        ".."

/* Ignored network interfaces */
#define VETH_INTERFACE                  "veth"

#define JSON_FIELD_NAMES                "names"
#define JSON_FIELD_ID                   "id"
#define JSON_FIELD_METADATA             "metadata"

#define JSON_SUBFIELD_IMAGE_NAME        "image-name\\\":\\\""
#define JSON_SUBFIELD_SIZE_IMAGE_NAME   15

#define CGROUP_V2_PATH                  "cgroup.controllers"
#define CGROUP_V1                       1
#define CGROUP_V2                       2

/* Paths in /proc subsystem */
#define PROCFS_PATH                     "/proc"
#define PROC_NET_SUFFIX                 "net/dev"

/* Paths in /sys subsystem */
#define SYSFS_PATH                      "/sys/fs/cgroup"
#define V1_SYSFS_MEMORY                 "memory"
#define V1_SYSFS_CPU                    "cpuacct"
#define V1_SYSFS_SYSTEMD                "systemd"
#define SYSFS_CONTAINER_PREFIX          "libpod"
#define SYSFS_LIBPOD_PARENT             "libpod_parent"
#define SYSFS_CONMON                    "conmon"

/* Default podman config file path, in case of not provided one */
#define PODMAN_CONFIG_DEFAULT_PATH      "/var/lib/containers/storage/overlay-containers/containers.json"

/* Markers of network values in /proc/<pid>/dev/net */
#define DEV_NET_IGNORE_LINES            2
#define DEV_NET_NAME                    0
#define DEV_NET_RX_BYTES                1
#define DEV_NET_RX_ERRORS               3
#define DEV_NET_TX_BYTES                9
#define DEV_NET_TX_ERRORS               11

/* Key names in .stat files */
#define STAT_KEY_RSS                    "rss"
#define STAT_KEY_CPU                    "usage_usec"
#define STAT_KEY_CPU_USER               "user_usec"

/* Static lists of fields in counters or gauges */
#define FIELDS_METRIC                  (char*[3]){"id", "name", "image" }
#define FIELDS_METRIC_WITH_IFACE       (char*[4]){"id", "name", "image", "interface" }

/* Files from sysfs containing required data (cgroups v1) */
#define V1_SYSFS_FILE_MEMORY           "memory.usage_in_bytes"
#define V1_SYSFS_FILE_MAX_MEMORY       "memory.max_usage_in_bytes"
#define V1_SYSFS_FILE_MEMORY_STAT      "memory.stat"
#define V1_SYSFS_FILE_MEMORY_LIMIT     "memory.limit_in_bytes"
#define V1_SYSFS_FILE_CPU_USER         "cpuacct.usage_user"
#define V1_SYSFS_FILE_CPU              "cpuacct.usage"
#define V1_SYSFS_FILE_PIDS             "cgroup.procs"

/* Files from sysfs containing required data (cgroups v2) */
#define V2_SYSFS_FILE_MEMORY           "memory.current"
#define V2_SYSFS_FILE_MAX_MEMORY       "memory.peak"
#define V2_SYSFS_FILE_MEMORY_STAT      "memory.stat"
#define V2_SYSFS_FILE_MEMORY_LIMIT     "memory.max"
#define V2_SYSFS_FILE_CPU_STAT         "cpu.stat"
#define V2_SYSFS_FILE_PIDS             "cgroup.procs"
#define V2_SYSFS_FILE_PIDS_ALT         "containers/cgroup.procs"

/* Values used to construct counters/gauges names and descriptions */
#define COUNTER_PREFIX                  "container"

#define COUNTER_MEMORY_PREFIX           "memory"
#define COUNTER_SPEC_MEMORY_PREFIX      "spec_memory"
#define COUNTER_MEMORY_USAGE            "usage_bytes"
#define DESCRIPTION_MEMORY_USAGE        "Container memory usage in bytes"
#define COUNTER_MEMORY_MAX_USAGE        "max_usage_bytes"
#define DESCRIPTION_MEMORY_MAX_USAGE    "Container max memory usage in bytes"
#define COUNTER_MEMORY_LIMIT            "limit_bytes"
#define DESCRIPTION_MEMORY_LIMIT        "Container memory limit in bytes"
#define GAUGE_MEMORY_RSS                "rss"
#define DESCRIPTION_MEMORY_RSS          "Container RSS in bytes"

#define COUNTER_CPU_PREFIX              "cpu"
#define COUNTER_CPU_USER                "user_seconds_total"
#define DESCRIPTION_CPU_USER            "Container cpu usage in seconds in user mode"
#define COUNTER_CPU                     "usage_seconds_total"
#define DESCRIPTION_CPU                 "Container cpu usage in seconds"

#define COUNTER_NETWORK_PREFIX          "network"
#define COUNTER_RX_BYTES                "receive_bytes_total"
#define DESCRIPTION_RX_BYTES            "Network received bytes"
#define COUNTER_RX_ERRORS               "receive_errors_total"
#define DESCRIPTION_RX_ERRORS           "Network received errors"
#define COUNTER_TX_BYTES                "transmit_bytes_total"
#define DESCRIPTION_TX_BYTES            "Network transmited bytes"
#define COUNTER_TX_ERRORS               "transmit_errors_total"
#define DESCRIPTION_TX_ERRORS           "Network transmitedd errors"


struct net_iface {
    flb_sds_t       name;
    uint64_t        rx_bytes;
    uint64_t        rx_errors;
    uint64_t        tx_bytes;
    uint64_t        tx_errors;
    struct mk_list  _head;
};

struct container {
    flb_sds_t       name;
    flb_sds_t       id;
    flb_sds_t       image_name;
    struct mk_list  _head;

    uint64_t        memory_usage;
    uint64_t        memory_max_usage;
    uint64_t        memory_limit;
    uint64_t        cpu;
    uint64_t        cpu_user;
    uint64_t        rss;

    struct mk_list  net_data;
};

struct sysfs_path {
    flb_sds_t       path;
    struct mk_list  _head;
};

struct flb_in_metrics {
    /* config map options */
    int scrape_on_start;
    int scrape_interval;
    flb_sds_t podman_config_path;

    /* container list */
    struct mk_list items;

    /* sysfs path list */
    struct mk_list sysfs_items;

    /* counters */
    struct cmt_counter *c_memory_usage;
    struct cmt_counter *c_memory_max_usage;
    struct cmt_counter *c_memory_limit;
    struct cmt_gauge   *g_rss;
    struct cmt_counter *c_cpu_user;
    struct cmt_counter *c_cpu;
    struct cmt_counter *rx_bytes;
    struct cmt_counter *rx_errors;
    struct cmt_counter *tx_bytes;
    struct cmt_counter *tx_errors;

    /* cgroup version used by host */
    int cgroup_version;

    /* podman config file path */
    flb_sds_t config;

    /* proc and sys paths, overwriting mostly for testing */
    flb_sds_t sysfs_path;
    flb_sds_t procfs_path;

    /* internal */
    int coll_fd_runtime;
    struct flb_input_instance *ins;
};

#endif