Skip to content

Conversation

@Slyker
Copy link
Contributor

@Slyker Slyker commented Oct 5, 2025

Refactor ping_function to handle both server and port for pinging. Added compatibility for checking TCP connections using netcat.

This pull request refactors the ping_function in plugins/ping.sh to improve its flexibility and output consistency. The main change is the addition of support for pinging servers specified with a port (host:port), measuring response time using nc instead of ping, and standardizing the output format to seconds.

Functionality and compatibility improvements:

  • Added support for pinging servers specified as host:port, using nc to measure connection time and outputting the duration in seconds.
  • Standardized the output for both regular host pings and host:port pings to display the duration in seconds, improving consistency.
  • Improved error handling by displaying -.--s when a ping or connection fails, making the output clearer.
  • Removed OS-specific case handling, simplifying the function and focusing on Linux/Darwin compatibility.

Refactor ping_function to handle both server and port for pinging. Added compatibility for checking TCP connections using netcat.
@2KAbhishek
Copy link
Owner

@Slyker thanks for the PR and apologies for the delay in response,

overall I think this seems like a good addition, but I don't think we would want to capture ping responses in seconds, lets keep it at ms as earlier

Also, can you please update the ping plugin docs to mention that servers with ports are supported as well

Copilot AI review requested due to automatic review settings October 29, 2025 18:18
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the ping plugin to add support for TCP port connectivity checks in addition to standard ICMP ping, and standardizes the output format to seconds across both methods.

  • Adds TCP port connectivity checking using nc when server is specified in host:port format
  • Standardizes output format to seconds (with .2fs format) for both ICMP ping and TCP checks
  • Removes OS-specific case statements in favor of a unified approach

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Slyker
Copy link
Contributor Author

Slyker commented Oct 29, 2025

@2KAbhishek I think it should be good as it is, I've updated the readme and changed some part to be more accurate

Comment on lines +37 to +55
avg_time=$(echo "$ping_output" | awk -F'=' '/min\/avg\/max/ {split($2, a, "/"); print a[2]}' | tr -d ' ')
# Detect float and unit
if [[ "$avg_time" == *.* ]]; then
# Assume ms if less than 50, s if >= 1
val="$avg_time"
unit="ms"
# Some pings can output >1, consider <10 as s, >10 as ms
if (( $(echo "$val >= 1" | bc) )) && (( $(echo "$val < 50" | bc) )); then
unit="ms"
elif (( $(echo "$val >= 50" | bc) )); then
unit="ms"
elif (( $(echo "$val < 1" | bc) )); then
unit="s"
fi
normalize_time "$val" "$unit"
else
normalize_time "$avg_time" ms
fi
;;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we please simply return ms instead, I am not planning to have ping time displayed in seconds right now

also lets get rid of the comments

Comment on lines +8 to +15
normalize_time() {
value="$1"
unit="$2"
# Convert to ms if needed
case "$unit" in
ms) printf "%.2f ms" "$value" ;;
s) printf "%.2f ms" "$(echo "$value * 1000" | bc -l)" ;;
*) printf "%s" "$value" ;;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't be necessary

Comment on lines +56 to +57
CYGWIN* | MINGW32* | MSYS* | MINGW*)
# Windows compatibility stub
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the placeholder for windows as well as well

Copy link
Owner

@2KAbhishek 2KAbhishek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some cleanup suggestions @Slyker

Can you confirm if you tested the changes in mac / linux or both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants