@@ -684,15 +684,8 @@ async function checkPing(target, timeout = 10) {
684684 const { stdout } = await execAsync ( command , { timeout : ( timeout + 2 ) * 1000 } ) ;
685685 const responseTime = Date . now ( ) - startTime ;
686686
687- // Debug logging for ping output
688- console . log ( ` [PING DEBUG] Target: ${ target } ` ) ;
689- console . log ( ` [PING DEBUG] Command: ${ command } ` ) ;
690- console . log ( ` [PING DEBUG] Raw stdout:\n${ stdout } ` ) ;
691- console . log ( ` [PING DEBUG] Elapsed time: ${ responseTime } ms` ) ;
692-
693687 const lossMatch = stdout . match ( / ( \d + ) % \s * (?: p a c k e t \s * ) ? l o s s / i) ;
694688 const packetLoss = lossMatch ? parseInt ( lossMatch [ 1 ] , 10 ) : null ;
695- console . log ( ` [PING DEBUG] Packet loss: ${ packetLoss } %` ) ;
696689
697690 if ( packetLoss === 100 ) {
698691 return {
@@ -703,30 +696,21 @@ async function checkPing(target, timeout = 10) {
703696 }
704697
705698 let pingTime = null ;
706- let matchedPattern = null ;
707699 const patterns = [
708- / t i m e [ = < ] ( \d + \. ? \d * ) \s * m s / i, // Most common: time=15ms, time=15.3 ms, time<1ms
709- / t i m e [ = < ] \s * ( \d + \. ? \d * ) \s * m s / i, // With space after =: time= 15 ms
710- / r t t \s + m i n \/ a v g \/ m a x \/ \S + \s * = \s * [ \d . ] + \/ ( [ \d . ] + ) / i, // Linux summary: rtt min/avg/max/mdev = 14.267/14.267/14.267/0.000 ms
711- / ( \d + \. ? \d * ) \s * m s \s * $ / im // Fallback: just "15.3 ms" at end of line
700+ / t i m e [ = < ] ( \d + \. ? \d * ) \s * m s / i,
701+ / t i m e [ = < ] \s * ( \d + \. ? \d * ) \s * m s / i,
702+ / r t t \s + m i n \/ a v g \/ m a x \/ \S + \s * = \s * [ \d . ] + \/ ( [ \d . ] + ) / i,
703+ / ( \d + \. ? \d * ) \s * m s \s * $ / im
712704 ] ;
713- for ( let i = 0 ; i < patterns . length ; i ++ ) {
714- const match = stdout . match ( patterns [ i ] ) ;
705+ for ( const pattern of patterns ) {
706+ const match = stdout . match ( pattern ) ;
715707 if ( match ) {
716708 pingTime = parseFloat ( match [ 1 ] ) ;
717- matchedPattern = i ;
718- console . log ( ` [PING DEBUG] Matched pattern ${ i } : ${ patterns [ i ] } ` ) ;
719- console . log ( ` [PING DEBUG] Extracted ping time: ${ pingTime } ms` ) ;
720709 break ;
721710 }
722711 }
723712
724- if ( pingTime === null ) {
725- console . log ( ` [PING DEBUG] No pattern matched! Using fallback.` ) ;
726- }
727-
728713 const finalResponseTime = pingTime ?? Math . min ( responseTime , timeout * 1000 ) ;
729- console . log ( ` [PING DEBUG] Final response time: ${ finalResponseTime } ms` ) ;
730714
731715 return {
732716 status : 'operational' ,
@@ -735,15 +719,40 @@ async function checkPing(target, timeout = 10) {
735719 } ;
736720 } catch ( error ) {
737721 const responseTime = Date . now ( ) - startTime ;
722+ const stdout = error . stdout || '' ;
723+
724+ const lossMatch = stdout . match ( / ( \d + ) % \s * (?: p a c k e t \s * ) ? l o s s / i) ;
725+ const packetLoss = lossMatch ? parseInt ( lossMatch [ 1 ] , 10 ) : null ;
738726
739- // Debug logging for ping errors
740- console . log ( ` [PING DEBUG] Target: ${ target } ` ) ;
741- console . log ( ` [PING DEBUG] Command: ${ command } ` ) ;
742- console . log ( ` [PING DEBUG] ERROR: ${ error . message } ` ) ;
743- console . log ( ` [PING DEBUG] Error code: ${ error . code } ` ) ;
744- console . log ( ` [PING DEBUG] Stderr: ${ error . stderr } ` ) ;
745- console . log ( ` [PING DEBUG] Stdout: ${ error . stdout } ` ) ;
746- console . log ( ` [PING DEBUG] Elapsed time: ${ responseTime } ms` ) ;
727+ if ( packetLoss === 100 ) {
728+ return {
729+ status : 'down' ,
730+ responseTime,
731+ message : 'Host unreachable (100% packet loss)'
732+ } ;
733+ }
734+
735+ if ( packetLoss !== null && packetLoss < 100 ) {
736+ let pingTime = null ;
737+ const patterns = [
738+ / t i m e [ = < ] ( \d + \. ? \d * ) \s * m s / i,
739+ / r t t \s + m i n \/ a v g \/ m a x \/ \S + \s * = \s * [ \d . ] + \/ ( [ \d . ] + ) / i,
740+ / ( \d + \. ? \d * ) \s * m s \s * $ / im
741+ ] ;
742+ for ( const pattern of patterns ) {
743+ const match = stdout . match ( pattern ) ;
744+ if ( match ) {
745+ pingTime = parseFloat ( match [ 1 ] ) ;
746+ break ;
747+ }
748+ }
749+
750+ return {
751+ status : 'operational' ,
752+ responseTime : pingTime ?? responseTime ,
753+ message : `Ping OK (${ pingTime ?? responseTime } ms)`
754+ } ;
755+ }
747756
748757 return {
749758 status : 'down' ,
0 commit comments