Darryl L. Pierce
2010-May-19 21:00 UTC
[Ovirt-devel] [PATCH matahari] Moved the follow platform details to the Platform class to keep the details abstract and platform-independent:
* UUID * hostname * hardware architecture * total system memory * hypervisor Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- src/host.cpp | 61 ++++------------------------------------------- src/linux_platform.cpp | 58 +++++++++++++++++++++++++++++++++++++++++---- src/platform.h | 37 ++++++++++++++++++++++++---- 3 files changed, 90 insertions(+), 66 deletions(-) diff --git a/src/host.cpp b/src/host.cpp index 34d4550..b9b4a7f 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -19,10 +19,7 @@ #include <fstream> -#include <libvirt/libvirt.h> #include <qpid/management/Manageable.h> -#include <sys/sysinfo.h> -#include <sys/utsname.h> #include "host.h" #include "platform.h" @@ -51,58 +48,12 @@ HostAgent::setup(ManagementAgent* agent) iter->setup(agent, this); } - struct utsname details; - string uuid = "Unknown"; - string hostname = "Unknown"; - string hypervisor = "Unknown"; - string architecture = "None"; - unsigned long memory = 0; - bool beeping = false; - - ifstream input("/var/lib/dbus/machine-id"); - - if(input.is_open()) - { - getline(input, uuid); - input.close(); - } - - if(!uname(&details)) - { - hostname = string(details.nodename); - architecture = string(details.machine); - } - else - { - throw runtime_error("Unable to retrieve system details"); - } - - virConnectPtr lvconn = virConnectOpenReadOnly(NULL); - - if(lvconn) - { - hypervisor = string(virConnectGetType(lvconn)); - virConnectClose(lvconn); - } - - struct sysinfo sysinf; - if(!sysinfo(&sysinf)) - { - memory = sysinf.totalram / 1024L; - } - else - { - throw runtime_error("Unable to retrieve system memory details."); - } - - cout << "memory: " << memory << endl; - - management_object->set_uuid(uuid); - management_object->set_hostname(hostname); - management_object->set_hypervisor(hypervisor); - management_object->set_arch(architecture); - management_object->set_memory(memory); - management_object->set_beeping(beeping); + management_object->set_uuid(Platform::instance()->getUUID()); + management_object->set_hostname(Platform::instance()->getHostname()); + management_object->set_hypervisor(Platform::instance()->getHypervisor()); + management_object->set_arch(Platform::instance()->getArchitecture()); + management_object->set_memory(Platform::instance()->getMemory()); + management_object->set_beeping(false); } void diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp index fbcfdb9..805f071 100644 --- a/src/linux_platform.cpp +++ b/src/linux_platform.cpp @@ -21,11 +21,14 @@ #include <iomanip> #include <iostream> #include <dirent.h> +#include <libvirt/libvirt.h> #include <net/if.h> #include <pcre.h> #include <stdexcept> #include <string.h> #include <sys/ioctl.h> +#include <sys/sysinfo.h> +#include <sys/utsname.h> // TODO remove this wrapper once rhbz#583747 is fixed extern "C" { @@ -39,6 +42,50 @@ LinuxPlatform::LinuxPlatform() int core_count = 0; string model = "unknown"; + string uuid; + ifstream* input; + + input = new ifstream("/var/lib/dbus/machine-id"); + + if(input->is_open()) + { + getline(*input, uuid); + input->close(); + this->setUUID(uuid); + } + input->close(); + delete input; + + struct utsname details; + + if(!uname(&details)) + { + this->setHostname(string(details.nodename)); + this->setArchitecture(string(details.machine)); + } + else + { + throw runtime_error("Unable to retrieve system details"); + } + + virConnectPtr lvconn = virConnectOpenReadOnly(NULL); + + if(lvconn) + { + this->setHypervisor(string(virConnectGetType(lvconn))); + virConnectClose(lvconn); + } + + struct sysinfo sysinf; + if(!sysinfo(&sysinf)) + { + this->setMemory(sysinf.totalram / 1024L); + } + else + { + throw runtime_error("Unable to retrieve system memory details."); + } + struct udev* udev = udev_new(); struct udev_enumerate* enumerator = udev_enumerate_new(udev); @@ -58,8 +105,8 @@ LinuxPlatform::LinuxPlatform() udev_enumerate_unref(enumerator); udev_unref(udev); - ifstream input("/proc/cpuinfo"); - if(input.is_open()) + input = new ifstream("/proc/cpuinfo"); + if(input->is_open()) { string regexstr = "(.*\\S)\\s*:\\s*(\\S.*)"; int expected = 3; @@ -73,11 +120,11 @@ LinuxPlatform::LinuxPlatform() regex = pcre_compile(regexstr.c_str(), 0, &pcre_error, &pcre_error_offset, NULL); if(!regex) { throw runtime_error("Unable to compile regular expression."); } - while(!input.eof() && !done) + while(!input->eof() && !done) { string line; - getline(input, line); + getline(*input, line); int match = pcre_exec(regex, NULL, line.c_str(), line.length(), 0, PCRE_NOTEMPTY,found, expected * 3); @@ -104,7 +151,8 @@ LinuxPlatform::LinuxPlatform() } } } - input.close(); + input->close(); + delete input; } } diff --git a/src/platform.h b/src/platform.h index 54fbe37..1a976a6 100644 --- a/src/platform.h +++ b/src/platform.h @@ -37,25 +37,50 @@ class Platform private: static Platform* _instance; - string processor_model; - unsigned int number_of_cores; + string _uuid; + string _hostname; + string _architecture; + string _hypervisor; + unsigned long _memory; + string _processor_model; + unsigned int _number_of_cores; protected: Platform() {} virtual~ Platform() {} - void set_processor_model(const string model) { processor_model = model; } - void set_number_of_cores(const int number) { number_of_cores = number; } + void setUUID(const string uuid) { _uuid = uuid; } + void setHostname(const string hostname) { _hostname = hostname; } + void setArchitecture(const string arch) { _architecture = arch; } + void setHypervisor(const string hypervisor) { _hypervisor = hypervisor; } + void setMemory(const unsigned long memory) { _memory = memory; } + void set_processor_model(const string model) { _processor_model = model; } + void set_number_of_cores(const int number) { _number_of_cores = number; } public: // the singleton instance static Platform* instance(); + // returns the UUID for the installation + string getUUID() const { return _uuid; } + + // returns the fully qualified hostname + string getHostname() const { return _hostname; } + + // returns the system architecture + string getArchitecture() const { return _architecture; } + + // returns the hypervisor + string getHypervisor() const { return _hypervisor; } + + // returns the total system memory + unsigned long getMemory() const { return _memory; } + // returns text describing the processor model. - string get_processor_model() const { return processor_model; } + string get_processor_model() const { return _processor_model; } // returns the number of cores in the processor. - int get_number_of_cores() const { return number_of_cores; } + int get_number_of_cores() const { return _number_of_cores; } // returns the load average for the platform virtual double get_load_average() const = 0; -- 1.6.6.1