当前位置:网站首页>Ups and esxi realize automatic shutdown after power failure

Ups and esxi realize automatic shutdown after power failure

2022-06-24 05:47:00 Rokas. Yang

ESXi Is a well-known commercial virtualization software , Only a few advanced UPS To adapt to this kind of enterprise software , Ordinary UPS Power off , Script linkage is required , Once a... Is detected action, Trigger or delay the shutdown action (UPS It can last for dozens of minutes ), Avoid file systems 、 Hard disk 、 The power supply is damaged , Do more harm than good , Empathy , As long as a device with a script like function is plugged in UPS, Can achieve the function of power-off and automatic shutdown , and UPS The highly supported devices can be configured through the data communication line and background , Relatively convenient , Like a cloud of light NAS.

One 、 Turn on ESXI Of ssh function , and ssh Sign in ESXI establish ups Script

1. Enter into ESXI web Console , Turn on ssh function

2.ssh Connect to ESXI, Create necessary files and scripts

Script logic : Every time 1 minute ping Specify at one time IP, At a time ping once , If it reaches 2 Time , Three minutes later ping once , If it still doesn't work , Write the record time to the log ups.log, And execute the shutdown command , You can change the specific time and logic at will :

[[email protected]:~] cd /vmfs/volumes/data   #cd To the corresponding hard disk volume , The name of your hard drive shall prevail 
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187]mkdir ups
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187]cd ups
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]touch ups.log  # Write a log with 
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim ups.sh  # Create the following script 
#!/bin/sh 
UPS_LOG=/vmfs/volumes/data/ups/ups.log
count=0
IP=192.168.1.200   #  Write your gateway IP, As long as the power is off ping It doesn't work IP Will do 
while :;do
ping -c 1 $IP > /dev/null
if [ $? -eq 0 ]; then 
        :
else
        count=$(expr $count + 1);
fi

sleep 60

if [ $count -ge 2 ]; then
        echo "$(date) AC Power maybe off, checking again after 3 minutes ! " >> $UPS_LOG
        sleep 180
        ping -c 1 $IP &>/dev/null
        if [ "$?" -eq 0 ]; then
                echo "$(date) Checkagain, AC Power OK ! " >> $UPS_LOG
        else
                echo "$(date) AC Power is already off, shut down NAS Now! " >> $UPS_LOG
                /bin/shutdown.sh
                halt
        fi

else

        continue

fi
done
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]chmod +x ups.sh  # Just give me permission to execute , It is not necessary to 777
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]

3. Verify that the script takes effect

ESXI Of sh It's a link to busybox Of , Equivalent to a cropped version bash, So grammar cannot be combined with bash Exactly the same as :

The execution permission has been added before ,./ups.sh and sh ups.sh Will do , To see the script processing logic , Add one -x Parameters :

ping Yes. , therefore $_ The return code of the last command is 0, Benchmarking expression , Meet the conditions ,: Placeholders mean doing nothing , Sleep 60 I'll continue in seconds ping, This indicates that the script is normal , If you want to see the entire script processing logic ,ping An impassable IP that will do .

Two 、 Daemons and NOHUP

There are two ways , One is the daemon , One is nohup+ Background operation , Write to the startup script , Choose one of the two methods

1. The daemon mode

The so-called daemon , seeing the name of a thing one thinks of its function , Is to guard the process it wants to guard , How to achieve ? The simplest daemon is to check whether the process is running normally at a specified interval , Call the startup script or command to make the process run without running , Always guard its whole life cycle .

[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim ups_daemon.sh  # Write the following script 
#!/bin/sh

result=$(ps -c |awk '!/awk/&&/ups.sh/{print}'|wc -l)  # adopt awk and wc Determine whether the process is running 

if [ "${result}" -lt "1" ]; then

        /vmfs/volumes/data/ups/ups.sh &
        echo "$(date) UPS daemon process running" >> /vmfs/volumes/data/ups/ups.log

fi
exit 0
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]

Write to boot auto start :

[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]cat /etc/rc.local.d/local.sh
/bin/kill $(cat /var/run/crond.pid)
/bin/echo '*/3  *  *  *  *   /vmfs/volumes/data/ups/ups_daemon.sh' >> /var/spool/cron/crontabs/root
/bin/crond
exit 0
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]

ESXI Of crond After the process restarts, the scheduled tasks written will be cleared , Only the system's , Write the startup and auto startup script to ensure that each restart occurs at crond There are daemon tasks inside , Execute the daemon script every three minutes , That is, every three minutes ups.sh Whether the script is running properly .

2.NOHUP

Only for lazy people , One command is enough , Automatic operation after startup or restart :

[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]vim /etc/rc.local.d/local.sh # Write the following 
{ nohup sh /vmfs/volumes/data/ups/ups.sh; } &>/dev/null & #nohup Running scripts in the background , It is forbidden to output on the console stdout or stderr, Disable output of text 
[[email protected]:/vmfs/volumes/5f174c56-6a79f5cc-c990-a03e6ba0a187/ups]

After using this method, the script will not run until the next boot , In order not to restart, you can run it once :

{ nohup sh /vmfs/volumes/data/ups/ups.sh; } &>/dev/null &

3. Ensure that the process is running normally in the background

There is no need to go into details about this ,ps The command looks directly at , And others linux Distribution ps The input parameters are somewhat different :

Here we are , Whole UPS and ESXI Linkage has been completed , Repeated power-off test , Once and for all ; If you do it locally DDNS, Can cooperate with dnspod Of D Monitoring function , Once you feel it IP:PORT or URL cannot access , texting / Email alert , Truly realize the perception of power failure and recovery time at home thousands of miles away .

原网站

版权声明
本文为[Rokas. Yang]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210803222531782t.html