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
30 #include "configfile.h"
34 #include <sys/sysctl.h>
41 #define UUID_RAW_LENGTH 16
42 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
43 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
45 static char *uuidfile = NULL;
47 static const char *config_keys[] = {
52 looks_like_a_uuid (const char *uuid)
61 if (len < UUID_PRINTABLE_COMPACT_LENGTH)
65 if (!isxdigit ((int)*uuid) && *uuid != '-')
73 uuid_parse_dmidecode(FILE *file)
77 while (fgets (line, sizeof (line), file) != NULL)
82 strstripnewline (line);
84 /* Look for a line reading:
85 * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
87 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
91 if (strcmp("UUID:", fields[0]) != 0)
94 if (!looks_like_a_uuid (fields[1]))
97 return (strdup (fields[1]));
103 uuid_get_from_dmidecode(void)
105 FILE *dmidecode = popen("dmidecode -t system 2>/dev/null", "r");
111 uuid = uuid_parse_dmidecode(dmidecode);
117 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
119 uuid_get_from_sysctlbyname(const char *name)
121 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
122 size_t len = sizeof (uuid);
123 if (sysctlbyname(name, &uuid, &len, NULL, 0) == -1)
125 return (strdup (uuid));
127 #elif defined(__OpenBSD__)
129 uuid_get_from_sysctl(void)
131 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1];
132 size_t len = sizeof (uuid);
138 if (sysctl(mib, 2, uuid, &len, NULL, 0) == -1)
140 return (strdup (uuid));
146 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
147 #define UUID_PROPERTY "smbios.system.uuid"
150 uuid_get_from_hal(void)
157 dbus_error_init(&error);
159 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)))
162 ctx = libhal_ctx_new();
163 libhal_ctx_set_dbus_connection(ctx, con);
165 if (!libhal_ctx_init(ctx, &error))
168 if (!libhal_device_property_exists(ctx,
174 char *uuid = libhal_device_get_property_string(ctx,
178 if (looks_like_a_uuid (uuid))
184 dbus_error_init(&ctxerror);
185 if (!(libhal_ctx_shutdown(ctx, &ctxerror)))
186 dbus_error_free(&ctxerror);
189 libhal_ctx_free(ctx);
192 if (dbus_error_is_set(&error))
193 dbus_error_free(&error);
199 uuid_get_from_file(const char *path)
202 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
204 file = fopen (path, "r");
208 if (!fgets(uuid, sizeof(uuid), file)) {
213 strstripnewline (uuid);
215 return (strdup (uuid));
223 /* Check /etc/uuid / UUIDFile before any other method. */
224 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL)
227 #if defined(__APPLE__)
228 if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL)
230 #elif defined(__FreeBSD__)
231 if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL)
233 #elif defined(__NetBSD__)
234 if ((uuid = uuid_get_from_sysctlbyname("machdep.dmi.system-uuid")) != NULL)
236 #elif defined(__OpenBSD__)
237 if ((uuid = uuid_get_from_sysctl()) != NULL)
239 #elif defined(__linux__)
240 if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL)
245 if ((uuid = uuid_get_from_hal()) != NULL)
249 if ((uuid = uuid_get_from_dmidecode()) != NULL)
252 #if defined(__linux__)
253 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL)
261 uuid_config (const char *key, const char *value)
263 if (strcasecmp (key, "UUIDFile") == 0) {
264 char *tmp = strdup (value);
278 char *uuid = uuid_get_local ();
281 sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
286 WARNING ("uuid: could not read UUID using any known method");
290 void module_register (void)
292 plugin_register_config ("uuid", uuid_config,
293 config_keys, STATIC_ARRAY_SIZE (config_keys));
294 plugin_register_init ("uuid", uuid_init);
298 * vim: set tabstop=4:
299 * vim: set shiftwidth=4:
300 * vim: set expandtab:
304 * indent-tabs-mode: nil