Add regular (non TCP) ping

This commit is contained in:
Lumeille 2024-03-09 22:57:03 +11:00
parent 590f2ed67b
commit 168b07b4ff
4 changed files with 52 additions and 17 deletions

18
package-lock.json generated
View File

@ -11,10 +11,12 @@
"dependencies": { "dependencies": {
"@network-utils/tcp-ping": "^1.2.3", "@network-utils/tcp-ping": "^1.2.3",
"discord-webhook-node": "^1.1.8", "discord-webhook-node": "^1.1.8",
"dotenv": "^16.3.1" "dotenv": "^16.3.1",
"ping": "^0.4.4"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.10.5", "@types/node": "^20.10.5",
"@types/ping": "^0.4.4",
"typescript": "^5.3.3" "typescript": "^5.3.3"
} }
}, },
@ -35,6 +37,12 @@
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
} }
}, },
"node_modules/@types/ping": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/@types/ping/-/ping-0.4.4.tgz",
"integrity": "sha512-ifvo6w2f5eJYlXm+HiVx67iJe8WZp87sfa683nlqED5Vnt9Z93onkokNoWqOG21EaE8fMxyKPobE+mkPEyxsdw==",
"dev": true
},
"node_modules/asynckit": { "node_modules/asynckit": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@ -130,6 +138,14 @@
} }
} }
}, },
"node_modules/ping": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/ping/-/ping-0.4.4.tgz",
"integrity": "sha512-56ZMC0j7SCsMMLdOoUg12VZCfj/+ZO+yfOSjaNCRrmZZr6GLbN2X/Ui56T15dI8NhiHckaw5X2pvyfAomanwqQ==",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/tr46": { "node_modules/tr46": {
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",

View File

@ -12,11 +12,13 @@
"license": "LGPL-3.0-only", "license": "LGPL-3.0-only",
"devDependencies": { "devDependencies": {
"@types/node": "^20.10.5", "@types/node": "^20.10.5",
"@types/ping": "^0.4.4",
"typescript": "^5.3.3" "typescript": "^5.3.3"
}, },
"dependencies": { "dependencies": {
"@network-utils/tcp-ping": "^1.2.3", "@network-utils/tcp-ping": "^1.2.3",
"discord-webhook-node": "^1.1.8", "discord-webhook-node": "^1.1.8",
"dotenv": "^16.3.1" "dotenv": "^16.3.1",
"ping": "^0.4.4"
} }
} }

View File

@ -19,14 +19,22 @@ export type Config = {
token: string token: string
destinationIds: string[] destinationIds: string[]
}))[], }))[],
pollEndpoints: { pollEndpoints: ({
name: string name: string
endpoint: string
interval?: number interval?: number
notifyInterval?: number notifyInterval?: number
type: "fetch" | "ping"
alertEndpoints: string[] alertEndpoints: string[]
}[] } & ({
type: "fetch"
url: string
} | {
type: "ping"
address: string
} | {
type: "tcp-ping"
address: string
port?: number
}))[]
} }
export const config = JSON.parse(readFileSync("./config.json").toString()) as Config export const config = JSON.parse(readFileSync("./config.json").toString()) as Config

View File

@ -2,6 +2,8 @@ import { ping } from "@network-utils/tcp-ping"
import { defaultInterval, config } from "./config" import { defaultInterval, config } from "./config"
import { sleep } from "./utils" import { sleep } from "./utils"
import { handleDown, handleUp } from "./handler" import { handleDown, handleUp } from "./handler"
import { Config } from "./config"
import { promise as pingPromise } from "ping"
let state = new Map(config.pollEndpoints.map(x => [x.name, { let state = new Map(config.pollEndpoints.map(x => [x.name, {
lastExec: 0, lastExec: 0,
@ -11,6 +13,22 @@ let state = new Map(config.pollEndpoints.map(x => [x.name, {
}])) }]))
export type State = typeof state export type State = typeof state
const isEndpointUp = async (endpoint: Config["pollEndpoints"][number]) => {
if (endpoint.type === "fetch" ) {
const r = await fetch(endpoint.url, {method: "GET"})
return r.ok
}
else if (endpoint.type === "tcp-ping" ) {
const r = await ping({address: endpoint.address, port:endpoint.port ?? 80})
return r.errors.length === 0
}
else if (endpoint.type === "ping") {
const r = await pingPromise.probe(endpoint.address)
return r.alive
}
else return true
}
const executor = async () => { const executor = async () => {
const curTime = Date.now() const curTime = Date.now()
config.pollEndpoints.map(async endpoint => { config.pollEndpoints.map(async endpoint => {
@ -19,18 +37,9 @@ const executor = async () => {
else if (curTime - endpointState.lastExec > ((endpoint.interval ?? defaultInterval) * 1000)) { else if (curTime - endpointState.lastExec > ((endpoint.interval ?? defaultInterval) * 1000)) {
console.log(`Time to poll ${endpoint.name}`) console.log(`Time to poll ${endpoint.name}`)
endpointState.lastExec = curTime endpointState.lastExec = curTime
if (endpoint.type === "fetch" ) { if (await isEndpointUp(endpoint)) handleUp(endpointState, curTime, endpoint)
const r = await fetch(endpoint.endpoint, {method: "GET"})
if (r.ok) handleUp(endpointState, curTime, endpoint)
else handleDown(endpointState, curTime, endpoint) else handleDown(endpointState, curTime, endpoint)
} }
else if (endpoint.type === "ping" ) {
const r = await ping({address: endpoint.endpoint})
console.log(r.errors)
if (r.errors.length === 0) handleUp(endpointState, curTime, endpoint)
else handleDown(endpointState, curTime, endpoint)
}
}
}) })
} }