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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "web/api/web_api_v1.h"
static inline void free_single_rrdrim(RRDDIM *temp_rd, int archive_mode)
{
if (unlikely(!temp_rd))
return;
freez((char *)temp_rd->id);
freez((char *)temp_rd->name);
if (unlikely(archive_mode)) {
temp_rd->rrdset->counter--;
if (!temp_rd->rrdset->counter) {
freez((char *)temp_rd->rrdset->name);
freez(temp_rd->rrdset->context);
freez(temp_rd->rrdset);
}
}
freez(temp_rd->state->metric_uuid);
freez(temp_rd->state);
freez(temp_rd);
}
static inline void free_rrddim_list(RRDDIM *temp_rd, int archive_mode)
{
if (unlikely(!temp_rd))
return;
RRDDIM *t;
while (temp_rd) {
t = temp_rd->next;
free_single_rrdrim(temp_rd, archive_mode);
temp_rd = t;
}
}
void free_context_param_list(struct context_param **param_list)
{
if (unlikely(!param_list || !*param_list))
return;
free_rrddim_list(((*param_list)->rd), (*param_list)->flags & CONTEXT_FLAGS_ARCHIVE);
freez((*param_list));
*param_list = NULL;
}
void rebuild_context_param_list(struct context_param *context_param_list, time_t after_requested)
{
RRDDIM *temp_rd = context_param_list->rd;
RRDDIM *new_rd_list = NULL, *t;
int is_archived = (context_param_list->flags & CONTEXT_FLAGS_ARCHIVE);
while (temp_rd) {
t = temp_rd->next;
RRDSET *st = temp_rd->rrdset;
time_t last_entry_t = is_archived ? st->last_entry_t : rrdset_last_entry_t(st);
if (last_entry_t >= after_requested) {
temp_rd->next = new_rd_list;
new_rd_list = temp_rd;
} else
free_single_rrdrim(temp_rd, is_archived);
temp_rd = t;
}
context_param_list->rd = new_rd_list;
};
void build_context_param_list(struct context_param **param_list, RRDSET *st)
{
if (unlikely(!param_list || !st))
return;
if (unlikely(!(*param_list))) {
*param_list = mallocz(sizeof(struct context_param));
(*param_list)->first_entry_t = LONG_MAX;
(*param_list)->last_entry_t = 0;
(*param_list)->flags = CONTEXT_FLAGS_CONTEXT;
(*param_list)->rd = NULL;
}
RRDDIM *rd1;
st->last_accessed_time = now_realtime_sec();
rrdset_rdlock(st);
(*param_list)->first_entry_t = MIN((*param_list)->first_entry_t, rrdset_first_entry_t_nolock(st));
(*param_list)->last_entry_t = MAX((*param_list)->last_entry_t, rrdset_last_entry_t_nolock(st));
rrddim_foreach_read(rd1, st) {
RRDDIM *rd = mallocz(rd1->memsize);
memcpy(rd, rd1, rd1->memsize);
rd->id = strdupz(rd1->id);
rd->name = strdupz(rd1->name);
rd->state = mallocz(sizeof(*rd->state));
memcpy(rd->state, rd1->state, sizeof(*rd->state));
memcpy(&rd->state->collect_ops, &rd1->state->collect_ops, sizeof(struct rrddim_collect_ops));
memcpy(&rd->state->query_ops, &rd1->state->query_ops, sizeof(struct rrddim_query_ops));
rd->state->metric_uuid = mallocz(sizeof(uuid_t));
uuid_copy(*rd->state->metric_uuid, *rd1->state->metric_uuid);
rd->next = (*param_list)->rd;
(*param_list)->rd = rd;
}
rrdset_unlock(st);
}
void rrd_stats_api_v1_chart(RRDSET *st, BUFFER *wb) {
rrdset2json(st, wb, NULL, NULL, 0);
}
void rrdr_buffer_print_format(BUFFER *wb, uint32_t format) {
switch(format) {
case DATASOURCE_JSON:
buffer_strcat(wb, DATASOURCE_FORMAT_JSON);
break;
case DATASOURCE_DATATABLE_JSON:
buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSON);
break;
case DATASOURCE_DATATABLE_JSONP:
buffer_strcat(wb, DATASOURCE_FORMAT_DATATABLE_JSONP);
break;
case DATASOURCE_JSONP:
buffer_strcat(wb, DATASOURCE_FORMAT_JSONP);
break;
case DATASOURCE_SSV:
buffer_strcat(wb, DATASOURCE_FORMAT_SSV);
break;
case DATASOURCE_CSV:
buffer_strcat(wb, DATASOURCE_FORMAT_CSV);
break;
case DATASOURCE_TSV:
buffer_strcat(wb, DATASOURCE_FORMAT_TSV);
break;
case DATASOURCE_HTML:
buffer_strcat(wb, DATASOURCE_FORMAT_HTML);
break;
case DATASOURCE_JS_ARRAY:
buffer_strcat(wb, DATASOURCE_FORMAT_JS_ARRAY);
break;
case DATASOURCE_SSV_COMMA:
buffer_strcat(wb, DATASOURCE_FORMAT_SSV_COMMA);
break;
default:
buffer_strcat(wb, "unknown");
break;
}
}
int rrdset2value_api_v1(
RRDSET *st
, BUFFER *wb
, calculated_number *n
, const char *dimensions
, long points
, long long after
, long long before
, int group_method
, long group_time
, uint32_t options
, time_t *db_after
, time_t *db_before
, int *value_is_null
) {
RRDR *r = rrd2rrdr(st, points, after, before, group_method, group_time, options, dimensions, NULL);
if(!r) {
if(value_is_null) *value_is_null = 1;
return HTTP_RESP_INTERNAL_SERVER_ERROR;
}
if(rrdr_rows(r) == 0) {
rrdr_free(r);
if(db_after) *db_after = 0;
if(db_before) *db_before = 0;
if(value_is_null) *value_is_null = 1;
return HTTP_RESP_BAD_REQUEST;
}
if(wb) {
if (r->result_options & RRDR_RESULT_OPTION_RELATIVE)
buffer_no_cacheable(wb);
else if (r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
buffer_cacheable(wb);
}
if(db_after) *db_after = r->after;
if(db_before) *db_before = r->before;
long i = (!(options & RRDR_OPTION_REVERSED))?rrdr_rows(r) - 1:0;
*n = rrdr2value(r, i, options, value_is_null);
rrdr_free(r);
return HTTP_RESP_OK;
}
int rrdset2anything_api_v1(
RRDSET *st
, BUFFER *wb
, BUFFER *dimensions
, uint32_t format
, long points
, long long after
, long long before
, int group_method
, long group_time
, uint32_t options
, time_t *latest_timestamp
, struct context_param *context_param_list
, char *chart_label_key
) {
if (context_param_list && !(context_param_list->flags & CONTEXT_FLAGS_ARCHIVE))
st->last_accessed_time = now_realtime_sec();
RRDR *r = rrd2rrdr(st, points, after, before, group_method, group_time, options, dimensions?buffer_tostring(dimensions):NULL, context_param_list);
if(!r) {
buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
return HTTP_RESP_INTERNAL_SERVER_ERROR;
}
RRDDIM *temp_rd = context_param_list ? context_param_list->rd : NULL;
if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
buffer_no_cacheable(wb);
else if(r->result_options & RRDR_RESULT_OPTION_ABSOLUTE)
buffer_cacheable(wb);
if(latest_timestamp && rrdr_rows(r) > 0)
*latest_timestamp = r->before;
switch(format) {
case DATASOURCE_SSV:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
rrdr2ssv(r, wb, options, "", " ", "");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
rrdr2ssv(r, wb, options, "", " ", "");
}
break;
case DATASOURCE_SSV_COMMA:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
rrdr2ssv(r, wb, options, "", ",", "");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
rrdr2ssv(r, wb, options, "", ",", "");
}
break;
case DATASOURCE_JS_ARRAY:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
rrdr2ssv(r, wb, options, "[", ",", "]");
rrdr_json_wrapper_end(r, wb, format, options, 0);
}
else {
wb->contenttype = CT_APPLICATION_JSON;
rrdr2ssv(r, wb, options, "[", ",", "]");
}
break;
case DATASOURCE_CSV:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
rrdr2csv(r, wb, format, options, "", ",", "\\n", "", temp_rd);
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
rrdr2csv(r, wb, format, options, "", ",", "\r\n", "", temp_rd);
}
break;
case DATASOURCE_CSV_MARKDOWN:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
rrdr2csv(r, wb, format, options, "", "|", "\\n", "", temp_rd);
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
rrdr2csv(r, wb, format, options, "", "|", "\r\n", "", temp_rd);
}
break;
case DATASOURCE_CSV_JSON_ARRAY:
wb->contenttype = CT_APPLICATION_JSON;
if(options & RRDR_OPTION_JSON_WRAP) {
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
buffer_strcat(wb, "[\n");
rrdr2csv(r, wb, format, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n", temp_rd);
buffer_strcat(wb, "\n]");
rrdr_json_wrapper_end(r, wb, format, options, 0);
}
else {
wb->contenttype = CT_APPLICATION_JSON;
buffer_strcat(wb, "[\n");
rrdr2csv(r, wb, format, options + RRDR_OPTION_LABEL_QUOTES, "[", ",", "]", ",\n", temp_rd);
buffer_strcat(wb, "\n]");
}
break;
case DATASOURCE_TSV:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
rrdr2csv(r, wb, format, options, "", "\t", "\\n", "", temp_rd);
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_PLAIN;
rrdr2csv(r, wb, format, options, "", "\t", "\r\n", "", temp_rd);
}
break;
case DATASOURCE_HTML:
if(options & RRDR_OPTION_JSON_WRAP) {
wb->contenttype = CT_APPLICATION_JSON;
rrdr_json_wrapper_begin(r, wb, format, options, 1, context_param_list, chart_label_key);
buffer_strcat(wb, "<html>\\n<center>\\n<table border=\\\"0\\\" cellpadding=\\\"5\\\" cellspacing=\\\"5\\\">\\n");
rrdr2csv(r, wb, format, options, "<tr><td>", "</td><td>", "</td></tr>\\n", "", temp_rd);
buffer_strcat(wb, "</table>\\n</center>\\n</html>\\n");
rrdr_json_wrapper_end(r, wb, format, options, 1);
}
else {
wb->contenttype = CT_TEXT_HTML;
buffer_strcat(wb, "<html>\n<center>\n<table border=\"0\" cellpadding=\"5\" cellspacing=\"5\">\n");
rrdr2csv(r, wb, format, options, "<tr><td>", "</td><td>", "</td></tr>\n", "", temp_rd);
buffer_strcat(wb, "</table>\n</center>\n</html>\n");
}
break;
case DATASOURCE_DATATABLE_JSONP:
wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
rrdr2json(r, wb, options, 1, context_param_list);
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_end(r, wb, format, options, 0);
break;
case DATASOURCE_DATATABLE_JSON:
wb->contenttype = CT_APPLICATION_JSON;
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
rrdr2json(r, wb, options, 1, context_param_list);
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_end(r, wb, format, options, 0);
break;
case DATASOURCE_JSONP:
wb->contenttype = CT_APPLICATION_X_JAVASCRIPT;
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
rrdr2json(r, wb, options, 0, context_param_list);
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_end(r, wb, format, options, 0);
break;
case DATASOURCE_JSON:
default:
wb->contenttype = CT_APPLICATION_JSON;
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_begin(r, wb, format, options, 0, context_param_list, chart_label_key);
rrdr2json(r, wb, options, 0, context_param_list);
if(options & RRDR_OPTION_JSON_WRAP)
rrdr_json_wrapper_end(r, wb, format, options, 0);
break;
}
rrdr_free(r);
return HTTP_RESP_OK;
}
|