LoginSignup
11
6

More than 5 years have passed since last update.

APCのUPSを監視するAPCUPSDからSlackに通知する

Last updated at Posted at 2017-01-31

背景

先日、OfficeにDeveloperが自由に使える開発用サーバを建てました。

image
image
image
image

バックアップ電源にAPCのUPSも同時に購入してCentOS7にAPCUPSDをインストールしていますが、もし何らかの電源異常を検知したときにroot宛にメール送信するようになってはいるのですが今の御時世メールっていらないのでは?Slackに通知を飛ばしたいなと思ったワケです。

前提

  • CentOS7にAPCUPSDをインストール済

設定ファイルたち

下記にみつけました。

[tokifuji@office ~]$ ll /etc/apcupsd/
合計 52
drwxr-xr-x   2 root root   121 12月 21 18:26 .
drwxr-xr-x. 85 root root  8192 12月 20 20:31 ..
-rwxr-xr-x   1 root root  4029 12月 20 20:27 apccontrol
-rw-r--r--   1 root root 13242 12月 20 20:32 apcupsd.conf
-rwxr--r--   1 root root   680 12月 21 18:26 changeme
-rwxr--r--   1 root root   707 12月 21 18:24 commfailure
-rwxr--r--   1 root root   708 12月 21 18:25 commok
-rwxr--r--   1 root root   679 12月 21 18:23 offbattery
-rwxr--r--   1 root root   644 12月 21 18:23 onbattery

上記の中でも通知系のモノは下記になろうかと思いました。

-rwxr--r--   1 root root   680 12月 21 18:26 changeme
-rwxr--r--   1 root root   707 12月 21 18:24 commfailure
-rwxr--r--   1 root root   708 12月 21 18:25 commok
-rwxr--r--   1 root root   679 12月 21 18:23 offbattery
-rwxr--r--   1 root root   644 12月 21 18:23 onbattery

onbatteryとかの中身を見てみる

大体同じような内容みたいです。ステータスに応じてメールで送信される内容とかが設定されています。中身はshellscript(bash)であることがわかります。

/etc/apcupsd/onbattery
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the UPS
# goes on batteries.
# We send an email message to root to notify him.
#

HOSTNAME=`hostname`
MSG="$HOSTNAME UPS $1 Power Failure !!!"
#
(
   echo "Subject: $MSG"
   echo " "
   echo "$MSG"
   echo " "
   /sbin/apcaccess status
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
exit 0

onbatteryにSlackへ通知を飛ばす処理を追加する

お、これはもしやcurlコマンド叩けばSlackに通知飛ばせるのでは?と思ったワケです。こんな感じでしょうか?incoming webhookはご自身で取得していただければと…それとChannelとか表示名とかは用途に合わせてくださいね。

/etc/apcupsd/onbattery
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the UPS
# goes on batteries.
# We send an email message to root to notify him.
#

HOSTNAME=`hostname`
MSG="$HOSTNAME UPS $1 Power Failure !!!"
#
(
   echo "Subject: $MSG"
   echo " "
   echo "$MSG"
   echo " "
   /sbin/apcaccess status
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN

curl -X POST --data-urlencode "payload={ \
  \"channel\": \"#infra\", \
  \"username\": \"APCUPSDさん\", \
  \"text\": \"${MSG}\", \
  \"icon_emoji\": \":elevennines:\" \
}" https://hooks.slack.com/services/******/*********/****************

exit 0

onbatteryをテストしてみる

ということでUPSのコンセントをえいっ!!!と抜いてやりました。しばらくすると・・・
image

お、上手く動きましたね!他のファイルも同様に修正すると良いことがあるかもしれませんね。

-rwxr--r--   1 root root   680 12月 21 18:26 changeme     # ←これとか
-rwxr--r--   1 root root   707 12月 21 18:24 commfailure  # ←これとか
-rwxr--r--   1 root root   708 12月 21 18:25 commok       # ←これとか
-rwxr--r--   1 root root   679 12月 21 18:23 offbattery   # ←これとか
-rwxr--r--   1 root root   644 12月 21 18:23 onbattery

これであなたも快適APCUPSDライフ(ってなんだよw)をエンジョイしてください。それではまた:raised_hand:

11
6
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
11
6