github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/health/statuscheck.go (about) 1 package health 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "time" 8 9 "github.com/gin-gonic/gin" 10 "github.com/mdaxf/iac/com" 11 "github.com/mdaxf/iac/config" 12 dbconn "github.com/mdaxf/iac/databases" 13 "github.com/mdaxf/iac/health/checks" 14 "github.com/mdaxf/iac/logger" 15 ) 16 17 // CheckSystemHealth is a function that checks the health of the system. 18 // It takes a gin.Context as input and returns a map[string]interface{} and an error. 19 // The function registers various health checks for different components of the system, 20 // such as HTTP, MongoDB, MySQL, MQTT, and SignalR. 21 // It measures the health of the system and returns the result as a JSON-encoded map. 22 23 func CheckSystemHealth(c *gin.Context) (map[string]interface{}, error) { 24 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "System Status Check"} 25 26 startTime := time.Now() 27 defer func() { 28 elapsed := time.Since(startTime) 29 iLog.PerformanceWithDuration("framework.health.CheckSystemHealth", elapsed) 30 }() 31 32 defer func() { 33 if r := recover(); r != nil { 34 c.JSON(500, gin.H{"error": r}) 35 } 36 }() 37 38 // ctx, _ := context.WithTimeout(context.Background(), time.Second*5) 39 ctx := c 40 h, _ := New(WithComponent(Component{ 41 Name: com.IACNode["Name"].(string), 42 Instance: com.IACNode["AppID"].(string), 43 InstanceName: com.IACNode["Description"].(string), 44 InstanceType: com.IACNode["Type"].(string), 45 Version: com.IACNode["Version"].(string), 46 })) 47 /* 48 , WithChecks(Config{ 49 Name: "http", 50 Timeout: time.Second * 5, 51 SkipOnErr: true, 52 Check: func(context context.Context) error { 53 checks.CheckHttpStatus(ctx, "/portal/uipage.html", time.Second*5) 54 return nil 55 }, 56 }) 57 */ 58 h.systemInfoEnabled = true 59 60 documentsConfig := config.GlobalConfiguration.DocumentConfig 61 62 if documentsConfig != nil { 63 64 var DatabaseType = documentsConfig["type"].(string) // "mongodb" 65 var DatabaseConnection = documentsConfig["connection"].(string) //"mongodb://localhost:27017" 66 var DatabaseName = documentsConfig["database"].(string) //"IAC_CFG" 67 68 if DatabaseType == "mongodb" && DatabaseConnection != "" && DatabaseName != "" { 69 h.Register(Config{ 70 Name: "mongodb", 71 Check: func(ctx context.Context) error { 72 return checks.CheckMongoDBStatus(ctx, DatabaseConnection, time.Second*5, time.Second*5, time.Second*5) 73 }, 74 }) 75 } 76 } 77 78 db := dbconn.DB 79 if db != nil { 80 h.Register(Config{ 81 Name: "mysql", 82 Check: func(ctx context.Context) error { 83 return checks.CheckMySQLStatus(ctx, db, "") 84 }, 85 }) 86 } 87 /* fmt.Println("MQTT Clients:", config.MQTTClients) 88 for key, value := range config.MQTTClients { 89 fmt.Println(key) 90 client := value 91 h.Register(Config{ 92 Name: "mqtt." + key, 93 Check: func(ctx context.Context) error { 94 return checks.CheckMqttClientStatus(ctx, client.Client) 95 }, 96 }) 97 } 98 */ 99 if com.SingalRConfig != nil { 100 SAddress := com.SingalRConfig["server"].(string) 101 WcAddress := com.SingalRConfig["serverwc"].(string) 102 fmt.Println("SignalR Address:", SAddress) 103 fmt.Println("SignalR Websocket Address:", WcAddress) 104 105 if WcAddress != "" { 106 107 h.Register(Config{ 108 Name: "signalr Websocket Server", 109 Check: func(ctx context.Context) error { 110 return checks.CheckSignalRSrvStatus(ctx, SAddress, WcAddress) 111 }, 112 }) 113 } 114 if SAddress != "" { 115 h.Register(Config{ 116 Name: "signalr Http Server", 117 Check: func(ctx context.Context) error { 118 return checks.CheckSignalRSrvHttpStatus(ctx, SAddress, WcAddress) 119 }, 120 }) 121 } 122 } 123 /* h.Register(Config{ 124 Name: "ping", 125 Check: func(ctx context.Context) error { 126 return checks.CheckPingStatus(ctx, "", time.Second*5) 127 }, 128 }) */ 129 130 m := h.Measure(ctx) 131 data, err := json.Marshal(m) 132 if err != nil { 133 return make(map[string]interface{}), err 134 } 135 136 jdata, err := com.ConvertbytesToMap(data) 137 138 if err != nil { 139 return make(map[string]interface{}), err 140 } 141 142 return jdata, nil 143 }