diff --git a/README.md b/README.md index 0c5fe92..a3abe61 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ Simple monitor to watch URLs (`HTTP`) or ports (`TCP`, `UDP`) and update [Cachet ## Configuration -cachet-monitor can monitor a list of services. Therefore it requires to setup all services in `./data/config.json`. __The id of each service has to match the cachet component id you want to update!__ You also can specify a custom timeout in seconds for each service. +cachet-monitor can monitor a list of services. Therefore it requires to setup all services in `./data/config.json`. __The id of each service has to match the cachet component id you want to update!__ You also can specify a custom timeout in seconds for each service. If the service timeout is passed the status will be `SLOW` (Cachet `Performance Issues`). -You also need to specify the interval (`cron`) your services should be checked. You can use the cron syntax from [`node-cron`](https://www.npmjs.com/package/node-cron). You also have to set `offlineTimeUntilMajor` which is the offline time in seconds until the state of an offline service turns from partial to major outage. Finally you need to provide the information to your cachet instance (`api` and `token`). +You also need to specify the interval (`cron`) your services should be checked. You can use the cron syntax from [`node-cron`](https://www.npmjs.com/package/node-cron). You also have to set `offlineTimeUntilMajor` which is the offline time in seconds until the state of an offline service turns from partial to major outage. Finally you need to provide the information to your cachet instance (`api` and `token`). The "global" `timeout` value will be used as a final request timeout for each service. If the check request does not complete in this time the service will be marked as offline. Example: @@ -30,6 +30,7 @@ Example: } ], "cron": "0 * * * * *", + "timeout": 30, "offlineTimeUntilMajor": 300, "api": "https:///api/v1", "token": "" diff --git a/index.js b/index.js index 69b8e0a..9253742 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ const cachetStatusMapping = { "INCIDENT": 4 }; -const checkHttp = async (url, performanceTimeout, requestTimeout) => { +const checkHttp = async (url, performanceTimeout, requestTimeout = 60) => { const controller = new abort.AbortController(); const timeout = setTimeout(() => controller.abort(), requestTimeout); try { @@ -47,7 +47,7 @@ const checkHttp = async (url, performanceTimeout, requestTimeout) => { } }; -const checkPort = async (host, port, type, performanceTimeout, requestTimeout) => { +const checkPort = async (host, port, type, performanceTimeout, requestTimeout = 60) => { return await new Promise(resolve => { nmap.scan({ range: [host], @@ -78,11 +78,11 @@ const checkPort = async (host, port, type, performanceTimeout, requestTimeout) = async function checkStatus(service) { switch (service.type) { case 'HTTP': - return await checkHttp(service.url, service.timeout * 1000, service.timeout * 2000); + return await checkHttp(service.url, service.timeout * 1000, config.timeout); case 'TCP': - return await checkPort(service.host, service.port, 'tcp', service.timeout * 1000, service.timeout * 2000); + return await checkPort(service.host, service.port, 'tcp', service.timeout * 1000, config.timeout); case 'UDP': - return await checkPort(service.host, service.port, 'udp', service.timeout * 1000, service.timeout * 2000); + return await checkPort(service.host, service.port, 'udp', service.timeout * 1000, config.timeout); default: throw new Error('unsupported type "' + type + '"') } @@ -123,7 +123,7 @@ const pushStatusToCachet = async (id, status) => { }; const check = async () => { - for (const service of config.services) { + await Promise.all(config.services.map(async service => { const oldStatus = cache[service.id]; const newStatus = await checkService(service, oldStatus); if (!oldStatus || oldStatus.status !== newStatus.status) { @@ -131,7 +131,7 @@ const check = async () => { await pushStatusToCachet(service.id, newStatus.status); cache[service.id] = newStatus; } - } + })); }; cron.schedule(config.cron, async () => await check(), {});