Network UPS Tools (NUT) on CentOS and Windows with sending SMS via smstools + playsms

Given the limited budget of a small company, the modest terminal server 2003 received uninterrupted power from the Ippon Smart Winner 1500, which was controlled by a standard utility. I can’t say something very bad about this UPS and its monstrous control utility with animation of current. It buzzes completely with its money.

Over time, windows had to burden a couple of Vmware virtual machines, and the park was replenished with a couple of small Linux servers with PBXs, Jabber and other office pleasures. Accordingly, there was a desire to also manage their power from Ippon, because * nix support was announced.

However, although the “adult” manufacturer has provided many advanced management capabilities, including clients for * nix and mac, SMS and email alerts, etc., attempts to put all this into practice are painful, and the result is unpredictable.

As all roads lead to Rome, so problems with Ippon lead to NUT . The network has a ton of information about this wonderful utility, including and on Habré there are examples of setup .
Under * nix, working with NUT is a pleasure. Simple, understandable, logical.

I inserted the Teleofis-RX104 GSM-modem into the CentOS server directly connected to the UPS via USB and made friends with smstools3 and playsmsto get an impromptu sms gateway with support for webservices api. As a result, we got a convenient universal tool for all sorts of alerts, including and office work.

Next, I installed the ready-made NUT from the EPEL repository, writing in the configs:

/etc/nut/nut.conf
MODE=netserver

/etc/nut/ups.conf
[winner1500]
driver = blazer_usb
port = /dev/usb/hiddev0
bus = "003"
port = "002"
vendorid = 06da
productid = 0003
desc = "Ippon Smart Winner 1500"
default.battery.voltage.high = 55
default.battery.voltage.low = 47
default.battery.voltage.nominal = 48

Information about ports and vendor / product from the output of lsusb, the voltage is selected experimentally, so that everyone has enough time for a correct shutdown, because the shutdown algorithm is activated when the residual voltage of the battery is lower than default.battery.voltage.low

NUT also has a variable battery.charge, in which the estimated mathematical remaining battery in% is written. It is visual, but not very convenient for management:

/etc/nut/upsd.users
[admin]
	password = pass
	actions = SET
	instcmds = ALL
[upsmon_local]
	password  = pass
	upsmon master
[upsmon_remote]
        password  = pass
        upsmon slave

Users who can connect and watch / manage / control the UPS. admin - for UPS management, upsmon_local - for monitoring the status of the UPS on the local server, upsmon_remote - for managed clients. Passwords to taste:

/etc/nut/upsmon.conf
RUN_AS_USER nut
MONITOR winner1500@127.0.0.1 1 upsmon_local pass master
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD /usr/local/ups/notifyme
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 45
DEADTIME 15
POWERDOWNFLAG /etc/killpower
NOTIFYFLAG ONLINE	SYSLOG+EXEC
NOTIFYFLAG ONBATT	SYSLOG+EXEC
NOTIFYFLAG LOWBATT	SYSLOG+EXEC
NOTIFYFLAG FSD	SYSLOG+EXEC
NOTIFYFLAG COMMOK	SYSLOG+EXEC
NOTIFYFLAG COMMBAD	SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN	SYSLOG+EXEC
NOTIFYFLAG REPLBATT	SYSLOG+EXEC
NOTIFYFLAG NOCOMM	SYSLOG+EXEC
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5

This is the most interesting config, which describes the behavior of NUT in different states of the UPS. MONITOR indicates that we are connecting to the local NUT service and will be the master, i.e., in which case, we will turn off the slaves, and we ourselves will die last. The NOTIFYCMD line indicates which command will be executed for notification. For me, this is a call to / usr / local / ups / notifyme with the following content:

#! /bin/bash
smssend 7903******* "servername:$*"

The NOTIFYFLAG arguments with the EXEC flag are passed to the command. All arguments passed are placed in the line after "servername:" (so that it is clear who writes) and sent to 7903 *******. That is, when the current is turned off, the SMS “servername: UPS winner1500 on battery” will come. The text of notifications can be changed.

The second Centos server is even easier to configure. In fact, we only need a status monitor for the master server and waiting for news from it. But since we have playsms, you can also receive SMS from him, for example, whether there is a connection with the master (if the NUT service died on it)

/etc/nut/nut.conf
MODE=netсlient

/etc/nut/upsmon.conf
RUN_AS_USER nut
MONITOR winner1500@10.10.0.4 1 upsmon_remote pass slave
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD /usr/local/ups/notifyme
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 45
DEADTIME 15
POWERDOWNFLAG /etc/killpower
NOTIFYFLAG ONLINE	SYSLOG
NOTIFYFLAG ONBATT	SYSLOG
NOTIFYFLAG LOWBATT	SYSLOG
NOTIFYFLAG FSD	SYSLOG+EXEC
NOTIFYFLAG COMMOK	SYSLOG+EXEC
NOTIFYFLAG COMMBAD	SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN	SYSLOG+EXEC
NOTIFYFLAG REPLBATT	SYSLOG
NOTIFYFLAG NOCOMM	SYSLOG+EXEC
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5

That is, we put EXEC where we are interested in communication problems and whether the command to disconnect power from the master passed.
To send SMS, I used playsms, to which curl sends the text as a GET-url. To do this, you initially need to enable webservices in the playsms user settings and generate webservices token. After that, in / usr / local / ups / notifyme of the second server, specify:

#! /bin/bash
msg="$(echo "$*" | tr ' ' '+')"
curl -v "http://10.10.0.4/smsd/index.php?app=webservices&h=f3fd0e7c758b5d529dea17c256f94b23&u=admin&ta=pv&to=7903*******&msg=servername:$msg"

That is, everything is the same as in the first embodiment, only replace the spaces with + and send via a GET request.

In windows, everything was a little overgrown with crutches. The NUT-Installer-2.6.5-3.msi package was installed on the server. It was installed by default in C: \ Program Files \ NUT and it started wonderfully with no problems. And curl to send notifications through playsms.

The NUT configuration is similar to the second slave server, i.e. MODE-netclient, main in upsmon.conf and crutches.

C: \ Program Files \ NUT \ etc \ upsmon.conf
MONITOR winner1500@10.10.0.4 1 upsmon_remote pass slave
MINSUPPLIES 1
SHUTDOWNCMD "C:/Common/curl/powerdown.cmd"
NOTIFYCMD "C:/Common/curl/notify.cmd"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 45
DEADTIME 15
POWERDOWNFLAG "C:/killpower"
# NOTIFYFLAG ONLINE	SYSLOG+WALL
NOTIFYFLAG ONBATT	SYSLOG+WALL
# NOTIFYFLAG LOWBATT	SYSLOG+WALL
NOTIFYFLAG FSD		SYSLOG+EXEC
NOTIFYFLAG COMMOK	SYSLOG
NOTIFYFLAG COMMBAD	SYSLOG
NOTIFYFLAG SHUTDOWN	SYSLOG+EXEC+WALL
# NOTIFYFLAG REPLBATT	SYSLOG+WALL
NOTIFYFLAG NOCOMM	SYSLOG+EXEC
# NOTIFYFLAG NOPARENT	SYSLOG+WALL
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5


Here is the notify.cmd notification file with the following content (the meaning of which does not change):
@echo off
SET "url=http://10.10.0.4/smsd/index.php?app=webservices&h=f3fd0e7c758b5d529dea17c256f94b23&u=admin&ta=pv&to=7903*******&msg="
SET "msg=%*"
SET "msg=%msg: =+%"
C:\Common\curl\curl.exe "%url%servername:+%msg%"

That's the whole story. If you need to clarify something, I’ll be glad to add.

By the way:

msg =% * in notify.cmd makes the batch file relatively universal for different tasks. That is, adding to the startup “notify.cmd started” we will receive a notification “servername: started” by SMS.

I didn’t fail to use this to monitor a hard reboot with BSOD 0x9, which symantec suited me. I did not clean up. Hard reset monitoring is useful:

eventtriggers /create /eid 6008 /l system /tr "hardreset" /tk "C:\Common\curl\notify.cmd 6008 hard reset occured"

Instead of powerdown.cmd, you can directly specify a command, for example C: /WINDOWS/system32/shutdown.exe -s -t 0 -c “Forced by NUT master”.

The path to the batch file appeared because it was advisable for me to turn off the virtual machine correctly using the vmrun from the VMware VIX bundle before shutting down the server.

Since the machine was started as a service using the AlwaysUp utility , the command to correctly stop the machine “C: \ Program Files \ VMware \ VMware VIX \ vmrun.exe” was stopped by S: \ vmachine.vmx in AlwaysUp, and I just controlled it in the batch file service and wrote a log, so that if anything - add timeouts. Happened:

@echo off
set "service=autocatalog (managed by AlwaysUpService)"
set "log=C:\Common\curl\fsd.log"
echo FSD command received %DATE% %TIME% >> %log%
sc query "%service%" | find "RUNNING" >nul 2>&1 && set running=true
if not "%running%"=="true" goto stopnow
echo VM machine is runnung. Let's stop it >> %log%
sc stop "%service%"
if ERRORLEVEL 1 goto stopnow
sleep 35
sc query "%service%" | find "STOPPED" >nul 2>&1 && echo VM machine soft off in 35 sec >> %log%
:stopnow
echo system shutdown initiated >> %log%
echo ------------------------- >> %log%
C:\WINDOWS\system32\shutdown.exe -s -t 0 -c "Forced by NUT master"

That is, when the battery is ticking, it is not worth waiting for the virtual machine to turn off longer than a certain limit if it hangs or is not running at all

Also popular now: