X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fuuid.c;h=8bae376103f0328ec9fb20763ee70c668217e928;hb=fa82af13503a1195e1bfaef597ac33a3dc71a758;hp=5bb4472e596c7ed6b816e14e4633ed0169385b9b;hpb=13c83b972a8ade7dff6f8f2d00832d446ef6f502;p=collectd.git diff --git a/src/uuid.c b/src/uuid.c index 5bb4472e..8bae3761 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -1,6 +1,7 @@ /** * collectd - src/uuid.c * Copyright (C) 2007 Red Hat Inc. + * Copyright (C) 2015 Ruben Kerkhof * * 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 @@ -26,9 +27,12 @@ #include "collectd.h" #include "common.h" -#include "configfile.h" #include "plugin.h" +#if HAVE_SYS_SYSCTL_H +#include +#endif + #if HAVE_LIBHAL_H #include #endif @@ -97,7 +101,7 @@ uuid_parse_dmidecode(FILE *file) static char * uuid_get_from_dmidecode(void) { - FILE *dmidecode = popen("dmidecode 2>/dev/null", "r"); + FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r"); char *uuid; if (!dmidecode) @@ -109,6 +113,33 @@ uuid_get_from_dmidecode(void) return (uuid); } +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) +static char * +uuid_get_from_sysctlbyname(const char *name) +{ + char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1]; + size_t len = sizeof (uuid); + if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1) + return NULL; + return (strdup (uuid)); +} +#elif defined(__OpenBSD__) +static char * +uuid_get_from_sysctl(void) +{ + char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1]; + size_t len = sizeof (uuid); + int mib[2]; + + mib[0] = CTL_HW; + mib[1] = HW_UUID; + + if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1) + return NULL; + return (strdup (uuid)); +} +#endif + #if HAVE_LIBHAL_H #define UUID_PATH "/org/freedesktop/Hal/devices/computer" @@ -192,17 +223,35 @@ uuid_get_local(void) if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL) return (uuid); +#if defined(__APPLE__) + if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL) + return (uuid); +#elif defined(__FreeBSD__) + if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL) + return (uuid); +#elif defined(__NetBSD__) + if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL) + return (uuid); +#elif defined(__OpenBSD__) + if ((uuid = uuid_get_from_sysctl()) != NULL) + return (uuid); +#elif defined(__linux__) + if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL) + return (uuid); +#endif + #if HAVE_LIBHAL_H - if ((uuid = uuid_get_from_hal()) != NULL) { - return uuid; - } + if ((uuid = uuid_get_from_hal()) != NULL) + return (uuid); #endif if ((uuid = uuid_get_from_dmidecode()) != NULL) return (uuid); +#if defined(__linux__) if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) return (uuid); +#endif return (NULL); }