2 * collectd - src/uuid.c
3 * Copyright (C) 2007 Red Hat Inc.
4 * Copyright (C) 2015 Ruben Kerkhof
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Dan Berrange <berrange@redhat.com>
21 * Richard W.M. Jones <rjones@redhat.com>
23 * Derived from UUID detection code by Dan Berrange <berrange@redhat.com>
24 * http://hg.et.redhat.com/virt/daemons/spectre--devel?f=f6e3a1b06433;file=lib/uuid.c
33 #include <sys/sysctl.h>
40 #define UUID_RAW_LENGTH 16
41 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
42 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
44 static char *uuidfile = NULL;
46 static const char *config_keys[] = {
51 looks_like_a_uuid (const char *uuid)
60 if (len < UUID_PRINTABLE_COMPACT_LENGTH)
64 if (!isxdigit ((int)*uuid) && *uuid != '-')
72 uuid_parse_dmidecode(FILE *file)
76 while (fgets (line, sizeof (line), file) != NULL)
81 strstripnewline (line);
83 /* Look for a line reading:
84 * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
86 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
90 if (strcmp("UUID:", fields[0]) != 0)
93 if (!looks_like_a_uuid (fields[1]))
96 return (strdup (fields[1]));
102 uuid_get_from_dmidecode(void)
104 FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r");
110 uuid = uuid_parse_dmidecode(dmidecode);
116 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
118 uuid_get_from_sysctlbyname(const char *name)
120 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
121 size_t len = sizeof (uuid);
122 if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1)
124 return (strdup (uuid));
126 #elif defined(__OpenBSD__)
128 uuid_get_from_sysctl(void)
130 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
131 size_t len = sizeof (uuid);
137 if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1)
139 return (strdup (uuid));
145 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
146 #define UUID_PROPERTY "smbios.system.uuid"
149 uuid_get_from_hal(void)
156 dbus_error_init(&error);
158 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)))
161 ctx = libhal_ctx_new();
162 libhal_ctx_set_dbus_connection(ctx, con);
164 if (!libhal_ctx_init(ctx, &error))
167 if (!libhal_device_property_exists(ctx,
173 char *uuid = libhal_device_get_property_string(ctx,
177 if (looks_like_a_uuid (uuid))
183 dbus_error_init(&ctxerror);
184 if (!(libhal_ctx_shutdown(ctx, &ctxerror)))
185 dbus_error_free(&ctxerror);
188 libhal_ctx_free(ctx);
191 if (dbus_error_is_set(&error))
192 dbus_error_free(&error);
198 uuid_get_from_file(const char *path)
201 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
203 file = fopen (path, "r");
207 if (!fgets(uuid, sizeof(uuid), file)) {
212 strstripnewline (uuid);
214 return (strdup (uuid));
222 /* Check /etc/uuid / UUIDFile before any other method. */
223 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL)
226 #if defined(__APPLE__)
227 if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL)
229 #elif defined(__FreeBSD__)
230 if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL)
232 #elif defined(__NetBSD__)
233 if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL)
235 #elif defined(__OpenBSD__)
236 if ((uuid = uuid_get_from_sysctl()) != NULL)
238 #elif defined(__linux__)
239 if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL)
244 if ((uuid = uuid_get_from_hal()) != NULL)
248 if ((uuid = uuid_get_from_dmidecode()) != NULL)
251 #if defined(__linux__)
252 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL)
260 uuid_config (const char *key, const char *value)
262 if (strcasecmp (key, "UUIDFile") == 0) {
263 char *tmp = strdup (value);
277 char *uuid = uuid_get_local ();
280 sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
285 WARNING ("uuid: could not read UUID using any known method");
289 void module_register (void)
291 plugin_register_config ("uuid", uuid_config,
292 config_keys, STATIC_ARRAY_SIZE (config_keys));
293 plugin_register_init ("uuid", uuid_init);
297 * vim: set tabstop=4:
298 * vim: set shiftwidth=4:
299 * vim: set expandtab:
303 * indent-tabs-mode: nil