-
Notifications
You must be signed in to change notification settings - Fork 5
/
librrd.c
478 lines (430 loc) · 12.4 KB
/
librrd.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright (c) 2016 Citrix
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "librrd.h"
#include "parson/parson.h"
#define MAGIC "DATASOURCES"
#define MAGIC_SIZE (sizeof (MAGIC)-1)
#define RRD_MAX_JSON (2048 * RRD_MAX_SOURCES)
#ifndef __APPLE__
#include <endian.h>
#define htonll(x) htobe64(x)
#endif
/*
* The type RRD_PLUGIN below is private to the implementation and entirely
* managed by it.
*/
struct rrd_plugin {
char *name; /* name of the plugin */
char *path; /* path to file */
RRD_SOURCE *sources[RRD_MAX_SOURCES];
JSON_Value *meta; /* meta data for the plugin */
char *buf; /* buffer where we keep protocol data */
rrd_domain_t domain; /* domain of this plugin */
uint32_t n; /* number of used slots */
size_t buf_size; /* size of the buffer */
int file; /* where we report data */
};
/*
* The struct represents the first 31 bytes in the protocol header. Fields
* are not 4-byte or 8-byte aligned which is why we need the packed
* attribute.
*/
struct rrd_header {
uint8_t rrd_magic[MAGIC_SIZE];
uint32_t rrd_checksum_value;
uint32_t rrd_checksum_meta;
uint32_t rrd_header_datasources;
uint64_t rrd_timestamp;
uint32_t rrd_data[];/* more data follows */
} __attribute__((packed));
typedef struct rrd_header RRD_HEADER;
/*
* write data to fd
*/
static int
write_exact(int fd, const void *data, size_t size)
{
size_t offset = 0;
ssize_t len;
while (offset < size) {
len = write(fd, (const char *)data + offset, size - offset);
if ((len == -1) && (errno == EINTR))
continue;
if (len <= 0)
return -1;
offset += len;
}
return 0;
}
/*
* invalidate the current buffer. It will be re-created by sample().
*/
static void
invalidate(RRD_PLUGIN * plugin)
{
assert(plugin);
free(plugin->buf);
plugin->buf = NULL;
plugin->buf_size = 0;
json_value_free(plugin->meta);
plugin->meta = NULL;
}
/*
* Generate JSON for a data source and return it as an JSON object (from
* where it can be rendered to a string.
*/
static JSON_Value *
json_for_source(RRD_SOURCE * source)
{
assert(source);
JSON_Value *json = json_value_init_object();
JSON_Object *src = json_value_get_object(json);
json_object_set_string(src, "description", source->description);
json_object_set_string(src, "units", source->rrd_units);
json_object_set_string(src, "min", source->min);
json_object_set_string(src, "max", source->max);
json_object_set_string(src, "default",
source->rrd_default ? "true" : "false");
char owner[128] = {0};
switch (source->owner) {
case RRD_HOST:
snprintf(owner, sizeof owner, "host");
break;
case RRD_VM:
snprintf(owner, sizeof owner, "vm %s", source->owner_uuid);
break;
case RRD_SR:
snprintf(owner, sizeof owner, "sr %s", source->owner_uuid);
break;
default:
abort();
}
json_object_set_string(src, "owner", owner);
char *value_type = NULL;
switch (source->type) {
case RRD_INT64:
value_type = "int64";
break;
case RRD_FLOAT64:
value_type = "float";
break;
default:
abort();
}
json_object_set_string(src, "value_type", value_type);
#define RRD_TRANSPORT_1_1_0
#ifdef RRD_TRANSPORT_1_1_0
#define GAUGE "gauge"
#define ABSOLUTE "absolute"
#define DERIVE "derive"
#else
#define GAUGE "absolute"
#define ABSOLUTE "rate"
#define DERIVE "absolute_to_rate"
#endif
char *scale = NULL;
switch (source->scale) {
case RRD_GAUGE:
scale = GAUGE;
break;
case RRD_ABSOLUTE:
scale = ABSOLUTE;
break;
case RRD_DERIVE:
scale = DERIVE;
break;
default:
abort();
}
json_object_set_string(src, "type", scale);
return json;
}
/*
* Generate JSON for a plugin. This is just a JSON object containing a
* sub-object for every data source.
*/
static JSON_Value *
json_for_plugin(RRD_PLUGIN * plugin)
{
assert(plugin);
JSON_Value *root_json = json_value_init_object();
JSON_Object *root = json_value_get_object(root_json);
JSON_Value *ds_json = json_value_init_object();
JSON_Object *ds = json_value_get_object(ds_json);
json_object_set_value(root, "datasources", ds_json);
for (size_t i = 0; i < RRD_MAX_SOURCES; i++) {
JSON_Value *src;
if (plugin->sources[i] == NULL)
continue;
src = json_for_source(plugin->sources[i]);
json_object_set_value(ds, plugin->sources[i]->name, src);
}
return root_json;
}
double get_timestamp()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return (double) tp.tv_sec + (double) tp.tv_usec / 1e6;
}
/*
* initialise the buffer that we update and write out to a file. Once
* initialised, it is kept up to date by sample().
*/
static int
initialise(RRD_PLUGIN * plugin)
{
RRD_HEADER *header;
uint32_t size_meta;
uint32_t size_total;
int64_t *p64;
int32_t *p32;
assert(plugin);
assert(plugin->meta == NULL);
assert(plugin->buf == NULL);
assert(plugin->n <= RRD_MAX_SOURCES);
plugin->meta = json_for_plugin(plugin);
size_meta = json_serialization_size_pretty(plugin->meta);
assert(size_meta < RRD_MAX_JSON); /* just a safeguard */
size_total = 0;
size_total += sizeof(RRD_HEADER);
size_total += RRD_MAX_SOURCES * sizeof(int64_t);
size_total += sizeof(uint32_t);
size_total += RRD_MAX_JSON;
plugin->buf_size = size_total;
plugin->buf = calloc(1, size_total);
if (!plugin->buf) {
/*
* fatal
*/
plugin->buf_size = 0;
return -1;
}
/*
* all values need to be in network byte order
*/
header = (RRD_HEADER *) plugin->buf;
memcpy(&header->rrd_magic, MAGIC, MAGIC_SIZE);
header->rrd_checksum_value = htonl(0x01234567);
header->rrd_header_datasources = htonl(plugin->n);
header->rrd_timestamp = htonll(get_timestamp());
if (header->rrd_timestamp == -1) {
free(plugin->buf);
plugin->buf_size = 0;
plugin->buf = NULL;
return -1;
}
p64 = (int64_t *) (plugin->buf + sizeof(RRD_HEADER));
for (size_t i = 0; i < plugin->n; i++) {
*p64++ = htonll(0x0011223344556677);
}
p32 = (int32_t *) p64;
*p32++ = htonl(size_meta);
json_serialize_to_buffer_pretty(plugin->meta, (char *)p32, size_meta);
uint32_t crc;
crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, (unsigned char *)p32, size_meta);
header->rrd_checksum_meta = htonl(crc);
return 0;
}
/*
* rrd_open creates the data structure that represents a plugin with
* initially no data source. Data sources will be added later by rrd_add_src.
*/
RRD_PLUGIN *
rrd_open(char *name, rrd_domain_t domain, char *path)
{
assert(name);
assert(path);
RRD_PLUGIN *plugin = malloc(sizeof(RRD_PLUGIN));
if (!plugin) {
return NULL;
}
plugin->name = name;
plugin->path = path;
plugin->domain = domain;
/*
* mark all slots for data sources as free
*/
for (int i = 0; i < RRD_MAX_SOURCES; i++) {
plugin->sources[i] = (RRD_SOURCE *) NULL;
}
plugin->n = 0;
plugin->buf_size = 0;
plugin->buf = NULL;
plugin->meta = NULL;
if (initialise(plugin) != 0) {
invalidate(plugin);
free(plugin);
return NULL;
}
plugin->file = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (plugin->file == -1) {
invalidate(plugin);
free(plugin);
return NULL;
}
if (write_exact(plugin->file, plugin->buf, plugin->buf_size) != 0) {
invalidate(plugin);
free(plugin);
return NULL;
}
return plugin;
}
/*
* unregister a plugin. Free all resources that we have allocted. Note that
* calling free(NULL) is fine in case some resource was already de-allocated.
*/
int
rrd_close(RRD_PLUGIN * plugin)
{
assert(plugin);
int rc;
rc = close(plugin->file);
if (rc == 0)
rc = unlink(plugin->path);
invalidate(plugin);
free(plugin);
return (rc == 0 ? RRD_OK : RRD_FILE_ERROR);
}
/*
* Add a new data source to a plugin. It is inserted into the first free slot
* available.
*/
int
rrd_add_src(RRD_PLUGIN * plugin, RRD_SOURCE * source)
{
assert(plugin);
assert(source);
/*
* find free slot
*/
size_t i;
for (i = 0; i < RRD_MAX_SOURCES; i++) {
if (plugin->sources[i] == NULL)
break;
}
if (i >= RRD_MAX_SOURCES) {
return RRD_TOO_MANY_SOURCES;
}
plugin->sources[i] = source;
plugin->n++;
invalidate(plugin);
return RRD_OK;
}
/*
* Remove a previously registered data source from a plugin,
*/
int
rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source)
{
assert(source);
size_t i;
/*
* find slot with source
*/
for (i = 0; i < RRD_MAX_SOURCES; i++) {
if (plugin->sources[i] == source)
break;
}
if (i >= RRD_MAX_SOURCES) {
return RRD_NO_SUCH_SOURCE;
}
plugin->sources[i] = NULL;
plugin->n--;
invalidate(plugin);
return RRD_OK;
}
/*
* Sample obtains a values form all data sources by calling their sample
* functions. It updates the buffer with all data and writes it out. If there
* is no meta data it means it was invalidated previously because a data
* source was added or removed. In that case in creates a new buffer first.
*/
int
rrd_sample(RRD_PLUGIN * plugin, time_t(*t) (time_t *))
{
assert(plugin);
int n = 0;
int64_t *p;
RRD_HEADER *header;
if (plugin->buf == NULL) {
int rc;
rc = initialise(plugin);
if (rc != 0) {
return RRD_ERROR;
}
}
assert(plugin->buf);
header = (RRD_HEADER *) plugin->buf;
/*
* sample n sources and write values to buffer
*/
p = (int64_t *) (plugin->buf + sizeof(RRD_HEADER));
for (size_t i = 0; i < RRD_MAX_SOURCES; i++) {
void *userdata;
if (plugin->sources[i] == NULL)
continue;
userdata = plugin->sources[i]->userdata;
rrd_value_t v = plugin->sources[i]->sample(userdata);
*p++ = htonll((uint64_t) v.int64);
n++;
}
/*
* must have sampled exactly n data sources
*/
assert(n == plugin->n);
/*
* update timestamp, calculate crc
*/
header->rrd_timestamp = htonll(get_timestamp());
uint32_t crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc,
(unsigned char *)&header->rrd_timestamp,
(n + 1) * sizeof(int64_t));
header->rrd_checksum_value = htonl(crc);
/*
* reset file pointer, write out buffer
*/
if (lseek(plugin->file, 0, SEEK_SET) < 0) {
return RRD_FILE_ERROR;
}
if (write_exact(plugin->file, plugin->buf, plugin->buf_size) != 0) {
return RRD_FILE_ERROR;
}
return RRD_OK;
}