Skip to content

Commit a1ff0b5

Browse files
committed
Checking Node health after getMongoData Execution
1 parent 9e3dff6 commit a1ff0b5

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

main.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func isDirectoryExist(OutputPrefix string) bool {
5252
return !os.IsNotExist(err)
5353
}
5454

55+
func isMongoNodeAlive(hostname string, port int) bool {
56+
// Attempt to connect to the MongoDB host on the specified port
57+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(hostname, strconv.Itoa(port)), 5*time.Second)
58+
if err != nil {
59+
return false
60+
}
61+
conn.Close()
62+
return true
63+
}
64+
5565
func main() {
5666
var err error
5767

@@ -136,7 +146,7 @@ func main() {
136146
for _, host := range clustertopology.Allnodes.Nodes {
137147

138148
dcrlog.Info(fmt.Sprintf("host: %s, port: %d", host.Hostname, host.Port))
139-
149+
fmt.Printf("\nCollecting logs for MongoDB node %s:%d\n", host.Hostname, host.Port)
140150
// determine if the data collection should abort due to not enough free space
141151
// we keep approx 1GB as limit
142152
fsHasFreeSpace, err := hasFreeSpace()
@@ -163,6 +173,9 @@ func main() {
163173
log.Fatal("Error creating output Directory for storing DCR outputs")
164174
}
165175

176+
isAliveBefore := isMongoNodeAlive(host.Hostname, host.Port)
177+
dcrlog.Info(fmt.Sprintf("host: %s, port: %d is alive: %t", host.Hostname, host.Port, isAliveBefore))
178+
166179
c := mongosh.CaptureGetMongoData{}
167180
c.S = &cred
168181
c.Outputdir = &outputdir
@@ -171,7 +184,25 @@ func main() {
171184
err = c.RunMongoShellWithEval()
172185
if err != nil {
173186
dcrlog.Error(fmt.Sprintf("Error Running getMongoData %v", err))
174-
// log.Fatal("Error Running getMongoData ", err)
187+
}
188+
189+
// fmt.Println("\nPaused the code for testing purpose, press enter to continue...") //use this if you want to crash a node intentionally for testing purposes
190+
// bufio.NewReader(os.Stdin).ReadBytes('\n')
191+
192+
if !isMongoNodeAlive(host.Hostname, host.Port) && isAliveBefore {
193+
dcrlog.Error(fmt.Sprintf("MongoDB node %s:%d became unreachable after collecting getMongoData.", host.Hostname, host.Port))
194+
195+
fmt.Printf("\n")
196+
fmt.Println("######################################################################")
197+
fmt.Println("# ERROR #")
198+
fmt.Println("######################################################################")
199+
fmt.Printf("\nMongoDB node %s:%d is unreachable post getMongoData collection.\nTerminating the execution!\n\n", host.Hostname, host.Port)
200+
201+
dcrlog.Error("Terminating DCR-CLI execution")
202+
os.Exit(1)
203+
204+
} else {
205+
dcrlog.Info(fmt.Sprintf("MongoDB node %s:%d is reachable after collecting getMongoData...", host.Hostname, host.Port))
175206
}
176207

177208
isLocalHost := false

0 commit comments

Comments
 (0)