-
21Install dependencies
apt-get install -y nodejs
cd /root
npm init -y
npm install express axios -
22Download the setup script:
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
-
23Run the setup script
sudo -E bash nodesource_setup.sh
-
24Install Node.js
sudo apt-get install -y nodejs
-
25Verify the installation
node -v
-
26Create the file you'll use to monitor and publish the UPS data
nano /root/psu_proxy.js
sudo chmod 644 /root/psu_proxy.js
sudo chown root:root /root/psu_proxy.js -
27Write to file contents
const axios = require('axios');
const cors = require('cors'); // Import CORS packageconst app = express();
const port = 3000;// Enable CORS for all origins (you can also restrict this to specific origins if needed)
app.use(cors()); // This will allow all origins by defaultapp.get('/proxy', async (req, res) => {
const host = req.query.host; // Get the host from query parameters
try {
const response = await axios.get(`http://192.168.88.240/cgi-bin/nut/upsstats.cgi?host=${host}`);
res.send(response.data);
} catch (error) {
console.error("Error fetching UPS data:", error);
res.status(500).send("Error fetching data");
}
});app.listen(port, () => {
console.log(`Proxy server running at http://localhost:${port}`);
}); -
28Start the file for testing
node proxy.js
-
29Once you confirm that everythign works, create a service
sudo nano /etc/systemd/system/psu-proxy.service
[Unit]
Description=PSU Proxy Server for NUT
After=network.target[Service]
ExecStart=/usr/bin/node /root/psu_proxy.js
Restart=always
User=root
Group=root
Environment=NODE_ENV=production
WorkingDirectory=/root[Install]
WantedBy=multi-user.targetReload system and enable services
sudo systemctl daemon-reload
sudo systemctl enable psu-proxy.service
sudo systemctl start psu-proxy.service -
30Optional - Create this usb_monitor.sh file so that should the USB driver fault, it will auto restart and create a service for it.
UPS_LIST=("EATON1150VA" "Trust1500VA" "APC1400VA") # Replace with your UPS device names
LOG_FILE="/var/log/usb_monitor.log"check_ups() {
for ups in "${UPS_LIST[@]}"; do
STATUS=$(upsc "$ups" 2>/dev/null | grep "ups.status" | awk '{print $2}')if [ -z "$STATUS" ]; then
echo "$(date): UPS $ups has stale data. Restarting NUT driver..." >> $LOG_FILE
sudo systemctl restart nut-driver
return
fi
done
}while true; do
check_ups
sleep 60 # Check every minute
done
Nicola
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.