{"id":4764,"date":"2024-11-28T12:00:35","date_gmt":"2024-11-28T20:00:35","guid":{"rendered":"https:\/\/www.cloudacm.com\/?p=4764"},"modified":"2024-11-23T09:20:25","modified_gmt":"2024-11-23T17:20:25","slug":"network-ups-tools-and-mqtt","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=4764","title":{"rendered":"Network UPS Tools and MQTT"},"content":{"rendered":"<p><a href=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4774\" src=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors.png\" alt=\"\" width=\"1006\" height=\"389\" srcset=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors.png 1006w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors-300x116.png 300w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors-768x297.png 768w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/UPS-Vendors-604x234.png 604w\" sizes=\"auto, (max-width: 1006px) 100vw, 1006px\" \/><\/a><\/p>\n<p>This post will cover the installation of NUT which will be used to monitor UPS units from a single host. The data gathered by NUT will then be published to a MQTT broker. From there the data will be processed in Node-Red to provide a at a glance view of the UPS units as well as conditional actions. There have been others who have built interfaces that are specific to a make and model of UPS, such as here <a href=\"https:\/\/www.jmayes.com\/cyberpower-backup-serial-protocol-and-interface\/\">https:\/\/www.jmayes.com\/cyberpower-backup-serial-protocol-and-interface\/<\/a>. These methods have the potential to use an embedded solution such as wireless ESP32 modules. But the investment of time and effort were not affordable and NUT has such a wide range of support that it bridges the gap more efficiently. Here is a description provided by Claude AI about UPS units, NUT, and the benefit of bringing it all together.<\/p>\n<p><a href=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4765\" src=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status.png\" alt=\"\" width=\"905\" height=\"345\" srcset=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status.png 905w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status-300x114.png 300w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status-768x293.png 768w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2024\/11\/Node-Red_UPS-Status-604x230.png 604w\" sizes=\"auto, (max-width: 905px) 100vw, 905px\" \/><\/a><\/p>\n<p><em>When uptime matters and power failures threaten critical operations, an Uninterruptible Power Supply (UPS) can offer protection. This essential component ensures work flow by instantly stepping in during power disruptions, preventing costly equipment damage, or devastating data loss. More than just a backup battery, modern UPS systems deliver clean, consistent power while protecting against surges, sags, and other power anomalies. Each UPS acts as a vigilant sentry, monitoring a wealth of vital statistics including battery health, load levels, runtime estimates, and power conditions. By transforming uncertain power conditions into reliable, protected power delivery, these systems have become the cornerstone of system operations.<\/em><\/p>\n<p><em>UPSs don&#8217;t just silently protect equipment, they&#8217;re constantly gathering vital intelligence about the power environment. Every data point they collect, from battery health and load levels to voltage fluctuations and temperature readings, tells a crucial part of the story. Monitoring these statistics helps spot potential issues before they become critical failures, plan maintenance windows, and ensures that backup power capacity matches actual needs. The estimated runtime calculations are particularly valuable for graceful shutdowns during extended outages. By transforming raw power metrics into actionable items, these monitoring capabilities elevate a UPS from a simple backup device to a strategic tool for maintaining uninterrupted operations.<\/em><\/p>\n<p><em>Network UPS Tools (NUT) power monitoring breaks free from the limitations of single-device management. Instead of juggling multiple monitoring tools from different manufacturers, NUT unifies the entire power environment under one intelligent system. This open-source solution acts as a universal translator, speaking fluently with UPS units from various vendors while consolidating data into a standardized format. Whether monitoring two UPS units or twenty, NUT eliminates the complexity of multiple management interfaces, streamlines alert handling, and enables coordinated shutdown procedures across the network. This centralized approach not only simplifies monitoring but transforms scattered power data into a cohesive picture of the entire power protection strategy.<\/em><\/p>\n<p>Here are the steps to install NUT on a linux host. Most of these steps were based off this posting, <a href=\"https:\/\/pimylifeup.com\/raspberry-pi-nut-server\/\">https:\/\/pimylifeup.com\/raspberry-pi-nut-server\/<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo apt update\r\nsudo apt upgrade\r\nsudo apt install nut\r\nlsusb<\/pre>\n<p>This provided the following lines based off of the hardware that was installed, yours may be different.<\/p>\n<ul>\n<li>Bus 001 Device 002: ID 0764:0501 Cyber Power System, Inc. CP1500 AVR UPS<\/li>\n<li>Bus 001 Device 003: ID 0764:0501 Cyber Power System, Inc. CP1500 AVR UPS<\/li>\n<\/ul>\n<p>The two pieces required are specified after \u201cID\u201c. These are the Vendor ID and Product ID, and they are separated by a semicolon (:).<\/p>\n<p>For example, from the above line, we can see our vendor ID is \u201c0764\u201d and our product ID is \u201c0501\u201c.<br \/>\nSince we have identical model UPS units, this will be a challenge.\u00a0 The device entry of &#8220;002&#8221; and &#8220;003&#8221; could be defined, but this has proven to be unreliable.\u00a0 The second UPS unit will be commented out to give reference on how an additional UPS unit would be defined.<\/p>\n<p>With this info in hand, we can modify NUT\u2019s configuration file so that it loads drivers for either one of the UPS units.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo nano \/etc\/nut\/ups.conf\r\n\r\n&lt;\/&gt; Nano Editor\r\n[CP1500UPS-Right]\r\ndriver = usbhid-ups\r\ndesc = \"CP1500 Right\"\r\nport = auto\r\nvendorid = 0764\r\nproductid = 0501\r\n# device = 002\r\n\r\n# [CP1500UPS-Left]\r\n#\u00a0driver = usbhid-ups\r\n# desc = \"CP1500 Left\"\r\n# port = auto\r\n# vendorid = 0764\r\n# productid = 0501\r\n# device = 003<\/pre>\n<p>Next the listening port for the service needs to be set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo nano \/etc\/nut\/upsd.conf\r\n\r\n&lt;\/&gt; Nano Editor\r\nLISTEN 0.0.0.0 3493<\/pre>\n<p>The service uses credentials for access, be sure change them from what is provided below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo nano \/etc\/nut\/upsd.users\r\n\r\n&lt;\/&gt; Nano Editor\r\n[CP1500UPS-Right]\r\n    password = something\r\n    CP1500UPS-Right primary\r\n    \r\n# [CP1500UPS-Left]\r\n#\u00a0    password = something\r\n#     CP1500UPS-Left primary<\/pre>\n<p>The monitoring service then needs to be configured with the relative details.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo nano \/etc\/nut\/upsmon.conf\r\n\r\n&lt;\/&gt; Nano Editor\r\nMONITOR CP1500UPS-Right@localhost 1 CP1500UPS-Right something primary\r\n# MONITOR CP1500UPS-Left@localhost 1 CP1500UPS-Left something primary<\/pre>\n<p>Finally, the NUT service needs to have network services activated<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo nano \/etc\/nut\/nut.conf\r\n\r\n&lt;\/&gt; Nano Editor\r\nMODE=netserver  # Change this line from MODE=none<\/pre>\n<p>To commit the changes and active, both NUT services need to be restarted.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">sudo systemctl restart nut-server\r\nsudo systemctl restart nut-monitor<\/pre>\n<p>If all is working, the following command should provide a listing of statistics for the UPS instance.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">upsc CP1500UPS-Right@localhost\r\n#\u00a0upsc CP1500UPS-Left@localhost<\/pre>\n<p>This concludes the NUT installation and configuration requirements for MQTT intagration. There are other options available for NUT, but those will not be convered here.<\/p>\n<p>The following bash script was expanded from an earlier post about MQTT Node-Red system monitoring, covered here <a href=\"https:\/\/www.cloudacm.com\/?p=4553.\">https:\/\/www.cloudacm.com\/?p=4553.<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">#!\/bin\/bash\r\n# cloudacm.com\r\n# Localhost Systems Resources to MQTT\r\n\r\nwhile true; \r\ndo \r\n  sysname=$(hostname -s)\r\n  upTime=$(awk '{print $1}' \/proc\/uptime)\r\n  cpuUser=$(top -bn1 | awk '\/Cpu\/ { print $4}')\r\n  cpuSystem=$(top -bn1 | awk '\/Cpu\/ { print $6}')\r\n  loadAve=$(uptime | tr -d , | awk '\/load average\/ { print $9}')\r\n  memUsage=$(free -m | awk '\/Mem\/{print $3}')\r\n  diskSize=$(df | awk '\/sda1\/{print $2}')\r\n  diskUsed=$(df | awk '\/sda1\/{print $3}')\r\n  diskAvail=$(df | awk '\/sda1\/{print $4}')\r\n  upsRightLoad=$(upsc CP1500UPS-Right@localhost | awk '\/ups.load:\/ {print $2}')\r\n  upsRightRuntime=$(upsc CP1500UPS-Right@localhost | awk '\/battery.runtime:\/ {print $2}')\r\n  upsRightUtility=$(upsc CP1500UPS-Right@localhost | awk '\/input.voltage:\/ {print $2}')\r\n  upsRightBattery=$(upsc CP1500UPS-Right@localhost | awk '\/battery.charge:\/ {print $2}')\r\n#   upsLeftLoad=$(upsc CP1500UPS-Left@localhost | awk '\/ups.load:\/ {print $2}')\r\n#   upsLeftRuntime=$(upsc CP1500UPS-Left@localhost | awk '\/battery.runtime:\/ {print $2}')\r\n#   upsLeftUtility=$(upsc CP1500UPS-Left@localhost | awk '\/input.voltage:\/ {print $2}')\r\n#   upsLeftBattery=$(upsc CP1500UPS-Left@localhost | awk '\/battery.charge:\/ {print $2}')\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$upTime\" -t $sysname\/upTime\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$cpuUser\" -t $sysname\/cpuUser\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$cpuSystem\" -t $sysname\/cpuSystem\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$loadAve\" -t $sysname\/loadAve\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$memUsage\" -t $sysname\/memUsage\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$diskSize\" -t $sysname\/diskSize\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$diskUsed\" -t $sysname\/diskUsed\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$diskAvail\" -t $sysname\/diskAvail\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$upsRightLoad\" -t $sysname\/upsRightLoad\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$upsRightRuntime\" -t $sysname\/upsRightRuntime\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$upsRightUtility\" -t $sysname\/upsRightUtility\r\n  mosquitto_pub -h &lt;Broker&gt; -m \"$upsRightBattery\" -t $sysname\/upsRightBattery\r\n #  mosquitto_pub -h &lt;Broker&gt; -m \"$upsLeftLoad\" -t $sysname\/upsLeftLoad\r\n #\u00a0 mosquitto_pub -h &lt;Broker&gt; -m \"$upsLeftRuntime\" -t $sysname\/upsLeftRuntime\r\n #  mosquitto_pub -h &lt;Broker&gt; -m \"$upsLeftUtility\" -t $sysname\/upsLeftUtility\r\n #  mosquitto_pub -h &lt;Broker&gt; -m \"$upsLeftBattery\" -t $sysname\/upsLeftBattery\r\nsleep 10; \r\ndone<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will cover the installation of NUT which will be used to monitor UPS units from a single host. The data gathered by NUT will then be published to a MQTT broker. From there the data will be processed in Node-Red to provide a at a glance view of the UPS units as well as conditional actions. There have been others who have built interfaces that are specific to a make and model of UPS, such as here https:\/\/www.jmayes.com\/cyberpower-backup-serial-protocol-and-interface\/&#8230;.<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=4764\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4764","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/4764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4764"}],"version-history":[{"count":10,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/4764\/revisions"}],"predecessor-version":[{"id":4777,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/4764\/revisions\/4777"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}