@@ -729,100 +729,7 @@ protected void runInContext() {
729729 s_logger .debug ("Found " + routers .size () + " running routers. " );
730730
731731 for (final DomainRouterVO router : routers ) {
732- final String privateIP = router .getPrivateIpAddress ();
733-
734- if (privateIP != null ) {
735- final boolean forVpc = router .getVpcId () != null ;
736- final List <? extends Nic > routerNics = _nicDao .listByVmId (router .getId ());
737- for (final Nic routerNic : routerNics ) {
738- final Network network = _networkModel .getNetwork (routerNic .getNetworkId ());
739- // Send network usage command for public nic in VPC VR
740- // Send network usage command for isolated guest nic of non) VPC VR
741-
742- //[TODO] Avoiding the NPE now, but I have to find out what is going on with the network. - Wilder Rodrigues
743- if (network == null ) {
744- s_logger .error ("Could not find a network with ID => " + routerNic .getNetworkId () + ". It might be a problem!" );
745- continue ;
746- }
747- if (forVpc && network .getTrafficType () == TrafficType .Public || !forVpc && network .getTrafficType () == TrafficType .Guest
748- && network .getGuestType () == Network .GuestType .Isolated ) {
749- final NetworkUsageCommand usageCmd = new NetworkUsageCommand (privateIP , router .getHostName (), forVpc , routerNic .getIPv4Address ());
750- final String routerType = router .getType ().toString ();
751- final UserStatisticsVO previousStats = _userStatsDao .findBy (router .getAccountId (), router .getDataCenterId (), network .getId (),
752- forVpc ? routerNic .getIPv4Address () : null , router .getId (), routerType );
753- NetworkUsageAnswer answer = null ;
754- try {
755- answer = (NetworkUsageAnswer ) _agentMgr .easySend (router .getHostId (), usageCmd );
756- } catch (final Exception e ) {
757- s_logger .warn ("Error while collecting network stats from router: " + router .getInstanceName () + " from host: " + router .getHostId (), e );
758- continue ;
759- }
760-
761- if (answer != null ) {
762- if (!answer .getResult ()) {
763- s_logger .warn ("Error while collecting network stats from router: " + router .getInstanceName () + " from host: " + router .getHostId ()
764- + "; details: " + answer .getDetails ());
765- continue ;
766- }
767- try {
768- if (answer .getBytesReceived () == 0 && answer .getBytesSent () == 0 ) {
769- s_logger .debug ("Recieved and Sent bytes are both 0. Not updating user_statistics" );
770- continue ;
771- }
772- final NetworkUsageAnswer answerFinal = answer ;
773- Transaction .execute (new TransactionCallbackNoReturn () {
774- @ Override
775- public void doInTransactionWithoutResult (final TransactionStatus status ) {
776- final UserStatisticsVO stats = _userStatsDao .lock (router .getAccountId (), router .getDataCenterId (), network .getId (),
777- forVpc ? routerNic .getIPv4Address () : null , router .getId (), routerType );
778- if (stats == null ) {
779- s_logger .warn ("unable to find stats for account: " + router .getAccountId ());
780- return ;
781- }
782-
783- if (previousStats != null
784- && (previousStats .getCurrentBytesReceived () != stats .getCurrentBytesReceived () || previousStats .getCurrentBytesSent () != stats
785- .getCurrentBytesSent ())) {
786- s_logger .debug ("Router stats changed from the time NetworkUsageCommand was sent. " + "Ignoring current answer. Router: "
787- + answerFinal .getRouterName () + " Rcvd: " + answerFinal .getBytesReceived () + "Sent: " + answerFinal .getBytesSent ());
788- return ;
789- }
790-
791- if (stats .getCurrentBytesReceived () > answerFinal .getBytesReceived ()) {
792- if (s_logger .isDebugEnabled ()) {
793- s_logger .debug ("Received # of bytes that's less than the last one. "
794- + "Assuming something went wrong and persisting it. Router: " + answerFinal .getRouterName () + " Reported: "
795- + answerFinal .getBytesReceived () + " Stored: " + stats .getCurrentBytesReceived ());
796- }
797- stats .setNetBytesReceived (stats .getNetBytesReceived () + stats .getCurrentBytesReceived ());
798- }
799- stats .setCurrentBytesReceived (answerFinal .getBytesReceived ());
800- if (stats .getCurrentBytesSent () > answerFinal .getBytesSent ()) {
801- if (s_logger .isDebugEnabled ()) {
802- s_logger .debug ("Received # of bytes that's less than the last one. "
803- + "Assuming something went wrong and persisting it. Router: " + answerFinal .getRouterName () + " Reported: "
804- + answerFinal .getBytesSent () + " Stored: " + stats .getCurrentBytesSent ());
805- }
806- stats .setNetBytesSent (stats .getNetBytesSent () + stats .getCurrentBytesSent ());
807- }
808- stats .setCurrentBytesSent (answerFinal .getBytesSent ());
809- if (!_dailyOrHourly ) {
810- // update agg bytes
811- stats .setAggBytesSent (stats .getNetBytesSent () + stats .getCurrentBytesSent ());
812- stats .setAggBytesReceived (stats .getNetBytesReceived () + stats .getCurrentBytesReceived ());
813- }
814- _userStatsDao .update (stats .getId (), stats );
815- }
816- });
817-
818- } catch (final Exception e ) {
819- s_logger .warn ("Unable to update user statistics for account: " + router .getAccountId () + " Rx: " + answer .getBytesReceived () + "; Tx: "
820- + answer .getBytesSent ());
821- }
822- }
823- }
824- }
825- }
732+ collectNetworkStatistics (router , null );
826733 }
827734 } catch (final Exception e ) {
828735 s_logger .warn ("Error while collecting network stats" , e );
@@ -3053,6 +2960,11 @@ public void prepareStop(final VirtualMachineProfile profile) {
30532960 // Collect network usage before stopping Vm
30542961
30552962 final DomainRouterVO router = _routerDao .findById (profile .getVirtualMachine ().getId ());
2963+ collectNetworkStatistics (router , null );
2964+ }
2965+
2966+ @ Override
2967+ public <T extends VirtualRouter > void collectNetworkStatistics (final T router , final Nic nic ) {
30562968 if (router == null ) {
30572969 return ;
30582970 }
@@ -3061,12 +2973,23 @@ public void prepareStop(final VirtualMachineProfile profile) {
30612973
30622974 if (privateIP != null ) {
30632975 final boolean forVpc = router .getVpcId () != null ;
3064- final List <? extends Nic > routerNics = _nicDao .listByVmId (router .getId ());
2976+ List <Nic > routerNics = new ArrayList <Nic >();
2977+ if (nic != null ) {
2978+ routerNics .add (nic );
2979+ } else {
2980+ routerNics .addAll (_nicDao .listByVmId (router .getId ()));
2981+ }
30652982 for (final Nic routerNic : routerNics ) {
30662983 final Network network = _networkModel .getNetwork (routerNic .getNetworkId ());
30672984 // Send network usage command for public nic in VPC VR
30682985 // Send network usage command for isolated guest nic of non VPC
30692986 // VR
2987+
2988+ //[TODO] Avoiding the NPE now, but I have to find out what is going on with the network. - Wilder Rodrigues
2989+ if (network == null ) {
2990+ s_logger .error ("Could not find a network with ID => " + routerNic .getNetworkId () + ". It might be a problem!" );
2991+ continue ;
2992+ }
30702993 if (forVpc && network .getTrafficType () == TrafficType .Public || !forVpc && network .getTrafficType () == TrafficType .Guest
30712994 && network .getGuestType () == Network .GuestType .Isolated ) {
30722995 final NetworkUsageCommand usageCmd = new NetworkUsageCommand (privateIP , router .getHostName (), forVpc , routerNic .getIPv4Address ());
0 commit comments