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