2 * RRDTool - src/rrd_client.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Florian octo Forster <octo at verplant.org>
25 * Sebastian tokkee Harl <sh at tokkee.org>
30 #include "rrd_client.h"
39 #include <sys/types.h>
40 #include <sys/socket.h>
46 #define ENODATA ENOENT
49 struct rrdc_response_s
56 typedef struct rrdc_response_s rrdc_response_t;
58 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
60 static FILE *sh = NULL;
61 static char *sd_path = NULL; /* cache the path for sd */
63 /* get_path: Return a path name appropriate to be sent to the daemon.
65 * When talking to a local daemon (thru a UNIX socket), relative path names
66 * are resolved to absolute path names to allow for transparent integration
67 * into existing solutions (as requested by Tobi). Else, absolute path names
68 * are not allowed, since path name translation is done by the server.
70 * One must hold `lock' when calling this function. */
71 static const char *get_path (const char *path, char *resolved_path) /* {{{ */
73 const char *ret = path;
77 || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
82 ret = realpath(path, resolved_path);
84 rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno));
89 if (*path == '/') /* not absolute path */
91 rrd_set_error ("absolute path names not allowed when talking "
92 "to a remote daemon");
98 } /* }}} char *get_path */
100 static size_t strsplit (char *string, char **fields, size_t size) /* {{{ */
109 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
119 } /* }}} size_t strsplit */
121 static int parse_header (char *line, /* {{{ */
122 char **ret_key, char **ret_value)
128 tmp = strchr (line, ':');
137 while ((tmp[0] == ' ') || (tmp[0] == '\t'));
144 } /* }}} int parse_header */
146 static int parse_ulong_header (char *line, /* {{{ */
147 char **ret_key, unsigned long *ret_value)
154 status = parse_header (line, ret_key, &str_value);
160 *ret_value = (unsigned long) strtol (str_value, &endptr, /* base = */ 0);
161 if ((endptr == str_value) || (errno != 0))
165 } /* }}} int parse_ulong_header */
167 static int parse_char_array_header (char *line, /* {{{ */
168 char **ret_key, char **array, size_t array_len, int alloc)
170 char *tmp_array[array_len];
176 status = parse_header (line, ret_key, &value);
180 num = strsplit (value, tmp_array, array_len);
181 if (num != array_len)
186 memcpy (array, tmp_array, sizeof (tmp_array));
192 for (i = 0; i < array_len; i++)
193 array[i] = strdup (tmp_array[i]);
197 } /* }}} int parse_char_array_header */
199 static int parse_value_array_header (char *line, /* {{{ */
200 time_t *ret_time, rrd_value_t *array, size_t array_len)
203 char *str_array[array_len];
209 status = parse_char_array_header (line, &str_key,
210 str_array, array_len, /* alloc = */ 0);
216 *ret_time = (time_t) strtol (str_key, &endptr, /* base = */ 10);
217 if ((endptr == str_key) || (errno != 0))
220 for (i = 0; i < array_len; i++)
223 array[i] = (rrd_value_t) strtod (str_array[i], &endptr);
224 if ((endptr == str_array[i]) || (errno != 0))
229 } /* }}} int parse_value_array_header */
231 /* One must hold `lock' when calling `close_connection'. */
232 static void close_connection (void) /* {{{ */
249 } /* }}} void close_connection */
251 static int buffer_add_string (const char *str, /* {{{ */
252 char **buffer_ret, size_t *buffer_size_ret)
260 buffer = *buffer_ret;
261 buffer_size = *buffer_size_ret;
266 while (buffer_pos < buffer_size)
270 buffer[buffer_pos] = ' ';
275 else if ((str[i] == ' ') || (str[i] == '\\'))
277 if (buffer_pos >= (buffer_size - 1))
279 buffer[buffer_pos] = '\\';
281 buffer[buffer_pos] = str[i];
286 buffer[buffer_pos] = str[i];
290 } /* while (buffer_pos < buffer_size) */
295 *buffer_ret = buffer + buffer_pos;
296 *buffer_size_ret = buffer_size - buffer_pos;
299 } /* }}} int buffer_add_string */
301 static int buffer_add_value (const char *value, /* {{{ */
302 char **buffer_ret, size_t *buffer_size_ret)
306 if (strncmp (value, "N:", 2) == 0)
307 snprintf (temp, sizeof (temp), "%lu:%s",
308 (unsigned long) time (NULL), value + 2);
310 strncpy (temp, value, sizeof (temp));
311 temp[sizeof (temp) - 1] = 0;
313 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
314 } /* }}} int buffer_add_value */
316 /* Remove trailing newline (NL) and carriage return (CR) characters. Similar to
317 * the Perl function `chomp'. Returns the number of characters that have been
319 static int chomp (char *str) /* {{{ */
329 while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r')))
337 } /* }}} int chomp */
339 static void response_free (rrdc_response_t *res) /* {{{ */
344 if (res->lines != NULL)
348 for (i = 0; i < res->lines_num; i++)
349 if (res->lines[i] != NULL)
350 free (res->lines[i]);
355 } /* }}} void response_free */
357 static int response_read (rrdc_response_t **ret_response) /* {{{ */
359 rrdc_response_t *ret;
369 ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
372 memset (ret, 0, sizeof (*ret));
376 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
377 if (buffer_ptr == NULL) {
383 ret->status = strtol (buffer, &ret->message, 0);
384 if (buffer == ret->message)
390 /* Skip leading whitespace of the status message */
391 ret->message += strspn (ret->message, " \t");
393 if (ret->status <= 0)
396 rrd_set_error("rrdcached: %s", ret->message);
401 ret->lines = (char **) malloc (sizeof (char *) * ret->status);
402 if (ret->lines == NULL)
408 memset (ret->lines, 0, sizeof (char *) * ret->status);
409 ret->lines_num = (size_t) ret->status;
411 for (i = 0; i < ret->lines_num; i++)
413 buffer_ptr = fgets (buffer, sizeof (buffer), sh);
414 if (buffer_ptr == NULL)
422 ret->lines[i] = strdup (buffer);
423 if (ret->lines[i] == NULL)
433 } /* }}} rrdc_response_t *response_read */
435 static int request (const char *buffer, size_t buffer_size, /* {{{ */
436 rrdc_response_t **ret_response)
439 rrdc_response_t *res;
444 status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
448 rrd_set_error("request: socket error (%d) while talking to rrdcached",
455 status = response_read (&res);
460 rrd_set_error("request: internal error while talking to rrdcached");
466 } /* }}} int request */
468 /* determine whether we are connected to the specified daemon_addr if
469 * NULL, return whether we are connected at all
471 int rrdc_is_connected(const char *daemon_addr) /* {{{ */
475 else if (daemon_addr == NULL)
477 /* here we have to handle the case i.e.
478 * UPDATE --daemon ...; UPDATEV (no --daemon) ...
479 * In other words: we have a cached connection,
480 * but it is not specified in the current command.
481 * Daemon is only implied in this case if set in ENV
483 if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
488 else if (strcmp(daemon_addr, sd_path) == 0)
493 } /* }}} int rrdc_is_connected */
495 static int rrdc_connect_unix (const char *path) /* {{{ */
497 struct sockaddr_un sa;
500 assert (path != NULL);
503 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
510 memset (&sa, 0, sizeof (sa));
511 sa.sun_family = AF_UNIX;
512 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
514 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
522 sh = fdopen (sd, "r+");
531 } /* }}} int rrdc_connect_unix */
533 static int rrdc_connect_network (const char *addr_orig) /* {{{ */
535 struct addrinfo ai_hints;
536 struct addrinfo *ai_res;
537 struct addrinfo *ai_ptr;
538 char addr_copy[NI_MAXHOST];
542 assert (addr_orig != NULL);
545 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
546 addr_copy[sizeof(addr_copy) - 1] = '\0';
550 memset (&ai_hints, 0, sizeof (ai_hints));
551 ai_hints.ai_flags = 0;
553 ai_hints.ai_flags |= AI_ADDRCONFIG;
555 ai_hints.ai_family = AF_UNSPEC;
556 ai_hints.ai_socktype = SOCK_STREAM;
559 if (*addr == '[') /* IPv6+port format */
561 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
564 port = strchr (addr, ']');
567 rrd_set_error("malformed address: %s", addr_orig);
579 rrd_set_error("garbage after address: %s", port);
582 } /* if (*addr == '[') */
585 port = rindex(addr, ':');
594 status = getaddrinfo (addr,
595 port == NULL ? RRDCACHED_DEFAULT_PORT : port,
599 rrd_set_error ("failed to resolve address `%s' (port %s): %s",
600 addr, port == NULL ? RRDCACHED_DEFAULT_PORT : port,
601 gai_strerror (status));
605 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
607 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
615 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
623 sh = fdopen (sd, "r+");
631 assert (status == 0);
636 } /* }}} int rrdc_connect_network */
638 int rrdc_connect (const char *addr) /* {{{ */
643 addr = getenv (ENV_RRDCACHED_ADDRESS);
648 pthread_mutex_lock(&lock);
650 if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
652 /* connection to the same daemon; use cached connection */
653 pthread_mutex_unlock (&lock);
662 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
663 status = rrdc_connect_unix (addr + strlen ("unix:"));
664 else if (addr[0] == '/')
665 status = rrdc_connect_unix (addr);
667 status = rrdc_connect_network(addr);
669 if (status == 0 && sd >= 0)
670 sd_path = strdup(addr);
673 char *err = rrd_test_error () ? rrd_get_error () : "Internal error";
674 /* err points the string that gets written to by rrd_set_error(), thus we
675 * cannot pass it to that function */
677 rrd_set_error("Unable to connect to rrdcached: %s",
679 ? (err ? err : "Internal error")
680 : rrd_strerror (status));
685 pthread_mutex_unlock (&lock);
687 } /* }}} int rrdc_connect */
689 int rrdc_disconnect (void) /* {{{ */
691 pthread_mutex_lock (&lock);
695 pthread_mutex_unlock (&lock);
698 } /* }}} int rrdc_disconnect */
700 int rrdc_update (const char *filename, int values_num, /* {{{ */
701 const char * const *values)
707 rrdc_response_t *res;
710 char file_path[PATH_MAX];
712 memset (buffer, 0, sizeof (buffer));
713 buffer_ptr = &buffer[0];
714 buffer_free = sizeof (buffer);
716 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
720 pthread_mutex_lock (&lock);
721 filename = get_path (filename, file_path);
722 if (filename == NULL)
724 pthread_mutex_unlock (&lock);
728 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
731 pthread_mutex_unlock (&lock);
735 for (i = 0; i < values_num; i++)
737 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
740 pthread_mutex_unlock (&lock);
745 assert (buffer_free < sizeof (buffer));
746 buffer_size = sizeof (buffer) - buffer_free;
747 assert (buffer[buffer_size - 1] == ' ');
748 buffer[buffer_size - 1] = '\n';
751 status = request (buffer, buffer_size, &res);
752 pthread_mutex_unlock (&lock);
757 status = res->status;
761 } /* }}} int rrdc_update */
763 int rrdc_flush (const char *filename) /* {{{ */
769 rrdc_response_t *res;
771 char file_path[PATH_MAX];
773 if (filename == NULL)
776 memset (buffer, 0, sizeof (buffer));
777 buffer_ptr = &buffer[0];
778 buffer_free = sizeof (buffer);
780 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
784 pthread_mutex_lock (&lock);
785 filename = get_path (filename, file_path);
786 if (filename == NULL)
788 pthread_mutex_unlock (&lock);
792 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
795 pthread_mutex_unlock (&lock);
799 assert (buffer_free < sizeof (buffer));
800 buffer_size = sizeof (buffer) - buffer_free;
801 assert (buffer[buffer_size - 1] == ' ');
802 buffer[buffer_size - 1] = '\n';
805 status = request (buffer, buffer_size, &res);
806 pthread_mutex_unlock (&lock);
811 status = res->status;
815 } /* }}} int rrdc_flush */
817 int rrdc_fetch (const char *filename, /* {{{ */
819 time_t *ret_start, time_t *ret_end,
820 unsigned long *ret_step,
821 unsigned long *ret_ds_num,
822 char ***ret_ds_names,
823 rrd_value_t **ret_data)
829 rrdc_response_t *res;
830 char path_buffer[PATH_MAX];
831 const char *path_ptr;
834 unsigned long flush_version;
839 unsigned long ds_num;
850 if ((filename == NULL) || (cf == NULL))
853 /* Send request {{{ */
854 memset (buffer, 0, sizeof (buffer));
855 buffer_ptr = &buffer[0];
856 buffer_free = sizeof (buffer);
858 status = buffer_add_string ("FETCH", &buffer_ptr, &buffer_free);
862 /* change to path for rrdcached */
863 path_ptr = get_path (filename, path_buffer);
864 if (path_ptr == NULL)
867 status = buffer_add_string (path_ptr, &buffer_ptr, &buffer_free);
871 status = buffer_add_string (cf, &buffer_ptr, &buffer_free);
875 if ((ret_start != NULL) && (*ret_start > 0))
878 snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_start);
879 tmp[sizeof (tmp) - 1] = 0;
880 status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
884 if ((ret_end != NULL) && (*ret_end > 0))
886 snprintf (tmp, sizeof (tmp), "%lu", (unsigned long) *ret_end);
887 tmp[sizeof (tmp) - 1] = 0;
888 status = buffer_add_string (tmp, &buffer_ptr, &buffer_free);
894 assert (buffer_free < sizeof (buffer));
895 buffer_size = sizeof (buffer) - buffer_free;
896 assert (buffer[buffer_size - 1] == ' ');
897 buffer[buffer_size - 1] = '\n';
900 status = request (buffer, buffer_size, &res);
904 status = res->status;
907 rrd_set_error ("rrdcached: %s", res->message);
911 /* }}} Send request */
918 /* Macros to make error handling a little easier (i. e. less to type and
919 * read. `BAIL_OUT' sets the error message, frees all dynamically allocated
920 * variables and returns the provided status code. */
921 #define BAIL_OUT(status, ...) do { \
922 rrd_set_error ("rrdc_fetch: " __VA_ARGS__); \
924 if (ds_names != 0) { size_t k; for (k = 0; k < ds_num; k++) free (ds_names[k]); } \
926 response_free (res); \
930 #define READ_NUMERIC_FIELD(name,type,var) do { \
932 unsigned long value; \
933 assert (current_line < res->lines_num); \
934 status = parse_ulong_header (res->lines[current_line], &key, &value); \
936 BAIL_OUT (-1, "Unable to parse header `%s'", name); \
937 if (strcasecmp (key, name) != 0) \
938 BAIL_OUT (-1, "Unexpected header line: Expected `%s', got `%s'", name, key); \
939 var = (type) value; \
943 if (res->lines_num < 1)
944 BAIL_OUT (-1, "Premature end of response packet");
946 /* We're making some very strong assumptions about the fields below. We
947 * therefore check the version of the `flush' command first, so that later
948 * versions can change the order of fields and it's easier to implement
949 * backwards compatibility. */
950 READ_NUMERIC_FIELD ("FlushVersion", unsigned long, flush_version);
951 if (flush_version != 1)
952 BAIL_OUT (-1, "Don't know how to handle flush format version %lu.",
955 if (res->lines_num < 5)
956 BAIL_OUT (-1, "Premature end of response packet");
958 READ_NUMERIC_FIELD ("Start", time_t, start);
959 READ_NUMERIC_FIELD ("End", time_t, end);
961 BAIL_OUT (-1, "Malformed start and end times: start = %lu; end = %lu;",
962 (unsigned long) start,
963 (unsigned long) end);
965 READ_NUMERIC_FIELD ("Step", unsigned long, step);
967 BAIL_OUT (-1, "Invalid number for Step: %lu", step);
969 READ_NUMERIC_FIELD ("DSCount", unsigned long, ds_num);
971 BAIL_OUT (-1, "Invalid number for DSCount: %lu", ds_num);
973 /* It's time to allocate some memory */
974 ds_names = calloc ((size_t) ds_num, sizeof (*ds_names));
975 if (ds_names == NULL)
976 BAIL_OUT (-1, "Out of memory");
978 status = parse_char_array_header (res->lines[current_line],
979 &str_tmp, ds_names, (size_t) ds_num, /* alloc = */ 1);
981 BAIL_OUT (-1, "Unable to parse header `DSName'");
982 if (strcasecmp ("DSName", str_tmp) != 0)
983 BAIL_OUT (-1, "Unexpected header line: Expected `DSName', got `%s'", str_tmp);
986 data_size = ds_num * (end - start) / step;
988 BAIL_OUT (-1, "No data returned or headers invalid.");
990 if (res->lines_num != (6 + (data_size / ds_num)))
991 BAIL_OUT (-1, "Got %zu lines, expected %zu",
992 res->lines_num, (6 + (data_size / ds_num)));
994 data = calloc (data_size, sizeof (*data));
996 BAIL_OUT (-1, "Out of memory");
1000 for (t = start + step; t <= end; t += step, current_line++)
1004 assert (current_line < res->lines_num);
1006 status = parse_value_array_header (res->lines[current_line],
1007 &tmp, data + data_fill, (size_t) ds_num);
1009 BAIL_OUT (-1, "Cannot parse value line");
1011 data_fill += (size_t) ds_num;
1017 *ret_ds_num = ds_num;
1018 *ret_ds_names = ds_names;
1021 response_free (res);
1023 #undef READ_NUMERIC_FIELD
1025 } /* }}} int rrdc_flush */
1027 /* convenience function; if there is a daemon specified, or if we can
1028 * detect one from the environment, then flush the file. Otherwise, no-op
1030 int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
1034 rrdc_connect(opt_daemon);
1036 if (rrdc_is_connected(opt_daemon))
1039 status = rrdc_flush (filename);
1041 if (status != 0 && !rrd_test_error())
1045 rrd_set_error("rrdc_flush (%s) failed: %s",
1046 filename, rrd_strerror(status));
1048 else if (status < 0)
1050 rrd_set_error("rrdc_flush (%s) failed with status %i.",
1054 } /* if (rrdc_is_connected(..)) */
1057 } /* }}} int rrdc_flush_if_daemon */
1060 int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
1065 rrdc_response_t *res;
1070 /* Protocol example: {{{
1072 * <- 5 Statistics follow
1074 * <- UpdatesWritten: 0
1075 * <- DataSetsWritten: 0
1076 * <- TreeNodesNumber: 0
1081 pthread_mutex_lock (&lock);
1082 status = request ("STATS\n", strlen ("STATS\n"), &res);
1083 pthread_mutex_unlock (&lock);
1088 if (res->status <= 0)
1090 response_free (res);
1096 for (i = 0; i < res->lines_num; i++)
1103 key = res->lines[i];
1104 value = strchr (key, ':');
1110 while ((value[0] == ' ') || (value[0] == '\t'))
1113 s = (rrdc_stats_t *) malloc (sizeof (rrdc_stats_t));
1116 memset (s, 0, sizeof (*s));
1118 s->name = strdup (key);
1121 if ((strcmp ("QueueLength", key) == 0)
1122 || (strcmp ("TreeDepth", key) == 0)
1123 || (strcmp ("TreeNodesNumber", key) == 0))
1125 s->type = RRDC_STATS_TYPE_GAUGE;
1126 s->value.gauge = strtod (value, &endptr);
1128 else if ((strcmp ("DataSetsWritten", key) == 0)
1129 || (strcmp ("FlushesReceived", key) == 0)
1130 || (strcmp ("JournalBytes", key) == 0)
1131 || (strcmp ("JournalRotate", key) == 0)
1132 || (strcmp ("UpdatesReceived", key) == 0)
1133 || (strcmp ("UpdatesWritten", key) == 0))
1135 s->type = RRDC_STATS_TYPE_COUNTER;
1136 s->value.counter = (uint64_t) strtoll (value, &endptr, /* base = */ 0);
1144 /* Conversion failed */
1145 if (endptr == value)
1162 } /* for (i = 0; i < res->lines_num; i++) */
1164 response_free (res);
1171 } /* }}} int rrdc_stats_get */
1173 void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
1178 while (this != NULL)
1184 if (this->name != NULL)
1186 free ((char *)this->name);
1192 } /* while (this != NULL) */
1193 } /* }}} void rrdc_stats_free */
1196 * vim: set sw=2 sts=2 ts=8 et fdm=marker :