summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_windows_exporter_metrics/we_os.c
blob: 299514d8c78ec4d886bd7e70f180df0252dc18d5 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 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_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_pack.h>

#ifndef UNICODE
#define UNICODE
#endif
#include <lm.h>
#include <psapi.h>
#include <timezoneapi.h>

#include "we.h"
#include "we_os.h"
#include "we_util.h"
#include "we_metric.h"

int we_os_init(struct flb_we *ctx)
{
    ctx->os = flb_calloc(1, sizeof(struct we_os_counters));
    if (!ctx->os) {
        flb_errno();
        return -1;
    }
    ctx->os->operational = FLB_FALSE;

    struct cmt_gauge *g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "info",
                         "Version information of OperatingSystem",
                         5, (char *[]) {"product", "version", "major_version", "minor_version", "build_number"});

    if (!g) {
        return -1;
    }
    ctx->os->info = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "physical_memory_free_bytes",
                         "Amount of free bytes of physical memory",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->physical_memory_free_bytes = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "time",
                         "Value of Local Time",
                         0, NULL);
    if (!g) {
        return -1;
    }
    ctx->os->time = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "timezone",
                         "Name of Local Timezone",
                         1, (char *[]) {"timezone"});
    if (!g) {
        return -1;
    }
    ctx->os->tz = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "virtual_memory_bytes",
                         "Total amount of bytes of virtual memory",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->virtual_memory_bytes = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "processes_limit",
                         "Number of processes limit",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->processes_limit = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "process_memory_limit_bytes",
                         "Limit of processes memory",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->process_memory_limit_bytes = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "processes",
                         "Number of processes",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->processes = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "users",
                         "Number of users",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->users = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "visible_memory_bytes",
                         "Total amount of bytes of visibile memory",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->visible_memory_bytes = g;

    g = cmt_gauge_create(ctx->cmt, "windows", "os", "virtual_memory_free_bytes",
                         "Amount of free bytes of virtual memory",
                         0, NULL);

    if (!g) {
        return -1;
    }
    ctx->os->virtual_memory_free_bytes = g;

    ctx->os->operational = FLB_TRUE;

    return 0;
}

int we_os_exit(struct flb_we *ctx)
{
    flb_free(ctx->os);
    return 0;
}

int we_os_update(struct flb_we *ctx)
{
    DWORD level = 102;
    LPWKSTA_INFO_102 wksta = NULL;
    NET_API_STATUS status;
    MEMORYSTATUSEX statex;
    PERFORMANCE_INFORMATION perf;
    DWORD size = 0;
    char version[65] = {0}, major[32] = {0}, minor[32] = {0};
    int users = 0;
    LONG ret;
    HKEY hkey;
    char caption[80], build_number[32];
    DWORD caption_len = sizeof(caption), build_len = sizeof(build_number);
    uint64_t timestamp = 0;
    char label_caption[90];
    TIME_ZONE_INFORMATION tzi;
    DWORD tztype = 0;
    char *displaytz;

    if (!ctx->os->operational) {
        flb_plg_error(ctx->ins, "os collector not yet in operational state");

        return -1;
    }

    timestamp = cfl_time_now();

    ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, WE_OS_CURRENT_VERSION_PATH, 0, KEY_QUERY_VALUE, &hkey);
    if (ret != ERROR_SUCCESS) {
        return -1;
    }
    ret = RegQueryValueExA(hkey, "ProductName", NULL, NULL, (LPBYTE)caption, &caption_len);
    if (ret != ERROR_SUCCESS) {
        return -1;
    }
    ret = RegQueryValueExA(hkey, "CurrentBuildNumber", NULL, NULL, (LPBYTE)build_number, &build_len);
    if (ret != ERROR_SUCCESS) {
        return -1;
    }
    RegCloseKey(hkey);

    status = NetWkstaGetInfo(NULL,
                             level,
                             (LPBYTE *)&wksta);

    if (status == NERR_Success) {
        snprintf(version, 65, "%d.%d", wksta->wki102_ver_major,
                 wksta->wki102_ver_minor);
        snprintf(major, 32, "%d", wksta->wki102_ver_major);
        snprintf(minor, 32, "%d", wksta->wki102_ver_minor);
        snprintf(label_caption, 90, "Microsoft %s", caption);

        users = wksta->wki102_logged_on_users;

        cmt_gauge_set(ctx->os->info, timestamp, 1.0, 5,
                      (char *[]) { label_caption, version, major, minor, build_number});
        cmt_gauge_set(ctx->os->users, timestamp, (double)users, 0, NULL);
    }
    else {
        if (wksta != NULL) {
            NetApiBufferFree(wksta);
        }
        flb_plg_error(ctx->ins, "A system error has occurred: %d\n", status);
        return -1;
    }

    cmt_gauge_set(ctx->os->time, timestamp, (double)timestamp/1000000000L, 0, NULL);

    tztype = GetTimeZoneInformation(&tzi);
    switch (tztype) {
    case TIME_ZONE_ID_STANDARD:
        displaytz = we_convert_wstr(tzi.StandardName, CP_UTF8);
        cmt_gauge_set(ctx->os->tz, timestamp, 1.0, 1, (char *[]) {displaytz});
        flb_free(displaytz);
        break;
    case TIME_ZONE_ID_DAYLIGHT:
        displaytz = we_convert_wstr(tzi.DaylightName, CP_UTF8);
        cmt_gauge_set(ctx->os->tz, timestamp, 1.0, 1, (char *[]) {displaytz});
        flb_free(displaytz);
        break;
    case TIME_ZONE_ID_UNKNOWN:
        /* The current timezone does not use daylight saving time. */
        displaytz = we_convert_wstr(tzi.StandardName, CP_UTF8);
        cmt_gauge_set(ctx->os->tz, timestamp, 1.0, 1, (char *[]) {displaytz});
        flb_free(displaytz);
        break;
    default:
        flb_plg_error(ctx->ins, "Error to retrieve timezone information with status: %d", GetLastError());
    }

    statex.dwLength = sizeof (statex);
    GlobalMemoryStatusEx(&statex);

    size = sizeof(perf);
    GetPerformanceInfo(&perf, size);

    cmt_gauge_set(ctx->os->physical_memory_free_bytes, timestamp, (double)statex.ullAvailPhys, 0, NULL);
    cmt_gauge_set(ctx->os->virtual_memory_free_bytes, timestamp, (double)statex.ullAvailPageFile, 0, NULL);
    /* The result is from $(Get-WMIObject Win32_OperatingSystem).MaxNumberOfProcesses. */
    cmt_gauge_set(ctx->os->processes_limit, timestamp, (double)4294967295, 0, NULL);
    cmt_gauge_set(ctx->os->process_memory_limit_bytes, timestamp, (double)statex.ullTotalVirtual, 0, NULL);
    cmt_gauge_set(ctx->os->processes, timestamp, (double)perf.ProcessCount, 0, NULL);
    cmt_gauge_set(ctx->os->virtual_memory_bytes, timestamp, (double)statex.ullTotalPageFile, 0, NULL);
    cmt_gauge_set(ctx->os->visible_memory_bytes, timestamp, (double)statex.ullTotalPhys, 0, NULL);

    if (wksta != NULL) {
        NetApiBufferFree(wksta);
    }

    return 0;
}