@@ -52,6 +52,24 @@ func isDirectoryExist(OutputPrefix string) bool {
52
52
return ! os .IsNotExist (err )
53
53
}
54
54
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
+
55
73
func main () {
56
74
var err error
57
75
@@ -135,8 +153,8 @@ func main() {
135
153
136
154
for _ , host := range clustertopology .Allnodes .Nodes {
137
155
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 ( " \n Collecting logs for MongoDB node %s:%d \n " , host . Hostname , host . Port )
140
158
// determine if the data collection should abort due to not enough free space
141
159
// we keep approx 1GB as limit
142
160
fsHasFreeSpace , err := hasFreeSpace ()
@@ -163,6 +181,11 @@ func main() {
163
181
log .Fatal ("Error creating output Directory for storing DCR outputs" )
164
182
}
165
183
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
+
166
189
c := mongosh.CaptureGetMongoData {}
167
190
c .S = & cred
168
191
c .Outputdir = & outputdir
@@ -171,7 +194,24 @@ func main() {
171
194
err = c .RunMongoShellWithEval ()
172
195
if err != nil {
173
196
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 ("\n MongoDB node %s:%d is unreachable post getMongoData collection.\n Terminating 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 ))
175
215
}
176
216
177
217
isLocalHost := false
0 commit comments