監視対象
- 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
で入るデフォルト)
- 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
以下の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
% sudo systemctl restart zabbix-agent.service
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
以下の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
% sudo systemctl restart zabbix-agent.service
Ubuntuではパッケージの更新により再起動が必要になった場合、以下の2ファイルが生成されます。
/var/run/reboot-required
/var/run/reboot-required.pkgs
これらのファイルの有無を調べることで再起動が必要であるかを判定できます。
以下の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
% sudo systemctl restart zabbix-agent.service
各サーバー毎にアイテムを作っても良いのですが、面倒なのでテンプレート化してしまいます。
デフォルトで入っている Template OS Linux
に追加しても良いのですが、標準で入っているものをいじるのはあまり良くないと思ったのでここでは別のテンプレートを作成します。
- Webで管理権限を持つユーザーでログインして、
Configuration > Templates
- 右上の
Create template
- フォーム埋めて
Add
- Template name:
Template Check Reboot Required
- Groups:
Templates
- Description:
Check whether reboot is required via zabbix-agent.
(必須ではない)
- Template name:
Template Check Reboot Required
のItems
- 右上の
Create item
- フォーム埋めて
Add
- Name:
Reboot is required
- Type:
Zabbix agent
- Key:
reboot.required
- Update interval:
1h
(お好みで) - History storage period:
4w
(お好みで) - Description:
Whether reboot is required.
(必須ではない)
- Name:
- 右上の
Create item
- フォーム埋めて
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.
(必須ではない)
- Name:
Triggers
- 右上の
Create trigger
- フォーム埋めて
Add
- Name:
Rebooting {HOST.NAME} is required
- Severity:
High
(お好みで) - Expression:
{Template Check Reboot Required:reboot.required.last()}=1
- Name:
Hosts
から監視対象を選択し、先程作ったTemplate Check Reboot Required
をリンク
CentOS 7でSELinuxがEnabledになっている場合、needs-restarting -r
の実行でエラーになるようです。