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__)
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));
131 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
132 #define UUID_PROPERTY "smbios.system.uuid"
135 uuid_get_from_hal(void)
142 dbus_error_init(&error);
144 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)))
147 ctx = libhal_ctx_new();
148 libhal_ctx_set_dbus_connection(ctx, con);
150 if (!libhal_ctx_init(ctx, &error))
153 if (!libhal_device_property_exists(ctx,
159 char *uuid = libhal_device_get_property_string(ctx,
163 if (looks_like_a_uuid (uuid))
169 dbus_error_init(&ctxerror);
170 if (!(libhal_ctx_shutdown(ctx, &ctxerror)))
171 dbus_error_free(&ctxerror);
174 libhal_ctx_free(ctx);
177 if (dbus_error_is_set(&error))
178 dbus_error_free(&error);
184 uuid_get_from_file(const char *path)
187 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
189 file = fopen (path, "r");
193 if (!fgets(uuid, sizeof(uuid), file)) {
198 strstripnewline (uuid);
200 return (strdup (uuid));
208 /* Check /etc/uuid / UUIDFile before any other method. */
209 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL)
212 #if defined(__APPLE__)
213 if ((uuid = uuid_get_from_sysctlbyname("kern.uuid")) != NULL)
215 #elif defined(__FreeBSD__)
216 if ((uuid = uuid_get_from_sysctlbyname("kern.hostuuid")) != NULL)
218 #elif defined(__linux__)
219 if ((uuid = uuid_get_from_file("/sys/class/dmi/id/product_uuid")) != NULL)
224 if ((uuid = uuid_get_from_hal()) != NULL)
228 if ((uuid = uuid_get_from_dmidecode()) != NULL)
231 #if defined(__linux__)
232 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL)
240 uuid_config (const char *key, const char *value)
242 if (strcasecmp (key, "UUIDFile") == 0) {
243 char *tmp = strdup (value);
257 char *uuid = uuid_get_local ();
260 sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
265 WARNING ("uuid: could not read UUID using any known method");
269 void module_register (void)
271 plugin_register_config ("uuid", uuid_config,
272 config_keys, STATIC_ARRAY_SIZE (config_keys));
273 plugin_register_init ("uuid", uuid_init);
277 * vim: set tabstop=4:
278 * vim: set shiftwidth=4:
279 * vim: set expandtab:
283 * indent-tabs-mode: nil