|
57 | 57 | import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
58 | 58 | import org.apache.cloudstack.managed.context.ManagedContextRunnable; |
59 | 59 | import org.apache.cloudstack.management.ManagementServerHost; |
| 60 | +import org.apache.cloudstack.network.RoutedIpv4Manager; |
60 | 61 | import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
61 | 62 | import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
62 | 63 | import org.apache.cloudstack.utils.bytescale.ByteScaleUtils; |
|
113 | 114 | import com.cloud.hypervisor.Hypervisor; |
114 | 115 | import com.cloud.hypervisor.Hypervisor.HypervisorType; |
115 | 116 | import com.cloud.network.as.AutoScaleManager; |
| 117 | +import com.cloud.network.dao.NetworkDao; |
| 118 | +import com.cloud.network.dao.NetworkVO; |
116 | 119 | import com.cloud.org.Cluster; |
117 | 120 | import com.cloud.resource.ResourceManager; |
118 | 121 | import com.cloud.resource.ResourceState; |
|
203 | 206 | @Component |
204 | 207 | public class StatsCollector extends ManagerBase implements ComponentMethodInterceptable, Configurable, DbStatsCollection { |
205 | 208 |
|
| 209 | + @Inject |
| 210 | + private NetworkDao networkDao; |
| 211 | + @Inject |
| 212 | + private RoutedIpv4Manager routedIpv4Manager; |
| 213 | + |
206 | 214 | public static enum ExternalStatsProtocol { |
207 | 215 | NONE("none"), GRAPHITE("graphite"), INFLUXDB("influxdb"); |
208 | 216 | String _type; |
@@ -268,9 +276,9 @@ public String toString() { |
268 | 276 | private static final ConfigKey<Integer> vmDiskStatsIntervalMin = new ConfigKey<>("Advanced", Integer.class, "vm.disk.stats.interval.min", "300", |
269 | 277 | "Minimal interval (in seconds) to report vm disk statistics. If vm.disk.stats.interval is smaller than this, use this to report vm disk statistics.", false); |
270 | 278 | private static final ConfigKey<Integer> vmNetworkStatsInterval = new ConfigKey<>("Advanced", Integer.class, "vm.network.stats.interval", "0", |
271 | | - "Interval (in seconds) to report vm network statistics (for Shared networks). Vm network statistics will be disabled if this is set to 0 or less than 0.", false); |
| 279 | + "Interval (in seconds) to report vm network statistics (for Shared and Routed networks). Vm network statistics will be disabled if this is set to 0 or less than 0.", false); |
272 | 280 | private static final ConfigKey<Integer> vmNetworkStatsIntervalMin = new ConfigKey<>("Advanced", Integer.class, "vm.network.stats.interval.min", "300", |
273 | | - "Minimal Interval (in seconds) to report vm network statistics (for Shared networks). If vm.network.stats.interval is smaller than this, use this to report vm network statistics.", |
| 281 | + "Minimal Interval (in seconds) to report vm network statistics (for Shared and Routed networks). If vm.network.stats.interval is smaller than this, use this to report vm network statistics.", |
274 | 282 | false); |
275 | 283 | private static final ConfigKey<Integer> StatsTimeout = new ConfigKey<>("Advanced", Integer.class, "stats.timeout", "60000", |
276 | 284 | "The timeout for stats call in milli seconds.", true, |
@@ -1601,9 +1609,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) { |
1601 | 1609 | SearchCriteria<NicVO> sc_nic = _nicDao.createSearchCriteria(); |
1602 | 1610 | sc_nic.addAnd("macAddress", SearchCriteria.Op.EQ, vmNetworkStatEntry.getMacAddress()); |
1603 | 1611 | NicVO nic = _nicDao.search(sc_nic, null).get(0); |
1604 | | - List<VlanVO> vlan = _vlanDao.listVlansByNetworkId(nic.getNetworkId()); |
1605 | | - if (vlan == null || vlan.size() == 0 || vlan.get(0).getVlanType() != VlanType.DirectAttached) |
1606 | | - continue; // only get network statistics for DirectAttached network (shared networks in Basic zone and Advanced zone with/without SG) |
| 1612 | + if (!isNetworkEligibleForNetworkStats(nic.getNetworkId())) { |
| 1613 | + continue; // only get network statistics for Shared or Routed network |
| 1614 | + } |
1607 | 1615 | UserStatisticsVO previousvmNetworkStats = _userStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), nic.getNetworkId(), |
1608 | 1616 | nic.getIPv4Address(), vmId, "UserVm"); |
1609 | 1617 | if (previousvmNetworkStats == null) { |
@@ -2159,6 +2167,27 @@ protected boolean isCurrentVmDiskStatsDifferentFromPrevious(VmDiskStatisticsVO p |
2159 | 2167 | return true; |
2160 | 2168 | } |
2161 | 2169 |
|
| 2170 | + /** |
| 2171 | + * Returns {@code true} if the given network is eligible for VM network statistics collection. |
| 2172 | + * Only Shared (DirectAttached) networks and Routed networks qualify. |
| 2173 | + * |
| 2174 | + * @param networkId the network id to evaluate |
| 2175 | + * @return {@code true} when the network is routed or direct-attached, {@code false} otherwise |
| 2176 | + */ |
| 2177 | + protected boolean isNetworkEligibleForNetworkStats(Long networkId) { |
| 2178 | + if (networkId == null) { |
| 2179 | + return false; |
| 2180 | + } |
| 2181 | + List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId); |
| 2182 | + boolean isDirectAttachedNetwork = CollectionUtils.isNotEmpty(vlans) |
| 2183 | + && vlans.get(0).getVlanType() == VlanType.DirectAttached; |
| 2184 | + if (isDirectAttachedNetwork) { |
| 2185 | + return true; |
| 2186 | + } |
| 2187 | + NetworkVO networkVO = networkDao.findById(networkId); |
| 2188 | + return networkVO != null && routedIpv4Manager.isRoutedNetwork(networkVO); |
| 2189 | + } |
| 2190 | + |
2162 | 2191 | /** |
2163 | 2192 | * Returns true if all the VmDiskStatsEntry are Zeros (Bytes read, Bytes write, IO read, and IO write must be all equals to zero) |
2164 | 2193 | */ |
|
0 commit comments