diff options
author | Peter Schiffer <pschiffe@redhat.com> | 2014-01-30 19:21:24 +0100 |
---|---|---|
committer | Peter Schiffer <pschiffe@redhat.com> | 2014-01-30 19:21:24 +0100 |
commit | 645170c90a1b50fc1e2516f8f5e5af713105e105 (patch) | |
tree | 48718021ac484307876a29b29846f2097a8bc0f5 /src/hardware | |
parent | d2e3f8517d5dc4e4ba8a06afe38425be149b7844 (diff) | |
download | openlmi-providers-645170c90a1b50fc1e2516f8f5e5af713105e105.tar.gz openlmi-providers-645170c90a1b50fc1e2516f8f5e5af713105e105.tar.xz openlmi-providers-645170c90a1b50fc1e2516f8f5e5af713105e105.zip |
Hardware: added disk capacity to LMI_DiskDriveProvider
Diffstat (limited to 'src/hardware')
-rw-r--r-- | src/hardware/LMI_DiskDriveProvider.c | 6 | ||||
-rw-r--r-- | src/hardware/lsblk.c | 27 | ||||
-rw-r--r-- | src/hardware/lsblk.h | 16 | ||||
-rw-r--r-- | src/hardware/smartctl.c | 27 | ||||
-rw-r--r-- | src/hardware/smartctl.h | 1 |
5 files changed, 67 insertions, 10 deletions
diff --git a/src/hardware/LMI_DiskDriveProvider.c b/src/hardware/LMI_DiskDriveProvider.c index 751e16f..ea2ea27 100644 --- a/src/hardware/LMI_DiskDriveProvider.c +++ b/src/hardware/LMI_DiskDriveProvider.c @@ -65,6 +65,7 @@ static CMPIStatus LMI_DiskDriveEnumInstances( LMI_DiskDrive lmi_hdd; const char *ns = KNameSpace(cop); unsigned i, j, rotational = 0; + unsigned long capacity = 0; char instance_id[INSTANCE_ID_LEN], path[PATH_MAX], *name; CMPIUint16 operational_status; SmartctlHdd *smtcl_hdds = NULL; @@ -86,6 +87,7 @@ static CMPIStatus LMI_DiskDriveEnumInstances( } name = lsblk_hdds[i].name; + capacity = lsblk_hdds[i].capacity; LMI_DiskDrive_Init(&lmi_hdd, _cb, ns); @@ -115,6 +117,9 @@ static CMPIStatus LMI_DiskDriveEnumInstances( } else if (strlen(smtcl_hdds[j].model)) { name = smtcl_hdds[j].model; } + if (!capacity) { + capacity = smtcl_hdds[j].capacity; + } LMI_DiskDrive_Init_OperationalStatus(&lmi_hdd, 1); LMI_DiskDrive_Set_OperationalStatus(&lmi_hdd, 0, operational_status); @@ -140,6 +145,7 @@ static CMPIStatus LMI_DiskDriveEnumInstances( LMI_DiskDrive_Set_Name(&lmi_hdd, name); LMI_DiskDrive_Set_ElementName(&lmi_hdd, name); + LMI_DiskDrive_Set_Capacity(&lmi_hdd, capacity); snprintf(path, PATH_MAX, SYSFS_BLOCK_PATH "/%s/queue/rotational", lsblk_hdds[i].basename); diff --git a/src/hardware/lsblk.c b/src/hardware/lsblk.c index ad4e177..4a06f2c 100644 --- a/src/hardware/lsblk.c +++ b/src/hardware/lsblk.c @@ -33,6 +33,7 @@ void init_lsblkhdd_struct(LsblkHdd *hdd) hdd->serial = NULL; hdd->revision = NULL; hdd->vendor = NULL; + hdd->capacity = 0; } /* @@ -94,14 +95,14 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) { short ret = -1; unsigned i, curr_hdd = 0, buffer_size = 0; - char **buffer = NULL, *path, *type; + char **buffer = NULL, *path, *type, *buf, errbuf[STRERROR_BUF_LEN]; struct stat sb; - char errbuf[STRERROR_BUF_LEN]; + unsigned long capacity = 0; lsblk_free_hdds(hdds, hdds_nb); /* get lsblk output */ - if (run_command("lsblk -dPpo NAME,TYPE,MODEL,SERIAL,REV,VENDOR", + if (run_command("lsblk -dPpo NAME,TYPE,MODEL,SIZE,SERIAL,REV,VENDOR", &buffer, &buffer_size) != 0) { goto done; } @@ -147,10 +148,30 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) continue; } + /* size looks like: SIZE="465.8G" */ + buf = get_part_of_string_between(buffer[i], "SIZE=\"", "\""); + if (buf) { + float size = 0; + sscanf(buf, "%f", &size); + if (size) { + const char *letters = "BKMGTPE"; + char unit = buf[strlen(buf) - 1]; + unsigned long multiplier = 0; + for (int i = 0; letters[i]; i++) { + if (unit == letters[i]) { + multiplier = powl(2, 10 * i); + break; + } + } + capacity = round(size * multiplier); + } + } + init_lsblkhdd_struct(&(*hdds)[curr_hdd]); (*hdds)[curr_hdd].name = path; (*hdds)[curr_hdd].type = type; + (*hdds)[curr_hdd].capacity = capacity; (*hdds)[curr_hdd].model = get_part_of_string_between(buffer[i], "MODEL=\"", "\""); (*hdds)[curr_hdd].serial = get_part_of_string_between(buffer[i], "SERIAL=\"", "\""); (*hdds)[curr_hdd].revision = get_part_of_string_between(buffer[i], "REV=\"", "\""); diff --git a/src/hardware/lsblk.h b/src/hardware/lsblk.h index 10c3a78..bb5cccf 100644 --- a/src/hardware/lsblk.h +++ b/src/hardware/lsblk.h @@ -21,19 +21,21 @@ #ifndef LSBLK_H_ #define LSBLK_H_ +#include <math.h> #include <sys/types.h> #include <sys/stat.h> #include "utils.h" /* Disk by lsblk program. */ typedef struct _LsblkHdd { - char *name; /* /dev/path as name */ - char *basename; /* Basename of /dev/path */ - char *type; /* Disk type; disk for disk, rom for cdrom */ - char *model; /* Model */ - char *serial; /* Serial number */ - char *revision; /* Revision */ - char *vendor; /* Vendor */ + char *name; /* /dev/path as name */ + char *basename; /* Basename of /dev/path */ + char *type; /* Disk type; disk for disk, rom for cdrom */ + char *model; /* Model */ + char *serial; /* Serial number */ + char *revision; /* Revision */ + char *vendor; /* Vendor */ + unsigned long capacity; /* Drive's capacity in bytes */ } LsblkHdd; /* diff --git a/src/hardware/smartctl.c b/src/hardware/smartctl.c index 835e8b5..b1b0ce7 100644 --- a/src/hardware/smartctl.c +++ b/src/hardware/smartctl.c @@ -39,6 +39,7 @@ void init_smctlhdd_struct(SmartctlHdd *hdd) hdd->port_speed = 0; hdd->max_port_speed = 0; hdd->rpm = 0xffffffff; + hdd->capacity = 0; } /* @@ -333,6 +334,32 @@ short smartctl_get_hdds(SmartctlHdd **hdds, unsigned *hdds_nb) buf = NULL; continue; } + /* Capacity */ + buf = copy_string_part_after_delim(buffer[i], "User Capacity:"); + if (buf) { + /* capacity is in bytes and uses thousand separator - + * it must be cleaned */ + char *buf2 = (char *)calloc(strlen(buf), sizeof(char)); + if (buf2) { + int k = 0; + for (int j = 0; buf[j]; j++) { + if (buf[j] >= '0' && buf[j] <= '9') { + buf2[k++] = buf[j]; + } else if (buf[j] == 'b') { + /* word after capacity is "bytes" */ + break; + } + } + buf2[k] = '\0'; + sscanf(buf2, "%lu", &(*hdds)[curr_hdd].capacity); + free(buf2); + } else { + warn("Failed to allocate memory."); + } + free(buf); + buf = NULL; + continue; + } } free_2d_buffer(&buffer, &buffer_size); diff --git a/src/hardware/smartctl.h b/src/hardware/smartctl.h index d73d078..8b277e3 100644 --- a/src/hardware/smartctl.h +++ b/src/hardware/smartctl.h @@ -47,6 +47,7 @@ typedef struct _SmartctlHdd { unsigned long port_speed; /* Current Port Speed in b/s */ unsigned long max_port_speed; /* Max Port Speed in b/s */ unsigned rpm; /* RPM of drive */ + unsigned long capacity; /* Drive's capacity in bytes */ } SmartctlHdd; /* |