31#define INFLUX_MEAS(m) IF_TYPE_MEAS, (m)
32#define INFLUX_TAG(k, v) IF_TYPE_TAG, (k), (v)
33#define INFLUX_F_STR(k, v) IF_TYPE_FIELD_STRING, (k), (v)
34#define INFLUX_F_FLT(k, v, p) IF_TYPE_FIELD_FLOAT, (k), (double)(v), (int)(p)
35#define INFLUX_F_INT(k, v) IF_TYPE_FIELD_INTEGER, (k), (long long)(v)
36#define INFLUX_F_BOL(k, v) IF_TYPE_FIELD_BOOLEAN, (k), ((v) ? 1 : 0)
37#define INFLUX_TS(ts) IF_TYPE_TIMESTAMP, (long long)(ts)
38#define INFLUX_END IF_TYPE_ARG_END
60int format_line(
char** buf,
int* len,
size_t used, ...);
65#define IF_TYPE_ARG_END 0
68#define IF_TYPE_FIELD_STRING 3
69#define IF_TYPE_FIELD_FLOAT 4
70#define IF_TYPE_FIELD_INTEGER 5
71#define IF_TYPE_FIELD_BOOLEAN 6
72#define IF_TYPE_TIMESTAMP 7
74int _escaped_append(
char** dest,
size_t* len,
size_t* used,
const char* src,
const char* escape_seq);
82 int sock = -1, ret_code = 0, content_length = 0;
83 struct sockaddr_in addr;
90 if (!(iv[0].iov_base = (
char*)malloc(len = 0x800))) {
97 snprintf((
char*)iv[0].iov_base, len,
98 "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\n"
100 "Accept: application/json\r\n"
101 "Content-type: text/plain\r\n"
102 "Authorization: Token %s\r\n"
103 "Content-Length: %zd\r\n"
106 if ((
int)iv[0].iov_len >= len && !(iv[0].iov_base = (
char*)realloc(iv[0].iov_base, len *= 2))) {
107 free(iv[1].iov_base);
108 free(iv[0].iov_base);
114 fprintf(stderr,
"influxdb-c::post_http: iv[0] = '%s'\n", (
char*)iv[0].iov_base);
115 fprintf(stderr,
"influxdb-c::post_http: iv[1] = '%s'\n", (
char*)iv[1].iov_base);
117 addr.sin_family = AF_INET;
118 addr.sin_port = htons(c->
port);
120 struct hostent* resolved_host = gethostbyname(c->
host);
121 if (!resolved_host) {
122 free(iv[1].iov_base);
123 free(iv[0].iov_base);
126 memcpy(&addr.sin_addr, resolved_host->h_addr_list[0], resolved_host->h_length);
135 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
136 free(iv[1].iov_base);
137 free(iv[0].iov_base);
141 if (connect(sock, (
struct sockaddr*)(&addr),
sizeof(addr)) < 0) {
146 if (writev(sock, iv, 2) < (
int)(iv[0].iov_len + iv[1].iov_len)) {
152#define _GET_NEXT_CHAR() \
153 (ch = (len >= (int)iv[0].iov_len && \
154 (iv[0].iov_len = recv(sock, iv[0].iov_base, iv[0].iov_len, len = 0)) == (size_t)(-1) \
156 : *((char*)iv[0].iov_base + len++)))
157#define _LOOP_NEXT(statement) \
159 if (!(_GET_NEXT_CHAR())) { \
165#define _UNTIL(c) _LOOP_NEXT(if (ch == c) break;)
166#define _GET_NUMBER(n) _LOOP_NEXT(if (ch >= '0' && ch <= '9') n = n * 10 + (ch - '0'); else break;)
168 if ((_GET_NEXT_CHAR()) != c) \
179 _(
'n')
_(
't')
_(
'-')
_(
'L')
_(
'e')
_(
'n')
_(
'g')
_(
't')
_(
'h')
_(
':')
_(
' ')
_GET_NUMBER(content_length)
break;
194 free(iv[0].iov_base);
195 free(iv[1].iov_base);
196 return ret_code / 100 == 2 ? 0 : ret_code;
207 int ret_code = 0, len = 0;
221 int sock = -1,
ret = 0;
222 struct sockaddr_in addr;
224 addr.sin_family = AF_INET;
225 addr.sin_port = htons(c->
port);
226 if ((addr.sin_addr.s_addr = inet_addr(c->
host)) == INADDR_NONE) {
231 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
236 if (sendto(sock, line, len, 0, (
struct sockaddr*)&addr,
sizeof(addr)) < len)
274 curl_global_init(CURL_GLOBAL_ALL);
278 curl = curl_easy_init();
280 return CURLE_FAILED_INIT;
283 char* url_string = (
char*)malloc(len);
284 snprintf(url_string, len,
"http://%s:%d/api/v2/write?org=%s&bucket=%s&precision=%s", c->
host ? c->
host :
"localhost",
287 curl_easy_setopt(curl, CURLOPT_URL, url_string);
290 char* token_string = (
char*)malloc(120 *
sizeof(
char));
291 sprintf(token_string,
"Authorization: Token %s", c->
token);
293 struct curl_slist* list = NULL;
294 list = curl_slist_append(list, token_string);
295 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
298 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
299 curl_easy_setopt(curl, CURLOPT_USERAGENT,
"libcurl-agent/1.0");
302 res = curl_easy_perform(curl);
304 if (res != CURLE_OK) {
305 fprintf(stderr,
"curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
309 curl_easy_cleanup(curl);
310 curl_global_cleanup();
327 if (!(*buf = (
char*)malloc(len)))
339#define _APPEND(fmter...) \
341 if ((written = snprintf(*buf + used, len - used, ##fmter)) < 0) \
343 if (used + written >= len && !(*buf = (char*)realloc(*buf, len *= 2))) \
352 int written = 0, type = 0, last_type = 0;
353 unsigned long long i = 0;
361 type = va_arg(ap,
int);
391 d = va_arg(ap,
double);
396 i = va_arg(ap,
long long);
406 i = va_arg(ap,
long long);
413 type = va_arg(ap,
int);
427int _escaped_append(
char** dest,
size_t* len,
size_t* used,
const char* src,
const char* escape_seq) {
431 if ((i = strcspn(src, escape_seq)) > 0) {
432 if (*used + i > *len && !(*dest = (
char*)realloc(*dest, (*len) *= 2)))
434 strncpy(*dest + *used, src, i);
439 if (*used + 2 > *len && !(*dest = (
char*)realloc(*dest, (*len) *= 2)))
441 (*dest)[(*used)++] =
'\\';
442 (*dest)[(*used)++] = *src++;
return ret
Definition hashmap.h:118
void HASHMAP free(HASHMAP(t) *hashmap)
Free all memory used by the given hashmap.
Definition hashmap.h:121
struct _influx_v2_client_t influx_v2_client_t
int format_line(char **buf, int *len, size_t used,...)
Definition influxdb.h:314
#define _APPEND(fmter...)
int _format_line2(char **buf, va_list ap, size_t *, size_t)
Definition influxdb.h:338
#define IF_TYPE_TAG
Definition influxdb.h:67
#define IF_TYPE_ARG_END
Definition influxdb.h:65
#define IF_TYPE_FIELD_STRING
Definition influxdb.h:68
int send_udp(influx_client_t *c,...)
Definition influxdb.h:246
#define IF_TYPE_FIELD_BOOLEAN
Definition influxdb.h:71
int post_http_send_line(influx_client_t *c, char *buf, int len)
Definition influxdb.h:81
int _escaped_append(char **dest, size_t *len, size_t *used, const char *src, const char *escape_seq)
Definition influxdb.h:427
int _format_line(char **buf, va_list ap)
Definition influxdb.h:332
#define IF_TYPE_FIELD_INTEGER
Definition influxdb.h:70
int send_udp_line(influx_client_t *c, char *line, int len)
Definition influxdb.h:220
int post_curl(influx_v2_client_t *c,...)
Definition influxdb.h:263
#define IF_TYPE_TIMESTAMP
Definition influxdb.h:72
int post_http(influx_client_t *c,...)
Definition influxdb.h:204
#define IF_TYPE_FIELD_FLOAT
Definition influxdb.h:69
struct _influx_client_t influx_client_t
#define IF_TYPE_MEAS
Definition influxdb.h:66
int _begin_line(char **buf)
Definition influxdb.h:325
#define fprintf(stream, fmt,...)
Definition lf_flexpret_support.h:95
char * pwd
Definition influxdb.h:45
int port
Definition influxdb.h:42
char * usr
Definition influxdb.h:44
char * token
Definition influxdb.h:46
char * db
Definition influxdb.h:43
char * host
Definition influxdb.h:41
int port
Definition influxdb.h:51
char * bucket
Definition influxdb.h:53
char * usr
Definition influxdb.h:55
char * precision
Definition influxdb.h:54
char * token
Definition influxdb.h:57
char * org
Definition influxdb.h:52
char * host
Definition influxdb.h:50
char * pwd
Definition influxdb.h:56