2 * collectd - src/common.c
3 * Copyright (C) 2005,2006 Florian octo Forster
5 * This program is free software; you can redistribute it and/
6 * or modify it under the terms of the GNU General Public Li-
7 * cence as published by the Free Software Foundation; either
8 * version 2 of the Licence, or any later version.
10 * This program is distributed in the hope that it will be use-
11 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public Licence for more details.
15 * You should have received a copy of the GNU General Public
16 * Licence along with this program; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 * Florian octo Forster <octo at verplant.org>
22 * Niki W. Waibel <niki.waibel@gmx.net>
26 #include "utils_debug.h"
32 extern int operating_mode;
35 extern kstat_ctl_t *kc;
38 void sstrncpy (char *d, const char *s, int len)
44 char *sstrdup (const char *s)
51 if((r = strdup (s)) == NULL)
53 DBG ("Not enough memory.");
60 void *smalloc (size_t size)
64 if ((r = malloc (size)) == NULL)
66 DBG("Not enough memory.");
74 void sfree (void **ptr)
86 ssize_t sread (int fd, void *buf, size_t count)
97 status = read (fd, (void *) ptr, nleft);
99 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
107 DBG ("Received EOF from fd %i. "
108 "Closing fd and returning error.",
114 assert (nleft >= status);
116 nleft = nleft - status;
124 ssize_t swrite (int fd, const void *buf, size_t count)
130 ptr = (const char *) buf;
135 status = write (fd, (const void *) ptr, nleft);
137 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
143 nleft = nleft - status;
150 int strsplit (char *string, char **fields, size_t size)
157 while ((fields[i] = strtok (ptr, " \t")) != NULL)
169 int strjoin (char *dst, size_t dst_len,
170 char **fields, size_t fields_num,
177 memset (dst, '\0', dst_len);
184 sep_len = strlen (sep);
186 for (i = 0; i < fields_num; i++)
188 if ((i > 0) && (sep_len > 0))
190 if (dst_len <= sep_len)
193 strncat (dst, sep, dst_len);
197 field_len = strlen (fields[i]);
199 if (dst_len <= field_len)
202 strncat (dst, fields[i], dst_len);
203 dst_len -= field_len;
206 return (strlen (dst));
209 int strsubstitute (char *str, char c_from, char c_to)
230 int escape_slashes (char *buf, int buf_len)
234 if (strcmp (buf, "/") == 0)
239 strncpy (buf, "root", buf_len);
243 /* Move one to the left */
244 memmove (buf, buf + 1, buf_len - 1);
246 for (i = 0; i < buf_len - 1; i++)
250 else if (buf[i] == '/')
258 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
260 if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
263 if ((tv0->tv_sec < tv1->tv_sec)
264 || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
267 ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
268 ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
270 if (ret->tv_nsec < 0)
272 assert (ret->tv_sec > 0);
274 ret->tv_nsec += 1000000000;
281 int check_create_dir (const char *file_orig)
291 int last_is_file = 1;
296 * Sanity checks first
298 if (file_orig == NULL)
301 if ((len = strlen (file_orig)) < 1)
307 * If `file_orig' ends in a slash the last component is a directory,
308 * otherwise it's a file. Act accordingly..
310 if (file_orig[len - 1] == '/')
314 * Create a copy for `strtok' to destroy
316 strncpy (file_copy, file_orig, 512);
317 file_copy[511] = '\0';
320 * Break into components. This will eat up several slashes in a row and
321 * remove leading and trailing slashes..
325 while ((fields[fields_num] = strtok (ptr, "/")) != NULL)
330 if (fields_num >= 16)
335 * For each component, do..
337 for (i = 0; i < (fields_num - last_is_file); i++)
340 * Do not create directories that start with a dot. This
341 * prevents `../../' attacks and other likely malicious
344 if (fields[i][0] == '.')
346 syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
351 * Join the components together again
353 if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
355 syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
359 if (stat (dir, &statbuf) == -1)
363 if (mkdir (dir, 0755) == -1)
365 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
371 syslog (LOG_ERR, "stat (%s): %s", dir, strerror (errno));
375 else if (!S_ISDIR (statbuf.st_mode))
377 syslog (LOG_ERR, "stat (%s): Not a directory!", dir);
385 static int log_create_file (char *filename, char **ds_def, int ds_num)
390 if (check_create_dir (filename))
393 log = fopen (filename, "w");
396 syslog (LOG_WARNING, "Failed to create %s: %s", filename,
401 fprintf (log, "epoch");
402 for (i = 0; i < ds_num; i++)
407 name = strchr (ds_def[i], ':');
410 syslog (LOG_WARNING, "Invalid DS definition '%s' for %s",
411 ds_def[i], filename);
418 tmp = strchr (name, ':');
421 syslog (LOG_WARNING, "Invalid DS definition '%s' for %s",
422 ds_def[i], filename);
428 /* The `%.*s' is needed because there is no null-byte behind
430 fprintf(log, ",%.*s", (int) (tmp - name), name);
438 int log_update_file (char *host, char *file, char *values,
439 char **ds_def, int ds_num)
444 char full_file[1024];
446 /* Cook the values a bit: Substitute colons with commas */
447 strsubstitute (values, ':', ',');
449 /* host == NULL => local mode */
452 if (snprintf (full_file, 1024, "%s/%s", host, file) >= 1024)
457 if (snprintf (full_file, 1024, "%s", file) >= 1024)
461 strncpy (full_file, file, 1024);
463 tmp = full_file + strlen (full_file) - 4;
464 assert ((tmp != NULL) && (tmp > full_file));
466 /* Change the filename for logfiles. */
467 if (strncmp (tmp, ".rrd", 4) == 0)
472 /* TODO: Find a way to minimize the calls to `localtime', since
473 * they are pretty expensive.. */
475 tm = localtime (&now);
477 strftime (tmp, 1024 - (tmp - full_file), "-%Y-%m-%d", tm);
479 /* `localtime(3)' returns a pointer to static data,
480 * therefore the pointer may not be free'd. */
483 DBG ("The filename ends with `%s' which is unexpected.", tmp);
485 if (stat (full_file, &statbuf) == -1)
489 if (log_create_file (full_file, ds_def, ds_num))
494 syslog (LOG_ERR, "stat %s: %s", full_file, strerror (errno));
498 else if (!S_ISREG (statbuf.st_mode))
500 syslog (LOG_ERR, "stat %s: Not a regular file!", full_file);
505 fp = fopen (full_file, "a");
508 syslog (LOG_WARNING, "Failed to append to %s: %s", full_file,
512 fprintf(fp, "%s\n", values);
516 } /* int log_update_file */
520 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
527 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
530 if (*ksp_ptr == NULL)
532 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
534 syslog (LOG_ERR, "Cound not find kstat %s", ident);
538 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
540 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
547 assert (*ksp_ptr != NULL);
548 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
551 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
553 syslog (LOG_WARNING, "kstat %s could not be read", ident);
557 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
559 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
566 long long get_kstat_value (kstat_t *ksp, char *name)
569 long long retval = -1LL;
572 assert (ksp != NULL);
573 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
577 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
580 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
582 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
587 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
590 if (kn->data_type == KSTAT_DATA_INT32)
591 retval = (long long) kn->value.i32;
592 else if (kn->data_type == KSTAT_DATA_UINT32)
593 retval = (long long) kn->value.ui32;
594 else if (kn->data_type == KSTAT_DATA_INT64)
595 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
596 else if (kn->data_type == KSTAT_DATA_UINT64)
597 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
599 syslog (LOG_WARNING, "get_kstat_value: Not a numeric value: %s", name);
603 #endif /* HAVE_LIBKSTAT */