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