From 4cdedd2b57b8a3d259c391abda28778ee185ae07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 11 Feb 2013 17:50:08 +0100 Subject: [PATCH] Feature #1739: Refactor VMMDriver poll so it can be called from IMMDriver --- include/VirtualMachineManagerDriver.h | 5 + src/im/InformationManagerDriver.cc | 43 +++- src/vmm/VirtualMachineManagerDriver.cc | 327 ++++++++++++++----------- 3 files changed, 228 insertions(+), 147 deletions(-) diff --git a/include/VirtualMachineManagerDriver.h b/include/VirtualMachineManagerDriver.h index f6a1ae0f16..59b0326111 100644 --- a/include/VirtualMachineManagerDriver.h +++ b/include/VirtualMachineManagerDriver.h @@ -68,6 +68,11 @@ public: const VirtualMachine * vm, const string& file_name) const = 0; + + static void process_poll(int id, const string &monitor_str); + + static void process_poll(VirtualMachine* vm, const string &monitor_str); + protected: /** * Gets a configuration attr from driver configuration file (single diff --git a/src/im/InformationManagerDriver.cc b/src/im/InformationManagerDriver.cc index 195ab201a0..f647c4ea10 100644 --- a/src/im/InformationManagerDriver.cc +++ b/src/im/InformationManagerDriver.cc @@ -17,6 +17,7 @@ #include "InformationManagerDriver.h" #include "NebulaLog.h" #include "Util.h" +#include "VirtualMachineManagerDriver.h" #include @@ -100,13 +101,20 @@ void InformationManagerDriver::protocol( goto error_driver_info; } - size_t pos; int rc; string hinfo64; string* hinfo; ostringstream oss; + int vmid; + char * error_msg; + string monitor_str; + + VectorAttribute* vatt; + vector vm_att; + vector::iterator it; + getline (is, hinfo64); hinfo = one_util::base64_decode(hinfo64); @@ -124,14 +132,47 @@ void InformationManagerDriver::protocol( goto error_common_info; } + // TODO The hinfo string is parsed again because HostTemplate has + // replace_mode set to true, but we expect several VM vector attributes + Template* tmpl = new Template(); + tmpl->parse(*hinfo, &error_msg); + + tmpl->remove("VM", vm_att); + delete hinfo; + host->remove_template_attribute("VM"); + host->touch(true); hpool->update(host); hpool->update_monitoring(host); host->unlock(); + + for (it=vm_att.begin(); it != vm_att.end(); it++) + { + vatt = dynamic_cast(*it); + + if (vatt == 0) + { + delete *it; + continue; + } + + rc = vatt->vector_value("ID", vmid); + + if (rc == 0) + { + // TODO: update last_poll vm->set_last_poll + + monitor_str = vatt->vector_value("POLL"); + + VirtualMachineManagerDriver::process_poll(vmid, monitor_str); + } + + delete *it; + } } else if (action == "LOG") { diff --git a/src/vmm/VirtualMachineManagerDriver.cc b/src/vmm/VirtualMachineManagerDriver.cc index 14fd61f8c1..0ff895d1b2 100644 --- a/src/vmm/VirtualMachineManagerDriver.cc +++ b/src/vmm/VirtualMachineManagerDriver.cc @@ -395,154 +395,9 @@ void VirtualMachineManagerDriver::protocol( { if (result == "SUCCESS") { - size_t pos; - - string tmp; - string var; - ostringstream os; - istringstream tiss; - - int cpu = -1; - int memory = -1; - long long net_tx = -1; - long long net_rx = -1; - char state = '-'; - string monitor_str = is.str(); - bool parse_error = false; - while(is.good()) - { - is >> tmp >> ws; - - pos = tmp.find('='); - - if ( pos == string::npos ) - { - parse_error = true; - continue; - } - - tmp.replace(pos,1," "); - - tiss.clear(); - - tiss.str(tmp); - - tiss >> var >> ws; - - if (!tiss.good()) - { - parse_error = true; - continue; - } - - if (var == "USEDMEMORY") - { - tiss >> memory; - } - else if (var == "USEDCPU") - { - tiss >> cpu; - } - else if (var == "NETRX") - { - tiss >> net_rx; - } - else if (var == "NETTX") - { - tiss >> net_tx; - } - else if (var == "STATE") - { - tiss >> state; - } - else if (!var.empty()) - { - string val; - - os.str(""); - os << "Adding custom monitoring attribute: " << tmp; - - vm->log("VMM",Log::WARNING,os); - - tiss >> val; - - vm->replace_template_attribute(var,val); - } - } - - if (parse_error) - { - os.str(""); - os << "Error parsing monitoring str:\"" << monitor_str <<"\""; - - vm->log("VMM",Log::ERROR,os); - - vm->set_template_error_message(os.str()); - vmpool->update(vm); - - vm->unlock(); - return; - } - - vm->update_info(memory,cpu,net_tx,net_rx); - vm->set_vm_info(); - - vmpool->update(vm); - vmpool->update_history(vm); - vmpool->update_monitoring(vm); - - if (state != '-' && - (vm->get_lcm_state() == VirtualMachine::RUNNING || - vm->get_lcm_state() == VirtualMachine::UNKNOWN)) - { - Nebula &ne = Nebula::instance(); - LifeCycleManager * lcm = ne.get_lcm(); - - switch (state) - { - case 'a': // Still active, good! - os.str(""); - os << "Monitor Information:\n" - << "\tCPU : "<< cpu << "\n" - << "\tMemory: "<< memory << "\n" - << "\tNet_TX: "<< net_tx << "\n" - << "\tNet_RX: "<< net_rx; - vm->log("VMM",Log::DEBUG,os); - - if ( vm->get_lcm_state() == VirtualMachine::UNKNOWN) - { - vm->log("VMM",Log::INFO,"VM was now found, new state is" - " RUNNING"); - vm->set_state(VirtualMachine::RUNNING); - vmpool->update(vm); - } - break; - - case 'p': // It's paused - vm->log("VMM",Log::INFO,"VM running but new state " - "from monitor is PAUSED."); - - lcm->trigger(LifeCycleManager::MONITOR_SUSPEND, id); - break; - - case 'e': //Failed - vm->log("VMM",Log::INFO,"VM running but new state " - "from monitor is ERROR."); - - lcm->trigger(LifeCycleManager::MONITOR_FAILURE, id); - break; - - case 'd': //The VM was not found - vm->log("VMM",Log::INFO,"VM running but it was not found." - " Restart and delete actions available or try to" - " recover it manually"); - - lcm->trigger(LifeCycleManager::MONITOR_DONE, id); - break; - } - } + process_poll(vm, monitor_str); } else { @@ -566,6 +421,186 @@ void VirtualMachineManagerDriver::protocol( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void VirtualMachineManagerDriver::process_poll( + int id, + const string &monitor_str) +{ + // Get the VM from the pool + VirtualMachine* vm = Nebula::instance().get_vmpool()->get(id,true); + + if ( vm == 0 ) + { + return; + } + + process_poll(vm, monitor_str); + + vm->unlock(); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void VirtualMachineManagerDriver::process_poll( + VirtualMachine* vm, + const string& monitor_str) +{ + size_t pos; + + string tmp; + string var; + ostringstream os; + istringstream tiss; + + int cpu = -1; + int memory = -1; + long long net_tx = -1; + long long net_rx = -1; + char state = '-'; + + bool parse_error = false; + + VirtualMachinePool * vmpool = Nebula::instance().get_vmpool(); + + istringstream is; + is.str(monitor_str); + + while(is.good()) + { + is >> tmp >> ws; + + pos = tmp.find('='); + + if ( pos == string::npos ) + { + parse_error = true; + continue; + } + + tmp.replace(pos,1," "); + + tiss.clear(); + + tiss.str(tmp); + + tiss >> var >> ws; + + if (!tiss.good()) + { + parse_error = true; + continue; + } + + if (var == "USEDMEMORY") + { + tiss >> memory; + } + else if (var == "USEDCPU") + { + tiss >> cpu; + } + else if (var == "NETRX") + { + tiss >> net_rx; + } + else if (var == "NETTX") + { + tiss >> net_tx; + } + else if (var == "STATE") + { + tiss >> state; + } + else if (!var.empty()) + { + string val; + + os.str(""); + os << "Adding custom monitoring attribute: " << tmp; + + vm->log("VMM",Log::WARNING,os); + + tiss >> val; + + vm->replace_template_attribute(var,val); + } + } + + if (parse_error) + { + os.str(""); + os << "Error parsing monitoring str:\"" << monitor_str <<"\""; + + vm->log("VMM",Log::ERROR,os); + + vm->set_template_error_message(os.str()); + vmpool->update(vm); + + return; + } + + vm->update_info(memory,cpu,net_tx,net_rx); + vm->set_vm_info(); + + vmpool->update(vm); + vmpool->update_history(vm); + vmpool->update_monitoring(vm); + + if (state != '-' && + (vm->get_lcm_state() == VirtualMachine::RUNNING || + vm->get_lcm_state() == VirtualMachine::UNKNOWN)) + { + Nebula &ne = Nebula::instance(); + LifeCycleManager * lcm = ne.get_lcm(); + + switch (state) + { + case 'a': // Still active, good! + os.str(""); + os << "Monitor Information:\n" + << "\tCPU : "<< cpu << "\n" + << "\tMemory: "<< memory << "\n" + << "\tNet_TX: "<< net_tx << "\n" + << "\tNet_RX: "<< net_rx; + vm->log("VMM",Log::DEBUG,os); + + if ( vm->get_lcm_state() == VirtualMachine::UNKNOWN) + { + vm->log("VMM",Log::INFO,"VM was now found, new state is" + " RUNNING"); + vm->set_state(VirtualMachine::RUNNING); + vmpool->update(vm); + } + break; + + case 'p': // It's paused + vm->log("VMM",Log::INFO,"VM running but new state " + "from monitor is PAUSED."); + + lcm->trigger(LifeCycleManager::MONITOR_SUSPEND, vm->get_oid()); + break; + + case 'e': //Failed + vm->log("VMM",Log::INFO,"VM running but new state " + "from monitor is ERROR."); + + lcm->trigger(LifeCycleManager::MONITOR_FAILURE, vm->get_oid()); + break; + + case 'd': //The VM was not found + vm->log("VMM",Log::INFO,"VM running but it was not found." + " Restart and delete actions available or try to" + " recover it manually"); + + lcm->trigger(LifeCycleManager::MONITOR_DONE, vm->get_oid()); + break; + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void VirtualMachineManagerDriver::recover() { NebulaLog::log("VMM",Log::INFO,"Recovering VMM drivers");