2
* RRDTool - src/rrd_client.c
3
* Copyright (C) 2008 Florian octo Forster
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License as published by the
7
* Free Software Foundation; only version 2 of the License is applicable.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Florian octo Forster <octo at verplant.org>
20
* Sebastian tokkee Harl <sh at tokkee.org>
25
#include "rrd_client.h"
34
#include <sys/types.h>
35
#include <sys/socket.h>
41
#define ENODATA ENOENT
44
struct rrdc_response_s
51
typedef struct rrdc_response_s rrdc_response_t;
53
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
55
static FILE *sh = NULL;
56
static char *sd_path = NULL; /* cache the path for sd */
58
/* get_path: Return a path name appropriate to be sent to the daemon.
60
* When talking to a local daemon (thru a UNIX socket), relative path names
61
* are resolved to absolute path names to allow for transparent integration
62
* into existing solutions (as requested by Tobi). Else, absolute path names
63
* are not allowed, since path name translation is done by the server.
65
* One must hold `lock' when calling this function. */
66
static const char *get_path (const char *path, char *resolved_path) /* {{{ */
68
const char *ret = path;
72
|| (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
75
if (*path == '/') /* absolute path */
79
rrd_set_error ("absolute path names not allowed when talking "
80
"to a remote daemon");
83
/* else: nothing to do */
85
else /* relative path */
89
realpath (path, resolved_path);
92
/* else: nothing to do */
95
} /* }}} char *get_path */
97
/* One must hold `lock' when calling `close_connection'. */
98
static void close_connection (void) /* {{{ */
115
} /* }}} void close_connection */
117
static int buffer_add_string (const char *str, /* {{{ */
118
char **buffer_ret, size_t *buffer_size_ret)
126
buffer = *buffer_ret;
127
buffer_size = *buffer_size_ret;
132
while (buffer_pos < buffer_size)
136
buffer[buffer_pos] = ' ';
141
else if ((str[i] == ' ') || (str[i] == '\\'))
143
if (buffer_pos >= (buffer_size - 1))
145
buffer[buffer_pos] = '\\';
147
buffer[buffer_pos] = str[i];
152
buffer[buffer_pos] = str[i];
156
} /* while (buffer_pos < buffer_size) */
161
*buffer_ret = buffer + buffer_pos;
162
*buffer_size_ret = buffer_size - buffer_pos;
165
} /* }}} int buffer_add_string */
167
static int buffer_add_value (const char *value, /* {{{ */
168
char **buffer_ret, size_t *buffer_size_ret)
172
if (strncmp (value, "N:", 2) == 0)
173
snprintf (temp, sizeof (temp), "%lu:%s",
174
(unsigned long) time (NULL), value + 2);
176
strncpy (temp, value, sizeof (temp));
177
temp[sizeof (temp) - 1] = 0;
179
return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
180
} /* }}} int buffer_add_value */
182
/* Remove trailing newline (NL) and carriage return (CR) characters. Similar to
183
* the Perl function `chomp'. Returns the number of characters that have been
185
static int chomp (char *str) /* {{{ */
195
while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
203
} /* }}} int chomp */
205
static void response_free (rrdc_response_t *res) /* {{{ */
210
if (res->lines != NULL)
214
for (i = 0; i < res->lines_num; i++)
215
if (res->lines[i] != NULL)
216
free (res->lines[i]);
221
} /* }}} void response_free */
223
static int response_read (rrdc_response_t **ret_response) /* {{{ */
225
rrdc_response_t *ret;
235
ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
238
memset (ret, 0, sizeof (*ret));
242
buffer_ptr = fgets (buffer, sizeof (buffer), sh);
243
if (buffer_ptr == NULL)
247
ret->status = strtol (buffer, &ret->message, 0);
248
if (buffer == ret->message)
253
/* Skip leading whitespace of the status message */
254
ret->message += strspn (ret->message, " \t");
256
if (ret->status <= 0)
259
rrd_set_error("rrdcached: %s", ret->message);
264
ret->lines = (char **) malloc (sizeof (char *) * ret->status);
265
if (ret->lines == NULL)
270
memset (ret->lines, 0, sizeof (char *) * ret->status);
271
ret->lines_num = (size_t) ret->status;
273
for (i = 0; i < ret->lines_num; i++)
275
buffer_ptr = fgets (buffer, sizeof (buffer), sh);
276
if (buffer_ptr == NULL)
283
ret->lines[i] = strdup (buffer);
284
if (ret->lines[i] == NULL)
293
} /* }}} rrdc_response_t *response_read */
295
static int request (const char *buffer, size_t buffer_size, /* {{{ */
296
rrdc_response_t **ret_response)
299
rrdc_response_t *res;
304
status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
308
rrd_set_error("request: socket error (%d) while talking to rrdcached",
315
status = response_read (&res);
320
rrd_set_error("request: internal error while talking to rrdcached");
326
} /* }}} int request */
328
/* determine whether we are connected to the specified daemon_addr if
329
* NULL, return whether we are connected at all
331
int rrdc_is_connected(const char *daemon_addr) /* {{{ */
335
else if (daemon_addr == NULL)
337
/* here we have to handle the case i.e.
338
* UPDATE --daemon ...; UPDATEV (no --daemon) ...
339
* In other words: we have a cached connection,
340
* but it is not specified in the current command.
341
* Daemon is only implied in this case if set in ENV
343
if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
348
else if (strcmp(daemon_addr, sd_path) == 0)
353
} /* }}} int rrdc_is_connected */
355
static int rrdc_connect_unix (const char *path) /* {{{ */
357
struct sockaddr_un sa;
360
assert (path != NULL);
363
sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
370
memset (&sa, 0, sizeof (sa));
371
sa.sun_family = AF_UNIX;
372
strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
374
status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
382
sh = fdopen (sd, "r+");
391
} /* }}} int rrdc_connect_unix */
393
static int rrdc_connect_network (const char *addr_orig) /* {{{ */
395
struct addrinfo ai_hints;
396
struct addrinfo *ai_res;
397
struct addrinfo *ai_ptr;
398
char addr_copy[NI_MAXHOST];
402
assert (addr_orig != NULL);
405
strncpy(addr_copy, addr_orig, sizeof(addr_copy));
406
addr_copy[sizeof(addr_copy) - 1] = '\0';
410
memset (&ai_hints, 0, sizeof (ai_hints));
411
ai_hints.ai_flags = 0;
413
ai_hints.ai_flags |= AI_ADDRCONFIG;
415
ai_hints.ai_family = AF_UNSPEC;
416
ai_hints.ai_socktype = SOCK_STREAM;
419
if (*addr == '[') /* IPv6+port format */
421
/* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
424
port = strchr (addr, ']');
427
rrd_set_error("malformed address: %s", addr_orig);
439
rrd_set_error("garbage after address: %s", port);
442
} /* if (*addr == '[') */
445
port = rindex(addr, ':');
454
status = getaddrinfo (addr,
455
port == NULL ? RRDCACHED_DEFAULT_PORT : port,
459
rrd_set_error ("failed to resolve address `%s' (port %s): %s",
460
addr, port == NULL ? RRDCACHED_DEFAULT_PORT : port,
461
gai_strerror (status));
465
for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
467
sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
475
status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
483
sh = fdopen (sd, "r+");
491
assert (status == 0);
496
} /* }}} int rrdc_connect_network */
498
int rrdc_connect (const char *addr) /* {{{ */
503
addr = getenv (ENV_RRDCACHED_ADDRESS);
508
pthread_mutex_lock(&lock);
510
if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
512
/* connection to the same daemon; use cached connection */
513
pthread_mutex_unlock (&lock);
522
if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
523
status = rrdc_connect_unix (addr + strlen ("unix:"));
524
else if (addr[0] == '/')
525
status = rrdc_connect_unix (addr);
527
status = rrdc_connect_network(addr);
529
if (status == 0 && sd >= 0)
530
sd_path = strdup(addr);
533
char *err = rrd_test_error () ? rrd_get_error () : "Internal error";
534
/* err points the string that gets written to by rrd_set_error(), thus we
535
* cannot pass it to that function */
537
rrd_set_error("Unable to connect to rrdcached: %s",
539
? (err ? err : "Internal error")
540
: rrd_strerror (status));
545
pthread_mutex_unlock (&lock);
547
} /* }}} int rrdc_connect */
549
int rrdc_disconnect (void) /* {{{ */
551
pthread_mutex_lock (&lock);
555
pthread_mutex_unlock (&lock);
558
} /* }}} int rrdc_disconnect */
560
int rrdc_update (const char *filename, int values_num, /* {{{ */
561
const char * const *values)
567
rrdc_response_t *res;
570
char file_path[PATH_MAX];
572
memset (buffer, 0, sizeof (buffer));
573
buffer_ptr = &buffer[0];
574
buffer_free = sizeof (buffer);
576
status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
580
pthread_mutex_lock (&lock);
581
filename = get_path (filename, file_path);
582
if (filename == NULL)
584
pthread_mutex_unlock (&lock);
588
status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
591
pthread_mutex_unlock (&lock);
595
for (i = 0; i < values_num; i++)
597
status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
600
pthread_mutex_unlock (&lock);
605
assert (buffer_free < sizeof (buffer));
606
buffer_size = sizeof (buffer) - buffer_free;
607
assert (buffer[buffer_size - 1] == ' ');
608
buffer[buffer_size - 1] = '\n';
611
status = request (buffer, buffer_size, &res);
612
pthread_mutex_unlock (&lock);
617
status = res->status;
621
} /* }}} int rrdc_update */
623
int rrdc_flush (const char *filename) /* {{{ */
629
rrdc_response_t *res;
631
char file_path[PATH_MAX];
633
if (filename == NULL)
636
memset (buffer, 0, sizeof (buffer));
637
buffer_ptr = &buffer[0];
638
buffer_free = sizeof (buffer);
640
status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
644
pthread_mutex_lock (&lock);
645
filename = get_path (filename, file_path);
646
if (filename == NULL)
648
pthread_mutex_unlock (&lock);
652
status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
655
pthread_mutex_unlock (&lock);
659
assert (buffer_free < sizeof (buffer));
660
buffer_size = sizeof (buffer) - buffer_free;
661
assert (buffer[buffer_size - 1] == ' ');
662
buffer[buffer_size - 1] = '\n';
665
status = request (buffer, buffer_size, &res);
666
pthread_mutex_unlock (&lock);
671
status = res->status;
675
} /* }}} int rrdc_flush */
678
/* convenience function; if there is a daemon specified, or if we can
679
* detect one from the environment, then flush the file. Otherwise, no-op
681
int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
685
rrdc_connect(opt_daemon);
687
if (rrdc_is_connected(opt_daemon))
690
status = rrdc_flush (filename);
692
if (status != 0 && !rrd_test_error())
696
rrd_set_error("rrdc_flush (%s) failed: %s",
697
filename, rrd_strerror(status));
701
rrd_set_error("rrdc_flush (%s) failed with status %i.",
705
} /* if (rrdc_is_connected(..)) */
708
} /* }}} int rrdc_flush_if_daemon */
711
int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
716
rrdc_response_t *res;
721
/* Protocol example: {{{
723
* <- 5 Statistics follow
725
* <- UpdatesWritten: 0
726
* <- DataSetsWritten: 0
727
* <- TreeNodesNumber: 0
732
pthread_mutex_lock (&lock);
733
status = request ("STATS\n", strlen ("STATS\n"), &res);
734
pthread_mutex_unlock (&lock);
739
if (res->status <= 0)
747
for (i = 0; i < res->lines_num; i++)
755
value = strchr (key, ':');
761
while ((value[0] == ' ') || (value[0] == '\t'))
764
s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
767
memset (s, 0, sizeof (*s));
769
s->name = strdup (key);
772
if ((strcmp ("QueueLength", key) == 0)
773
|| (strcmp ("TreeDepth", key) == 0)
774
|| (strcmp ("TreeNodesNumber", key) == 0))
776
s->type = RRDC_STATS_TYPE_GAUGE;
777
s->value.gauge = strtod (value, &endptr);
779
else if ((strcmp ("DataSetsWritten", key) == 0)
780
|| (strcmp ("FlushesReceived", key) == 0)
781
|| (strcmp ("JournalBytes", key) == 0)
782
|| (strcmp ("JournalRotate", key) == 0)
783
|| (strcmp ("UpdatesReceived", key) == 0)
784
|| (strcmp ("UpdatesWritten", key) == 0))
786
s->type = RRDC_STATS_TYPE_COUNTER;
787
s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
795
/* Conversion failed */
813
} /* for (i = 0; i < res->lines_num; i++) */
822
} /* }}} int rrdc_stats_get */
824
void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
835
if (this->name != NULL)
837
free ((char *)this->name);
843
} /* while (this != NULL) */
844
} /* }}} void rrdc_stats_free */
847
* vim: set sw=2 sts=2 ts=8 et fdm=marker :