Simple internet outages monitor in golang with slack notification on reconnection
Run the program in background (daemon) and monitor the internet connection status after set interval. In the event of disconnection the program should notify using a notifier (for now it supports slack only, PR's welcome)
This program depends on the nc (or netcat) utility. Most of the OS today have this utility pre-installed, but just to be sure you can check this by running following command:
nc -dzw1 google.com 443
-
Go to https://api.slack.com/apps and
Create New App
usingFrom Scratch
-
Under
Add features and functionality
chooseIncoming Webhooks
and turn on theActivate Incoming Webhooks
option -
Create a new webhook URL for your app by clicking the button
Add New Webhook to Workspace
-
Grant the permission to post in any channel of your choosing and copy the webhook URL. It should look like this:
https://hooks.slack.com/services/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
-
Export the webhook URL as
SLACK_WEBHOOK_URL
using command:export SLACK_WEBHOOK_URL=<your slack webhook URL>
-
To test if your slack app is working correctly you can use the following command to send
Hello, World!
message to your channel:curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' <your slack webhook URL>
You should see the
ok
as output and your slack channel should receive theHello, World!
message.
This program uses the following env variables. Set them according to your needs.
Variable Name | Required | Default Value | Summary |
---|---|---|---|
SLACK_WEBHOOK_URL | Yes | N/A | The webhook URL for your slack app from Step 1 |
NC_DOMAIN | No | google.com |
Domain you want to use to check internet connection |
NC_PORT | No | 443 |
Port you want to use for netcat |
SLACK_NOTIFY_ON_REGISTER | No | true |
If true you will get a message Slack Notifier registered everytime the program starts |
TICK_INTERVAL | No | 30s |
Interval between two internet checks. e.g. 15s , 1m etc |
If you prefer using .env
files instead of exporting individual variable, here is a sample .env
file:
TICK_INTERVAL=30s
NC_DOMAIN=google.com
NC_PORT=443
SLACK_NOTIFY_ON_REGISTER=true
SLACK_WEBHOOK_URL=<your slack webhook URL>
To use the .env
file don't forget to source it using command:
source .env
You can run the program by downloading the binary from latest release or if you have Go (golang) installed you can directly download the source code and run using following command:
go run .