Debian環境でacpidのログをsyslogに出力する2(acpi event & action)

acpidのイベントをすべてログに出力できるよう設定しました．

Debian環境でacpidのログをsyslogに出力する – matoken’s meme

しかしちょっと冗長です．ACアダプタの状況だけをロギングするよう設定してみました．

まずACアダプタの抜き差し時のログを確認します．

ACアダプタ切断時

Jul 11 23:05:51 yoga-260 acpid: completed netlink event "ac_adapter ACPI0003:00 00000080 00000000"

ACアダプタ接続時

Jul 11 23:05:55 yoga-260 acpid: received netlink event "ac_adapter ACPI0003:00 00000080 00000001"

manのEXAMPLE を確認します．

man acpidより

EXAMPLE
This example will shut down your system if you press the power button.

Create a file named /etc/acpi/events/power that contains the following:

event=button/power
action=/etc/acpi/power.sh "%e"

Then create a file named /etc/acpi/power.sh that contains the following:

/sbin/shutdown -h now "Power button pressed"

Now, when acpid is running, a press of the power button will cause the rule in /etc/acpi/events/power to trigger the script in
/etc/acpi/power.sh. The script will then shut down the system.

このEXAMPLEを真似してこんなファイルを作成しました．acpidから ac_adapter のイベント実行時にaction に指定されたscript を実行するようにしています．

/etc/acpi/events/ac_event_logging

event=ac_adapter
action=/etc/acpi/ac_event_logging.sh %e

そして呼び出されるscript として以下のファイルを用意．

/etc/acpi/ac_event_logging.sh

#!/bin/sh
case "$4" in
00000000)
echo 'AC offline'
logger -t acpid AC offline
;;
00000001)
echo 'AC online'
logger -t acpid AC online
;;
esac

このscriptには実行権も付与します．

$ sudo chmod +x /etc/acpi/ac_event_logging.sh

動作を確認するために acpid を再起動します．

$ sudo service acpid restart

この状態でACアダプタを抜き差しするとこんな感じのログが出力されます．

$ sudo tail -f /var/log/syslog | grep acpid:

Jul 11 23:51:10 yoga-260 acpid: received netlink event "battery PNP0C0A:00 00000080 00000001"
Jul 11 23:51:10 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:10 yoga-260 acpid: completed netlink event "battery PNP0C0A:00 00000080 00000001"
Jul 11 23:51:10 yoga-260 acpid: received netlink event "ac_adapter ACPI0003:00 00000080 00000000" (1)
Jul 11 23:51:10 yoga-260 acpid: rule from /etc/acpi/events/ac_event_logging matched (2)
Jul 11 23:51:10 yoga-260 acpid: executing action "/etc/acpi/ac_event_logging.sh ac_adapter ACPI0003:00 00000080 00000000" (3)
Jul 11 23:51:10 yoga-260 acpid: AC offline (4)
Jul 11 23:51:10 yoga-260 acpid: action exited with status 0 (5)
Jul 11 23:51:10 yoga-260 acpid: 1 total rule matched
Jul 11 23:51:10 yoga-260 acpid: completed netlink event "ac_adapter ACPI0003:00 00000080 00000000"
Jul 11 23:51:11 yoga-260 acpid: received netlink event "ibm/hotkey LEN0068:00 00000080 00006030"
Jul 11 23:51:11 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:11 yoga-260 acpid: completed netlink event "ibm/hotkey LEN0068:00 00000080 00006030"
Jul 11 23:51:11 yoga-260 acpid: received netlink event "thermal_zone LNXTHERM:00 00000081 00000000"
Jul 11 23:51:11 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:11 yoga-260 acpid: completed netlink event "thermal_zone LNXTHERM:00 00000081 00000000"
Jul 11 23:51:12 yoga-260 acpid: received netlink event "battery PNP0C0A:00 00000080 00000001"
Jul 11 23:51:12 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:12 yoga-260 acpid: completed netlink event "battery PNP0C0A:00 00000080 00000001"
Jul 11 23:51:13 yoga-260 acpid: received netlink event "ac_adapter ACPI0003:00 00000080 00000001"
Jul 11 23:51:13 yoga-260 acpid: rule from /etc/acpi/events/ac_event_logging matched
Jul 11 23:51:13 yoga-260 acpid: executing action "/etc/acpi/ac_event_logging.sh ac_adapter ACPI0003:00 00000080 00000001"
Jul 11 23:51:13 yoga-260 acpid: AC online
Jul 11 23:51:13 yoga-260 acpid: action exited with status 0
Jul 11 23:51:13 yoga-260 acpid: 1 total rule matched
Jul 11 23:51:13 yoga-260 acpid: completed netlink event "ac_adapter ACPI0003:00 00000080 00000001"
Jul 11 23:51:13 yoga-260 acpid: received netlink event "ibm/hotkey LEN0068:00 00000080 00006030"
Jul 11 23:51:13 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:13 yoga-260 acpid: completed netlink event "ibm/hotkey LEN0068:00 00000080 00006030"
Jul 11 23:51:13 yoga-260 acpid: received netlink event "thermal_zone LNXTHERM:00 00000081 00000000"
Jul 11 23:51:13 yoga-260 acpid: 0 total rules matched
Jul 11 23:51:13 yoga-260 acpid: completed netlink event "thermal_zone LNXTHERM:00 00000081 00000000"

ACアダプタ切断のログ

ACアダプタ用のeventを見つける

ACアダプタロギング用のscriptを%e を使って引数を渡しつつ実行

ロギング用のscriptでログを出力

action終了

うまく行ったようです :)

ということで他のイベントログを要らないので， /etc/default/acpid を編集して引数を消します．

--- a/default/acpid
+++ b/default/acpid
@@ -1,7 +1,7 @@
# Options to pass to acpid
#
# OPTIONS are appended to the acpid command-line
-OPTIONS="-l"
+#OPTIONS="-l"

# Linux kernel modules to load before starting acpid
#

反映するために acpid を再起動します．

$ sudo service acpid restart
$ ps aux | grep [/]sbin/acpid
root 3990040 0.0 0.0 2352 780 ? Ss 23:59 0:00 /usr/sbin/acpid

再度ログを確認しながらACアダプタの抜き差しをしてログが必要なものだけ出力されているのを確認しました．

sudo tail -f /var/log/syslog | grep acpid:

Jul 12 00:00:04 yoga-260 acpid: AC offline
Jul 12 00:00:07 yoga-260 acpid: AC online

良さそうです :)
サスペンドレジュームも設定してみようかな?

環境

$ dpkg-query -W acpid rsyslog systemd bsdutils
acpid 1:2.0.32-1
bsdutils 1:2.36.1-7
rsyslog 8.2102.0-2
systemd 247.3-5
$ lsb_release -dr
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
$ arch
x86_64
$ sudo lshw | grep -E '^\ \ \ \ product:|^\ \ \ \ version:'
product: 20FEA02WJP (LENOVO_MT_20FE_BU_Think_FM_ThinkPad Yoga 260)
version: ThinkPad Yoga 260