paint-brush
Build an Uptime Monitor in Minutes with Slack, Standard Library and Node.js by@notoriaga
5,424 reads
5,424 reads

Build an Uptime Monitor in Minutes with Slack, Standard Library and Node.js

by SteveOctober 19th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Making sure your website or API is up and running well can be a round-the-clock job. To help in this tireless effort, we can make uptime reports and status alerts visible within the <a href="//gzht888.com/tagged/tools" target="_blank">tools</a> we frequently use, like <a href="//slack.com" target="_blank">Slack</a>. With a platform like <a href="//stdlib.com" target="_blank">Standard Library</a>, an integration like that is nearly effortless. This guide will show you how to set up an API on <a href="//stdlib.com" target="_blank">Standard Library</a> that will ping a URL and send a message to <a href="//gzht888.com/tagged/slack" target="_blank">Slack</a> if the URL doesn’t return a 2xx status code. It will leverage <a href="//stdlib.com/blog/code-xyz-09-26-2018-scheduled-tasks" target="_blank">Standard Library’s scheduled tasks,</a> which allow you to execute APIs based on nearly any schedule.

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Build an Uptime Monitor in Minutes with Slack, Standard Library and Node.js
Steve HackerNoon profile picture

Making sure your website or API is up and running well can be a round-the-clock job. To help in this tireless effort, we can make uptime reports and status alerts visible within the tools we frequently use, like . With a platform like , an integration like that is nearly effortless. This guide will show you how to set up an API on that will ping a URL and send a message to Slack if the URL doesn’t return a 2xx status code. It will leverage which allow you to execute APIs based on nearly any schedule.

If you’re not familiar with , we’re a serverless platform that enables everyone to build, ship, and integrate scalable APIs in a matter of seconds. With the help of our online text editor, , you can do it right from your browser.

Step 1: Create a Slack App

Head over to the , and click “Create New App” in the top-right corner of the screen. After a moment, you’ll be redirected to a page labeled “Basic Information”. On the left side of the screen, you’ll see “OAuth and Permissions” under the “Features” category.

Once there, scroll down to “Scopes”. We need the bot to be able to send messages to Slack on behalf of itself. That means we need to give it “chat:write:bot” permissions, so search for that in the “Select Permission Scopes” drop-down.

With the desired permissions set, scroll back up to the top of the “OAuth and Permissions” page and click the green “Install App to Workspace” button. Once you authorize it, the page will refresh and where the install button once was will be an OAuth token. Click copy; you’ll need that in just a moment.

Step 2: Get the Ping API Template

With Slack all squared away, open up . If you follow that link, a template for the ping API will automatically open. Otherwise, you can click on the “Community API Sources” tab and search for “steve/ping”.

Once it’s loaded, you’ll land on the __main__.js endpoint. It takes a single parameter, url . The function makes an HTTP request to url and checks if the status code is in the 2xx range. If it’s not, it uses the to send a message to a channel specified by the environment variable SLACK_CHANNEL_ID .

The heavy lifting is done in the /src/request.js file. Feel free to peruse that file, but in essence, it makes a GET request to a URL and records some information about the request. It will return the status code and timing information like the time to first byte and total request duration.

After the ping, we check if the status code is less than 200 or greater than 299. If it is, message slack with the details of the request, formatted with the function formatPing. formatPing takes the object returned by the request function, stringifies it, and wraps it up in triple backticks. Slack will display anything surrounded by three backticks as a code block, which can make JSON look a little cleaner. For more on formatting see the .

Step 2b : Set the Environment Variables

If you open up env.json, you’ll see two environment variables, SLACK_OAUTH_TOKEN and SLACK_CHANNEL_ID. The former is what we just got from the Slack app dashboard, and the latter is the name of the channel you want the bot to post to, like #general or #alerts. Make sure you fill out the “dev” set, as shown below.

Now seems like an appropriate time to test this API out. Go back to __main__.js and press cmd/ctrl+b to open the parameters pane in the bottom-left of the screen. Type in a URL and hit enter. If all is well for whatever website/API is at that URL, you should see nothing!

That’s because if the ping API can reach the URL with a 2xx status code, it’s a noop. To try a failing case use [//bad.stdlib.com/](//bad.stdlib.com/) as the URL. In the Code.xyz results panel you’ll see the response from Slack’s API saying a message was sent. If you navigate to the channel you put in env.json

You’ll see a message that //bad.stdlib.com/ was unreachable, and some information about the request.

Step 3: Set a Scheduled Task

With the ping API up, we can now schedule the API to run every few minutes. Click on the “tasks” icon at the top of . On the right side of the scheduled tasks page, under “Scheduled a New Task”, search for the API you just deployed.

Once you open it, you’ll see the task creation widget. To set the schedule, you have two options. By default you pick from a few drop-downs, allowing you to create intervals like “run once a minute” or “run twelve times an hour”. You can also click on “Advanced (cron)” if you like cron syntax. Whichever way you choose, it’s probably best to have the API run at least once every five minutes.

Click the blue “Schedule Task” and the task should appear on the left side of the screen.

Digging Deeper

There are many ways to expand upon this simple ping API. For instance, maybe you want to include status codes in the 3xx range (redirects) as acceptable. Well, all you have to do is change one line of code! Changing



if (ping.statusCode < 200 || ping.statusCode > 299) {...}

to



if (ping.statusCode < 200 || ping.statusCode > 399) {...}

Will do the trick! Now perhaps you guilty of not checking Slack as often as you should, and prefer good old SMS. With , SMS integration is just a line of code away! In fact; there is another endpoint in the ping API already set up with SMS notifications. Open up extra.js, and you’ll see —

It’s the same as __main__.js but with the extra conditional to check if a phone number was provided. If so, it’ll send a message to prompting the recipient to get on Slack to see whats happening.

The point of these small changes is to show that at the end of the day scheduled tasks, and any API on Standard Library for that matter, are just JavaScript and fully customization to your exact use case.

That’s All!

I hope this tutorial has been helpful in showing you how easy it is to get started with . For more inspiration as to how you can better use , you can check out more guides written by the team . If you have a neat idea you’d like to share, reach out to me directly by e-mail: [email protected], or follow the team and on Twitter.

Steve Meyer is a recent graduate of Oberlin College and Software Engineer at Standard Library. When he’s not programming, you can find him baking bread, or playing Spider-Man.

바카라사이트 바카라사이트 온라인바카라