2 * libcollectdclient - src/libcollectdclient/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>
26 /* Set to C99 and POSIX code */
28 # include "standards.h"
29 #endif /* COLLECT_STANDARDS */
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
35 #include "lcc_features.h"
40 #include <sys/types.h>
41 #include <sys/socket.h>
51 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
52 * to no longer define it. We'll use the old, RFC 2553 value here. */
54 # define NI_MAXHOST 1025
57 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
60 # define EILSEQ EPROTO
62 # define EILSEQ EINVAL
66 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
67 * termination. They work for static buffers only, because they use `sizeof'.
68 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
69 * is very useful to add formatted stuff to the end of a buffer. */
70 #define SSTRCPY(d,s) do { \
71 strncpy ((d), (s), sizeof (d)); \
72 (d)[sizeof (d) - 1] = 0; \
75 #define SSTRCAT(d,s) do { \
76 size_t _l = strlen (d); \
77 strncpy ((d) + _l, (s), sizeof (d) - _l); \
78 (d)[sizeof (d) - 1] = 0; \
81 #define SSTRCATF(d, ...) do { \
82 char _b[sizeof (d)]; \
83 snprintf (_b, sizeof (_b), __VA_ARGS__); \
84 _b[sizeof (_b) - 1] = 0; \
89 #define LCC_SET_ERRSTR(c, ...) do { \
90 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
91 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
95 # define LCC_DEBUG(...) printf (__VA_ARGS__)
97 # define LCC_DEBUG(...) /**/
103 struct lcc_connection_s
109 struct lcc_response_s
116 typedef struct lcc_response_s lcc_response_t;
121 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
126 strerror_r (err, c->errbuf, sizeof (c->errbuf));
127 c->errbuf[sizeof (c->errbuf) - 1] = 0;
130 } /* }}} int lcc_set_errno */
132 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
134 __attribute__((malloc, nonnull (1)))
135 static char *lcc_strdup (const char *str) /* {{{ */
140 strsize = strlen (str) + 1;
141 ret = (char *) malloc (strsize);
143 memcpy (ret, str, strsize);
145 } /* }}} char *lcc_strdup */
147 __attribute__((nonnull (1, 2)))
148 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
156 assert (dest_size >= 3);
158 dest[dest_pos] = '"';
163 if ((dest_pos == (dest_size - 2))
164 || (src[src_pos] == 0))
167 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
169 /* Check if there is enough space for both characters.. */
170 if (dest_pos == (dest_size - 3))
173 dest[dest_pos] = '\\';
177 dest[dest_pos] = src[src_pos];
182 assert (dest_pos <= (dest_size - 2));
184 dest[dest_pos] = '"';
192 } /* }}} char *lcc_strescape */
194 /* lcc_chomp: Removes all control-characters at the end of a string. */
195 static void lcc_chomp (char *str) /* {{{ */
199 str_len = strlen (str);
202 if (str[str_len - 1] >= 32)
204 str[str_len - 1] = 0;
207 } /* }}} void lcc_chomp */
209 static void lcc_response_free (lcc_response_t *res) /* {{{ */
216 for (i = 0; i < res->lines_num; i++)
217 free (res->lines[i]);
220 } /* }}} void lcc_response_free */
222 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
226 LCC_DEBUG ("send: --> %s\n", command);
228 status = fprintf (c->fh, "%s\r\n", command);
231 lcc_set_errno (c, errno);
236 } /* }}} int lcc_send */
238 static int lcc_receive (lcc_connection_t *c, /* {{{ */
239 lcc_response_t *ret_res)
246 memset (&res, 0, sizeof (res));
248 /* Read the first line, containing the status and a message */
249 ptr = fgets (buffer, sizeof (buffer), c->fh);
252 lcc_set_errno (c, errno);
256 LCC_DEBUG ("receive: <-- %s\n", buffer);
258 /* Convert the leading status to an integer and make `ptr' to point to the
259 * beginning of the message. */
262 res.status = strtol (buffer, &ptr, 0);
263 if ((errno != 0) || (ptr == &buffer[0]))
265 lcc_set_errno (c, errno);
269 /* Skip white spaces after the status number */
270 while ((*ptr == ' ') || (*ptr == '\t'))
273 /* Now copy the message. */
274 strncpy (res.message, ptr, sizeof (res.message));
275 res.message[sizeof (res.message) - 1] = 0;
277 /* Error or no lines follow: We're done. */
280 memcpy (ret_res, &res, sizeof (res));
284 /* Allocate space for the char-pointers */
285 res.lines_num = (size_t) res.status;
287 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
288 if (res.lines == NULL)
290 lcc_set_errno (c, ENOMEM);
294 /* Now receive all the lines */
295 for (i = 0; i < res.lines_num; i++)
297 ptr = fgets (buffer, sizeof (buffer), c->fh);
300 lcc_set_errno (c, errno);
304 LCC_DEBUG ("receive: <-- %s\n", buffer);
306 res.lines[i] = lcc_strdup (buffer);
307 if (res.lines[i] == NULL)
309 lcc_set_errno (c, ENOMEM);
314 /* Check if the for-loop exited with an error. */
315 if (i < res.lines_num)
326 memcpy (ret_res, &res, sizeof (res));
328 } /* }}} int lcc_receive */
330 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
331 const char *command, lcc_response_t *ret_res)
338 lcc_set_errno (c, EBADF);
342 status = lcc_send (c, command);
346 memset (&res, 0, sizeof (res));
347 status = lcc_receive (c, &res);
349 memcpy (ret_res, &res, sizeof (*ret_res));
352 } /* }}} int lcc_sendreceive */
354 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
356 struct sockaddr_un sa;
361 assert (c->fh == NULL);
362 assert (path != NULL);
364 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
366 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
369 lcc_set_errno (c, errno);
373 memset (&sa, 0, sizeof (sa));
374 sa.sun_family = AF_UNIX;
375 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
377 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
380 lcc_set_errno (c, errno);
385 c->fh = fdopen (fd, "r+");
388 lcc_set_errno (c, errno);
394 } /* }}} int lcc_open_unixsocket */
396 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
397 const char *addr_orig)
399 struct addrinfo ai_hints;
400 struct addrinfo *ai_res;
401 struct addrinfo *ai_ptr;
402 char addr_copy[NI_MAXHOST];
409 assert (c->fh == NULL);
410 assert (addr_orig != NULL);
412 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
413 addr_copy[sizeof(addr_copy) - 1] = '\0';
416 memset (&ai_hints, 0, sizeof (ai_hints));
417 ai_hints.ai_flags = 0;
419 ai_hints.ai_flags |= AI_ADDRCONFIG;
421 ai_hints.ai_family = AF_UNSPEC;
422 ai_hints.ai_socktype = SOCK_STREAM;
425 if (*addr == '[') /* IPv6+port format */
427 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
430 port = strchr (addr, ']');
433 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
445 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
448 } /* if (*addr = ']') */
449 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
451 port = strrchr (addr, ':');
460 status = getaddrinfo (addr,
461 port == NULL ? LCC_DEFAULT_PORT : port,
465 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
469 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
471 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
479 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
488 c->fh = fdopen (fd, "r+");
497 assert (status == 0);
503 lcc_set_errno (c, status);
508 } /* }}} int lcc_open_netsocket */
510 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
518 assert (c->fh == NULL);
519 assert (addr != NULL);
521 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
522 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
523 else if (addr[0] == '/')
524 status = lcc_open_unixsocket (c, addr);
526 status = lcc_open_netsocket (c, addr);
529 } /* }}} int lcc_open_socket */
534 unsigned int lcc_version (void) /* {{{ */
536 return (LCC_VERSION);
537 } /* }}} unsigned int lcc_version */
539 const char *lcc_version_string (void) /* {{{ */
541 return (LCC_VERSION_STRING);
542 } /* }}} const char *lcc_version_string */
544 const char *lcc_version_extra (void) /* {{{ */
546 return (LCC_VERSION_EXTRA);
547 } /* }}} const char *lcc_version_extra */
549 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
560 c = (lcc_connection_t *) malloc (sizeof (*c));
563 memset (c, 0, sizeof (*c));
565 status = lcc_open_socket (c, address);
574 } /* }}} int lcc_connect */
576 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
589 } /* }}} int lcc_disconnect */
591 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
592 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
594 char ident_str[6 * LCC_NAME_LEN];
595 char ident_esc[12 * LCC_NAME_LEN];
596 char command[14 * LCC_NAME_LEN];
600 gauge_t *values = NULL;
601 char **values_names = NULL;
611 lcc_set_errno (c, EINVAL);
615 /* Build a commend with an escaped version of the identifier string. */
616 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
620 snprintf (command, sizeof (command), "GETVAL %s",
621 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
622 command[sizeof (command) - 1] = 0;
624 /* Send talk to the daemon.. */
625 status = lcc_sendreceive (c, command, &res);
631 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
632 lcc_response_free (&res);
636 values_num = res.lines_num;
638 #define BAIL_OUT(e) do { \
639 lcc_set_errno (c, (e)); \
641 if (values_names != NULL) { \
642 for (i = 0; i < values_num; i++) { \
643 free (values_names[i]); \
646 free (values_names); \
647 lcc_response_free (&res); \
651 /* If neither the values nor the names are requested, return here.. */
652 if ((ret_values == NULL) && (ret_values_names == NULL))
654 if (ret_values_num != NULL)
655 *ret_values_num = values_num;
656 lcc_response_free (&res);
660 /* Allocate space for the values */
661 if (ret_values != NULL)
663 values = (gauge_t *) malloc (values_num * sizeof (*values));
668 if (ret_values_names != NULL)
670 values_names = (char **) calloc (values_num, sizeof (*values_names));
671 if (values_names == NULL)
675 for (i = 0; i < res.lines_num; i++)
682 value = strchr (key, '=');
693 values[i] = strtod (value, &endptr);
695 if ((endptr == value) || (errno != 0))
699 if (values_names != NULL)
701 values_names[i] = lcc_strdup (key);
702 if (values_names[i] == NULL)
705 } /* for (i = 0; i < res.lines_num; i++) */
707 if (ret_values_num != NULL)
708 *ret_values_num = values_num;
709 if (ret_values != NULL)
710 *ret_values = values;
711 if (ret_values_names != NULL)
712 *ret_values_names = values_names;
715 } /* }}} int lcc_getval */
717 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
719 char ident_str[6 * LCC_NAME_LEN];
720 char ident_esc[12 * LCC_NAME_LEN];
721 char command[1024] = "";
726 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
727 || (vl->values == NULL) || (vl->values_types == NULL))
729 lcc_set_errno (c, EINVAL);
733 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
738 SSTRCATF (command, "PUTVAL %s",
739 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
741 if (vl->interval > 0)
742 SSTRCATF (command, " interval=%i", vl->interval);
745 SSTRCATF (command, "%u", (unsigned int) vl->time);
747 SSTRCAT (command, "N");
749 for (i = 0; i < vl->values_len; i++)
751 if (vl->values_types[i] == LCC_TYPE_COUNTER)
752 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
753 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
755 if (isnan (vl->values[i].gauge))
756 SSTRCPY (command, ":U");
758 SSTRCATF (command, ":%g", vl->values[i].gauge);
760 } /* for (i = 0; i < vl->values_len; i++) */
762 status = lcc_sendreceive (c, command, &res);
768 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
769 lcc_response_free (&res);
773 lcc_response_free (&res);
775 } /* }}} int lcc_putval */
777 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
778 lcc_identifier_t *ident, int timeout)
780 char command[1024] = "";
786 lcc_set_errno (c, EINVAL);
790 SSTRCPY (command, "FLUSH");
793 SSTRCATF (command, " timeout=%i", timeout);
797 char buffer[2 * LCC_NAME_LEN];
798 SSTRCATF (command, " plugin=%s",
799 lcc_strescape (buffer, plugin, sizeof (buffer)));
804 char ident_str[6 * LCC_NAME_LEN];
805 char ident_esc[12 * LCC_NAME_LEN];
807 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
811 SSTRCATF (command, " identifier=%s",
812 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
815 status = lcc_sendreceive (c, command, &res);
821 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
822 lcc_response_free (&res);
826 lcc_response_free (&res);
828 } /* }}} int lcc_flush */
830 /* TODO: Implement lcc_putnotif */
832 int lcc_listval (lcc_connection_t *c, /* {{{ */
833 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
839 lcc_identifier_t *ident;
845 if ((ret_ident == NULL) || (ret_ident_num == NULL))
847 lcc_set_errno (c, EINVAL);
851 status = lcc_sendreceive (c, "LISTVAL", &res);
857 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
858 lcc_response_free (&res);
862 ident_num = res.lines_num;
863 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
866 lcc_response_free (&res);
867 lcc_set_errno (c, ENOMEM);
871 for (i = 0; i < res.lines_num; i++)
876 /* First field is the time. */
877 time_str = res.lines[i];
879 /* Set `ident_str' to the beginning of the second field. */
880 ident_str = time_str;
881 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
883 while ((*ident_str == ' ') || (*ident_str == '\t'))
891 lcc_set_errno (c, EILSEQ);
896 status = lcc_string_to_identifier (c, ident + i, ident_str);
901 lcc_response_free (&res);
910 *ret_ident_num = ident_num;
913 } /* }}} int lcc_listval */
915 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
918 return ("Invalid object");
920 } /* }}} const char *lcc_strerror */
922 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
923 char *string, size_t string_size, const lcc_identifier_t *ident)
925 if ((string == NULL) || (string_size < 6) || (ident == NULL))
927 lcc_set_errno (c, EINVAL);
931 if (ident->plugin_instance[0] == 0)
933 if (ident->type_instance[0] == 0)
934 snprintf (string, string_size, "%s/%s/%s",
939 snprintf (string, string_size, "%s/%s/%s-%s",
943 ident->type_instance);
947 if (ident->type_instance[0] == 0)
948 snprintf (string, string_size, "%s/%s-%s/%s",
951 ident->plugin_instance,
954 snprintf (string, string_size, "%s/%s-%s/%s-%s",
957 ident->plugin_instance,
959 ident->type_instance);
962 string[string_size - 1] = 0;
964 } /* }}} int lcc_identifier_to_string */
966 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
967 lcc_identifier_t *ident, const char *string)
972 char *plugin_instance;
976 string_copy = lcc_strdup (string);
977 if (string_copy == NULL)
979 lcc_set_errno (c, ENOMEM);
984 plugin = strchr (host, '/');
987 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
994 type = strchr (plugin, '/');
997 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1004 plugin_instance = strchr (plugin, '-');
1005 if (plugin_instance != NULL)
1007 *plugin_instance = 0;
1011 type_instance = strchr (type, '-');
1012 if (type_instance != NULL)
1018 memset (ident, 0, sizeof (*ident));
1020 SSTRCPY (ident->host, host);
1021 SSTRCPY (ident->plugin, plugin);
1022 if (plugin_instance != NULL)
1023 SSTRCPY (ident->plugin_instance, plugin_instance);
1024 SSTRCPY (ident->type, type);
1025 if (type_instance != NULL)
1026 SSTRCPY (ident->type_instance, type_instance);
1030 } /* }}} int lcc_string_to_identifier */
1032 /* vim: set sw=2 sts=2 et fdm=marker : */