Zabbix⇒Amazon SNS連携手順

# 1　はじめに
　Zabbix⇒Amazon SNSの連携手順を書いていきます。
※ほとんど自分用の記事ですが、何かの参考になれば。

>AWS EC2
>CentOS8 Stream
>Zabbix 5.0.13
>MySQL / Apache + PHP

# 2　AWSまわりの設定
## 2-1　IAMの作成・設定
①
![WS0022.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/7de52b57-d5dd-204d-5e2d-6f2c18411c2f.jpeg)
②
![WS0023.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/3cdbbc2b-f9de-3469-ba13-8c9699acd142.jpeg)
③
![WS0024.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/8df42fa0-f07d-b60c-4818-4286a296256b.jpeg)
④
![WS0025.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/e5f7c836-2e75-9252-782d-3805d96f5f10.jpeg)
⑤
![WS0026.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/9ef04398-5bd0-621b-7dde-2c93611024b3.jpeg)
※権限については、より制限したい場合は変更してください。

⑥
![WS0027.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/76604cde-2d12-e19e-617f-e91bbca3b0ed.jpeg)
※タグは自由に

⑦
![WS0028.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/6ae2fdcb-9302-6291-b5c8-8899a38a3c5f.jpeg)
⑧
![WS0029.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/31d77de3-4f44-7f32-6d44-bec2f4fdbec8.jpeg)
※CSVはダウンロードしてください。

## 2-2　Amazon SNSのトピック作成・設定
①
![WS0030.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/27499f43-5965-85da-4353-401aa671bfbd.jpeg)
②
![WS0031.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/5dc95a2e-b16b-6c56-2eba-0a9c541b41c6.jpeg)
③
![WS0032.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/731eb90b-5700-c45a-b9ca-cf2c7b521662.jpeg)
④
![WS0033.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/d64e2a85-d43d-258b-ef4b-abc8d9192ef0.jpeg)
⑤
![WS0034.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/a153e727-da0e-27b8-c4bd-c184f69b4dde.jpeg)
⑥
![WS0035.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/7c41bfaa-6bb9-13c1-28f3-6f0dd053e40b.jpeg)
⑦
![WS0036.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/6574a757-ff67-0d89-2db4-0cd0e1a15266.jpeg)
⑧
![WS0037.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/db3eacb4-44c1-db43-3ffc-bda010af2697.jpeg)
⑨
![a.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/42ab19b5-069e-6e8f-7c8a-e239a1c559a4.jpeg)

⑩こんな感じのメールが届きます。
![WS0039.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/f723528c-fc67-45f9-fc97-68254b8dd39e.jpeg)
⑪
![WS0040.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/b26adf60-e4c9-fc2a-50cc-f6b5823dbd44.jpeg)
⑫
![WS0041.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/74b8cdc1-f877-8beb-1e5b-7532e5628790.jpeg)

# 3　Zabbixまわりの設定
## 3-1　スクリプト作成
①スクリプトを作成します

```
vi /usr/lib/zabbix/alertscripts/awssns.sh
```


```/usr/lib/zabbix/alertscripts/awssns.sh
#!/bin/sh

TOPIC_ARN=$1
SUBJECT=$2
MESSAGE=$3

echo "`date` $TOPIC_ARN $SUBJECT" >> /var/log/zabbix/awssns.log

if [ "$SUBJECT" = "" -o "$TOPIC_ARN" = "" ]; then
  echo "usage: $0 {ARN SUBJECT MESSAGE}"
  exit 1
fi

if [ "$MESSAGE" = "" ]; then
  MESSAGE=$SUBJECT
fi

aws sns publish --topic-arn $TOPIC_ARN --subject "$SUBJECT" --message "$MESSAGE"
```

③権限を付与

```
chmod 755 /usr/lib/zabbix/alertscripts/awssns.sh
chown zabbix:zabbix /usr/lib/zabbix/alertscripts/awssns.sh
```

## 3-2　AWS連携設定
①AWSコマンドをインストール

```
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
```

②Zabbixユーザー用の連携設定の作成

```
mkdir /var/lib/zabbix
mkdir /var/lib/zabbix/.aws
touch /var/lib/zabbix/.aws/credentials
touch /var/lib/zabbix/.aws/config
chmod 755 /var/lib/zabbix/.aws/c*
chown zabbix:zabbix /var/lib/zabbix/.aws/c*
```
③AWS連携設定

```
aws configure
```
```:root/awsconfigure
AWS Access Key ID : 2-1⑧を入力
AWS Secret Access Key : 2-1⑧を入力
Default region name : ap-northeast-1
Default output format : json
```
```
cat /etc/passwd | grep zabbix
su -s /bin/bash - zabbix
aws configure
```
```:zabbix/awsconfigure
AWS Access Key ID : 2-1⑧を入力
AWS Secret Access Key : 2-1⑧を入力
Default region name : ap-northeast-1
Default output format : json
```
```
aws configure list
```
## 3-3　メディアタイプの設定
①
![WS0042.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/789f4e1c-0f0d-285b-cb39-b2edd043d7ee.jpeg)
②
![WS0043.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/94d66d70-4659-6a33-ceb9-e0c20fdf4a94.jpeg)
③
![WS0044.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/d510b83c-981c-3eb9-ffe6-73430877d5e1.jpeg)
```
名前：Amazon SNS
スクリプト名：awssns.sh
パラメータ：
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
```

④
![WS0063.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/85e4bfaf-1b95-c09f-ad73-0dc31e51b8bb.jpeg)
⑤
![WS0061.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/7b6e61eb-4080-10fc-f213-3d4b8e0c9a4e.jpeg)
```
■障害
件名：【障害連絡】{HOST.NAME1} {EVENT.NAME}
メッセージ：
Zabbixから障害イベントが通知されました。
発生日時：{EVENT.DATE} {EVENT.TIME}
ホスト名：{HOST.NAME1}
障害の深刻度：{TRIGGER.SEVERITY}
アラート名：{EVENT.NAME}
最新データ値：{ITEM.LASTVALUE1}
```
```
■障害復旧
件名：【復旧連絡】{HOST.NAME1} {EVENT.RECOVERY.NAME}
メッセージ：
Zabbixから復旧イベントが通知されました。
復旧日時：{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
ホスト名：{HOST.NAME1}
アラート名：{EVENT.RECOVERY.NAME}
障害継続時間：{EVENT.DURATION}
最新データ値：{ITEM.LASTVALUE1}
```

⑥
![WS0062.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/a9c018f7-f283-c353-0dca-213ccd0eeada.jpeg)


## 3-4　ユーザーの設定
①
![WS0045.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/5de1eaec-22db-053d-a59f-c33c69a2fce1.jpeg)

②
![WS0046.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/debe9489-fabc-6781-8e5f-efcfa4d6510c.jpeg)

③
![WS0047.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/107bb603-c169-95d5-a139-239db0458c28.jpeg)
2-2⑥を入力

④
![WS0052.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/ebfe08de-0641-6654-18f1-4f6fbaabf1fe.jpeg)


## 3-5　アクションの設定
![WS0049.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/4a103a76-7326-cd58-28e1-1b3b15ef4327.jpeg)
![WS0057.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/d138f5ff-f38d-3851-6f63-f6111b192ab3.jpeg)
![WS0050.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/feebca9c-4580-6527-9fc3-d622f9d70e2a.jpeg)
※名前は任意
※実行条件は任意
![WS0051.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/1d01d152-2aef-b0b6-62e1-dbf19577961c.jpeg)
![WS0055.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/426a7f12-ea98-6c8e-6876-fccf9f0497f5.jpeg)
![WS0056.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/640068/07d595bd-df41-5065-8b54-b3d3988119d0.jpeg)


# 4　参考文献（偉大なる先人たち）
https://hacknote.jp/archives/6587/
https://qiita.com/shin1x1/items/0350db7a8c2752b99e5c
https://qiita.com/akikinyan/items/94c2a9dbe37fc90b2658
https://qiita.com/t-yasukawa/items/ae0386d028afea5cfb7a