Skip to content

Commit db7c0c6

Browse files
authored
Merge pull request #31 from 10gen/getMongoDataCheck
Get mongo data check
2 parents 9e3dff6 + 770179d commit db7c0c6

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

main.go

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

55+
// isMongoNodeAlive checks if a MongoDB node is reachable at the specified hostname and port.
56+
// It attempts to establish a TCP connection to the given address within a 5-second timeout.
57+
// Parameters:
58+
// - hostname: The hostname or IP address of the MongoDB node.
59+
// - port: The port number on which the MongoDB node is listening.
60+
// Returns:
61+
// - bool: True if the node is reachable, false otherwise.
62+
// - error: An error object if the connection attempt fails.
63+
func isMongoNodeAlive(hostname string, port int) (bool, error) {
64+
// Attempt to connect to the MongoDB host on the specified port
65+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(hostname, strconv.Itoa(port)), 5*time.Second)
66+
if err != nil {
67+
return false, err
68+
}
69+
conn.Close()
70+
return true, nil
71+
}
72+
5573
func main() {
5674
var err error
5775

@@ -135,8 +153,8 @@ func main() {
135153

136154
for _, host := range clustertopology.Allnodes.Nodes {
137155

138-
dcrlog.Info(fmt.Sprintf("host: %s, port: %d", host.Hostname, host.Port))
139-
156+
dcrlog.Info(fmt.Sprintf("Collecting logs for MongoDB node - host: %s, port: %d", host.Hostname, host.Port))
157+
fmt.Printf("\nCollecting logs for MongoDB node %s:%d\n", host.Hostname, host.Port)
140158
// determine if the data collection should abort due to not enough free space
141159
// we keep approx 1GB as limit
142160
fsHasFreeSpace, err := hasFreeSpace()
@@ -163,6 +181,11 @@ func main() {
163181
log.Fatal("Error creating output Directory for storing DCR outputs")
164182
}
165183

184+
isAliveBefore, err := isMongoNodeAlive(host.Hostname, host.Port)
185+
if err != nil {
186+
dcrlog.Error(fmt.Sprintf("Error checking if host: %s, port: %d is alive: \n %v", host.Hostname, host.Port, err))
187+
}
188+
166189
c := mongosh.CaptureGetMongoData{}
167190
c.S = &cred
168191
c.Outputdir = &outputdir
@@ -171,7 +194,24 @@ func main() {
171194
err = c.RunMongoShellWithEval()
172195
if err != nil {
173196
dcrlog.Error(fmt.Sprintf("Error Running getMongoData %v", err))
174-
// log.Fatal("Error Running getMongoData ", err)
197+
}
198+
199+
isAliveAfter, err := isMongoNodeAlive(host.Hostname, host.Port)
200+
201+
if !isAliveAfter && isAliveBefore {
202+
dcrlog.Error(fmt.Sprintf("MongoDB node %s:%d became unreachable after collecting getMongoData.\n %v", host.Hostname, host.Port, err))
203+
204+
fmt.Printf("\n")
205+
fmt.Println("######################################################################")
206+
fmt.Println("# ERROR #")
207+
fmt.Println("######################################################################")
208+
fmt.Printf("\nMongoDB node %s:%d is unreachable post getMongoData collection.\nTerminating the execution!\n\n", host.Hostname, host.Port)
209+
210+
dcrlog.Error("Terminating DCR-CLI execution")
211+
os.Exit(1)
212+
213+
} else {
214+
dcrlog.Info(fmt.Sprintf("MongoDB node %s:%d is reachable after collecting getMongoData...", host.Hostname, host.Port))
175215
}
176216

177217
isLocalHost := false

0 commit comments

Comments
 (0)