安価な UPS には検出ポートがないため、停電後にサーバーをシャットダウンするには手動介入が必要です。このスクリプトは、停電を自動的に検出し、サーバーをシャットダウンできます。
このスクリプトは Linux システムのみをサポートします。
動作原理
動作にはリファレンスデバイスが必要です。参照デバイスには IP アドレスがあり、UPS システムの外部に接続されている必要があります。停電後はIPアドレスが失われます。サーバーが ping に失敗した場合、指定された時間内にサーバーは自動的にシャットダウンします。
crontab のタイミング設定
crontab は root に設定する必要があります。他のユーザーにはコンピューターをシャットダウンする権限がありません。
1
2
|
# m h dom mon dow command
* * * * * /root/detect_ups.sh >> /var/log/detect_ups.log
|
- * * * * * は 1 分ごとにテストすることを意味します
- /root/detect_ups.sh はスクリプトの場所を示します
検出スクリプト
/root/detect_ups.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#!/bin/bash
me="$(basename "$0")";
running=$(ps h -C "$me" | grep -wv $$ | wc -l);
[[ $running > 1 ]] && exit;
detect_ip=192.168.1.1
ping_count=1
detect_delay=300
echo " detect ip:=$detect_ip"
echo " detect delay:=$detect_delay sec"
ping -c ${ping_count} ${detect_ip} > /dev/NULL
if [ $? -eq 0 ]
then
echo ' AC Power OK !'
else
echo " AC Power maybe off, checking again after ${detect_delay} second !"
sleep ${detect_delay} #延时秒
ping -c ${ping_count} ${detect_ip} > /dev/NULL
if [ $? -eq 0 ]
then
echo ' AC Power recover, return !'
else
echo ' AC Power always off, poweroff !'
/usr/sbin/poweroff
fi
fi
|