Skip to content

Commit 3317f08

Browse files
committed
Ping Fixed + Dedicated > TCP
1 parent ce9e76b commit 3317f08

3 files changed

Lines changed: 59 additions & 50 deletions

File tree

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,20 @@ Example configuration showcasing all available monitor types:
282282

283283
### Monitor Options
284284

285-
| Option | Type | Description |
286-
| ---------------- | ------- | ---------------------------------- |
287-
| `name` | string | Display name |
288-
| `type` | string | Monitor type (see above) |
289-
| `target` | string | What to monitor |
290-
| `description` | string | Optional description |
291-
| `showTarget` | boolean | Show target URL publicly |
292-
| `showHistory` | boolean | Display 90-day history |
293-
| `notify` | boolean | Send notifications |
294-
| `inverse` | boolean | Reverse logic (down = operational) |
295-
| `applyToOverall` | boolean | Include in overall status |
296-
| `expectedStatus` | array | HTTP status codes (http only) |
297-
| `timeout` | number | Request timeout in ms (ping only) |
298-
| `jsonPath` | string | Path to value (json only) |
299-
| `expectedValue` | any | Expected value (json only) |
300-
| `recordType` | string | DNS record type (dns only) |
301-
| `expectedIp` | string | Expected IP (dns only) |
285+
| Option | Type | Description |
286+
| ---------------- | ------- | ------------------------------------------- |
287+
| `name` | string | Display name |
288+
| `type` | string | Monitor type (see above) |
289+
| `target` | string | What to monitor |
290+
| `description` | string | Optional description |
291+
| `showTarget` | boolean | Show target URL publicly |
292+
| `showHistory` | boolean | Display 90-day history |
293+
| `notify` | boolean | Send notifications |
294+
| `inverse` | boolean | Reverse logic (down = operational) |
295+
| `applyToOverall` | boolean | Include in overall status |
296+
| `expectedStatus` | array | HTTP status codes (http only) |
297+
| `timeout` | number | Request timeout in ms (in seconds for ping) |
298+
| `jsonPath` | string | Path to value (json only) |
299+
| `expectedValue` | any | Expected value (json only) |
300+
| `recordType` | string | DNS record type (dns only) |
301+
| `expectedIp` | string | Expected IP (dns only) |

monitors.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"monitors": [
99
{
1010
"name": "Dedicated Server",
11-
"type": "ping",
12-
"target": "207.166.129.77",
11+
"type": "tcp",
12+
"target": "207.166.129.77:443",
1313
"showTarget": false,
1414
"showHistory": true,
15-
"timeout": 15
15+
"timeout": 15000
1616
},
1717
{
1818
"name": "Main Website",

scripts/check-monitors.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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*(?:packet\s*)?loss/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-
/time[=<](\d+\.?\d*)\s*ms/i, // Most common: time=15ms, time=15.3 ms, time<1ms
709-
/time[=<]\s*(\d+\.?\d*)\s*ms/i, // With space after =: time= 15 ms
710-
/rtt\s+min\/avg\/max\/\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*ms\s*$/im // Fallback: just "15.3 ms" at end of line
700+
/time[=<](\d+\.?\d*)\s*ms/i,
701+
/time[=<]\s*(\d+\.?\d*)\s*ms/i,
702+
/rtt\s+min\/avg\/max\/\S+\s*=\s*[\d.]+\/([\d.]+)/i,
703+
/(\d+\.?\d*)\s*ms\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*(?:packet\s*)?loss/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+
/time[=<](\d+\.?\d*)\s*ms/i,
739+
/rtt\s+min\/avg\/max\/\S+\s*=\s*[\d.]+\/([\d.]+)/i,
740+
/(\d+\.?\d*)\s*ms\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

Comments
 (0)