@@ -107,6 +107,21 @@ public void setPrimaryStorage(string id, string path)
107107 config . Save ( ConfigurationSaveMode . Modified ) ;
108108 ConfigurationManager . RefreshSection ( "appSettings" ) ;
109109 }
110+
111+ public List < string > getAllPrimaryStorages ( )
112+ {
113+ List < string > poolPaths = new List < string > ( ) ;
114+ Configuration config = ConfigurationManager . OpenExeConfiguration ( ConfigurationUserLevel . None ) ;
115+ KeyValueConfigurationCollection settings = config . AppSettings . Settings ;
116+ foreach ( string key in settings . AllKeys )
117+ {
118+ if ( key . Contains ( "primary_storage_" ) )
119+ {
120+ poolPaths . Add ( settings [ key ] . Value ) ;
121+ }
122+ }
123+ return poolPaths ;
124+ }
110125 }
111126
112127 /// <summary>
@@ -775,16 +790,67 @@ public JContainer CheckOnHostCommand([FromBody]dynamic cmd)
775790 using ( log4net . NDC . Push ( Guid . NewGuid ( ) . ToString ( ) ) )
776791 {
777792 logger . Info ( CloudStackTypes . CheckOnHostCommand + Utils . CleanString ( cmd . ToString ( ) ) ) ;
793+ string details = "host is not alive" ;
794+ bool result = true ;
795+ try
796+ {
797+ foreach ( string poolPath in config . getAllPrimaryStorages ( ) )
798+ {
799+ if ( IsHostAlive ( poolPath , ( string ) cmd . host . privateNetwork . ip ) )
800+ {
801+ result = false ;
802+ details = "host is alive" ;
803+ break ;
804+ }
805+ }
806+ }
807+ catch ( Exception e )
808+ {
809+ logger . Error ( "Error Occurred in " + CloudStackTypes . CheckOnHostCommand + " : " + e . Message ) ;
810+ }
811+
778812 object ansContent = new
779813 {
780- result = true ,
781- details = "resource is alive" ,
814+ result = result ,
815+ details = details ,
782816 contextMap = contextMap
783817 } ;
784818 return ReturnCloudStackTypedJArray ( ansContent , CloudStackTypes . CheckOnHostAnswer ) ;
785819 }
786820 }
787821
822+ private bool IsHostAlive ( string poolPath , string privateIp )
823+ {
824+ bool hostAlive = false ;
825+ try
826+ {
827+ string hbFile = Path . Combine ( poolPath , "hb-" + privateIp ) ;
828+ FileInfo file = new FileInfo ( hbFile ) ;
829+ using ( StreamReader sr = file . OpenText ( ) )
830+ {
831+ string epoch = sr . ReadLine ( ) ;
832+ string [ ] dateTime = epoch . Split ( '@' ) ;
833+ string [ ] date = dateTime [ 0 ] . Split ( '-' ) ;
834+ string [ ] time = dateTime [ 1 ] . Split ( ':' ) ;
835+ DateTime epochTime = new DateTime ( Convert . ToInt32 ( date [ 0 ] ) , Convert . ToInt32 ( date [ 1 ] ) , Convert . ToInt32 ( date [ 2 ] ) , Convert . ToInt32 ( time [ 0 ] ) ,
836+ Convert . ToInt32 ( time [ 1 ] ) , Convert . ToInt32 ( time [ 2 ] ) , DateTimeKind . Utc ) ;
837+ DateTime currentTime = DateTime . UtcNow ;
838+ DateTime ThreeMinuteLaterEpoch = epochTime . AddMinutes ( 3 ) ;
839+ if ( currentTime . CompareTo ( ThreeMinuteLaterEpoch ) < 0 )
840+ {
841+ hostAlive = true ;
842+ }
843+ sr . Close ( ) ;
844+ }
845+ }
846+ catch ( Exception e )
847+ {
848+ logger . Info ( "Exception occurred in verifying host " + e . Message ) ;
849+ }
850+
851+ return hostAlive ;
852+ }
853+
788854 // POST api/HypervResource/CheckSshCommand
789855 // TODO: create test
790856 [ HttpPost ]
@@ -956,6 +1022,27 @@ public JContainer ModifyStoragePoolCommand([FromBody]dynamic cmd)
9561022 contextMap = contextMap
9571023 } ;
9581024
1025+ if ( result )
1026+ {
1027+ try
1028+ {
1029+ if ( ( bool ) cmd . add )
1030+ {
1031+ logger . Info ( "Adding HeartBeat Task to task scheduler for pool " + ( string ) cmd . pool . uuid ) ;
1032+ Utils . AddHeartBeatTask ( ( string ) cmd . pool . uuid , hostPath , config . PrivateIpAddress ) ;
1033+ }
1034+ else
1035+ {
1036+ logger . Info ( "Deleting HeartBeat Task from task scheduler for pool " + ( string ) cmd . pool . uuid ) ;
1037+ Utils . RemoveHeartBeatTask ( cmd . pool . uuid ) ;
1038+ }
1039+ }
1040+ catch ( Exception e )
1041+ {
1042+ logger . Error ( "Error occurred in adding/delete HeartBeat Task to/from Task Scheduler : " + e . Message ) ;
1043+ }
1044+ }
1045+
9591046 return ReturnCloudStackTypedJArray ( ansContent , CloudStackTypes . ModifyStoragePoolAnswer ) ;
9601047 }
9611048 }
@@ -2151,6 +2238,7 @@ public JContainer StartupCommand([FromBody]dynamic cmdArray)
21512238
21522239 // Insert networking details
21532240 string privateIpAddress = strtRouteCmd . privateIpAddress ;
2241+ config . PrivateIpAddress = privateIpAddress ;
21542242 string subnet ;
21552243 System . Net . NetworkInformation . NetworkInterface privateNic = GetNicInfoFromIpAddress ( privateIpAddress , out subnet ) ;
21562244 strtRouteCmd . privateIpAddress = privateIpAddress ;
0 commit comments