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
28 #include "configfile.h"
35 #define UUID_RAW_LENGTH 16
36 #define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
37 #define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
39 static char *uuidfile = NULL;
41 static const char *config_keys[] = {
46 looks_like_a_uuid (const char *uuid)
54 if (len < UUID_PRINTABLE_COMPACT_LENGTH)
58 if (!isxdigit ((int)*uuid) && *uuid != '-') return 0;
65 uuid_parse_dmidecode(FILE *file)
69 while (fgets (line, sizeof (line), file) != NULL)
74 strstripnewline (line);
76 /* Look for a line reading:
77 * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
79 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
83 if (strcmp("UUID:", fields[0]) != 0)
86 if (!looks_like_a_uuid (fields[1]))
89 return strdup (fields[1]);
95 uuid_get_from_dmidecode(void)
97 FILE *dmidecode = popen("dmidecode 2>/dev/null", "r");
104 uuid = uuid_parse_dmidecode(dmidecode);
112 #define UUID_PATH "/org/freedesktop/Hal/devices/computer"
113 #define UUID_PROPERTY "smbios.system.uuid"
116 uuid_get_from_hal(void)
123 dbus_error_init(&error);
125 if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) {
129 ctx = libhal_ctx_new();
130 libhal_ctx_set_dbus_connection(ctx, con);
132 if (!libhal_ctx_init(ctx, &error)) {
136 if (!libhal_device_property_exists(ctx,
143 char *uuid = libhal_device_get_property_string(ctx,
147 if (looks_like_a_uuid (uuid)) {
154 dbus_error_init(&ctxerror);
155 if (!(libhal_ctx_shutdown(ctx, &ctxerror))) {
156 dbus_error_free(&ctxerror);
160 libhal_ctx_free(ctx);
161 //dbus_connection_unref(con);
164 if (dbus_error_is_set(&error)) {
165 /*printf("Error %s\n", error.name);*/
166 dbus_error_free(&error);
173 uuid_get_from_file(const char *path)
176 char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
178 file = fopen (path, "r");
182 if (!fgets(uuid, sizeof(uuid), file)) {
187 strstripnewline (uuid);
189 return strdup (uuid);
197 /* Check /etc/uuid / UUIDFile before any other method. */
198 if ((uuid = uuid_get_from_file(uuidfile ? uuidfile : "/etc/uuid")) != NULL){
203 if ((uuid = uuid_get_from_hal()) != NULL) {
208 if ((uuid = uuid_get_from_dmidecode()) != NULL) {
212 if ((uuid = uuid_get_from_file("/sys/hypervisor/uuid")) != NULL) {
220 uuid_config (const char *key, const char *value)
222 if (strcasecmp (key, "UUIDFile") == 0) {
223 char *tmp = strdup (value);
238 char *uuid = uuid_get_local ();
241 sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
246 WARNING ("uuid: could not read UUID using any known method");
250 void module_register (void)
252 plugin_register_config ("uuid", uuid_config,
253 config_keys, STATIC_ARRAY_SIZE (config_keys));
254 plugin_register_init ("uuid", uuid_init);
258 * vim: set tabstop=4:
259 * vim: set shiftwidth=4:
260 * vim: set expandtab:
264 * indent-tabs-mode: nil