forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatsd.c
287 lines (249 loc) · 7.36 KB
/
statsd.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
/*
* Copyright (c) 2024 Thibaut VARENE
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "acarsdec.h"
#include "statsd.h"
#define STATSD_UDP_BUFSIZE 1432 ///< udp buffer size. Untold rule seems to be that the datagram must not be fragmented.
#define STATSD_NAMESPACE "acarsdec."
#define ERRPFX "ERROR: STATSD: "
#define WARNPFX "WARNING: STATSD: "
static struct {
char *namespace; ///< statsd namespace prefix (dot-terminated)
struct sockaddr_storage ai_addr;
socklen_t ai_addrlen;
int sockfd;
} statsd_runtime = {};
/**
* Resolve remote host and open socket.
* @param parms host=foo,port=1234
* @param idstation station id
* @return socket fd or negative error
*/
int statsd_init(char *params, const char *idstation)
{
char *host = NULL, *port = NULL, *retp;
struct params_s statsdp[] = {
{ .name = "host", .valp = &host, },
{ .name = "port", .valp = &port, },
};
int sockfd;
struct addrinfo hints;
struct addrinfo *result, *rp;
int ret;
retp = parse_params(¶ms, statsdp, ARRAY_SIZE(statsdp));
if (retp) {
fprintf(stderr, ERRPFX "invalid parameter '%s'\n", retp);
return -1;
}
if (!host || !port) {
fprintf(stderr, ERRPFX "invalid parameters\n");
return -1;
}
// obtain address(es) matching host/port
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
ret = getaddrinfo(host, port, &hints, &result);
if (ret) {
fprintf(stderr, ERRPFX "getaddrinfo: %s\n", gai_strerror(ret));
return -1;
}
// try each address until one succeeds
for (rp = result; rp; rp = rp->ai_next) {
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (-1 != sockfd)
break; // success
}
if (!rp) {
fprintf(stderr, ERRPFX "Could not reach server\n");
goto cleanup;
}
memcpy(&statsd_runtime.ai_addr, rp->ai_addr, rp->ai_addrlen); // ai_addrlen is guaranteed to be <= sizeof(sockaddr_storage)
statsd_runtime.ai_addrlen = rp->ai_addrlen;
ret = strlen(STATSD_NAMESPACE);
if (idstation)
ret += strlen(idstation);
statsd_runtime.namespace = malloc(ret + 2);
if(!statsd_runtime.namespace) {
perror("statsd");
goto cleanup;
}
retp = stpcpy(statsd_runtime.namespace, STATSD_NAMESPACE);
if (idstation) {
strcpy(retp, idstation);
statsd_runtime.namespace[ret++] = '.';
statsd_runtime.namespace[ret] = '\0';
}
statsd_runtime.sockfd = sockfd;
cleanup:
freeaddrinfo(result);
return sockfd;
}
#ifdef DEBUG
static int statsd_validate(const char * stat)
{
const char * p;
for (p = stat; *p; p++) {
switch (*p) {
case ':':
case '|':
case '@':
return (-1);
default:
; // nothing
}
}
return 0;
}
#endif
/**
* Update StatsD metrics.
* @param pfx a prefix prepended to each metric name
* @param metrics an array of metrics to push to StatsD
* @param nmetrics the array size
* @return exec status
* @warning not thread-safe.
*/
int statsd_update(const char *pfx, const struct statsd_metric * const metrics, const unsigned int nmetrics)
{
static char sbuffer[STATSD_UDP_BUFSIZE];
const char *mtype;
char * buffer;
bool zerofirst;
int ret;
ssize_t sent;
size_t avail;
unsigned int i;
if (!statsd_runtime.namespace)
return 0;
buffer = sbuffer;
avail = STATSD_UDP_BUFSIZE;
for (i = 0; i < nmetrics; i++) {
#ifdef DEBUG
if ((statsd_validate(metrics[i].name) != 0)) {
fprintf(stderr, WARNPFX "ignoring invalid name \"%s\"", metrics[i].name);
continue;
}
#endif
zerofirst = false;
switch (metrics[i].type) {
case STATSD_LGAUGE:
mtype = "g";
if (metrics[i].value.l < 0)
zerofirst = true;
break;
case STATSD_FGAUGE:
mtype = "g";
if (metrics[i].value.f < 0.0F)
zerofirst = true;
break;
case STATSD_UCOUNTER:
mtype = "c";
break;
default:
ret = -1;
goto cleanup;
}
restartzero:
// StatsD has a schizophrenic idea of what a gauge is (negative values are subtracted from previous data and not registered as is): work around its dementia
if (zerofirst) {
ret = snprintf(buffer, avail, "%s%s%s:0|%s\n", statsd_runtime.namespace ? statsd_runtime.namespace : "", pfx ? pfx : "", metrics[i].name, mtype);
if (ret < 0) {
ret = -1;
goto cleanup;
}
else if ((size_t)ret >= avail) {
// send what we have, reset buffer, restart - no need to add '\0': sendto will truncate anyway
sendto(statsd_runtime.sockfd, sbuffer, STATSD_UDP_BUFSIZE - avail, 0, (struct sockaddr *)&statsd_runtime.ai_addr, statsd_runtime.ai_addrlen);
buffer = sbuffer;
avail = STATSD_UDP_BUFSIZE;
goto restartzero;
}
buffer += ret;
avail -= (size_t)ret;
}
restartbuffer:
switch (metrics[i].type) {
case STATSD_LGAUGE:
ret = snprintf(buffer, avail, "%s%s%s:%ld|%s\n", statsd_runtime.namespace ? statsd_runtime.namespace : "", pfx ? pfx : "", metrics[i].name, metrics[i].value.l, mtype);
break;
case STATSD_UCOUNTER:
ret = snprintf(buffer, avail, "%s%s%s:%lu|%s\n", statsd_runtime.namespace ? statsd_runtime.namespace : "", pfx ? pfx : "", metrics[i].name, metrics[i].value.u, mtype);
break;
case STATSD_FGAUGE:
ret = snprintf(buffer, avail, "%s%s%s:%f|%s\n", statsd_runtime.namespace ? statsd_runtime.namespace : "", pfx ? pfx : "", metrics[i].name, metrics[i].value.f, mtype);
break;
default:
ret = 0;
break; // cannot happen thanks to previous switch()
}
if (ret < 0) {
ret = -1;
goto cleanup;
}
else if ((size_t)ret >= avail) {
// send what we have, reset buffer, restart - no need to add '\0': sendto will truncate anyway
sendto(statsd_runtime.sockfd, sbuffer, STATSD_UDP_BUFSIZE - avail, 0, (struct sockaddr *)&statsd_runtime.ai_addr, statsd_runtime.ai_addrlen);
buffer = sbuffer;
avail = STATSD_UDP_BUFSIZE;
goto restartbuffer;
}
buffer += ret;
avail -= (size_t)ret;
}
ret = 0;
cleanup:
// we only check for sendto() errors here
sent = sendto(statsd_runtime.sockfd, sbuffer, STATSD_UDP_BUFSIZE - avail, 0, (struct sockaddr *)&statsd_runtime.ai_addr, statsd_runtime.ai_addrlen);
if (-1 == sent)
perror(ERRPFX);
return ret;
}
static int statsd_count(const char *pfx, const char *stat, unsigned long value)
{
struct statsd_metric m = {
.type = STATSD_UCOUNTER,
.name = stat,
.value.u = value,
};
return statsd_update(pfx, &m, 1);
}
static int statsd_inc(const char *pfx, const char *counter)
{
return statsd_count(pfx, counter, 1);
}
/**
* @param ch channel number
* @param counter name of counter to incremetn
*/
int statsd_inc_per_channel(int ch, const char *counter)
{
if (!statsd_runtime.namespace)
return 0;
char pfx[16];
snprintf(pfx, sizeof(pfx), "%u.", R.channels[ch].Fr ? R.channels[ch].Fr : ch+1);
return statsd_inc(pfx, counter);
}