This patch includes one small change: the Processors::get_load_average() method is now const since it does not change the object's state.
Darryl L. Pierce
2010-Mar-22 15:39 UTC
[Ovirt-devel] [PATCH] Added the new Processors agent.
The Processors agent reports details regarding the processing power for the managed node. Defined the agent within schema.xml. Wrote the wrapper code to create and populate an instance of the agent. The properties for the Processors agent have also been trimmed back to what is readily available via HAL. Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- src/Makefile.am | 26 +++++++++------- src/host.cpp | 2 + src/host.h | 3 ++ src/processors.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/processors.h | 48 +++++++++++++++++++++++++++++ src/schema.xml | 11 +++++++ 6 files changed, 165 insertions(+), 11 deletions(-) create mode 100644 src/processors.cpp create mode 100644 src/processors.h diff --git a/src/Makefile.am b/src/Makefile.am index 8c94fba..ee30443 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,14 +5,16 @@ sbin_PROGRAMS = matahari first = qmf/com/redhat/matahari/Host.cpp generated_file_list = \ - qmf/com/redhat/matahari/CPU.cpp\ - qmf/com/redhat/matahari/NIC.cpp\ - qmf/com/redhat/matahari/Package.cpp\ - qmf/com/redhat/matahari/Host.h\ - qmf/com/redhat/matahari/CPU.h\ - qmf/com/redhat/matahari/NIC.h\ - qmf/com/redhat/matahari/Package.h\ - qmf/com/redhat/matahari/ArgsNICIdentify_nic.h + qmf/com/redhat/matahari/ArgsNICIdentify_nic.h \ + qmf/com/redhat/matahari/CPU.cpp \ + qmf/com/redhat/matahari/CPU.h \ + qmf/com/redhat/matahari/Host.h \ + qmf/com/redhat/matahari/NIC.cpp \ + qmf/com/redhat/matahari/NIC.h \ + qmf/com/redhat/matahari/Package.cpp \ + qmf/com/redhat/matahari/Package.h \ + qmf/com/redhat/matahari/Processors.cpp \ + qmf/com/redhat/matahari/Processors.h nodist_matahari_SOURCES = $(generated_file_list) $(first) @@ -25,11 +27,13 @@ matahari_SOURCES = \ host.h \ main.cpp \ nic.cpp \ - nic.h + nic.h \ + processors.cpp \ + processors.h -$(generated_file_list): $(first) +$(generated_file_list): $(generated_file_list) -$(first): schema.xml +$(first): schema.xml $(generated_file_list) qmf-gen -o ./qmf ./schema.xml BUILT_SOURCES = $(generated_file_list) $(first) diff --git a/src/host.cpp b/src/host.cpp index 0d22211..c81d2f1 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -78,6 +78,8 @@ void HostWrapper::setupQMFObjects(ManagementAgent *agent) agent->addObject(mgmt_object); syncQMFHostObject(); + processors.setup(agent); + // Iterate over list and set up CPU objects for (vector<CPUWrapper*>::iterator iter = cpus.begin(); iter!= cpus.end(); diff --git a/src/host.h b/src/host.h index 06bdbf0..148c91f 100644 --- a/src/host.h +++ b/src/host.h @@ -25,6 +25,7 @@ #include "cpu.h" #include "nic.h" +#include "processors.h" using namespace qpid::management; using namespace std; @@ -49,6 +50,8 @@ class HostWrapper : public Manageable vector<CPUWrapper*> cpus; vector<NICWrapper*> nics; + ProcessorsAgent processors; + // QMF related fields ManagementAgent *agent; qmf::com::redhat::matahari::Host *mgmt_object; diff --git a/src/processors.cpp b/src/processors.cpp new file mode 100644 index 0000000..32a19d4 --- /dev/null +++ b/src/processors.cpp @@ -0,0 +1,86 @@ +/* processor.cpp - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include <iostream> +#include <fstream> +#include <sstream> +#include <string> +#include <vector> +#include <stdexcept> + +#include <hal/libhal.h> + +#include <pcre.h> + +#include "hal.h" +#include "processors.h" + +#include "qmf/com/redhat/matahari/ArgsProcessorsGet_load_average.h" + +using namespace std; +namespace _qmf = qmf::com::redhat::matahari; + +extern DBusError dbus_error; + +void +ProcessorsAgent::setup(ManagementAgent* agent) +{ + // setup the management object + management_object = new _qmf::Processors(agent, this); + agent->addObject(management_object); + + LibHalContext* context = get_hal_ctx(); + + int num_results; + char** processors = libhal_find_device_by_capability(context,"processor", &num_results, &dbus_error); + + if (!processors) + throw runtime_error("Error: could not query processors via HAL."); + + // populate the managed object's values + management_object->set_model(libhal_device_get_property_string(context, processors[0], "info.product", &dbus_error)); + management_object->set_cores(num_results); +} + +Manageable::status_t +ProcessorsAgent::ManagementMethod(uint32_t id, Args& args, string& text) +{ + switch(id) + { + case _qmf::Processors::METHOD_GET_LOAD_AVERAGE: + _qmf::ArgsProcessorsGet_load_average& loadavg = (_qmf::ArgsProcessorsGet_load_average&) args; + loadavg.o_ret = get_load_average(); + return STATUS_OK; + } + + return STATUS_NOT_IMPLEMENTED; +} + +double +ProcessorsAgent::get_load_average(void) const +{ + double result; + ifstream input; + + input.open("/proc/loadavg", ios::in); + input >> result; + input.close(); + + return result; +} diff --git a/src/processors.h b/src/processors.h new file mode 100644 index 0000000..6833c10 --- /dev/null +++ b/src/processors.h @@ -0,0 +1,48 @@ +/* processor.h - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce <dpierce at redhat.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. A copy of the GNU General Public License is + * also available at http://www.gnu.org/copyleft/gpl.html. + */ + +#include <qpid/agent/ManagementAgent.h> +#include <qpid/management/Manageable.h> +#include <qpid/management/ManagementObject.h> +#include <qpid/agent/ManagementAgent.h> + +#include "qmf/com/redhat/matahari/Processors.h" + +using namespace qpid::management; +using namespace std; + +using qpid::management::Manageable; + +class ProcessorsAgent : public Manageable +{ + private: + qmf::com::redhat::matahari::Processors* management_object; + + public: + ProcessorsAgent() {} + virtual ~ProcessorsAgent() {} + + ManagementObject* GetManagementObject(void) const { return management_object; } + Manageable::status_t ManagementMethod(uint32_t id, Args& args, string& text); + + void setup(ManagementAgent* agent); + + // agent methods + double get_load_average(void) const; +}; diff --git a/src/schema.xml b/src/schema.xml index 5fb683f..40ab3d0 100644 --- a/src/schema.xml +++ b/src/schema.xml @@ -14,6 +14,17 @@ </class> + <!-- The processor for the node. --> + <class name="Processors"> + <property name="node" type="objId" access="RO" desc="The managed node." /> + <property name="model" type="lstr" access="RO" desc="The processor label." /> + <property name="cores" type="int8" access="RO" desc="The number of cores." /> + + <method name="get_load_average" desc="Returns the load average."> + <arg name="ret" dir="O" type="double" /> + </method> + </class> + <class name="CPU"> <property name="host" type="objId" access="RO" desc="Host that this cpu belongs to" index="y" references="Host" parentRef="y"/> -- 1.6.6.1