Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG - Ping

## 1.0.3 / 2026-07-23

***Fixed***:

* Fix the Ping check on non-English Windows. It no longer crashes on localized output (``'utf-8' codec can't decode byte 0x81``) and no longer reports reachable hosts as down. The check now uses the UTF-8 code page and matches ping's untranslated ``ms`` unit instead of the localized "time" label ([#3075](https://github.com/DataDog/integrations-extras/pull/3075)).

## 1.0.2 / 2021-09-30

***Fixed***:
Expand Down
9 changes: 5 additions & 4 deletions ping/datadog_checks/ping/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _exec_ping(self, timeout, target_host):
cmd = "ping"

if platform.system() == "Windows": # pragma: nocover
precmd = ["cmd", "/c", "chcp 437 &"] # Set code page to English for non-US Windows
precmd = ["cmd", "/c", "chcp 65001 &"] # UTF-8 code page so non-ASCII ping output decodes correctly
countOption = "-n"
timeoutOption = "-w"
# The timeout option is in ms on Windows
Expand Down Expand Up @@ -85,12 +85,13 @@ def check(self, instance):

try:
lines = self._exec_ping(timeout, host)
regex = re.compile(r"time[<=]((\d|\.)*)")
# Match on the untranslated "ms" unit, since ping localizes the "time" label (e.g. German "Zeit=")
regex = re.compile(r"[<=]\s*([\d.]+)\s*ms")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

request
I'd keep this only as a fallback, keeping the old regex as the primary one.
Unless we are absolutely certain that there is no way another time unit is returned by the command (e.g. s, µs, etc.)

result = regex.findall(lines)
if result:
length = result[0][0]
length = result[0]
else:
raise CheckException("No time= found ({})".format(lines))
raise CheckException("No response time found ({})".format(lines))

except CheckException as e:
self.log.info("%s is DOWN (%s)", host, e)
Expand Down
19 changes: 19 additions & 0 deletions ping/tests/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def mock_exec_ping():
round-trip min/avg/max/stddev = 0.093/0.093/0.093/0.000 ms"""


def mock_exec_ping_german():
return (
"Antwort von 127.0.0.1: Bytes=32 Zeit=3ms TTL=117\n"
"Ping-Statistik für 127.0.0.1:\n"
" Minimum = 3ms, Maximum = 3ms, Mittelwert = 3ms"
)


def test_empty_check(empty_instance):
check = PingCheck("ping", {}, {})

Expand Down Expand Up @@ -49,6 +57,17 @@ def test_valid_check_ipv6(aggregator, instance_ipv6):
aggregator.assert_all_metrics_covered()


def test_localized_output(aggregator, instance_response_time):
check = PingCheck("ping", {}, {})

with mock.patch.object(check, "_exec_ping", return_value=mock_exec_ping_german()):
check.check(instance_response_time)
aggregator.assert_service_check("network.ping.can_connect", AgentCheck.OK)
aggregator.assert_metric("network.ping.can_connect", value=1)
aggregator.assert_metric("network.ping.response_time", value=3)
aggregator.assert_all_metrics_covered()


@pytest.mark.usefixtures("dd_environment")
def test_integration(aggregator, instance):
check = PingCheck("ping", {}, {})
Expand Down
Loading