diff --git a/src/monitor/include/HostMonitorManager.h b/src/monitor/include/HostMonitorManager.h index d112d9f241..0b749fd40b 100644 --- a/src/monitor/include/HostMonitorManager.h +++ b/src/monitor/include/HostMonitorManager.h @@ -117,13 +117,19 @@ public: /** * Sets the monitor information of the VM. * @param oid VM id - * @param result * @param tmpl monitoring template */ void monitor_vm(int oid, - const std::string &deploy_id, const Template &tmpl); + /** + * Sets the monitor information of the VM. + * @param deploy_id Wild VM deploy_id + * @param tmpl monitoring template + */ + void monitor_wild_vm(const std::string &deploy_id, + const Template &tmpl); + /** * Receive start monitor failure/success from driver * @param oid host id diff --git a/src/monitor/include/VMRPCPool.h b/src/monitor/include/VMRPCPool.h index 31f01873eb..5924322257 100644 --- a/src/monitor/include/VMRPCPool.h +++ b/src/monitor/include/VMRPCPool.h @@ -53,6 +53,11 @@ public: */ int update_monitoring(const VirtualMachineMonitorInfo& vm); + /** + * Read last monitoring from DB + */ + bool get_monitoring(int vmid, VirtualMachineMonitorInfo& vm); + /** * Gets a VM ID by its deploy_id * @param deploy_id to search the id for diff --git a/src/monitor/src/data_model/VMRPCPool.cc b/src/monitor/src/data_model/VMRPCPool.cc index 0d2dbe5447..7db54db334 100644 --- a/src/monitor/src/data_model/VMRPCPool.cc +++ b/src/monitor/src/data_model/VMRPCPool.cc @@ -62,6 +62,38 @@ int VMRPCPool::update_monitoring(const VirtualMachineMonitorInfo& monitoring) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +bool VMRPCPool::get_monitoring(int vmid, VirtualMachineMonitorInfo& vm) +{ + ostringstream cmd; + string monitor_str; + + cmd << "SELECT " << one_db::vm_monitor_table << ".body FROM " + << one_db::vm_monitor_table + << " WHERE vmid = " << vmid + << " AND last_poll=(SELECT MAX(last_poll) FROM " + << one_db::vm_monitor_table + << " WHERE vmid = " << vmid << ")"; + + string_cb cb(1); + + cb.set_callback(&monitor_str); + + int rc = db->exec_rd(cmd, &cb); + + cb.unset_callback(); + + if (rc == 0 && !monitor_str.empty()) + { + vm.from_xml(monitor_str); + return true; + } + + return false; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VMRPCPool::get_vmid(const string& deploy_id) { int rc; diff --git a/src/monitor/src/monitor/HostMonitorManager.cc b/src/monitor/src/monitor/HostMonitorManager.cc index 226524c989..8820927e7f 100644 --- a/src/monitor/src/monitor/HostMonitorManager.cc +++ b/src/monitor/src/monitor/HostMonitorManager.cc @@ -307,7 +307,6 @@ void HostMonitorManager::update_last_monitor(int oid) /* -------------------------------------------------------------------------- */ void HostMonitorManager::monitor_vm(int oid, - const string& uuid, const Template &tmpl) { if (!is_leader) @@ -315,20 +314,11 @@ void HostMonitorManager::monitor_vm(int oid, return; } - if (oid < 0) - { - // Wild VM, check if it is imported to OpenNebula - oid = vmpool->get_vmid(uuid); - - if (oid < 0) - { - // Not imported VM, ignore monitoring - return; - } - } - VirtualMachineMonitorInfo monitoring(oid, time(nullptr)); + //Get previous monitor info to merge with new data + vmpool->get_monitoring(oid, monitoring); + if (monitoring.from_template(tmpl) != 0) { string str; @@ -349,6 +339,29 @@ void HostMonitorManager::monitor_vm(int oid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void HostMonitorManager::monitor_wild_vm(const string& deploy_id, + const Template &tmpl) +{ + if (!is_leader) + { + return; + } + + // Wild VM, check if it is imported to OpenNebula + int oid = vmpool->get_vmid(deploy_id); + + if (oid < 0) + { + // Not imported VM, ignore monitoring + return; + } + + monitor_vm(oid, tmpl); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void HostMonitorManager::start_monitor_failure(int oid) { if (!is_leader) diff --git a/src/monitor/src/monitor/MonitorDriverProtocol.cc b/src/monitor/src/monitor/MonitorDriverProtocol.cc index d16bab8d2e..57ab93dd83 100644 --- a/src/monitor/src/monitor/MonitorDriverProtocol.cc +++ b/src/monitor/src/monitor/MonitorDriverProtocol.cc @@ -68,12 +68,12 @@ void MonitorDriverProtocol::_monitor_vm(message_t msg) return; } - map> vms_templ; + map vms_templ; vector vms; tmpl.get("VM", vms); - // Merge all attributes by deploy_id + // Merge all attributes by ID for (const auto& vm : vms) { int id = -1; @@ -90,35 +90,47 @@ void MonitorDriverProtocol::_monitor_vm(message_t msg) auto monitor_plain = one_util::base64_decode(monitor_b64); - if (monitor_plain != nullptr) + if (monitor_plain == nullptr) { - Template mon_tmpl; + NebulaLog::error("MDP", "Error decoding VM monitor attribute: " + + monitor_b64); + continue; + } - rc = mon_tmpl.parse(*monitor_plain, &error_msg); + Template mon_tmpl; - if (rc != 0) - { - NebulaLog::error("MDP", "Error parsing VM monitor attribute: " - + *monitor_plain + ", error: " + error_msg); + rc = mon_tmpl.parse(*monitor_plain, &error_msg); - delete monitor_plain; - - free(error_msg); - continue; - } + if (rc != 0) + { + NebulaLog::error("MDP", "Error parsing VM monitor attribute: " + + *monitor_plain + ", error: " + error_msg); delete monitor_plain; - auto it = vms_templ.find(deploy_id); + free(error_msg); + continue; + } + + delete monitor_plain; + + if (id < 0) + { + // Wild VM, no need to merge storage monitor data + hm->monitor_wild_vm(deploy_id, mon_tmpl); + } + else + { + // OpenNebula VM, merge templates with same ID + auto it = vms_templ.find(id); if (it == vms_templ.end()) { - vms_templ.insert(make_pair(std::move(deploy_id), - make_pair(id, std::move(mon_tmpl)))); + vms_templ[id] = std::move(mon_tmpl); } else { - it->second.second.merge(&mon_tmpl); + it->second.merge(&mon_tmpl); } } } @@ -126,7 +138,7 @@ void MonitorDriverProtocol::_monitor_vm(message_t msg) // Process all monitoring templates for (const auto& vm : vms_templ) { - hm->monitor_vm(vm.second.first, vm.first, vm.second.second); + hm->monitor_vm(vm.first, vm.second); } } @@ -247,7 +259,7 @@ void MonitorDriverProtocol::_state_vm(message_t msg) */ void MonitorDriverProtocol::_start_monitor(message_t msg) { - NebulaLog::debug("MDP", "Received start monitor for host " + + NebulaLog::ddebug("MDP", "Received start monitor for host " + to_string(msg->oid()) + ": " + msg->payload()); if (msg->status() != "SUCCESS") diff --git a/src/tm_mad/shared/monitor b/src/tm_mad/shared/monitor index 5ebacd89bf..b689f6b0de 100755 --- a/src/tm_mad/shared/monitor +++ b/src/tm_mad/shared/monitor @@ -78,13 +78,15 @@ echo "TOTAL_MB=\$TOTAL_MB" if [ $MONITOR_VM_DISKS -eq 1 ]; then vms=\$(ls "$BASE_PATH" | grep '^[0-9]\+$') + monitor="" + for vm in \$vms; do vmdir="${BASE_PATH}/\${vm}" disks=\$(ls "\$vmdir" 2>/dev/null | grep '^disk\.[0-9]\+$') [ -z \$disks ] && continue - echo -n "VM=[ID=\$vm,POLL=\"" + vm_monitor="" for disk in \$disks; do disk_id="\$(echo "\$disk" | cut -d. -f2)" @@ -92,7 +94,8 @@ if [ $MONITOR_VM_DISKS -eq 1 ]; then snap_dir="\${vmdir}/\${disk}.snap" [ -z "\$disk_size" ] && continue - echo -n "DISK_SIZE=[ID=\${disk_id},SIZE=\${disk_size}] " + + vm_monitor="\${vm_monitor} DISK_SIZE = [ ID=\${disk_id}, SIZE=\${disk_size}]" if [ -e "\$snap_dir" ]; then snaps="\$(ls "\$snap_dir" 2>/dev/null | grep '^[0-9]$')" @@ -100,13 +103,20 @@ if [ $MONITOR_VM_DISKS -eq 1 ]; then for snap in \$snaps; do snap_size="\$(du -mL "\${snap_dir}/\${snap}" 2>/dev/null | awk '{print \$1}')" [ -z "\$snap_size" ] && continue - echo -n "SNAPSHOT_SIZE=[ID=\${snap},DISK_ID=\${disk_id},SIZE=\${snap_size}] " + + vm_monitor="\${vm_monitor}\nSNAPSHOT_SIZE = "\ + "[ ID=\${snap}, DISK_ID=\${disk_id}, SIZE=\${snap_size}]" done fi done - echo "\"]" + vm_monitor_b64="\$(echo \$vm_monitor | base64 -w 0)" + monitor="\${monitor}VM=[ID=\$vm,MONITOR=\"\$vm_monitor_b64\"] " done + + monitor_b64="\$(echo \$monitor | ruby -e "require 'zlib';\ + puts Zlib::Deflate.deflate(STDIN.read)" | base64 -w 0)" + echo MONITOR_VM SUCCESS 0 \$monitor_b64 | nc -u -w0 127.0.0.1 4124 fi EOF