2 * collectd - src/common.c
3 * Copyright (C) 2005 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"
29 extern kstat_ctl_t *kc;
33 static char *rra_def[] =
35 "RRA:AVERAGE:0.0:1:1500",
36 "RRA:AVERAGE:0.2:6:1500",
37 "RRA:AVERAGE:0.1:180:1680",
38 "RRA:AVERAGE:0.1:2160:1520",
41 "RRA:MIN:0.1:180:1680",
42 "RRA:MIN:0.1:2160:1520",
45 "RRA:MAX:0.1:180:1680",
46 "RRA:MAX:0.1:2160:1520",
49 static int rra_num = 12;
50 #endif /* HAVE_LIBRRD */
52 void sstrncpy (char *d, const char *s, int len)
58 char *sstrdup (const char *s)
65 if((r = strdup (s)) == NULL)
67 DBG ("Not enough memory.");
74 void *smalloc (size_t size)
78 if ((r = malloc (size)) == NULL)
80 DBG("Not enough memory.");
88 void sfree (void **ptr)
100 int strsplit (char *string, char **fields, size_t size)
107 while ((fields[i] = strtok (ptr, " \t")) != NULL)
119 int strjoin (char *dst, size_t dst_len,
120 char **fields, size_t fields_num,
127 memset (dst, '\0', dst_len);
134 sep_len = strlen (sep);
136 for (i = 0; i < fields_num; i++)
138 if ((i > 0) && (sep_len > 0))
140 if (dst_len <= sep_len)
143 strncat (dst, sep, dst_len);
147 field_len = strlen (fields[i]);
149 if (dst_len <= field_len)
152 strncat (dst, fields[i], dst_len);
153 dst_len -= field_len;
156 return (strlen (dst));
159 int escape_slashes (char *buf, int buf_len)
163 if (strcmp (buf, "/") == 0)
168 strncpy (buf, "root", buf_len);
172 /* Move one to the left */
173 memmove (buf, buf + 1, buf_len - 1);
175 for (i = 0; i < buf_len - 1; i++)
179 else if (buf[i] == '/')
188 int check_create_dir (const char *file_orig)
198 int last_is_file = 1;
203 * Sanity checks first
205 if (file_orig == NULL)
208 if ((len = strlen (file_orig)) < 1)
214 * If `file_orig' ends in a slash the last component is a directory,
215 * otherwise it's a file. Act accordingly..
217 if (file_orig[len - 1] == '/')
221 * Create a copy for `strtok' to destroy
223 strncpy (file_copy, file_orig, 512);
224 file_copy[511] = '\0';
227 * Break into components. This will eat up several slashes in a row and
228 * remove leading and trailing slashes..
232 while ((fields[fields_num] = strtok (ptr, "/")) != NULL)
237 if (fields_num >= 16)
242 * For each component, do..
244 for (i = 0; i < (fields_num - last_is_file); i++)
247 * Do not create directories that start with a dot. This
248 * prevents `../../' attacks and other likely malicious
251 if (fields[i][0] == '.')
253 syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
258 * Join the components together again
260 if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
262 syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
266 if (stat (dir, &statbuf) == -1)
270 if (mkdir (dir, 0755) == -1)
272 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
278 syslog (LOG_ERR, "stat (%s): %s", dir, strerror (errno));
282 else if (!S_ISDIR (statbuf.st_mode))
284 syslog (LOG_ERR, "stat (%s): Not a directory!", dir);
292 int rrd_create_file (char *filename, char **ds_def, int ds_num)
299 if (check_create_dir (filename))
302 argc = ds_num + rra_num + 4;
304 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
306 syslog (LOG_ERR, "rrd_create failed: %s", strerror (errno));
313 argv[3] = COLLECTD_STEP;
316 for (i = 0; i < ds_num; i++)
317 argv[j++] = ds_def[i];
318 for (i = 0; i < rra_num; i++)
319 argv[j++] = rra_def[i];
322 optind = 0; /* bug in librrd? */
324 if (rrd_create (argc, argv) == -1)
326 syslog (LOG_ERR, "rrd_create failed: %s: %s", filename, rrd_get_error ());
334 #endif /* HAVE_LIBRRD */
336 int rrd_update_file (char *host, char *file, char *values,
337 char **ds_def, int ds_num)
341 char full_file[1024];
342 char *argv[4] = { "update", full_file, values, NULL };
344 /* host == NULL => local mode */
347 if (snprintf (full_file, 1024, "%s/%s", host, file) >= 1024)
352 if (snprintf (full_file, 1024, "%s", file) >= 1024)
356 if (stat (full_file, &statbuf) == -1)
360 if (rrd_create_file (full_file, ds_def, ds_num))
365 syslog (LOG_ERR, "stat %s: %s", full_file, strerror (errno));
369 else if (!S_ISREG (statbuf.st_mode))
371 syslog (LOG_ERR, "stat %s: Not a regular file!", full_file);
375 optind = 0; /* bug in librrd? */
377 if (rrd_update (3, argv) == -1)
379 syslog (LOG_WARNING, "rrd_update failed: %s: %s", full_file, rrd_get_error ());
382 #endif /* HAVE_LIBRRD */
388 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
395 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
398 if (*ksp_ptr == NULL)
400 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
402 syslog (LOG_ERR, "Cound not find kstat %s", ident);
406 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
408 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
415 assert (*ksp_ptr != NULL);
416 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
419 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
421 syslog (LOG_WARNING, "kstat %s could not be read", ident);
425 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
427 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
434 long long get_kstat_value (kstat_t *ksp, char *name)
437 long long retval = -1LL;
440 assert (ksp != NULL);
441 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
445 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
448 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
450 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
455 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
458 if (kn->data_type == KSTAT_DATA_INT32)
459 retval = (long long) kn->value.i32;
460 else if (kn->data_type == KSTAT_DATA_UINT32)
461 retval = (long long) kn->value.ui32;
462 else if (kn->data_type == KSTAT_DATA_INT64)
463 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
464 else if (kn->data_type == KSTAT_DATA_UINT64)
465 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
467 syslog (LOG_WARNING, "get_kstat_value: Not a numeric value: %s", name);
471 #endif /* HAVE_LIBKSTAT */