/**
* collectd - src/common.c
- * Copyright (C) 2005-2010 Florian octo Forster
+ * Copyright (C) 2005-2014 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.
+ * 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:
*
- * 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.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
*
- * 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
+ * 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 <octo at collectd.org>
return (ret);
} /* int ssnprintf */
+char *ssnprintf_alloc (char const *format, ...) /* {{{ */
+{
+ char static_buffer[1024] = "";
+ char *alloc_buffer;
+ size_t alloc_buffer_size;
+ int status;
+ va_list ap;
+
+ /* Try printing into the static buffer. In many cases it will be
+ * sufficiently large and we can simply return a strdup() of this
+ * buffer. */
+ va_start (ap, format);
+ status = vsnprintf (static_buffer, sizeof (static_buffer), format, ap);
+ va_end (ap);
+ if (status < 0)
+ return (NULL);
+
+ /* "status" does not include the null byte. */
+ alloc_buffer_size = (size_t) (status + 1);
+ if (alloc_buffer_size <= sizeof (static_buffer))
+ return (strdup (static_buffer));
+
+ /* Allocate a buffer large enough to hold the string. */
+ alloc_buffer = malloc (alloc_buffer_size);
+ if (alloc_buffer == NULL)
+ return (NULL);
+ memset (alloc_buffer, 0, alloc_buffer_size);
+
+ /* Print again into this new buffer. */
+ va_start (ap, format);
+ status = vsnprintf (alloc_buffer, alloc_buffer_size, format, ap);
+ va_end (ap);
+ if (status < 0)
+ {
+ sfree (alloc_buffer);
+ return (NULL);
+ }
+
+ return (alloc_buffer);
+} /* }}} char *ssnprintf_alloc */
+
char *sstrdup (const char *s)
{
char *r;
if (buf[i] != '\\')
continue;
- if ((i >= buf_len) || (buf[i + 1] == '\0')) {
+ if (((i + 1) >= buf_len) || (buf[i + 1] == 0)) {
ERROR ("string unescape: backslash found at end of string.");
+ /* Ensure null-byte at the end of the buffer. */
+ buf[i] = 0;
return (-1);
}
break;
}
+ /* Move everything after the position one position to the left.
+ * Add a null-byte as last character in the buffer. */
memmove (buf + i + 1, buf + i + 2, buf_len - i - 2);
+ buf[buf_len - 1] = 0;
}
return (0);
} /* int strunescape */
return (buffer_len);
} /* size_t strstripnewline */
-int escape_slashes (char *buf, int buf_len)
+int escape_slashes (char *buffer, size_t buffer_size)
{
int i;
+ size_t buffer_len;
- if (strcmp (buf, "/") == 0)
- {
- if (buf_len < 5)
- return (-1);
+ buffer_len = strlen (buffer);
- strncpy (buf, "root", buf_len);
+ if (buffer_len <= 1)
+ {
+ if (strcmp ("/", buffer) == 0)
+ {
+ if (buffer_size < 5)
+ return (-1);
+ sstrncpy (buffer, "root", buffer_size);
+ }
return (0);
}
- if (buf_len <= 1)
- return (0);
-
/* Move one to the left */
- if (buf[0] == '/')
- memmove (buf, buf + 1, buf_len - 1);
+ if (buffer[0] == '/')
+ {
+ memmove (buffer, buffer + 1, buffer_len);
+ buffer_len--;
+ }
- for (i = 0; i < buf_len - 1; i++)
+ for (i = 0; i < buffer_len - 1; i++)
{
- if (buf[i] == '\0')
- break;
- else if (buf[i] == '/')
- buf[i] = '_';
+ if (buffer[i] == '/')
+ buffer[i] = '_';
}
- buf[i] = '\0';
return (0);
} /* int escape_slashes */
char ident[128];
*ksp_ptr = NULL;
-
+
if (kc == NULL)
return (-1);
retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
else
WARNING ("get_kstat_value: Not a numeric value: %s", name);
-
+
return (retval);
}
#endif /* HAVE_LIBKSTAT */
const char *plugin, const char *plugin_instance,
const char *type, const char *type_instance)
{
- int status;
+ char *buffer;
+ size_t buffer_size;
+
+ buffer = ret;
+ buffer_size = (size_t) ret_len;
+
+#define APPEND(str) do { \
+ size_t l = strlen (str); \
+ if (l >= buffer_size) \
+ return (ENOBUFS); \
+ memcpy (buffer, (str), l); \
+ buffer += l; buffer_size -= l; \
+} while (0)
- assert (plugin != NULL);
- assert (type != NULL);
+ assert (plugin != NULL);
+ assert (type != NULL);
- if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
- {
- if ((type_instance == NULL) || (strlen (type_instance) == 0))
- status = ssnprintf (ret, ret_len, "%s/%s/%s",
- hostname, plugin, type);
- else
- status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
- hostname, plugin, type,
- type_instance);
- }
- else
- {
- if ((type_instance == NULL) || (strlen (type_instance) == 0))
- status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
- hostname, plugin, plugin_instance,
- type);
- else
- status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
- hostname, plugin, plugin_instance,
- type, type_instance);
- }
+ APPEND (hostname);
+ APPEND ("/");
+ APPEND (plugin);
+ if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
+ {
+ APPEND ("-");
+ APPEND (plugin_instance);
+ }
+ APPEND ("/");
+ APPEND (type);
+ if ((type_instance != NULL) && (type_instance[0] != 0))
+ {
+ APPEND ("-");
+ APPEND (type_instance);
+ }
+ assert (buffer_size > 0);
+ buffer[0] = 0;
- if ((status < 1) || (status >= ret_len))
- return (-1);
- return (0);
+#undef APPEND
+ return (0);
} /* int format_name */
int format_values (char *ret, size_t ret_len, /* {{{ */
}
if (value == endptr) {
- sfree (value);
ERROR ("parse_value: Failed to parse string as %s: %s.",
DS_TYPE_TO_STRING (ds_type), value);
+ sfree (value);
return -1;
}
else if ((NULL != endptr) && ('\0' != *endptr))
while ((ent = readdir (dh)) != NULL)
{
int status;
-
+
if (include_hidden)
{
if ((strcmp (".", ent->d_name) == 0)
return (0);
}
-int read_file_contents (const char *filename, char *buf, int bufsize)
+ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize)
{
FILE *fh;
- int n;
+ ssize_t ret;
- if ((fh = fopen (filename, "r")) == NULL)
- return -1;
+ fh = fopen (filename, "r");
+ if (fh == NULL)
+ return (-1);
- n = fread(buf, 1, bufsize, fh);
- fclose(fh);
+ ret = (ssize_t) fread (buf, 1, bufsize, fh);
+ if ((ret == 0) && (ferror (fh) != 0))
+ {
+ ERROR ("read_file_contents: Reading file \"%s\" failed.",
+ filename);
+ ret = -1;
+ }
- return n;
+ fclose(fh);
+ return (ret);
}
counter_t counter_diff (counter_t old_value, counter_t new_value)
return (0);
} /* }}} value_t rate_to_value */
+int value_to_rate (value_t *ret_rate, derive_t value, /* {{{ */
+ value_to_rate_state_t *state,
+ int ds_type, cdtime_t t)
+{
+ double interval;
+
+ /* Another invalid state: The time is not increasing. */
+ if (t <= state->last_time)
+ {
+ memset (state, 0, sizeof (*state));
+ return (EINVAL);
+ }
+
+ interval = CDTIME_T_TO_DOUBLE(t - state->last_time);
+
+ /* Previous value is invalid. */
+ if (state->last_time == 0) /* {{{ */
+ {
+ if (ds_type == DS_TYPE_DERIVE)
+ {
+ state->last_value.derive = value;
+ }
+ else if (ds_type == DS_TYPE_COUNTER)
+ {
+ state->last_value.counter = (counter_t) value;
+ }
+ else if (ds_type == DS_TYPE_ABSOLUTE)
+ {
+ state->last_value.absolute = (absolute_t) value;
+ }
+ else
+ {
+ assert (23 == 42);
+ }
+
+ state->last_time = t;
+ return (EAGAIN);
+ } /* }}} */
+
+ if (ds_type == DS_TYPE_DERIVE)
+ {
+ ret_rate->gauge = (value - state->last_value.derive) / interval;
+ state->last_value.derive = value;
+ }
+ else if (ds_type == DS_TYPE_COUNTER)
+ {
+ ret_rate->gauge = (((counter_t)value) - state->last_value.counter) / interval;
+ state->last_value.counter = (counter_t) value;
+ }
+ else if (ds_type == DS_TYPE_ABSOLUTE)
+ {
+ ret_rate->gauge = (((absolute_t)value) - state->last_value.absolute) / interval;
+ state->last_value.absolute = (absolute_t) value;
+ }
+ else
+ {
+ assert (23 == 42);
+ }
+
+ state->last_time = t;
+ return (0);
+} /* }}} value_t rate_to_value */
+
int service_name_to_port_number (const char *service_name)
{
struct addrinfo *ai_list;