#!/bin/bash # WebHookのURL WEBHOOK_URL='https://hooks.slack.com/services/***************************' # メッセージを一時保存する場所 MESSAGEFILE=$(mktemp -t ssl-certificate-notice-XXXX) # 送信先のチャンネル CHANNEL=${CHANNEL:-'#lee_test'} # 終了時に削除 trap "rm ${MESSAGEFILE}" 0 send_notice_to_slack_renew_start () { # 見出し hd=${HEAD:-"start to ssl certificate renew.\n"} # json形式に整形 payload="payload={ \"channel\": \"${CHANNEL}\", \"text\": \"${hd}\" }" curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null } send_notice_to_slack_renew_success () { # 見出し hd=${HEAD:-"ssl certificate renew success.\n"} # 絵文字 emoji=${EMOJI:-':carlton:'} # json形式に整形 payload="payload={ \"channel\": \"${CHANNEL}\", \"icon_emoji\": \"${emoji}\", \"text\": \"${hd}\" }" curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null } send_notice_to_slack_renew_fail () { # 改行処理 cat ${MESSAGEFILE} | tr '\n' '\\' | sed 's/\\/\\n/g' > ${MESSAGEFILE} # 絵文字 emoji=${EMOJI:-':aaw_yeah:'} # 見出し hd=${HEAD:-"ssl certificate renew fail.\n"} # メッセージをシンタックスハイライト付きで取得 msg='```'`cat ${MESSAGEFILE}`'```' # json形式に整形 payload="payload={ \"channel\": \"${CHANNEL}\", \"icon_emoji\": \"${emoji}\", \"text\": \"${hd}${msg}\" }" curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null } send_notice_to_slack_renew_start /usr/bin/systemctl stop httpd.service /usr/bin/certbot renew --force-renew --quiet 2> ${MESSAGEFILE} RENEW_RESULT=$? /usr/bin/systemctl start httpd.service if [ ${RENEW_RESULT} -eq 0 ]; then send_notice_to_slack_renew_success else send_notice_to_slack_renew_fail fi