2 * collectd - src/uuid.c
3 * Copyright (C) 2007 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Dan Berrange <berrange@redhat.com>
20 * Richard W.M. Jones <rjones@redhat.com>
22 * Derived from UUID detection code by Dan Berrange <berrange@redhat.com>
23 * http://hg.et.redhat.com/virt/daemons/spectre--devel?f=f6e3a1b06433;file=lib/uuid.c
29 #include "configfile.h"
36 #define UUID_RAW_LENGTH 16
37 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
38 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
40 static char *uuidfile = NULL;
42 static const char *config_keys[] = {
47 looks_like_a_uuid (const char *uuid)
55 if (len < UUID_PRINTABLE_COMPACT_LENGTH)
59 if (!isxdigit ((int)*uuid) && *uuid != '-') return 0;
66 uuid_parse_dmidecode(FILE *file)
70 while (fgets (line, sizeof (line), file) != NULL)
75 strstripnewline (line);
77 /* Look for a line reading:
78 * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
80 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
84 if (strcmp("UUID:", fields[0]) != 0)
87 if (!looks_like_a_uuid (fields[1]))
90 return strdup (fields[1]);
96 uuid_get_from_dmidecode(void)
98 FILE *dmidecode = popen("dmidecode 2>/dev/null", "r");
105 uuid = uuid_parse_dmidecode(dmidecode);
113 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
114 #define UUID_PROPERTY "smbios.system.uuid"
117 uuid_get_from_hal(void)
124 dbus_error_init(&error);
126 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) {
130 ctx = libhal_ctx_new();
131 libhal_ctx_set_dbus_connection(ctx, con);
133 if (!libhal_ctx_init(ctx, &error)) {
137 if (!libhal_device_property_exists(ctx,
144 char *uuid = libhal_device_get_property_string(ctx,
148 if (looks_like_a_uuid (uuid)) {
155 dbus_error_init(&ctxerror);
156 if (!(libhal_ctx_shutdown(ctx, &ctxerror))) {
157 dbus_error_free(&ctxerror);
161 libhal_ctx_free(ctx);
162 //dbus_connection_unref(con);
165 if (dbus_error_is_set(&error)) {
166 /*printf("Error %s\n", error.name);*/
167 dbus_error_free(&error);
174 uuid_get_from_file(const char *path)
177 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
179 file = fopen (path, "r");
183 if (!fgets(uuid, sizeof(uuid), file)) {
188 strstripnewline (uuid);
190 return strdup (uuid);
198 /* Check /etc/uuid / UUIDFile before any other method. */
199 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL){
204 if ((uuid = uuid_get_from_hal()) != NULL) {
209 if ((uuid = uuid_get_from_dmidecode()) != NULL) {
213 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) {
221 uuid_config (const char *key, const char *value)
223 if (strcasecmp (key, "UUIDFile") == 0) {
224 char *tmp = strdup (value);
239 char *uuid = uuid_get_local ();
242 sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
247 WARNING ("uuid: could not read UUID using any known method");
251 void module_register (void)
253 plugin_register_config ("uuid", uuid_config,
254 config_keys, STATIC_ARRAY_SIZE (config_keys));
255 plugin_register_init ("uuid", uuid_init);
259 * vim: set tabstop=4:
260 * vim: set shiftwidth=4:
261 * vim: set expandtab:
265 * indent-tabs-mode: nil