LoginSignup
0
0

OpenVPNの接続・切断をSlackに通知する

Last updated at Posted at 2019-10-15

はじめに

OpenVPNに接続あるいは切断された際に、以下のようにSlackへ通知する仕組みを追加してみます。
image.png

手順

1. SlackのIncoming Webhooksを用意する

下記URLの手順に従い、Incoming Webhooksを用意します。あわせて、投稿先のワークスペースとチャンネルも用意します。
https://api.slack.com/incoming-webhooks

2. 接続・切断通知用のスクリプトを作成する

/opt/scripts/send-slack-connected-message.sh
SCRIPT_DIR=`echo $(cd $(dirname $0) && pwd)`
/bin/sh ${SCRIPT_DIR}/send-slack-message.sh connect $common_name $untrusted_ip $ifconfig_pool_remote_ip
/opt/scripts/send-slack-disconnected-message.sh
SCRIPT_DIR=`echo $(cd $(dirname $0) && pwd)`
/bin/sh ${SCRIPT_DIR}/send-slack-message.sh disconnect $common_name $untrusted_ip $ifconfig_pool_remote_ip
URL="<Incomming WebhooksのURL>"
#BGCOLOR="gray"
MESSAGE=""
if [ "$1" = "connect" ]; then
        #BGCOLOR="yellow"
        MESSAGE="VPN: $2 is connected.\nClient-IP: $3\nVPN-IP: $4"
fi
if [ "$1" = "disconnect" ]; then
        #BGCOLOR="green"
        MESSAGE="VPN: $2 is disconnected.\nClient-IP: $3\nVPN-IP: $4"
fi
curl -H "Content-Type: application/json" \
     -X POST \
     -m 5 \
     -d "{\"text\": \"$MESSAGE\" }" \
     $URL &

3. server.confに設定を追加する

/etc/openvpn/server.conf に以下の設定を追加します。

client-connect /opt/scripts/send-slack-connected-message.sh
client-disconnect /opt/scripts/send-slack-disconnected-message.sh
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