博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
discord china_如何在Ubuntu 18.04上使用Discord Webhooks获取有关您的网站状态的通知
阅读量:2509 次
发布时间:2019-05-11

本文共 14402 字,大约阅读时间需要 48 分钟。

discord china

The author selected to receive a donation as part of the program.

作者选择作为计划的一部分来接受捐赠。

介绍 (Introduction)

When you have critical websites you’ll want to know when they are experiencing any issues so you can fix them before there’s an impact on your users. One way to receive notifications is to set up alerts through a text-based chat system.

当您拥有重要的网站时,您将想知道它们何时遇到任何问题,以便在对用户造成影响之前进行修复。 接收通知的一种方法是通过基于文本的聊天系统设置警报。

is a hosted chat system similar to Slack. With Discord, you can set up a free messaging system that lets you communicate with text messages, images, audio, and video. While it offers premium features, you can , and it has clients available for Windows, macOS, Linux, Android, and iOS.

是类似于Slack的托管聊天系统。 使用Discord,您可以设置一个免费的消息传递系统,使您可以与文本消息,图像,音频和视频进行通信。 它提供高级功能,但您可以 ,并且具有适用于Windows,macOS,Linux,Android和iOS的客户端。

In this tutorial, you will configure your own Discord server, create a Discord webhook, write a Bash script that will check the status of a list of websites, and test notifications from your server to your Discord channel.

在本教程中,您将配置自己的Discord服务器,创建Discord Webhook,编写一个Bash脚本以检查网站列表的状态,并测试从服务器到Discord频道的通知。

先决条件 (Prerequisites)

Before you begin this guide you’ll need the following:

在开始本指南之前,您需要满足以下条件:

  • An Ubuntu 18.04 server that will run your monitoring script and talk to Discord (monitor-server). You can set this server up by following this .

    一台Ubuntu 18.04服务器,它将运行您的监视脚本并与Discord( monitor-server )通信。 您可以按照《 来设置该服务器。

  • If you don’t have a website you would like to monitor yet, set up the apache-server with and make sure to complete Step 5 on setting up a virtual host.

    如果您还没有要监控的网站,请使用设置apache-server ,并确保完成设置虚拟主机的第5步。

  • A Discord account. You can visit the to sign up for a free Discord account.

    Discord帐户。 您可以访问以注册免费的Discord帐户。

第1步-设置Discord Webhook (Step 1 — Setting Up Your Discord Webhook)

Once you have opened your Discord account you can create your own private Discord server.

打开Discord帐户后,您可以创建自己的私有Discord服务器。

First, in your browser or and click on the Create a server button.

首先, 浏览器中或 ,然后单击“ 创建服务器”按钮。

Then pick a name for your server and click on the Create a server button.

然后为服务器选择一个名称,然后单击“ 创建服务器”按钮。

Next, you will configure your Discord webhook. The webhooks are unique URLs that you can use to link services together. Discord’s webhooks allow you to automate your messages and send data updates to your Discord text channels.

接下来,您将配置Discord Webhook。 Web钩子是唯一的URL,可用于将服务链接在一起。 Discord的webhook允许您自动执行消息并将数据更新发送到Discord文本通道。

In this tutorial, you will send a notification to your webhook when a specific service on your server goes down, and Discord will make sure that you get those messages on your channel.

在本教程中,当服务器上的特定服务出现故障时,您将向Webhook发送通知,Discord将确保您在频道中收到这些消息。

To create a webhook you have to first click on your channel and then click on the Edit Channel button right next to your channel’s name.

要创建Webhook,您必须首先单击您的频道,然后单击频道名称旁边的“ 编辑频道”按钮。

Then click on the Webhooks tab and click the Create Webhook button.

然后单击“ Webhooks”选项卡,然后单击“ 创建Webhook”按钮。

After that, pick a name for your webhook, in this tutorial we’ll use Alerts as this is what our Bash script will do—alert us in the case that one of our websites goes down.

然后,为您的Webhook选择一个名称,在本教程中,我们将使用Alerts因为这是我们的Bash脚本将要执行的操作-在我们的一个网站出现故障的情况下向我们发出警报。

Copy your webhook URL and save it for later. Finally, click the Save button.

复制您的webhook URL并保存以备后用。 最后,单击“ 保存”按钮。

You now have your Discord account, server, and webhook. You can now move on to create a test file for your script to monitor.

现在,您有了Discord帐户,服务器和Webhook。 现在,您可以继续创建测试文件供脚本监视。

第2步-创建测试文件(可选) (Step 2 — Creating a Test File (Optional))

If you don’t have your own website to test already, complete this step to add a test file to check how your monitoring script will work. Make sure to run these commands on your apache-server.

如果您还没有自己的网站要测试,请完成此步骤以添加测试文件,以检查监视脚本的工作方式。 确保在apache-server上运行这些命令。

First, create the test file with the following command:

首先,使用以下命令创建测试文件:

  • nano /var/www/your_domain/test

    纳米/ var / www / your_domain / test

Add some content to your file, so you can check it’s working:

向文件中添加一些内容,以便您可以检查其是否正常工作:

test
测试
test

Save and exit the file.

保存并退出文件。

Now navigate to http://your_domain/test in your browser to make sure you can receive the text in the test file.

现在,在浏览器中导航到http:// your_domain /test以确保您可以在test文件中接收文本。

In the next step, you’ll start to build your monitoring script.

在下一步中,您将开始构建监视脚本。

步骤3 —创建您的监视脚本 (Step 3 — Creating Your Monitoring Script)

Now that you’ve configured your webhook, you’ll go ahead and create your Bash script that will check the response code of your websites. In the event that any of the websites returns a status different from 200 OK, your script will send a request to the Discord webhook so you will receive a notification in your Discord channel.

现在,您已经配置了Webhook,接下来将继续创建Bash脚本,该脚本将检查网站的响应代码。 如果任何网站返回的状态不同于200 OK ,则脚本将向Discord Webhook发送请求,因此您将在Discord频道中收到通知。

Note: You could use any other programming language to build a script like this.

注意:您可以使用任何其他编程语言来构建这样的脚本。

First SSH in to your monitor-server that you’re using to run your monitoring script.

首先SSH到用于运行监视脚本的监视服务器

Start by creating a file in your home folder. We’ll call the file check_status.sh in this tutorial.

首先在您的主文件夹中创建一个文件。 在本教程中,我们将文件称为check_status.sh

With your favorite text editor open the file:

使用您喜欢的文本编辑器打开文件:

  • nano ~/check_status.sh

    纳米〜/ check_status.sh

For the program loader to recognize this executable file as a Bash script, add the following line to the top of the file:

为了使程序加载器将此可执行文件识别为Bash脚本,请在文件顶部添加以下行:

~/check_status.sh
〜/ check_status.sh
#!/bin/bash

All lines starting with a # are a comment. The comments are optional, but having comments in your script will make it easier for other people to understand what the script does:

#开头的所有行均为注释。 注释是可选的,但是在脚本中包含注释将使其他人更容易理解脚本的作用:

~/check_status.sh
〜/ check_status.sh
#!/bin/bash### Website status check script# Author: Your Name# Date: 01/01/2020##. . .

Next, specify your Discord webhook as a variable. Add the URL you copied earlier for your webhook:

接下来,将Discord webhook指定为变量。 添加您先前为Webhook复制的URL:

~/check_status.sh
〜/ check_status.sh
. . .### Discord webhook# Change the 'your_discord_webhook_name' with your actual Discord Webhook##url="your_discord_webhook_name". . .

As you’ll possibly use this script to check the status of multiple websites, make a variable called websites_list and store the domain names or IP addresses of the websites that you want to monitor. If you’re using the optional test file in this tutorial, make sure to add /test after your domain name or IP address:

由于您可能会使用此脚本来检查多个网站的状态,因此请创建一个名为websites_list的变量,并存储要监视的网站的域名或IP地址。 如果您在本教程中使用可选的测试文件,请确保在域名或IP地址后添加/test

~/check_status.sh
〜/ check_status.sh
. . .### List of websites to check##websites_list="your_domain/test". . .

In the case that you are monitoring more than one website, you can add more domain names or IP addresses to the websites_list variable and use space as a separator:

如果要监视多个网站,则可以将更多域名或IP地址添加到websites_list变量中,并使用空格作为分隔符:

~/check_status.sh
〜/ check_status.sh
. . .websites_list="your_domain1/test your_domain2 your_domain3". . .

Now you’ll want to loop through the list of websites and check their status. To do so add the following for loop to your file:

现在,您将要遍历网站列表并检查其状态。 为此,将以下for循环添加到您的文件中:

~/check_status.sh
〜/ check_status.sh
. . .for website in ${websites_list} ; do        status_code=$(curl --write-out %{http_code} --silent --output /dev/null -L ${website})        echo $status_codedone. . .

This for loop will go through each item in the websites_list variable and check the status of the website with the curl command.

for循环将遍历websites_list变量中的每个项目,并使用curl命令检查websites_list的状态。

The echo $status_code statement will print out the response status of the curl command. If the website is running as expected the curl command will return a response code 200 OK, meaning that the website is up and running. Otherwise you’ll receive another response code.

echo $status_code语句将打印出curl命令的响应状态。 如果网站按预期运行,则curl命令将返回响应代码200 OK ,表示网站已启动并正在运行。 否则,您将收到另一个响应代码。

Inside the for loop, add an if statement to check if the response code is 200 or not. If the response code is 200 then this means that the website is running and you wouldn’t need a Discord notification. Add the following conditional block to your file:

for循环内,添加一个if语句以检查响应代码是否为200 。 如果响应代码为200则表示该网站正在运行,您不需要Discord通知。 将以下条件块添加到文件中:

~/check_status.sh
〜/ check_status.sh
. . .        if [[ "$status_code" -ne 200 ]] ; then            # POST request to Discord Webhook with the domain name and the HTTP status code        else            echo "${website} is running!"        fi. . .

If there are any problems with the website then you’ll get a different response code, in this event you’ll want to receive a notification via your Discord webhook.

如果网站有任何问题,您将获得一个不同的响应代码,在这种情况下,您将希望通过Discord Webhook收到通知。

To send the notification you can use the curl command to submit a POST request to the Discord webhook URL.

要发送通知,您可以使用curl命令将POST请求提交到Discord Webhook URL。

Add the following curl request inside your if statement:

if语句内添加以下curl请求:

~/check_status.sh
〜/ check_status.sh
. . .        if [[ "$status_code" -ne 200 ]] ; then            # POST request to Discord Webhook with the domain name and the HTTP status code            curl -H "Content-Type: application/json" -X POST -d '{"content":"'"${domain} returned: ${status_code}"'"}'  $url        else            echo "${website} is running!"        fi. . .

Now let’s examine the different arguments:

现在让我们检查不同的参数:

  • -H: Tells curl that you want to add an extra header in your request.

    -H :告诉curl您要在请求中添加额外的标头。

  • "Content-Type: application/json": Defines the data type the webhook should expect (HTTP JSON).

    "Content-Type: application/json" :定义Webhook应该期望的数据类型(HTTP JSON)。

  • -X POST: Specifies that you want to use a POST as the request method.

    -X POST :指定要使用POST作为请求方法。

  • -d: Sends the specified JSON data to the Discord Webhook.

    -d :将指定的JSON数据发送到Discord Webhook。

This will be the final version of your script:

这将是脚本的最终版本:

~/check_status.sh
〜/ check_status.sh
#!/bin/bash### Website status check script# Author: Your Name# Date: 01/01/2020##### Discord webhook# Change the 'your_discord_webhook_name' with your actual Discord Webhook##url="your_discord_webhook_name"### List of websites to check# To add more websites just use space as a separator, for example:# websites_list="your_domain1 your_domain2 your_domain3"##websites_list="your_domain/test your_domain2"for website in ${websites_list} ; do        status_code=$(curl --write-out %{http_code} --silent --output /dev/null -L ${website})        if [[ "$status_code" -ne 200 ]] ; then            # POST request to Discord Webhook with the domain name and the HTTP status code            curl -H "Content-Type: application/json" -X POST -d '{"content":"'"${domain} : ${status_code}"'"}'  $url        else            echo "${website} is running!"        fidone

The script will loop through this list and check the status for each website.

该脚本将遍历此列表,并检查每个网站的状态。

Run the script with the following command to make sure that it works as expected:

使用以下命令运行脚本以确保其可以正常运行:

  • bash ~/check_status.sh

    bash〜/ check_status.sh

After running the script, you will receive the following output in your terminal confirming that your website is running:

运行脚本之后,您将在终端中收到以下输出,确认您的网站正在运行:

Output   
your_domain1 is running!your_domain2 is running!

Next, you’ll test your Discord notifications.

接下来,您将测试Discord通知。

第4步-测试不一致通知 (Step 4 — Testing Your Discord Notifications)

Now it’s time to check if your webhooks are working as expected by using the test file.

现在是时候使用测试文件检查您的网络钩子是否按预期工作了。

On the apache-server run the following command to to this file:

apache-server上,运行以下命令以对该文件的 :

  • sudo chmod 000 /var/www/your_domain/test

    须藤chmod 000 / var / www / your_domain / test

Next, return to your monitor-server and run the script:

接下来,返回到监视服务器并运行脚本:

  • bash check_status.sh

    bash check_status.sh

Move to your Discord app and check your alerts, you’ll receive a :403 error. This shows that you do not have the permissions set correctly and are forbidden from viewing the file.

移动到Discord应用并检查警报,您将收到:403错误。 这表明您没有正确设置权限,并且被禁止查看文件。

Now, to test a different error, remove this file entirely from your apache-server:

现在,要测试其他错误,请从apache-server中完全删除此文件:

  • sudo rm /var/www/your_domain/test

    须藤rm / var / www / your_domain / test

Next, return to your monitor-server and run the script:

接下来,返回到监视服务器并运行脚本:

  • bash check_status.sh

    bash check_status.sh

Move to your Discord app and check your alerts, you’ll receive a :404 error. This shows that the file is unavailable.

移动到Discord应用并检查警报,您将收到:404错误。 这表明该文件不可用。

If you have Discord installed on your phone you’ll receive alerts there as well.

如果您的手机上安装了Discord,您也会在那收到警报。

You now have a script that alerts you when your websites are experiencing any issues. Let’s next configure the script to run automatically every five minutes.

现在,您有了一个脚本,可以在您的网站遇到任何问题时提醒您。 接下来,我们将脚本配置为每五分钟自动运行一次。

第5步-自动化流程 (Step 5 — Automating the Process)

One of the ways to automate the checks is to create a that will run every 5 minutes or so.

自动执行检查的一种方法是创建一个 ,该每5分钟左右运行一次。

First, go back to your monitor-server. Then before you can run the script automatically, you need to sort out your and make sure that the script is executable, otherwise, it will not run. To make the script executable run:

首先,返回到您的monitor-server 。 然后,在可以自动运行脚本之前,您需要整理并确保脚本是可执行的,否则它将无法运行。 要使脚本可执行运行:

  • chmod u+x ~/check_status.sh

    chmod u + x〜/ check_status.sh

Run the following to edit your crontab:

运行以下命令以编辑crontab:

  • crontab -e

    crontab -e

Then add the following to the file:

然后将以下内容添加到文件中:

*/5 * * * * /home/your_user/check_status.sh

Then save the crontab and your script will then execute every 5 minutes.

然后保存crontab ,脚本将每5分钟执行一次。

结论 (Conclusion)

In this article, you configured your own Discord webhook and created a script to notify you in case a specific error occurs on your website. Now you can use your favorite programming language and write a more complex bot.

在本文中,您配置了自己的Discord Webhook,并创建了一个脚本来在您的网站上发生特定错误时通知您。 现在,您可以使用自己喜欢的编程语言并编写一个更复杂的机器人。

To learn more about setting up monitoring infrastructure, check out our .

要了解有关设置监控基础架构的更多信息,请查看我们的 。

And, if you are interested in learning more about shell scripting, check out our .

而且,如果您有兴趣了解有关Shell脚本的更多信息,请查看我们的 。

To learn more about Discord webhooks, you can check the official .

要了解有关Discord Webhooks的更多信息,可以查看官方 。

翻译自:

discord china

转载地址:http://jtegb.baihongyu.com/

你可能感兴趣的文章
zynq修改ramdisk文件系统
查看>>
C#测量程序运行时间及cpu使用时间
查看>>
并发编程
查看>>
我自己曾经经历的CMMI3认证通过关于软件测试的访谈【转载】
查看>>
C# 操作Excel ——Excel获取数据、时间、图片
查看>>
【Express系列】第3篇——接入mysql
查看>>
js 高亮显示关键字
查看>>
CPU工作原理简图
查看>>
进程互斥于同步
查看>>
小米公布2017二季度手机出货量:环比增长70%
查看>>
IntelliJ Idea 集成svn 和使用[转自网络]
查看>>
VS2013 密钥 – 所有版本
查看>>
缓冲一日
查看>>
apache常用配置文件讲解
查看>>
html设置透明度
查看>>
读写锁详解
查看>>
<taglib>报错
查看>>
sublime 3 注册码 - 亲测可用
查看>>
CENTOS7配置静态IP
查看>>
P3332 [ZJOI2013]K大数查询
查看>>