diff --git a/include/HostPool.h b/include/HostPool.h index 4e01d1c48e..e6ca8b7b74 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -244,10 +244,12 @@ public: * * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all + * @param seconds Retrieve monitor records in the last seconds * * @return 0 on success */ - int dump_monitoring(string& oss, const string& where); + int dump_monitoring(std::string& oss, const std::string& where, + const int seconds); /** * Dumps the HOST monitoring information for a single HOST @@ -263,7 +265,7 @@ public: filter << "oid = " << hostid; - return dump_monitoring(oss, filter.str()); + return dump_monitoring(oss, filter.str(), -1); } /** diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 122dc8261c..fa5a78fbb0 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -324,10 +324,12 @@ public: * * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all + * @param seconds Retrieve monitor records in the last seconds * * @return 0 on success */ - int dump_monitoring(string& oss, const string& where); + int dump_monitoring(std::string& oss, const std::string& where, + const int seconds); /** * Dumps the VM monitoring information for a single VM @@ -343,7 +345,7 @@ public: filter << "oid = " << vmid; - return dump_monitoring(oss, filter.str()); + return dump_monitoring(oss, filter.str(), -1); } /** diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 4286924f73..7fee7fe9b1 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -165,20 +165,72 @@ int HostPool::update(PoolObjectSQL * objsql) int HostPool::dump_monitoring( string& oss, - const string& where) + const string& where, + const int seconds) { ostringstream cmd; - cmd << "SELECT " << one_db::host_monitor_table << ".body FROM " - << one_db::host_monitor_table << " INNER JOIN " << one_db::host_table - << " ON hid = oid"; - - if ( !where.empty() ) + switch(seconds) { - cmd << " WHERE " << where; - } + case 0: //Get last monitor value + /* + * SELECT host_monitoring.body + * FROM host_monitoring + * INNER JOIN ( + * SELECT hid, MAX(last_mon_time) as last_mon_time + * FROM host_monitoring + * GROUP BY hid + * ) lmt on lmt.hid = host_monitoring.hid AND lmt.last_mon_time = host_monitoring.last_mon_time + * INNER JOIN host_pool ON host_monitoring.hid = oid + * ORDER BY oid; + */ + cmd << "SELECT " << one_db::host_monitor_table << ".body " + << "FROM " << one_db::host_monitor_table << " INNER JOIN (" + << "SELECT hid, MAX(last_mon_time) as last_mon_time FROM " + << one_db::host_monitor_table << " GROUP BY hid) as lmt " + << "ON lmt.hid = " << one_db::host_monitor_table << ".hid " + << "AND lmt.last_mon_time = " << one_db::host_monitor_table + << ".last_mon_time INNER JOIN " << one_db::host_table + << " ON " << one_db::host_monitor_table << ".hid = oid"; - cmd << " ORDER BY hid, " << one_db::host_monitor_table << ".last_mon_time;"; + if ( !where.empty() ) + { + cmd << " WHERE " << where; + } + + cmd << " ORDER BY oid"; + + break; + + case -1: //Get all monitoring + cmd << "SELECT " << one_db::host_monitor_table << ".body FROM " + << one_db::host_monitor_table << " INNER JOIN " << one_db::host_table + << " ON hid = oid"; + + if ( !where.empty() ) + { + cmd << " WHERE " << where; + } + + cmd << " ORDER BY hid, " << one_db::host_monitor_table << ".last_mon_time;"; + + break; + + default: //Get monitor in last s seconds + cmd << "SELECT " << one_db::host_monitor_table << ".body FROM " + << one_db::host_monitor_table << " INNER JOIN " << one_db::host_table + << " ON hid = oid WHERE " << one_db::host_monitor_table + << ".last_mon_time > " << time(nullptr) - seconds; + + if ( !where.empty() ) + { + cmd << " AND " << where; + } + + cmd << " ORDER BY hid, " << one_db::host_monitor_table << ".last_mon_time;"; + + break; + } return PoolSQL::dump(oss, "MONITORING_DATA", cmd); } diff --git a/src/rm/RequestManagerPoolInfoFilter.cc b/src/rm/RequestManagerPoolInfoFilter.cc index de8c4236f2..1dc9216af2 100644 --- a/src/rm/RequestManagerPoolInfoFilter.cc +++ b/src/rm/RequestManagerPoolInfoFilter.cc @@ -540,9 +540,10 @@ void VirtualMachinePoolMonitoring::request_execute( string oss; string where; - string and_clause = ""; int rc; + int seconds = -1; + if ( filter_flag < GROUP ) { att.resp_msg = "Incorrect filter_flag"; @@ -552,33 +553,12 @@ void VirtualMachinePoolMonitoring::request_execute( if (paramList.size() > 2) { - ostringstream oss; - int s = xmlrpc_c::value_int(paramList.getInt(2)); - - switch (s) - { - case 0: //Get last monitor value - oss << one_db::vm_monitor_table << ".last_poll = " - << "(SELECT MAX(last_poll) FROM " << one_db::vm_monitor_table - << " AS t WHERE t.vmid = " << one_db::vm_monitor_table << ".vmid)"; - - and_clause = oss.str(); - break; - - case -1: //Get all monitoring - and_clause = ""; - break; - - default: //Get monitor in last s seconds - oss << one_db::vm_monitor_table << ".last_poll > " << time(nullptr) - s; - and_clause = oss.str(); - break; - } + seconds = xmlrpc_c::value_int(paramList.getInt(2)); } - where_filter(att, filter_flag, -1, -1, and_clause, "", false, false, false, where); + where_filter(att, filter_flag, -1, -1, "", "", false, false, false, where); - rc = (static_cast(pool))->dump_monitoring(oss, where); + rc = (static_cast(pool))->dump_monitoring(oss, where, seconds); if ( rc != 0 ) { @@ -760,41 +740,19 @@ void HostPoolMonitoring::request_execute( { string oss; string where; - string and_clause = ""; int rc; + int seconds = -1; + if (paramList.size() > 1) { - ostringstream oss; - int s = xmlrpc_c::value_int(paramList.getInt(1)); - - switch (s) - { - case 0: //Get last monitor value - oss << one_db::host_monitor_table << ".last_mon_time = " - << "(SELECT MAX(last_mon_time) FROM " << one_db::host_monitor_table - << " AS t WHERE t.hid = " << one_db::host_monitor_table << ".hid)"; - - and_clause = oss.str(); - break; - - case -1: //Get all monitoring - and_clause = ""; - break; - - default: //Get monitor in last s seconds - oss << one_db::host_monitor_table << ".last_mon_time > " << time(nullptr) - s; - and_clause = oss.str(); - break; - } - - and_clause = oss.str(); + seconds = xmlrpc_c::value_int(paramList.getInt(1)); } - where_filter(att, ALL, -1, -1, and_clause, "", false, false, false, where); + where_filter(att, ALL, -1, -1, "", "", false, false, false, where); - rc = (static_cast(pool))->dump_monitoring(oss, where); + rc = (static_cast(pool))->dump_monitoring(oss, where, seconds); if ( rc != 0 ) { diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index e47cdd8711..f87fc8f695 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -334,20 +334,72 @@ int VirtualMachinePool::dump_showback(string& oss, int VirtualMachinePool::dump_monitoring( string& oss, - const string& where) + const string& where, + const int seconds) { ostringstream cmd; - cmd << "SELECT " << one_db::vm_monitor_table << ".body FROM " - << one_db::vm_monitor_table - << " INNER JOIN " << one_db::vm_table << " ON vmid = oid"; - - if ( !where.empty() ) + switch (seconds) { - cmd << " WHERE " << where; - } + case 0: //Get last monitor value + /* + * SELECT vm_monitoring.body + * FROM vm_monitoring + * INNER JOIN ( + * SELECT vmid, MAX(last_poll) as last_poll + * FROM vm_monitoring + * GROUP BY vmid + * ) lpt on lpt.vmid = vm_monitoring.vmid AND lpt.last_poll = vm_monitoring.last_poll + * INNER JOIN vm_pool ON vm_monitoring.vmid = oid + * ORDER BY oid; + */ + cmd << "SELECT " << one_db::vm_monitor_table << ".body " + << "FROM " << one_db::vm_monitor_table << " INNER JOIN (" + << "SELECT vmid, MAX(last_poll) as last_poll FROM " + << one_db::vm_monitor_table << " GROUP BY vmid) as lpt " + << "ON lpt.vmid = " << one_db::vm_monitor_table << ".vmid " + << "AND lpt.last_poll = " << one_db::vm_monitor_table + << ".last_poll INNER JOIN " << one_db::vm_table + << " ON " << one_db::vm_monitor_table << ".vmid = oid"; - cmd << " ORDER BY vmid, " << one_db::vm_monitor_table << ".last_poll;"; + if ( !where.empty() ) + { + cmd << " WHERE " << where; + } + + cmd << " ORDER BY oid"; + + break; + + case -1: //Get all monitoring + cmd << "SELECT " << one_db::vm_monitor_table << ".body FROM " + << one_db::vm_monitor_table << " INNER JOIN " << one_db::vm_table + << " ON vmid = oid"; + + if ( !where.empty() ) + { + cmd << " WHERE " << where; + } + + cmd << " ORDER BY vmid, " << one_db::vm_monitor_table << ".last_poll;"; + + break; + + default: //Get monitor in last s seconds + cmd << "SELECT " << one_db::vm_monitor_table << ".body FROM " + << one_db::vm_monitor_table << " INNER JOIN " << one_db::vm_table + << " ON vmid = oid WHERE " << one_db::vm_monitor_table + << ".last_poll > " << time(nullptr) - seconds; + + if ( !where.empty() ) + { + cmd << " ANS " << where; + } + + cmd << " ORDER BY vmid, " << one_db::vm_monitor_table << ".last_poll;"; + + break; + } return PoolSQL::dump(oss, "MONITORING_DATA", cmd); }