SMTPが使えない環境(但しhttpなProxyは存在する)から、Zabbixでメール発報させてみたく、調べてみました。
(Zabbixは要らない場合、以下のhoge.shを適当に使って下さい)
準備する物
- SendGridのAPIキー
- 送信メアドのドメインのSendGrid認証
作成するスクリプト(hoge.sh)
- httpで直接外に出ることが可能な環境の場合
#!/bin/bash curl --request POST \ --url https://api.sendgrid.com/v3/mail/send \ -H "Authorization: Bearer [APIキー]" \ --header 'Content-Type: application/json' \ --data "{\"personalizations\": [{\"to\": [{\"email\": \"$1\"}]}],\"from\": {\"email\": \"[送信元メールアドレス]\"},\"subject\": \"$2\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$3\"}]}"
- Proxy配下の場合
#!/bin/bash curl --request POST \ --url https://api.sendgrid.com/v3/mail/send \ -H "Authorization: Bearer [APIキー]" \ --header 'Content-Type: application/json' \ --data "{\"personalizations\": [{\"to\": [{\"email\": \"$1\"}]}],\"from\": {\"email\": \"[送信元メールアドレス]\"},\"subject\": \"$2\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$3\"}]}" \ --proxy http://[ProxyサーバのIPアドレス]:[Proxyサーバのポート番号]
- 認証Proxy配下の場合
#!/bin/bash curl --request POST \ --url https://api.sendgrid.com/v3/mail/send \ -H "Authorization: Bearer [APIキー]" \ --header 'Content-Type: application/json' \ --data "{\"personalizations\": [{\"to\": [{\"email\": \"$1\"}]}],\"from\": {\"email\": \"[送信元メールアドレス]\"},\"subject\": \"$2\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$3\"}]}" \ --proxy http://[ProxyサーバのIPアドレス]:[Proxyサーバのポート番号] \ --proxy-user [Proxyサーバ用のログインID]:[Proxyサーバ用のログインパスワード]
data行に引数を反映する必要があるため、SendGridの公式サイトスクリプトのシングルクォートをダブルクォートに変更してあります。
記述例
| パラメータ名 | 値 | 
| APIキー | hoge.fuga.piyo | 
| ProxyサーバのIPアドレス | 192.168.0.1 | 
| Proxyサーバの待ち受けポート番号 | 10080 | 
| 認証ID | foo | 
| 認証パスワード | bar | 
| 送信元メールアドレス | alert@example.com | 
の場合、
curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer hoge.fuga.piyo' \
  --header 'Content-Type: application/json' \
  --data "{\"personalizations\": [{\"to\": [{\"email\": \"$1\"}]}],\"from\": {\"email\": \"alert@example.com\"},\"subject\": \"$2\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$3\"}]}" \
  --proxy http://192.168.0.1:10080 \
  --proxy-user foo:bar
となります。
作ったスクリプトは、
grep AlertScripts /etc/zabbix/zabbix_server.conf
で調べた場所に、実行権限を付けて保存しておきます。
Zabbix側の設定
名前と説明は適当に記述下さい。スクリプト名は、作成したスクリプトの名前に合わせて下さい。
これでZabbixからメールの発報が出来ると思います。
以上です。
 
