Open School Solutions

@OpenSchoolZ

How To Shut Down XenServer automatically With NUT

Our core infrastructure at the school is secured with a UPS. Unfortunately we always have power failures (mostly short ones) and with the help of our UPS we can at least bridge them a little. On our UPS hangs a Raspberry Pi, which is configured as a NUT server. If the power fails and the UPS switches to battery operation, the Raspberry Pi sends a command to all connected servers after a certain charging status. These then automatically shut down. All our virtual machines, backup systems and servers will be shut down. However, one essential server was still missing: the virtualization host. So today I would like to briefly describe how to shut down XenServer automatically by setting it up as a NUT client.

Installing the NUT Client

XenServer is based on CentOS, but the installation of third-party packages is not allowed. You should also be aware that you may no longer receive support (if you have a support contract) if you install additional packages.

In order to install the NUT client, we have to modify our XenServer 7 in the file /etc/yum.repos.d/CentOS-Base.repo. In the section [extras] we replaced $releaseserver in the line baseurl with a 7.

$ nano /ect/yum.repos.d/CentOS-Base.repo

[extras]
name=CentOS-$releasever — Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&rep$
baseurl=http://mirror.centos.org/centos/7/extras/$basearch/

Then we can set up the Epel Repo and install the NUT client:

$ yum clean all
$ yum — enablerepo=extras -y install epel-release

Now let’s see which package provides our NUT client and install it:

$ yum provides /usr/sbin/upsmon
Geladene Plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: mirrors.bangmodhosting.com
nut-2.7.2–3.el7.x86_64 : Network UPS Tools
Quelle : epel
Übereinstimmung von:
Dateiname : /usr/sbin/upsmon
$ yum install nut-client-2.7.2–3.el7.x86_64

NUT Client Setup

Now that the NUT client is installed, we need to configure it so that our XenServer shuts down in case of power failure. First we have to create a file to tell NUT that our XenServer is a client:

$ nano /etc/ups/nut.conf
MODE=netclient

The main configuration takes place in the file /etc/ups/upsmon.conf. There we change two lines:

$ nano /etc/ups/upsmon.conf

MONITOR ups@192.168.1.2 1 upsremote password slave

SHUTDOWNCMD "etc/ups/xen-shutdown.sh"

Depending on the configuration of the NUT master, the UPS name, the IP, as well as the user name and password must still be adapted here.

Before our XenServer shuts down, it must switch off all virtual machines first. I found the following script, which we download and make executable.

$ cd /etc/ups/
$ wget https://raw.githubusercontent.com/serrc-techops/NUT-Configuration/master/slave/xen/xen-shutdown.sh
$ chmod +x xen-shutdown.sh

So that the NUT client also starts automatically, we still have to set up a systemd service. We create the file /etc/systemd/system/nut-monitor.service and insert the following content:

$ nano /etc/systemd/system/nut-monitor.service
[Unit]
Description=Network UPS Tools — power device monitor and shutdown controller
After=local-fs.target network.target
[Service]
ExecStart=/usr/sbin/upsmon
PIDFile=/run/nut/upsmon.pid
Type=forking
[Install]
WantedBy=multi-user.target

Now we activate this service and start it:

$ systemctl enable nut-monitor.service
$ systemctl daemon-reload
$ systemctl start nut-monitor

With systemctl status nut-monitor we can see if everything is running:

nut-monitor.service — Network UPS Tools — power device monitor and shutdown controller
Loaded: loaded (/etc/systemd/system/nut-monitor.service; enabled; vendor preset: disabled)
Active: active (running) since Mi 2018–04–18 17:13:42 ICT; 1 day 12h ago
Process: 997 ExecStart=/usr/sbin/upsmon (code=exited, status=0/SUCCESS)
Main PID: 1001 (upsmon)
CGroup: /system.slice/nut-monitor.service
├─1000 /usr/sbin/upsmon
└─1001 /usr/sbin/upsmon

Testing Automatic Shutdown

First we can test the script to shut down the XenServer and see if all started VMs are shut down. To do this, we simply execute the script:

$ sh /etc/ups/xen-shutdown.sh

To simulate a power failure and we can issue the following command on the NUT Master:

$ sudo upsmon -c fsd

After that you have to switch on all devices manually or wake them up via Wake-on-Lan.

Conclusion

NUT is a great project and it allows us very flexible scenarios. Currently we have only one large UPS connected to the NUT Master, but it is also conceivable to connect several UPS to the NUT Master. In combination with Check_MK we can constantly monitor the status of the UPS and also get statistics on how often the power has failed.

Originally published at openschoolsolutions.org. Sign up to our newsletter to get access to a FREE PDF with great open source apps for your classroom or follow @OpenSchoolZ on Twitter.

More by Open School Solutions

Topics of interest

More Related Stories