Skip to content

Instantly share code, notes, and snippets.

@yt-siden
Last active November 17, 2020 07:02
Show Gist options
  • Save yt-siden/17e7a11c24d035dc9308b8f42296c753 to your computer and use it in GitHub Desktop.
Save yt-siden/17e7a11c24d035dc9308b8f42296c753 to your computer and use it in GitHub Desktop.

Zabbixで監視しているLinuxマシンの再起動が必要かどうかをチェックする方法

はじめに

監視対象

  • CentOS 7
  • Ubuntu 16.04

※それぞれRHEL/Fedora, Debian系その他でも大丈夫だと思われます。 (テストはしてません)

zabbix-agentのユーザーパラメータを追加して監視対象のシステムの再起動が必要かを監視します。 本記事では監視対象のアイテムのキーを以下の通りとします。

  • reboot.required
    • 0/1: 再起動が必要でない/である
  • reboot.pkgs
    • 起因するパッケージのリスト

テスト環境

  • Zabbix server 4.0.19 on CentOS 7
  • Zabbix agent
    • CentOS 7: 4.0.19
    • Ubuntu 16.04: 1:2.4.7+dfsg-2ubuntu2.1 (apt で入るデフォルト)

zabbix-agent側の設定

CentOS 7の場合

yum-utils パッケージに含まれている needs-restarting コマンドを使用することで再起動が必要かどうか判定することができます。 また、同コマンドは再起動が必要な場合はどのパッケージが再起動を必要としているかの情報を出力するので、そこから起因するパッケージリストを生成します。

例. 再起動が必要ない場合

% needs-restarting -r
No core libraries or services have been updated.
Reboot is probably not necessary.

% echo $?
0

例. 再起動が必要な場合

% needs-restarting -r
Core libraries or services have been updated:
  kernel -> 3.10.0-1062.18.1.el7
  systemd -> 219-67.el7_7.4

Reboot is required to ensure that your system benefits from these updates.

More information:
https://access.redhat.com/solutions/27943

% echo $?
1

1. /etc/zabbix/zabbix_agentd.conf を編集

以下の2行を追加。

UserParameter=reboot.required,needs-restarting -r > /dev/null; echo $? | tr -d "\n"
UserParameter=reboot.pkgs,if [ `needs-restarting -r | grep '\->' | wc -l` -gt 0 ]; then needs-restarting -r | grep '\->' | awk '{print $1}' | tr "\n" , | sed s/,$//; else echo "none" | tr -d "\n"; fi

2. zabbix-agent を再起動

% sudo systemctl restart zabbix-agent.service

CentOS 8の場合

CentOS 8ではCentOS 7のneeds-restartingコマンドがdnfに統合されて、dnf needs-restartingでほぼ同じことができるようになりました。

例. 再起動が必要でない場合

% dnf needs-restarting -r
No core libraries or services have been updated since boot-up.
Reboot should not be necessary.
% dnf needs-restarting -r > /dev/null; echo $?
0

例. 再起動が必要な場合

% dnf needs-restarting -r
Core libraries or services have been updated since boot-up:
  * kernel

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943
% dnf needs-restarting -r > /dev/null; echo $?
1

1. /etc/zabbix/zabbix_agentd.conf を編集

以下の2行を追加。

UserParameter=reboot.required,dnf needs-restarting -r > /dev/null; echo $? | tr -d "\n"
UserParameter=reboot.pkgs,if [ `needs-restarting -r | grep '*' | wc -l` -gt 0 ]; then needs-restarting -r | grep '*' | awk '{print $2}' | tr "\n" , | sed s/,$//; else echo "none" | tr -d "\n"; fi

2. zabbix-agent を再起動

% sudo systemctl restart zabbix-agent.service

Ubuntu 16.04の場合

Ubuntuではパッケージの更新により再起動が必要になった場合、以下の2ファイルが生成されます。

  • /var/run/reboot-required
  • /var/run/reboot-required.pkgs

これらのファイルの有無を調べることで再起動が必要であるかを判定できます。

1. /etc/zabbix/zabbix_agentd.conf を編集

以下の2行を追加。

UserParameter=reboot.required,test ! -e /var/run/reboot-required; echo $? | tr -d "\n"
UserParameter=reboot.pkgs,if [ -e /var/run/reboot-required.pkgs ]; then cat /var/run/reboot-required.pkgs | tr "\n" , | sed 's/,$//'; else echo "none" | tr -d "\n"; fi

2. zabbix-agent を再起動

% sudo systemctl restart zabbix-agent.service

サーバー側でitemを作成

各サーバー毎にアイテムを作っても良いのですが、面倒なのでテンプレート化してしまいます。 デフォルトで入っている Template OS Linux に追加しても良いのですが、標準で入っているものをいじるのはあまり良くないと思ったのでここでは別のテンプレートを作成します。

  1. Webで管理権限を持つユーザーでログインして、 Configuration > Templates
  2. 右上の Create template
  3. フォーム埋めて Add
    • Template name: Template Check Reboot Required
    • Groups: Templates
    • Description: Check whether reboot is required via zabbix-agent. (必須ではない)
  4. Template Check Reboot RequiredItems
  5. 右上の Create item
  6. フォーム埋めて Add
    • Name: Reboot is required
    • Type: Zabbix agent
    • Key: reboot.required
    • Update interval: 1h (お好みで)
    • History storage period: 4w (お好みで)
    • Description: Whether reboot is required. (必須ではない)
  7. 右上の Create item
  8. フォーム埋めて Add
    • Name: Updated core packages
    • Type: Zabbix agent
    • Key: reboot.pkgs
    • Type of Information: Text
    • Update interval: 1h (お好みで)
    • History storage period: 4w (お好みで)
    • Description: List of updated packages which make reboot of the system required. (必須ではない)
  9. Triggers
  10. 右上の Create trigger
  11. フォーム埋めて Add
    • Name: Rebooting {HOST.NAME} is required
    • Severity: High (お好みで)
    • Expression: {Template Check Reboot Required:reboot.required.last()}=1
  12. Hosts から監視対象を選択し、先程作った Template Check Reboot Required をリンク

要対応

CentOS 7でSELinuxがEnabledになっている場合、needs-restarting -r の実行でエラーになるようです。

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment