X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_client.c;h=043a144ad455ab841d21bc9bcea4d0d325828a95;hp=04b54a8042ef29f27a70a68ae0af3e18c40d4a6c;hb=e5b05bec82bbff5db8add4e58dd6f0fcf2670291;hpb=7953cece011b214be4a2663beaa46149c8c58eee diff --git a/src/rrd_client.c b/src/rrd_client.c index 04b54a8..043a144 100644 --- a/src/rrd_client.c +++ b/src/rrd_client.c @@ -2,18 +2,23 @@ * RRDTool - src/rrd_client.c * Copyright (C) 2008-2010 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. * * Authors: * Florian octo Forster @@ -68,30 +73,31 @@ static const char *get_path (const char *path, char *resolved_path) /* {{{ */ const char *ret = path; int is_unix = 0; + if ((path == NULL) || (resolved_path == NULL) || (sd_path == NULL)) + return (NULL); + if ((*sd_path == '/') || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0)) is_unix = 1; - if (*path == '/') /* absolute path */ + if (is_unix) { - if (! is_unix) - { - rrd_set_error ("absolute path names not allowed when talking " - "to a remote daemon"); - return (NULL); - } - /* else: nothing to do */ + ret = realpath(path, resolved_path); + if (ret == NULL) + rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno)); + return ret; } - else /* relative path */ + else { - if (is_unix) + if (*path == '/') /* not absolute path */ { - realpath (path, resolved_path); - ret = resolved_path; + rrd_set_error ("absolute path names not allowed when talking " + "to a remote daemon"); + return NULL; } - /* else: nothing to do */ } - return (ret); + + return path; } /* }}} char *get_path */ static size_t strsplit (char *string, char **fields, size_t size) /* {{{ */ @@ -310,6 +316,16 @@ static int buffer_add_value (const char *value, /* {{{ */ return (buffer_add_string (temp, buffer_ret, buffer_size_ret)); } /* }}} int buffer_add_value */ +static int buffer_add_ulong (const unsigned long value, /* {{{ */ + char **buffer_ret, size_t *buffer_size_ret) +{ + char temp[4096]; + + snprintf (temp, sizeof (temp), "%lu", value); + temp[sizeof (temp) - 1] = 0; + return (buffer_add_string (temp, buffer_ret, buffer_size_ret)); +} /* }}} int buffer_add_ulong */ + /* Remove trailing newline (NL) and carriage return (CR) characters. Similar to * the Perl function `chomp'. Returns the number of characters that have been * removed. */ @@ -371,14 +387,17 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ ret->lines_num = 0; buffer_ptr = fgets (buffer, sizeof (buffer), sh); - if (buffer_ptr == NULL) + if (buffer_ptr == NULL) { + close_connection(); return (-3); + } chomp (buffer); ret->status = strtol (buffer, &ret->message, 0); if (buffer == ret->message) { response_free (ret); + close_connection(); return (-4); } /* Skip leading whitespace of the status message */ @@ -396,6 +415,7 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ if (ret->lines == NULL) { response_free (ret); + close_connection(); return (-5); } memset (ret->lines, 0, sizeof (char *) * ret->status); @@ -407,6 +427,7 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ if (buffer_ptr == NULL) { response_free (ret); + close_connection(); return (-6); } chomp (buffer); @@ -415,6 +436,7 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ if (ret->lines[i] == NULL) { response_free (ret); + close_connection(); return (-7); } } @@ -805,6 +827,320 @@ int rrdc_flush (const char *filename) /* {{{ */ return (status); } /* }}} int rrdc_flush */ +rrd_info_t * rrdc_info (const char *filename) /* {{{ */ +{ + char buffer[4096]; + char *buffer_ptr; + size_t buffer_free; + size_t buffer_size; + rrdc_response_t *res; + int status; + char file_path[PATH_MAX]; + rrd_info_t *data = NULL, *cd; + rrd_infoval_t info; + unsigned int l; + rrd_info_type_t itype; + char *k, *s; + + if (filename == NULL) { + rrd_set_error ("rrdc_info: no filename"); + return (NULL); + } + + memset (buffer, 0, sizeof (buffer)); + buffer_ptr = &buffer[0]; + buffer_free = sizeof (buffer); + + status = buffer_add_string ("info", &buffer_ptr, &buffer_free); + if (status != 0) { + rrd_set_error ("rrdc_info: out of memory"); + return (NULL); + } + + pthread_mutex_lock (&lock); + filename = get_path (filename, file_path); + if (filename == NULL) + { + pthread_mutex_unlock (&lock); + return (NULL); + } + + status = buffer_add_string (filename, &buffer_ptr, &buffer_free); + if (status != 0) + { + pthread_mutex_unlock (&lock); + rrd_set_error ("rrdc_info: out of memory"); + return (NULL); + } + + assert (buffer_free < sizeof (buffer)); + buffer_size = sizeof (buffer) - buffer_free; + assert (buffer[buffer_size - 1] == ' '); + buffer[buffer_size - 1] = '\n'; + + res = NULL; + status = request (buffer, buffer_size, &res); + pthread_mutex_unlock (&lock); + + if (status != 0) { + rrd_set_error ("rrdcached: %s", res->message); + return (NULL); + } + data = cd = NULL; + for( l=0 ; l < res->lines_num ; l++ ) { + /* first extract the keyword */ + for(k = s = res->lines[l];s && *s;s++) { + if(*s == ' ') { *s = 0; s++; break; } + } + if(!s || !*s) break; + itype = atoi(s); /* extract type code */ + for(;*s;s++) { if(*s == ' ') { *s = 0; s++; break; } } + if(!*s) break; + /* finally, we're pointing to the value */ + switch(itype) { + case RD_I_VAL: + if(*s == 'N') { info.u_val = DNAN; } else { info.u_val = atof(s); } + break; + case RD_I_CNT: + info.u_cnt = atol(s); + break; + case RD_I_INT: + info.u_int = atoi(s); + break; + case RD_I_STR: + chomp(s); + info.u_str = (char*)malloc(sizeof(char) * (strlen(s) + 1)); + strcpy(info.u_str,s); + break; + case RD_I_BLO: + rrd_set_error ("rrdc_info: BLOB objects are not supported"); + return (NULL); + default: + rrd_set_error ("rrdc_info: Unsupported info type %d",itype); + return (NULL); + } + + cd = rrd_info_push(cd, sprintf_alloc("%s",k), itype, info); + if(!data) data = cd; + } + response_free (res); + + return (data); +} /* }}} int rrdc_info */ + +time_t rrdc_last (const char *filename) /* {{{ */ +{ + char buffer[4096]; + char *buffer_ptr; + size_t buffer_free; + size_t buffer_size; + rrdc_response_t *res; + int status; + char file_path[PATH_MAX]; + time_t lastup; + + if (filename == NULL) { + rrd_set_error ("rrdc_last: no filename"); + return (-1); + } + + memset (buffer, 0, sizeof (buffer)); + buffer_ptr = &buffer[0]; + buffer_free = sizeof (buffer); + + status = buffer_add_string ("last", &buffer_ptr, &buffer_free); + if (status != 0) { + rrd_set_error ("rrdc_last: out of memory"); + return (-1); + } + + pthread_mutex_lock (&lock); + filename = get_path (filename, file_path); + if (filename == NULL) + { + pthread_mutex_unlock (&lock); + return (-1); + } + + status = buffer_add_string (filename, &buffer_ptr, &buffer_free); + if (status != 0) + { + pthread_mutex_unlock (&lock); + rrd_set_error ("rrdc_last: out of memory"); + return (-1); + } + + assert (buffer_free < sizeof (buffer)); + buffer_size = sizeof (buffer) - buffer_free; + assert (buffer[buffer_size - 1] == ' '); + buffer[buffer_size - 1] = '\n'; + + res = NULL; + status = request (buffer, buffer_size, &res); + pthread_mutex_unlock (&lock); + + if (status != 0) { + rrd_set_error ("rrdcached: %s", res->message); + return (-1); + } + lastup = atol(res->message); + response_free (res); + + return (lastup); +} /* }}} int rrdc_last */ + +time_t rrdc_first (const char *filename, int rraindex) /* {{{ */ +{ + char buffer[4096]; + char *buffer_ptr; + size_t buffer_free; + size_t buffer_size; + rrdc_response_t *res; + int status; + char file_path[PATH_MAX]; + time_t firstup; + + if (filename == NULL) { + rrd_set_error ("rrdc_first: no filename specified"); + return (-1); + } + + memset (buffer, 0, sizeof (buffer)); + buffer_ptr = &buffer[0]; + buffer_free = sizeof (buffer); + + status = buffer_add_string ("first", &buffer_ptr, &buffer_free); + if (status != 0) { + rrd_set_error ("rrdc_first: out of memory"); + return (-1); + } + + pthread_mutex_lock (&lock); + filename = get_path (filename, file_path); + if (filename == NULL) + { + pthread_mutex_unlock (&lock); + return (-1); + } + + status = buffer_add_string (filename, &buffer_ptr, &buffer_free); + if (status != 0) + { + pthread_mutex_unlock (&lock); + rrd_set_error ("rrdc_first: out of memory"); + return (-1); + } + status = buffer_add_ulong (rraindex, &buffer_ptr, &buffer_free); + if (status != 0) + { + pthread_mutex_unlock (&lock); + rrd_set_error ("rrdc_first: out of memory"); + return (-1); + } + + assert (buffer_free < sizeof (buffer)); + buffer_size = sizeof (buffer) - buffer_free; + assert (buffer[buffer_size - 1] == ' '); + buffer[buffer_size - 1] = '\n'; + + res = NULL; + status = request (buffer, buffer_size, &res); + pthread_mutex_unlock (&lock); + + if (status != 0) { + rrd_set_error ("rrdcached: %s", res->message); + return (-1); + } + firstup = atol(res->message); + response_free (res); + + return (firstup); +} /* }}} int rrdc_first */ + +int rrdc_create (const char *filename, /* {{{ */ + unsigned long pdp_step, + time_t last_up, + int no_overwrite, + int argc, + const char **argv) +{ + char buffer[4096]; + char *buffer_ptr; + size_t buffer_free; + size_t buffer_size; + rrdc_response_t *res; + int status; + char file_path[PATH_MAX]; + int i; + + if (filename == NULL) { + rrd_set_error ("rrdc_create: no filename specified"); + return (-1); + } + + memset (buffer, 0, sizeof (buffer)); + buffer_ptr = &buffer[0]; + buffer_free = sizeof (buffer); + + status = buffer_add_string ("create", &buffer_ptr, &buffer_free); + if (status != 0) { + rrd_set_error ("rrdc_create: out of memory"); + return (-1); + } + + pthread_mutex_lock (&lock); + filename = get_path (filename, file_path); + if (filename == NULL) + { + pthread_mutex_unlock (&lock); + return (-1); + } + + status = buffer_add_string (filename, &buffer_ptr, &buffer_free); + status = buffer_add_string ("-b", &buffer_ptr, &buffer_free); + status = buffer_add_ulong (last_up, &buffer_ptr, &buffer_free); + status = buffer_add_string ("-s", &buffer_ptr, &buffer_free); + status = buffer_add_ulong (pdp_step, &buffer_ptr, &buffer_free); + if(no_overwrite) { + status = buffer_add_string ("-O", &buffer_ptr, &buffer_free); + } + if (status != 0) + { + pthread_mutex_unlock (&lock); + rrd_set_error ("rrdc_create: out of memory"); + return (-1); + } + + for( i=0; imessage); + return (-1); + } + response_free (res); + return(0); +} /* }}} int rrdc_create */ + int rrdc_fetch (const char *filename, /* {{{ */ const char *cf, time_t *ret_start, time_t *ret_end, @@ -819,7 +1155,7 @@ int rrdc_fetch (const char *filename, /* {{{ */ size_t buffer_size; rrdc_response_t *res; char path_buffer[PATH_MAX]; - char *path_ptr; + const char *path_ptr; char *str_tmp; unsigned long flush_version;