0
0

Monitoring GORA Node with Slack API

Last updated at Posted at 2023-04-22

Monitoring GORA Node with Slack API

SlackAPIでGORA Nodeを監視する方法

GORA nodes are run using Docker containers, so if the Docker container goes down due to some issue, the node runner needs to deal with it. It is very convenient to receive notifications on your smartphone when the Docker container goes down. This script provides a solution using Slack API.

However, if the VPS itself shuts down, no notification will be sent, so it is recommended to use an app called UptimeRobot to receive notifications on your smartphone in that case. It is very easy to set up, so I will create another guide for that separately.

I hope these guides will be helpful for all the node runners.

@Moon1215i

【日本語】

GORAノードは、Dockerコンテナを使って走らせていますが、Dockerコンテナが何かしらの不具合でダウンしてしまった場合、ノードランナーはそれに対処しなければなりません。そのためDockerコンテナがダウンした時に、スマートフォンへ通知が来ることはとても便利です。このスクリプトは、SlackのAPIを使ったソリューションを提供します。

ただしVPS自体がシャットダウンした場合は、通知が着ませんので、それは別途に UptimeRobot というアプリでスマートフォンへ通知が来るようにするとよいでしょう。それはとても簡単なことなので、また別のガイドを作成します。
このガイドが、ノードランナーの皆さんにとって役立つことを願っています。

@Moon1215i

1. How to Create a Slack Account

1. Slackアカウントの作成

Here are the steps to create a Slack account:

  1. Go to the Slack website at https://slack.com/.
  2. Click on "Get started" or "Start for free".
  3. You will be asked to fill out a form to create your workspace. The form will ask for the following information:
    • Workspace name: Enter the name of the workspace you want to create.
    • Email address: Enter the email address you want to associate with your Slack account.
    • Password: Enter a password you want to use to access your account.
  4. Review the information you entered and make any necessary changes.
  5. Click on "Create Workspace".
  6. Slack will send a confirmation email to the email address you provided. Click on the link in the email to activate your account.
  7. Once your account is activated, you can log in to Slack and start using it.

That's it! These are the steps to create a Slack account.

After creating a Slack account, please install the Slack app on your iPhone or Android smartphone.

【日本語】
  1. Slackの公式サイトにアクセスします。URLは https://slack.com/ です。
  2. 「始める」または「無料で始める」ボタンをクリックします。
  3. ワークスペースを作成するためのフォームが表示されます。必要な情報を入力してください。フォームには、以下の情報が含まれます。
    • ワークスペース名:あなたが作成するワークスペースの名前を入力します。
    • メールアドレス:Slackアカウントに関連付けるメールアドレスを入力します。
    • パスワード:アカウントにアクセスするためのパスワードを入力します。
  4. 入力した情報を確認し、必要に応じて修正してください。
  5. 「ワークスペースを作成する」ボタンをクリックします。
  6. Slackは、入力されたメールアドレスに確認メールを送信します。メールのリンクをクリックして、アカウントを有効化してください。
  7. アカウントが有効化されたら、Slackにログインして使用を開始できます。

以上が、Slackアカウントの作成手順です。

Clack アカウントを作成したら、iPhoneもしくはAndroidのスマートフォンにSlackアプリがインストールしてください。

2. How to Obtain an Incoming Webhook API in Slack

2. Slack Webhook API の取得

Here are the steps to obtain an Incoming Webhook API in Slack:

  1. Log in to Slack. The URL for Slack is https://slack.com/.
  2. Go to your workspace in Slack and open the app.
  3. Click on "Add apps" from the sidebar.
  4. Click on "Browse Slack App Directory" and type in "Incoming Webhooks" in the search bar.
  5. From the search results, click on "Incoming Webhooks" and then click on "Add to Slack" on the next screen.
  6. Next, select the channel for which you want to obtain the Webhook URL. Once you have selected the channel, click on "Add Incoming Webhooks integration."
  7. The Webhook URL will be displayed. You can use this URL to send notifications from external services to Slack.

These are the steps to obtain an Incoming Webhook API in Slack.

【日本語】

SlackのIncoming Webhookを使うには、以下の手順に従ってWebhook URLを取得する必要があります。

  1. Slackにログインします。SlackのURLは https://slack.com/ です。
  2. Slackのワークスペースに移動し、アプリを開始します。
  3. 「アプリを追加する」をクリックします。
  4. 「Browse Slack App Directory」をクリックし、検索バーに「Incoming Webhooks」と入力します。
  5. 検索結果から「Incoming Webhooks」をクリックし、次の画面で「Slackに追加(Add to Slack)」をクリックします。
  6. 次に、Webhook URLを取得するためのチャンネルを選択します。チャンネルを選択したら、「Incoming Webhooksインテグレーションの追加(Add Incoming Webhooks integration)」をクリックします。
  7. Webhook URLが表示されます。このURLを使用して、外部サービスからSlackに通知を送信することができます。

以上が、SlackのIncoming Webhooks APIを取得するための手順です。

Screenshot by Dropbox Capture.png

Screenshot by Dropbox Capture.png

Screenshot by Dropbox Capture.png

3. Exporting Slack Webhook as Env Variable.

3. Slack Webhook URLを環境変数として設定

  1. Log in to GORA Node VPS via terminal.

  2. Execute the following command:

    • Replace YOUR_WEBHOOK_URL in the command with the Webhook URL you obtained, paste it into the terminal, and run it.
    3-1
    echo 'export SLACK_WEBHOOK_URL=YOUR_WEBHOOK_URL' >> ~/.bashrc && \
    source ~/.profile
    

    This command sets an environment variable using the echo command.

    The command echo 'export SLACK_WEBHOOK_URL=YOUR_WEBHOOK_URL' >> ~/.bashrc appends export SLACK_WEBHOOK_URL=YOUR_WEBHOOK_URL to the end of ~/.bashrc, which is a shell script that is run every time you start a new terminal session. This command sets the SLACK_WEBHOOK_URL environment variable, which can be used by other programs and scripts.

    The source ~/.profile command is used to load any changes to your environment variables into the current shell session, so that they are immediately available for use.

    Example

    3-2
    echo 'export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/S74YSN147Z/A837QB4J77J/AfiGK8MEne3tB8tKZv8DZ96d' >> ~/.bashrc && \
    source ~/.profile 
    

    Screenshot by Dropbox Capture.png

【日本語】
  1. GORAノードのVPSにターミナルでログインする

  2. 次のコマンドを実行します。

    • コマンド内のYOUR_WEBHOOK_URLのところに、あなたが取得したWebhook URLをに書き換えて、ターミナルに貼り付けて実行してください。
    3-1
    echo 'export SLACK_WEBHOOK_URL=YOUR_WEBHOOK_URL' >> ~/.bashrc && \
    source ~/.profile
    

    このコマンドは、echo コマンドを使用して環境変数を設定する方法を説明したコマンドです。また、~/.bashrc ファイルに export SLACK_WEBHOOK_URL=YOUR_WEBHOOK_URL という環境変数を追加します。このコマンドを実行すると、環境変数は永続的に設定され、ログインするたびに自動的に読み込まれます。また、source ~/.profile コマンドを使用することで、環境変数の変更を即座に反映することができます。

    3-2
    echo 'export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/S74YSN147Z/A837QB4J77J/AfiGK8MEne3tB8tKZv8DZ96d' >> ~/.bashrc && \
    source ~/.profile 
    

    Screenshot by Dropbox Capture.png

4. Bash script for Docker monitoring and Slack notifications

4. Dockerの監視とSlack通知のためのBashスクリプト

  1. Paste the following.
    -- 以下を貼り付ける。 --

    4-1
    cat > monitor_gora.sh
    

    Screenshot by Dropbox Capture.png

  2. Paste the following, leaving the last line blank, and close it with Control + D.
    -- 以下を貼り付ける。最後の行は空白にして、Control + Dで閉じる --

    4-2
    #!/bin/bash
    
    # Dockerコンテナの名前を定義
    CONTAINER_NAME="gora-nr"
    
    # ホスト名とIPアドレスを取得
    HOST_NAME=$(hostname)
    IP_ADDRESS=$(ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -n1)
    
    # Dockerコンテナの状態を定期的にチェックし、変化があった場合はSlackに通知する
    LAST_STATUS=""
    while true; do
        STATUS=$(docker inspect --format='{{.State.Status}}' $CONTAINER_NAME)
        if [ "$STATUS" != "$LAST_STATUS" ]; then
            if [ "$STATUS" == "exited" ]; then
                MESSAGE="$HOST_NAME"_"$IP_ADDRESS: Docker container $CONTAINER_NAME has stopped."
            else
                MESSAGE="$HOST_NAME"_"$IP_ADDRESS: Docker container $CONTAINER_NAME has started."
            fi
            # Slackに通知を送信
            curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" $SLACK_WEBHOOK_URL
            LAST_STATUS=$STATUS
        fi
        sleep 10
    done
    # Press enter on the next line and close the terminal by pressing Control + D
    # ↓次の行は1行あけて、`Control + D`で閉じる
    
    

    Press enter on the next line and close the terminal by pressing Control + D
    -- 最後の行は空白にして、Control + Dで閉じてください。 --
    Screenshot by Dropbox Capture.png

  3. Granting execute permission.
    -- 実行権限を付与する。 --

    4-3
    chmod +x monitor_gora.sh
    

    Screenshot by Dropbox Capture.png

Please refer to the following website for more details.
詳細は以下のサイトを参照してください。

5 Executing and Verifying Shell Scripts

5. シェルスクリプトの実行と確認

Execution

nohup is a command to continue running a program even after the terminal session has ended. & is a command to run a command in the background. In other words, it is a command that runs a program in the background and instructs it to continue running even after the terminal session has ended.

5-1
nohup ./monitor_gora.sh &
# Exit with `Control + C`

If the following screen appears, exit with Control + C.
Screenshot by Dropbox Capture.png
It can be seen that the PID is 25266.

PID is an abbreviation for Process IDentifier and refers to a number used to uniquely identify a process in UNIX-like operating systems. A process refers to a running program.

ps is a command to display process information in UNIX-like operating systems, and can display information about processes such as process ID (PID), CPU usage, and memory usage.

ps aux is an option to display process information in detail, and can display more detailed information such as CPU time, memory usage, and executing user.

【日本語】

実行

nohupは、ターミナルセッションが終了しても、プログラムを実行し続けるためのコマンドです。&は、コマンドをバックグラウンドで実行するためのコマンドです。つまり、プログラムをバックグラウンドで実行し、かつターミナルセッションが終了してもプログラムを実行し続けるように指示するコマンドです。

5-1
nohup ./monitor_gora.sh &
# `Control + C`で終了

以下のような画面が出たら、Control + Cで終了
Screenshot by Dropbox Capture.png
PID25266であることがわかります。

PIDとは、 プロセスID(Process IDentifier) の略称で、UNIX系オペレーティングシステムでプロセスを一意に識別するための番号のことを指します。プロセスとは、実行中のプログラムのことを指します。

psは、UNIX系オペレーティングシステムでプロセス情報を表示するコマンドで、プロセスID(PID)やCPU使用率、メモリ使用量など、プロセスに関する情報を表示できます。

ps auxは、プロセス情報を詳細に表示するためのオプションで、CPU時間やメモリ使用量、実行ユーザーなど、より詳細な情報を表示することができます。

Confirmation

5-3
ps aux | grep gora

Screenshot by Dropbox Capture.png

Make sure that the first line shows ./monitor_gora.sh. You can confirm that the PID is 25266. The second line shows grep --color=auto gora, which simply indicates the process that is currently being searched by the grep command. It is important to check that ./monitor_gora.sh is displayed.

【日本語】

確認

5-3
ps aux | grep gora

Screenshot by Dropbox Capture.png

1行目の./monitor_gora.shが表示されていることを確認してください。PIDはPID25266とあることが確認できます。2行目にも、grep --color=auto goraと見えますが、これは現在grepコマンドで検索しているプロセスを示しているに過ぎません。./monitor_gora.shが表示されていることが大切です。

6 Testing Notification on Smartphone

6. スマートフォンへの通知テスト

We will perform a test to check if notifications are received on your smartphone.

Please ensure that the Slack app is installed on your iPhone or Android smartphone, and notification settings are properly configured. Slack has detailed notification settings, so please check and confirm them on your own. If notifications are not received, the cause is likely to be the notification settings in Slack on your smartphone.

【日本語】

スマートフォンに通知が来るかどうかを確認するテストをします。

iPhoneもしくはAndroidのスマートフォンにSlackアプリがインストールされていることを確認してください。そして通知設定はとても重要です。Slackは詳細な通知設定がありますので、ご自身で調べて確認してください。通知が届かない場合原因は、スマートフォンでのSlackの通知設定である可能性が多いにあります。

IMG_20230425_134026_380.jpg
IMG_20230425_134027_325.jpg

  • Notification for Docker-stop
    -- Docker-stopの通知 --

    ./gora docker-stop
    

    IMG_9293.PNG

  • Notification for Docker-start
    -- Docker-startの通知 --

    ./gora docker-start --background
    

    IMG_9294.PNG

7 How to stop the monitor_gora process

monitor_goraのプロセスを停止する方法

Option A

Execute the command ps aux | grep gora and confirm that the line showing ./monitor_gora.sh is displayed. Check the PID and use the kill command to stop the process.

Screenshot by Dropbox Capture.png

Enter the monitor_gora.sh PID in the kill command and press Enter.

7-1
kill 21633
# Enter the monitor_gora PID in the kill command and press Enter
【日本語】

選択肢 A
ps aux | grep goraを実行して、./monitor_gora.shと表示されている行のPID確認し、killコマンドでプロセスを止めます。
Screenshot by Dropbox Capture.png
killコマンドに monitor_gora.shPIDを入力してEnter

7-1
kill 21633
# killコマンドに monitor_gora のPIDを入力してEnter

Option B

Execute the following command:

7-2
kill $(ps aux | grep '[m]onitor_gora' | awk '{print $2}')

The command ps aux shows all processes, grep '[m]onitor_gora' extracts lines containing monitor_gora. [m] is a regular expression used to extract lines containing the string monitor_gora, and is used to prevent the grep command from including itself. The extracted lines contain the PID of monitor_gora, which is extracted using awk '{print $2}'. Finally, the kill command is used to terminate the process.

【日本語】

選択肢 B
次のコマンドを実行します。

7-2
kill $(ps aux | grep '[m]onitor_gora' | awk '{print $2}')

ps auxで全てのプロセスを表示し、grep '[m]onitor_gora'でmonitor_goraを含む行を抽出します。[m]というのは、monitor_goraという文字列を含む行を抽出するための正規表現です。ここでgrepコマンドが自分自身を含まないようにするために使われます。抽出された行にはmonitor_goraのPIDが含まれているため、awk '{print $2}'で2列目にあるPIDを抽出し、killコマンドでプロセスを終了します。

If the process still doesn't terminate, add the -9 option to force termination.

7-3
kill -9 21633

by specifying the -9 option, a process can be forcefully terminated. It should be noted that using this method to terminate a process can result in the process not releasing resources properly, which can lead to data loss or corruption. Therefore, this method should be used as a last resort.

【日本語】

それでも終了しない場合は、-9オプションを足してください。

7-3
kill -9 21633

-9オプションを指定することで、プロセスを強制的に終了させることができます。ただし、この方法でプロセスを終了すると、プロセスが保持していたリソースが正常に解放されず、データの損失や不整合が発生する可能性があるため、最後の手段として使用する必要があります。

8. If VPS is rebooted

VPSの再起動した場合

If your VPS has been rebooted, the monitor_gora.sh process will have stopped, so please execute Command 5-1:

5-1
nohup ./monitor_gora.sh &
# exit with 'Control' + 'C'
【日本語】

VPSが再起動した場合は、monitor_gora.shのプロセスは止まっていますので、コマンド5-1を実行してください。

5-1
nohup ./monitor_gora.sh &
# 'Control' + 'C'で終了

Auther

@Moon1215i


Github

https://github.com/Moon1215i


GORA Node SETUP INSTRUCTIONS【English & 日本語】

Algorand Node with GORA【English & 日本語】

GORA Backup Repository for Mac & Linux Users【English & 日本語】

Twitter
https://twitter.com/Moon1215i

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0