@@ -3,7 +3,9 @@ package controller
33import (
44 "context"
55 "fmt"
6+ "net/http"
67 "os/exec"
8+ "strings"
79 "time"
810
911 v1 "cloudstackctl/apis/v1"
@@ -189,6 +191,52 @@ func (c *Controller) CheckVMHealth(vm *v1.VirtualMachine) (bool, error) {
189191 } else {
190192 conn .Close ()
191193 }
194+ case "http" , "https" :
195+ // HTTP/HTTPS GET check. Default port 80 for http and 443 for https.
196+ scheme := "http"
197+ if hc .Type == "https" {
198+ scheme = "https"
199+ }
200+ port := ""
201+ if hc .Port != 0 {
202+ port = fmt .Sprintf ("%d" , hc .Port )
203+ } else {
204+ if scheme == "http" {
205+ port = "80"
206+ } else {
207+ port = "443"
208+ }
209+ }
210+ path := "/"
211+ if hc .Path != "" {
212+ path = hc .Path
213+ if ! strings .HasPrefix (path , "/" ) {
214+ path = "/" + path
215+ }
216+ }
217+ // Use net.JoinHostPort to properly bracket IPv6 addresses
218+ hostPort := net .JoinHostPort (vmIP , port )
219+ urlStr := fmt .Sprintf ("%s://%s%s" , scheme , hostPort , path )
220+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
221+ defer cancel ()
222+ req , err := http .NewRequestWithContext (ctx , "GET" , urlStr , nil )
223+ if err != nil {
224+ log .Printf ("VM %s %s check to %s failed to build request: %v" , vm .Metadata .Name , hc .Type , urlStr , err )
225+ overallHealthy = false
226+ break
227+ }
228+ client := & http.Client {Timeout : timeout }
229+ resp , err := client .Do (req )
230+ if err != nil {
231+ log .Printf ("VM %s %s check to %s failed: %v" , vm .Metadata .Name , hc .Type , urlStr , err )
232+ overallHealthy = false
233+ } else {
234+ resp .Body .Close ()
235+ if resp .StatusCode < 200 || resp .StatusCode >= 400 {
236+ log .Printf ("VM %s %s check to %s returned status %d" , vm .Metadata .Name , hc .Type , urlStr , resp .StatusCode )
237+ overallHealthy = false
238+ }
239+ }
192240 default :
193241 // Unknown check type: mark as not healthy and log
194242 log .Printf ("Unknown health check type %s for VM %s" , hc .Type , vm .Metadata .Name )
0 commit comments