X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=bcdf59aafdbd8a52a36579073f49ff2bf9b6c8f8;hb=7293eb23871200cea4de5c594610fe782891339a;hp=922bd15e4406077b162b40c5052e07b3ebddd5eb;hpb=da724d1a59236efa7d6845465a0fa4dfba57d8ab;p=collectd.git diff --git a/src/common.c b/src/common.c index 922bd15e..bcdf59aa 100644 --- a/src/common.c +++ b/src/common.c @@ -20,8 +20,12 @@ * Niki W. Waibel **/ +#if HAVE_CONFIG_H +# include "config.h" +#endif + #include "common.h" -#include "utils_debug.h" +#include "plugin.h" #ifdef HAVE_MATH_H # include @@ -51,20 +55,29 @@ char *sstrdup (const char *s) if((r = strdup (s)) == NULL) { - DBG ("Not enough memory."); + DEBUG ("Not enough memory."); exit(3); } return (r); } +/* Don't use the return value of `strerror_r', because the GNU-people got + * inventive there.. -octo */ +char *sstrerror (int errnum, char *buf, size_t buflen) +{ + buf[0] = '\0'; + strerror_r (errnum, buf, buflen); + return (buf); +} /* char *sstrerror */ + void *smalloc (size_t size) { void *r; if ((r = malloc (size)) == NULL) { - DBG("Not enough memory."); + DEBUG("Not enough memory."); exit(3); } @@ -105,7 +118,7 @@ ssize_t sread (int fd, void *buf, size_t count) if (status == 0) { - DBG ("Received EOF from fd %i. " + DEBUG ("Received EOF from fd %i. " "Closing fd and returning error.", fd); close (fd); @@ -351,7 +364,7 @@ int check_create_dir (const char *file_orig) */ if (fields[i][0] == '.') { - syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig); + ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig); return (-2); } @@ -362,7 +375,7 @@ int check_create_dir (const char *file_orig) if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute, fields, i + 1, "/") < 0) { - syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i); + ERROR ("strjoin failed: `%s', component #%i", file_orig, i); return (-1); } @@ -372,19 +385,25 @@ int check_create_dir (const char *file_orig) { if (mkdir (dir, 0755) == -1) { - syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno)); + char errbuf[1024]; + ERROR ("mkdir (%s): %s", dir, + sstrerror (errno, + errbuf, sizeof (errbuf))); return (-1); } } else { - syslog (LOG_ERR, "stat (%s): %s", dir, strerror (errno)); + char errbuf[1024]; + ERROR ("stat (%s): %s", dir, + sstrerror (errno, errbuf, + sizeof (errbuf))); return (-1); } } else if (!S_ISDIR (statbuf.st_mode)) { - syslog (LOG_ERR, "stat (%s): Not a directory!", dir); + ERROR ("stat (%s): Not a directory!", dir); return (-1); } } @@ -407,13 +426,13 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name) { if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL) { - syslog (LOG_ERR, "Cound not find kstat %s", ident); + ERROR ("Cound not find kstat %s", ident); return (-1); } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { - syslog (LOG_WARNING, "kstat %s has wrong type", ident); + WARNING ("kstat %s has wrong type", ident); *ksp_ptr = NULL; return (-1); } @@ -426,13 +445,13 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name) if (kstat_read (kc, *ksp_ptr, NULL) == -1) { - syslog (LOG_WARNING, "kstat %s could not be read", ident); + WARNING ("kstat %s could not be read", ident); return (-1); } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { - syslog (LOG_WARNING, "kstat %s has wrong type", ident); + WARNING ("kstat %s has wrong type", ident); return (-1); } @@ -472,7 +491,7 @@ long long get_kstat_value (kstat_t *ksp, char *name) else if (kn->data_type == KSTAT_DATA_UINT64) retval = (long long) kn->value.ui64; /* XXX: Might overflow! */ else - syslog (LOG_WARNING, "get_kstat_value: Not a numeric value: %s", name); + WARNING ("get_kstat_value: Not a numeric value: %s", name); return (retval); } @@ -485,7 +504,7 @@ unsigned long long ntohll (unsigned long long n) #else return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32); #endif -} +} /* unsigned long long ntohll */ unsigned long long htonll (unsigned long long n) { @@ -494,4 +513,41 @@ unsigned long long htonll (unsigned long long n) #else return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32); #endif -} +} /* unsigned long long htonll */ + +int format_name (char *ret, int ret_len, + const char *hostname, + const char *plugin, const char *plugin_instance, + const char *type, const char *type_instance) +{ + int status; + + assert (plugin != NULL); + assert (type != NULL); + + if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0)) + { + if ((type_instance == NULL) || (strlen (type_instance) == 0)) + status = snprintf (ret, ret_len, "%s/%s/%s", + hostname, plugin, type); + else + status = snprintf (ret, ret_len, "%s/%s/%s-%s", + hostname, plugin, type, + type_instance); + } + else + { + if ((type_instance == NULL) || (strlen (type_instance) == 0)) + status = snprintf (ret, ret_len, "%s/%s-%s/%s", + hostname, plugin, plugin_instance, + type); + else + status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s", + hostname, plugin, plugin_instance, + type, type_instance); + } + + if ((status < 1) || (status >= ret_len)) + return (-1); + return (0); +} /* int format_name */