📜 ⬆️ ⬇️

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

With the limited budget of a small company, a modest terminal server 2003 received uninterrupted electricity from Ippon Smart Winner 1500, which was controlled by a standard utility. I can’t say something bad about this UPS and its monstrous control utility with current animation. Humming quite on their money.

Over time, windows had to burden a pair of virtual machines Vmware, and the park was replenished with a pair of small Linux servers with PBX, Jabber and other office joys. Accordingly, there was a desire to also manage their power from Ippon, for support is declared * nix.

However, although the manufacturer “for adults” has provided many advanced management options, including clients under * nix and mac, sms and email notification, etc., attempts to put it into practice are painful, and the result is unpredictable.

As all roads lead to Rome, so problems with Ippon lead to NUT . There is a lot 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.
')
In the CentOS server, directly connected to the UPS via USB, I plugged in the Teleofis-RX104 GSM modem and made friends with smstools3 and playsms in order to get an improvised sms gateway with webservices api support. As a result, we got a convenient universal tool for all sorts of alerts, incl. and office work

Next, I installed a 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 the ports and vendor / product from the output of lsusb, the voltage is chosen experimentally, so that everyone has enough time to turn off correctly, because the shutdown algorithm is enabled when the residual battery voltage is lower than default.battery.voltage.low

In NUT there is also the variable battery.charge, in which the estimated mathematical remainder of the battery is written in%. It is visual, but for management it is not very convenient:

/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 / control / control the UPS. admin - to manage the UPS, upsmon_local - to monitor 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 ourselves will die last. The NOTIFYCMD string indicates which command will be executed for the notification. My call is / usr / local / ups / notifyme with the following contents:

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

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

The second Centos server is even easier to set up. In fact, we need only a monitor of the status of the master server and the expectation of messages from it. But since we have playsms, then we can receive SMS from him, for example, whether there is a connection with the master (has the NUT service died on him)

/etc/nut/nut.conf
 MODE=netlient 

/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 problems with communication and if the power outage command passed the master.
For sending SMS, I used playsms, to which curl sends the text as a GET-url. To do this, initially you need to enable webservices in user settings playsms and generate webservices token. After that, specify the following in / usr / local / ups / notifyme of the second server:

 #! /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 variant, just replace the spaces with + and send it 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 the service started up wonderfully without any problems. And curl to send notifications via playsms.

The NUT setting is similar to the second server slave, i.e. MODE-netclient, mainly 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 the notification file notify.cmd is as follows (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 actually the whole story. If you need something to clarify, I will be glad to add

By the way:

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

I didn’t fail to use this to monitor hard reset with BSOD 0x9, which symantec arranged for me. Clean did not. Monitoring hard reset 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 the 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 desirable for me to shut down the virtual machine correctly using vmrun from VMware VIX before shutting down the server.

Since the machine was started as a service by the AlwaysUp utility, the command to correctly stop the machine “C: \ Program Files \ VMware \ VMware VIX \ vmrun.exe” stop S: \ vmachine.vmx I registered in AlwaysUp, and in the batch file I only controlled service and wrote a log, so that if something - 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’s not worth waiting for the virtual machine to turn off longer than a certain limit if it hangs or is not running at all

Source: https://habr.com/ru/post/246671/


All Articles