louislam / uptime-kuma

A fancy self-hosted monitoring tool
https://uptime.kuma.pet
MIT License
56.1k stars 5.05k forks source link

[ping] Packet loss monitoring #4234

Open smitty-nieto opened 9 months ago

smitty-nieto commented 9 months ago

⚠️ Please verify that this feature request has NOT been suggested before.

🏷️ Feature Request Type

New Notification, Other

🔖 Feature description

Implement packet loss and latency/round trip times

✔️ Solution

I would think the easiest way to implement this would be in the existing ping monitor. Adding a packet loss threshold (30%, 50%, etc) Would prob need to ensure the ping monitor does multiple pings, so maybe a ping count ? (ie : send 5 ping packets)

Since we are already capturing the response time (47 ms) would be good to be able to alert on this.

❓ Alternatives

No response

📝 Additional Context

We have a client right now that we monitor with ping monitor. Since SOME of the pings make it through, it constantly shows up as green. Further investigation shows 33-80% packet loss

5 packets transmitted, 1 received, 80% packet loss, time 4095ms rtt min/avg/max/mdev = 5.757/5.757/5.757/0.000 ms

Not only packet loss, but being able to set a threshold on the latency would be great. Meaning, if it's normally 47ms for the ping, and it suddenly jumps to 150-200ms, I would want to alert on this.

smitty-nieto commented 9 months ago

The Proof of concept here shows it is already a part of the ping module. Would just need to account for the new packets sent count (default to 5) and packet loss. Don't think we would need to graph packet loss, just alert or mark down if packet loss is above a certain percentage.

ping-module.js

```js const ping = require('ping'); exports.pingAsync = function (hostname, ipv6 = false, size = 56, count = 5) { return new Promise((resolve, reject) => { let received = 0; let sent = 0; const pingOptions = { v6: ipv6, packetSize: size, }; const pingResults = []; for (let i = 0; i < count; i++) { ping.promise.probe(hostname, pingOptions).then((res) => { sent++; if (res.alive) { received++; pingResults.push(res); } else { pingResults.push(null); // Indicate a failed ping } if (sent === count) { const packetLoss = ((sent - received) / sent) * 100; const averagePingTime = pingResults.reduce((sum, result) => { if (result && result.time) { return sum + parseFloat(result.time); } return sum; }, 0) / received; resolve({ packetLoss, averagePingTime, sent, received, }); } }).catch((err) => { sent++; pingResults.push(err); if (sent === count) { const packetLoss = ((sent - received) / sent) * 100; const averagePingTime = pingResults.reduce((sum, result) => { if (result && result.time) { return sum + parseFloat(result.time); } return sum; }, 0) / received; resolve({ packetLoss, averagePingTime, sent, received, }); } }); } }); }; ```

ping-test.js

```js const { pingAsync } = require('./ping-module.js'); async function main() { try { const hostname = 'www.google.com'; // Replace with the host you want to ping const result = await pingAsync(hostname, false, 56, 5); console.log(`Packet loss: ${result.packetLoss.toFixed(2)}%`); console.log(`Ping count: Sent = ${result.sent}, Received = ${result.received}`); if (!isNaN(result.averagePingTime)) { console.log(`Average ping time (for successful pings): ${result.averagePingTime.toFixed(2)} ms`); } else { console.log('Average ping time (for successful pings): N/A'); } } catch (error) { console.error('Error:', error.message); } } main(); ```

node ping-test.js

Packet loss: 0.00%
Ping count: Sent = 5, Received = 5
Average ping time (for successful pings): 9.05 ms
smitty-nieto commented 9 months ago

Output of packet loss

Packet loss: 80.00%
Ping count: Sent = 5, Received = 1
Average ping time (for successful pings): 9.41 ms
L-69 commented 8 months ago

Does the host name for ping type monitoring support the cidr format?

CommanderStorm commented 8 months ago

@L-69 's question is answered in #4338

hadwinfu commented 6 months ago

The Proof of concept here shows it is already a part of the ping module. Would just need to account for the new packets sent count (default to 5) and packet loss. Don't think we would need to graph packet loss, just alert or mark down if packet loss is above a certain percentage.

ping-module.js ping-test.js node ping-test.js

Packet loss: 0.00%
Ping count: Sent = 5, Received = 5
Average ping time (for successful pings): 9.05 ms

but in some cases, we need to monitor if a network service is stable, not only if it's down or up. Please support graph packet loss. Many thanks!