0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Debian / Ubuntu: unattended-upgrades + Webhook通知 + 再起動後デーモン確認 設定手順

0
Posted at

最近DebianにUnattended upgrades設定をする機会があり、備忘録としてAIに手順書を作ってもらいました。

UbuntuだとUnattended upgradesはデフォルトで設定済みなので、Debianで同等の設定をする手順が前半、後半はUbuntu/Debian共通のWebhook通知や自動再起動までする設定手順となっています。


Debian / Ubuntu: unattended-upgrades + Webhook通知 + 再起動後デーモン確認 設定手順

Debian / Ubuntu サーバー環境において、セキュリティパッチの自動適用に加えて、更新結果のWebhook通知(Google Chat / Slack / Teams)と再起動後の主要デーモン起動確認を行うための手順書です。

前提の整理

unattended-upgrades パッケージ自体(セキュリティ更新の自動適用機能)は、環境によって導入状況が異なります。

環境 デフォルト状態
Ubuntu Server / Desktop 16.10以降、デフォルトでインストール・有効化済み
Debian (GNOMEデスクトップ) Debian 9以降、デフォルトでインストール済み
Debian (サーバー最小構成) デフォルトでは未インストール。手動導入が必要

一方、この手順書の主な価値であるWebhook通知と再起動後のデーモン起動確認は、どの環境にもデフォルトでは存在しません。そのため以下の手順は、Ubuntu / Debian のどちらであっても、これらの機能を追加したい場合に共通で必要になります。


1. ベース確認・パッケージインストール

環境を問わず、まず以下を実行して前提パッケージを揃えます(Ubuntuで実行してもすでに導入済みであれば無害です)。

sudo apt update
sudo apt install -y unattended-upgrades jq curl

# 有効化状態の確認・設定
sudo dpkg-reconfigure -plow unattended-upgrades
  • unattended-upgrades: セキュリティ更新の自動適用本体(Debianサーバーで特に必須)
  • jq / curl: 後述のWebhook通知スクリプトが依存するツール(OSを問わず必須)

1.1. unattended-upgrades の基本設定

設定ファイル: /etc/apt/apt.conf.d/50unattended-upgrades

// 不要な依存パッケージの自動削除
Unattended-Upgrade::Remove-Unused-Dependencies "true";

// 必要に応じて自動再起動を有効化(任意・運用ポリシーに応じて設定)
// Unattended-Upgrade::Automatic-Reboot "true";
// Unattended-Upgrade::Automatic-Reboot-Time "03:00";

2. Webhook通知 + 再起動後デーモン確認(共通コア部分)

ここからが本手順書の本体です。Ubuntu / Debian 共通でそのまま利用できます。

2.1. 共通設定ファイル

通知先URLと、確認したいデーモン一覧を一元管理します。

sudo nano /etc/unattended-upgrades-webhook.conf
# Webhook URL
WEBHOOK_URL="https://chat.googleapis.com/v1/spaces/YOUR_WEBHOOK_URL"

# 通知先サービス (google_chat / slack / teams)
SERVICE_TYPE="google_chat"

# 再起動後に起動確認したいデーモンのリスト(スペース区切り)
CHECK_SERVICES=("nginx" "postgresql" "docker")

2.2. 更新結果通知スクリプト

sudo nano /usr/local/bin/send-webhook.sh
#!/bin/bash

source /etc/unattended-upgrades-webhook.conf

LOG_FILE="/var/log/unattended-upgrades/unattended-upgrades.log"
FLAG_FILE="/var/log/unattended-upgrades/reboot-pending.flag"

[ ! -f "$LOG_FILE" ] && exit 0

RECENT_LOG=$(tail -n 50 "$LOG_FILE" | grep -E "Packages that were upgraded:|All upgrades installed|Error:")

if [ -n "$RECENT_LOG" ]; then
    HOSTNAME=$(hostname)

    REBOOT_MSG=""
    if [ -f "/var/run/reboot-required" ]; then
        touch "$FLAG_FILE"
        REBOOT_MSG="\n⚠️ *OSの再起動が必要です。再起動後にサービス状態を報告します。*"
    fi

    case "$SERVICE_TYPE" in
        "google_chat"|"slack")
            MESSAGE="*[${HOSTNAME}]* unattended-upgrades 実行結果:\n\`\`\`\n${RECENT_LOG}\n\`\`\`${REBOOT_MSG}"
            PAYLOAD=$(jq -n --arg text "$MESSAGE" '{text: $text}')
            ;;
        "teams")
            MESSAGE="**[${HOSTNAME}]** unattended-upgrades 実行結果:\n\n\`\`\`text\n${RECENT_LOG}\n\`\`\`${REBOOT_MSG}"
            PAYLOAD=$(jq -n --arg text "$MESSAGE" '{text: $text}')
            ;;
    esac

    curl -s -H "Content-Type: application/json" -X POST -d "$PAYLOAD" "$WEBHOOK_URL" > /dev/null
fi
sudo chmod +x /usr/local/bin/send-webhook.sh

2.3. 再起動後デーモン確認スクリプト

sudo nano /usr/local/bin/check-services-post-boot.sh
#!/bin/bash

FLAG_FILE="/var/log/unattended-upgrades/reboot-pending.flag"

[ ! -f "$FLAG_FILE" ] && exit 0

sleep 20

source /etc/unattended-upgrades-webhook.conf

HOSTNAME=$(hostname)
STATUS_REPORT=""
HAS_ERROR=0

for SERVICE in "${CHECK_SERVICES[@]}"; do
    if systemctl is-active --quiet "$SERVICE"; then
        STATUS_REPORT="${STATUS_REPORT}・${SERVICE}: ✅ active (起動中)\n"
    else
        STATUS_REPORT="${STATUS_REPORT}・${SERVICE}: ❌ inactive/failed (異常・停止)\n"
        HAS_ERROR=1
    fi
done

HEADER="*[${HOSTNAME}]* OS再起動後のサービス稼働状態レポート:"
[ $HAS_ERROR -eq 1 ] && HEADER="*[${HOSTNAME}]* ⚠️ 【警告】OS再起動後に停止しているサービスがあります:"

FULL_MSG="${HEADER}\n${STATUS_REPORT}"

PAYLOAD=$(jq -n --arg text "$FULL_MSG" '{text: $text}')
curl -s -H "Content-Type: application/json" -X POST -d "$PAYLOAD" "$WEBHOOK_URL" > /dev/null

rm -f "$FLAG_FILE"
sudo chmod +x /usr/local/bin/check-services-post-boot.sh

2.4. systemd への登録

自動更新終了時のフック:

sudo systemctl edit unattended-upgrades.service
[Service]
ExecStopPost=/usr/local/bin/send-webhook.sh

再起動後チェック用のユニット:

sudo nano /etc/systemd/system/unattended-upgrades-postboot.service
[Unit]
Description=Check services status after unattended-upgrades reboot
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-services-post-boot.sh

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable unattended-upgrades-postboot.service

3. (任意)自動再起動の有効化

運用ポリシー上、パッチ適用後に自動で再起動させたい場合のみ、セクション1.1のコメントアウトを解除してください。無停止運用が必須なサーバーでは有効化しないことを推奨します。

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";

4. 動作検証

4.1. デーモン確認スクリプトの単体テスト

sudo touch /var/log/unattended-upgrades/reboot-pending.flag
sudo /usr/local/bin/check-services-post-boot.sh

各チャットツールに ✅ active マーク付きでサービス一覧が届くことを確認します。

4.2. ログ確認コマンド

  • 更新ログ: /var/log/unattended-upgrades/unattended-upgrades.log
  • 再起動後チェックログ: journalctl -u unattended-upgrades-postboot.service
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?