Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve ping totals parsing in case of errors #246

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions src/command/handlers/ping/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function parse (rawOutput: string): PingParseOutput {
const resolvedAddress = String(header?.groups?.['addr']);
const timeLines = lines.slice(1).map(l => parseStatsLine(l)).filter(Boolean) as PingTimings[];

const resolvedHostname = (/(?<=from\s).*?(?=\s\(|:\s)/.exec((lines[1] ?? '')))?.[0];
const resolvedHostname = (/(?<=from\s).*?(?=\s\(|:\s)/i.exec((lines[1] ?? '')))?.[0];
const summaryHeaderIndex = lines.findIndex(l => /^---\s(.*)\sstatistics ---/.test(l));
const summary = parseSummary(lines.slice(summaryHeaderIndex + 1));

Expand Down Expand Up @@ -78,10 +78,13 @@ function parseSummary (lines: string[]): PingStats {
}

if (packets) {
const packetsMatch = /(?<total>\d+)\spackets\stransmitted,\s(?<rcv>\d+)\s(received|packets received),\s(?<loss>\d*(?:\.\d+)?)%\spacket\sloss/.exec(packets);
stats.total = Number.parseInt(packetsMatch?.groups?.['total'] ?? '-1', 10);
stats.loss = Number.parseFloat(packetsMatch?.groups?.['loss'] ?? '-1');
stats.rcv = Number.parseInt(packetsMatch?.groups?.['rcv'] ?? '-1', 10);
const totalMatch = /\b(?<total>\d+)\spackets\stransmitted/.exec(packets);
const rcvMatch = /\b(?<rcv>\d+)\s(received|packets received)/.exec(packets);
const lossMatch = /\b(?<loss>\d*(?:\.\d+)?)%\spacket\sloss/.exec(packets);

stats.total = Number.parseInt(totalMatch?.groups?.['total'] ?? '-1', 10);
stats.rcv = Number.parseInt(rcvMatch?.groups?.['rcv'] ?? '-1', 10);
stats.loss = Number.parseFloat(lossMatch?.groups?.['loss'] ?? '-1');
stats.drop = stats.total - stats.rcv;
}

Expand Down
20 changes: 20 additions & 0 deletions test/mocks/ping-unreachable-linux.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"testId": "test",
"measurementId": "measurement",
"result": {
"status": "finished",
"resolvedAddress": "104.18.186.31",
"resolvedHostname": "eth2-1109-fsn-lf-e03.productsup.int",
"stats": {
"min": null,
"avg": null,
"max": null,
"total": 1,
"loss": 100,
"rcv": 0,
"drop": 1
},
"timings": [],
"rawOutput": "PING (104.18.186.31) 56(84) bytes of data.\nFrom eth2-1109-fsn-lf-e03.productsup.int (10.254.254.17) icmp_seq=1 Destination Port Unreachable\n\n--- ping statistics ---\n1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms\n"
}
}
5 changes: 5 additions & 0 deletions test/mocks/ping-unreachable-linux.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PING (104.18.186.31) 56(84) bytes of data.
From eth2-1109-fsn-lf-e03.productsup.int (10.254.254.17) icmp_seq=1 Destination Port Unreachable

--- ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
2 changes: 1 addition & 1 deletion test/unit/command/ping-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('ping command executor', () => {
sandbox.reset();
});

const successfulCommands = [ 'ping-success-linux', 'ping-success-linux-no-domain', 'ping-no-source-ip-linux' ];
const successfulCommands = [ 'ping-success-linux', 'ping-success-linux-no-domain', 'ping-no-source-ip-linux', 'ping-unreachable-linux' ];

for (const command of successfulCommands) {
it(`should run and parse successful commands - ${command}`, async () => {
Expand Down
Loading