ZabbixでCloudWatchの値を監視する

## zabbix-agent側の設定
### zabbixユーザー用の.bash_profile(無ければ作る)に下記を追加
```
vi /var/lib/zabbix/.bash_profile
-----
export AWS_ACCESS_KEY_ID=**********
export AWS_SECRET_ACCESS_KEY=**********
export AWS_DEFAULT_REGION=**********
-----
```
### ELB(ALB) 「ELB_5XX」の監視
#### 1.監視用スクリプト
```
vi /var/lib/zabbix/HTTPCode_ELB_5XX_Count.sh
-----------------------------------------
#!/bin/bash

source /var/lib/zabbix/.bash_profile

START_DATE=`date "+%Y-%m-%dT%H:%M:00Z" -d '6 minutes ago' --utc`
END_DATE=`date "+%Y-%m-%dT%H:%M:59Z" -d '1 minutes ago' --utc`

error_count=$(aws cloudwatch get-metric-statistics \
--namespace AWS/ApplicationELB \
--metric-name HTTPCode_ELB_5XX_Count \
--statistics Sum \
--period 60 \
--dimensions Name=LoadBalancer,Value=app/*****/***** \
--start-time $START_DATE \
--end-time $END_DATE \
--query "reverse(sort_by(Datapoints,&Timestamp))[?Sum>\`0\`][Sum]" \
--output text | head -n 1)

if [ -z "$error_count" ]; then
	error_count=0
fi

echo -n $error_count
-----------------------------------------
```
※直近5分間の最大値を取得
※--dimensionsのValueは環境に合わせて変更

#### 2.zabbix-agentの設定
```
vi /etc/zabbix/zabbix_agentd.d/userparameter_HTTPCode_ELB_5XX_Count.conf
-----------------------------------------
UserParameter=HTTPCode_ELB_5XX_Count.HTTPCode_ELB_5XX_Count,HOME=/var/lib/zabbix /bin/bash /var/lib/zabbix/HTTPCode_ELB_5XX_Count.sh
-----------------------------------------
```

### RDS 「BurstBalance」の監視
#### 1.監視用スクリプト
```
vi /var/lib/zabbix/RDS_BurstBalance_Average.sh
-----------------------------------------
#!/bin/bash

source /var/lib/zabbix/.bash_profile

START_DATE=`date "+%Y-%m-%dT%H:%M:00Z" -d '6 minutes ago' --utc`
END_DATE=`date "+%Y-%m-%dT%H:%M:59Z" -d '1 minutes ago' --utc`

average_percent=$(aws cloudwatch get-metric-statistics \
--namespace AWS/RDS \
--metric-name BurstBalance \
--statistics Average \
--period 300 \
--dimensions Name=DBInstanceIdentifier,Value=***** \
--start-time $START_DATE \
--end-time $END_DATE \
--output text | awk '{print $2}')

if [ -z "$average_percent" ]; then
	average_percent=99
fi

echo -n $average_percent
-----------------------------------------
```
※直近5分間の平均値を取得
※--dimensionsのValueは環境に合わせて変更

#### 2.zabbix-agentの設定
```
vi /etc/zabbix/zabbix_agentd.d/userparameter_RDS_BurstBalance_Average.conf
-----------------------------------------
UserParameter=RDS_BurstBalance_Average.RDS_BurstBalance_Average,HOME=/var/lib/zabbix /bin/bash /var/lib/zabbix/RDS_BurstBalance_Average.sh
-----------------------------------------
```

### RDS 「CPUUtilization」の監視
#### 1.監視用スクリプト
```
vi /var/lib/zabbix/RDS_CPUUtilization_Average.sh
-----------------------------------------
#!/bin/bash

source /var/lib/zabbix/.bash_profile

START_DATE=`date "+%Y-%m-%dT%H:%M:00Z" -d '6 minutes ago' --utc`
END_DATE=`date "+%Y-%m-%dT%H:%M:59Z" -d '1 minutes ago' --utc`

average_percent=$(aws cloudwatch get-metric-statistics \
--namespace AWS/RDS \
--metric-name CPUUtilization \
--statistics Average \
--period 300 \
--dimensions Name=DBInstanceIdentifier,Value=***** \
--start-time $START_DATE \
--end-time $END_DATE \
--output text | awk '{print $2}')

if [ -z "$average_percent" ]; then
	average_percent=0
fi

echo -n $average_percent
-----------------------------------------
```
※直近5分間の平均値を取得
※--dimensionsのValueは環境に合わせて変更

#### 2.zabbix-agentの設定
```
vi /etc/zabbix/zabbix_agentd.d/userparameter_RDS_CPUUtilization_Average.conf
-----------------------------------------
UserParameter=RDS_CPUUtilization_Average.RDS_CPUUtilization_Average,HOME=/var/lib/zabbix /bin/bash /var/lib/zabbix/RDS_CPUUtilization_Average.sh
-----------------------------------------
```

### RDS 「DatabaseConnections」の監視
#### 1.監視用スクリプト
```
vi /var/lib/zabbix/RDS_DatabaseConnections_Maximum.sh
-----------------------------------------
#!/bin/bash

source /var/lib/zabbix/.bash_profile

START_DATE=`date "+%Y-%m-%dT%H:%M:00Z" -d '6 minutes ago' --utc`
END_DATE=`date "+%Y-%m-%dT%H:%M:59Z" -d '1 minutes ago' --utc`

maximum=$(aws cloudwatch get-metric-statistics \
--namespace AWS/RDS \
--metric-name DatabaseConnections \
--statistics Maximum \
--period 60 \
--dimensions Name=DBInstanceIdentifier,Value=***** \
--start-time $START_DATE \
--end-time $END_DATE \
--query "reverse(sort_by(Datapoints,&Timestamp))[?Maximum>\`0\`][Maximum]" \
--output text  | head -n 1)

if [ -z "$maximum" ]; then
	maximum=0
fi

echo -n $maximum
-----------------------------------------
```
※直近5分間の最大値を取得
※--dimensionsのValueは環境に合わせて変更

#### 2.zabbix-agentの設定
```
vi /etc/zabbix/zabbix_agentd.d/userparameter_RDS_DatabaseConnections_Maximum.conf
-----------------------------------------
UserParameter=RDS_DatabaseConnections_Maximum.RDS_DatabaseConnections_Maximum,HOME=/var/lib/zabbix /bin/bash /var/lib/zabbix/RDS_DatabaseConnections_Maximum.sh
-----------------------------------------
```
### オーナーを変更
```
chown -R zabbix:zabbix /var/lib/zabbix
```
### zabbix-agentの再起動
```
systemctl restart zabbix-agent
```
## zabbixの設定
### アイテム(HTTPCode_ELB_5XX_Count)の追加
![2023-10-30 13_48_01-.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/bc8dd545-cf46-2f06-9ee4-27135ee00cf1.png)
### アイテム(RDS_BurstBalance_Average)の追加
![2023-10-30 13_09_20-zabbix.ca-ret.co.jp_ アイテムの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/a5c2a72e-b38a-6472-c98c-3c7fe0930f2a.png)
### アイテム(RDS_CPUUtilization_Average)の追加
![2023-10-30 13_10_43-zabbix.ca-ret.co.jp_ アイテムの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/92e11342-254c-2055-bcd1-f3a18174b9a0.png)
### アイテム(RDS_DatabaseConnections_Maximum)の追加
![2023-10-30 13_11_47-zabbix.ca-ret.co.jp_ アイテムの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/723a71c7-510d-6517-8c0d-7237bc9e7df4.png)

### トリガー(HTTPCode_ELB_5XX_Count)の追加
![2023-10-30 13_13_07-zabbix.ca-ret.co.jp_ トリガーの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/2394e1ca-a2c3-878b-d6f9-a8de37063d37.png)
※最新の取得値が0より大きければアラートを出す

### トリガー(RDS_BurstBalance_Average)の追加
![2023-10-30 13_14_27-zabbix.ca-ret.co.jp_ トリガーの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/ded10b2a-0135-7b01-d765-28414e8dd3be.png)
※最新の取得値が50(%)未満になったらアラートを出す

### トリガー(RDS_CPUUtilization_Average)の追加
![2023-10-30 13_18_03-zabbix.ca-ret.co.jp_ トリガーの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/ce1544fe-2526-bfb2-9794-76c822fb0bb4.png)
※最新の取得値が60(%)を超えたらアラートを出す

### トリガー(RDS_CPUUtilization_Average)の追加
![2023-10-30 13_19_25-zabbix.ca-ret.co.jp_ トリガーの設定 — Mozilla Firefox.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2726042/373481eb-e541-27dd-6498-ff6c8f70962e.png)
※最新の取得値が2000を超えたらアラートを出す