-
Notifications
You must be signed in to change notification settings - Fork 1
/
monit-objects-mavg-dump.c
266 lines (209 loc) · 5.3 KB
/
monit-objects-mavg-dump.c
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
/*
* xenoeye
*
* Copyright (c) 2022, Vladimir Misyurov, Michael Kogan
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <stdatomic.h>
#include "utils.h"
#include "netflow.h"
#include "monit-objects.h"
#include "monit-objects-common.h"
#define MAVG_VAL(DATUM, I, SIZE) ((struct mavg_val *)&DATUM[SIZE * I])
static int
mavg_dump_tr(FILE *out, struct mo_mavg *mavg, tkvdb_tr *tr,
size_t val_itemsize)
{
size_t i;
int ret = 0;
tkvdb_cursor *c;
size_t mem_used;
MAVG_TYPE wnd_size_ns;
struct timespec tmsp;
uint64_t time_ns;
if (clock_gettime(CLOCK_REALTIME_COARSE, &tmsp) < 0) {
LOG("clock_gettime() failed: %s", strerror(errno));
}
time_ns = tmsp.tv_sec * 1e9 + tmsp.tv_nsec;
/* time window in nanoseconds */
wnd_size_ns = (MAVG_TYPE)mavg->size_secs * 1e9;
c = tkvdb_cursor_create(tr);
if (!c) {
LOG("tkvdb_cursor_create() failed");
goto cursor_fail;
}
if (c->first(c) != TKVDB_OK) {
ret = 1;
goto empty;
}
/* print memory used by database */
mem_used = tr->mem(tr);
fprintf(out, "mem used/avail: %luM/%luM (%lu/%lu bytes)\n",
1 + mem_used / (1024 * 1024), mavg->db_mem / (1024 * 1024),
mem_used, mavg->db_mem);
/* iterate over all set */
do {
uint8_t *data = c->key(c);
uint8_t *pval = c->val(c);
for (i=0; i<mavg->fieldset.n_naggr; i++) {
struct field *fld = &mavg->fieldset.naggr[i];
monit_object_field_print(fld, out, data, 1);
data += fld->size;
}
fprintf(out, " :: ");
for (i=0; i<mavg->fieldset.n_aggr; i++) {
size_t j;
struct mavg_val *val;
MAVG_TYPE v;
val = MAVG_VAL(pval, i, val_itemsize);
v = val->val;
/* correct value */
if (time_ns > (val->time_prev + wnd_size_ns)) {
v = 0.0;
} else {
v = v - (time_ns - val->time_prev)
/ wnd_size_ns * v;
v /= (MAVG_TYPE)mavg->size_secs;
}
fprintf(out, "%lu ", (uint64_t)v);
/* limits */
fprintf(out, "(");
for (j=0; j<mavg->noverlimit; j++) {
/* */
fprintf(out, "%lu ", (uint64_t)val->limits_max[j]);
}
fprintf(out, ")");
}
fprintf(out, "\n");
} while (c->next(c) == TKVDB_OK);
fprintf(out, "\n");
ret = 1;
empty:
c->free(c);
cursor_fail:
return ret;
}
static int
mavg_dump_do(struct mo_mavg *mavg, size_t nthreads, struct monit_object *mo,
int append)
{
FILE *f;
char dump_path[PATH_MAX * 2];
size_t i;
char timebuf[100];
time_t t;
t = time(NULL);
if (t == (time_t)-1) {
LOG("time() failed: %s", strerror(errno));
return 0;
}
sprintf(dump_path, append? "%s/%s.adump" : "%s/%s.dump",
mo->dir, mavg->name);
if (strlen(dump_path) >= PATH_MAX) {
LOG("Filename too big: %s/%s", mo->dir, mavg->name);
return 0;
}
f = fopen(dump_path, append? "a": "w");
if (!f) {
LOG("Can't open '%s': %s", dump_path, strerror(errno));
return 0;
}
fprintf(f, "%s", ctime_r(&t, timebuf));
for (i=0; i<nthreads; i++) {
tkvdb_tr *db;
db = atomic_load_explicit(&mavg->data[i].db,
memory_order_relaxed);
mavg_dump_tr(f, mavg, db, mavg->data[i].val_itemsize);
}
if (append) {
/* extra empty line */
fprintf(f, "\n");
}
fclose(f);
return 1;
}
static int
mavg_dump(struct mo_mavg *mavg, size_t nthreads, struct monit_object *mo)
{
char enabled[PATH_MAX * 2];
struct stat statbuf;
int dump = 0, append = 0;
sprintf(enabled, "%s/%s.d", mo->dir, mavg->name);
if (strlen(enabled) >= PATH_MAX) {
LOG("Filename too big: %s/%s", mo->dir, mavg->name);
return 0;
}
if (stat(enabled, &statbuf) == 0) {
dump = 1;
}
sprintf(enabled, "%s/%s.a", mo->dir, mavg->name);
if (stat(enabled, &statbuf) == 0) {
append = 1;
}
if (!dump && !append) {
/* skip */
return 0;
}
if (dump) {
mavg_dump_do(mavg, nthreads, mo, 0);
}
if (append) {
mavg_dump_do(mavg, nthreads, mo, 1);
}
return 1;
}
void *
mavg_dump_thread(void *arg)
{
struct xe_data *data = (struct xe_data *)arg;
for (;;) {
time_t t;
size_t i, j;
if (atomic_load_explicit(&data->stop, memory_order_relaxed)) {
/* stop */
break;
}
t = time(NULL);
if (t == ((time_t)-1)) {
LOG("time() failed: %s", strerror(errno));
return NULL;
}
/* for each monitoring object */
for (i=0; i<data->nmonit_objects; i++) {
struct monit_object *mo = &data->monit_objects[i];
/* for each moving average */
for (j=0; j<mo->nmavg; j++) {
struct mo_mavg *mavg = &mo->mavgs[j];
if (mavg->dump_secs == 0) {
/* skip */
continue;
}
if ((mavg->last_dump_check + mavg->dump_secs)
<= t) {
/* time to dump */
mavg_dump(mavg, data->nthreads, mo);
mavg->last_dump_check = t;
}
}
}
sleep(1);
}
return NULL;
}