537f455e10468b066f49668d03ff8f8698c44c98
jsonnutserver
Small HTTP server that runs upsc <ups> and returns the result as JSON.
GET /jsonstatus?=nutdev1@localhost
Requirements
- Python 3 (standard library only)
upscfrom the NUT client package, and a reachableupsd
# Debian/Ubuntu
sudo apt install python3 nut-client
# Fedora/RHEL
sudo dnf install python3 nut-client
Check that upsc works before going further:
upsc nutdev1@localhost
Install to /opt
sudo mkdir -p /opt/jsonnutserver
sudo wget https://git.shihaam.dev/shihaam/jsonnutserver/raw/branch/main/server.py -O /opt/jsonnutserver/server.py
sudo chmod 0755 /opt/jsonnutserver/server.py
systemd service
sudo wget https://git.shihaam.dev/shihaam/jsonnutserver/raw/branch/main/jsonnutserver.service -O /etc/systemd/system/jsonnutserver.service
sudo chmod 0644 /etc/systemd/system/jsonnutserver.service
sudo systemctl daemon-reload
sudo systemctl enable --now jsonnutserver.service
The unit runs /usr/bin/python3 /opt/jsonnutserver/server.py as an unprivileged
DynamicUser and restarts it if it crashes. If python3 or upsc lives
somewhere else on your system, adjust ExecStart (check with command -v python3).
Change the listen address or port
The server reads LISTEN_HOST (default 0.0.0.0) and LISTEN_PORT
(default 8000). Override them without editing the unit file:
sudo systemctl edit jsonnutserver.service
[Service]
Environment=LISTEN_HOST=127.0.0.1
Environment=LISTEN_PORT=9000
sudo systemctl restart jsonnutserver.service
Check it
systemctl status jsonnutserver.service
journalctl -u jsonnutserver.service -f
curl 'http://localhost:8000/jsonstatus?=nutdev1@localhost'
Serve through Apache (optional)
To expose /jsonstatus on an existing Apache site, proxy it to the local server.
Consider setting LISTEN_HOST=127.0.0.1 (see above) so port 8000 is only
reachable through Apache.
sudo a2enmod proxy proxy_http
sudo tee /etc/apache2/conf-available/nut-json.conf > /dev/null <<'EOF'
ProxyPass /jsonstatus http://127.0.0.1:8000/jsonstatus
ProxyPassReverse /jsonstatus http://127.0.0.1:8000/jsonstatus
EOF
sudo a2enconf nut-json
sudo apachectl configtest && sudo systemctl restart apache2
curl 'http://localhost/jsonstatus?=nutdev1@localhost'
Update
sudo wget https://git.shihaam.dev/shihaam/jsonnutserver/raw/branch/main/server.py -O /opt/jsonnutserver/server.py
sudo chmod 0755 /opt/jsonnutserver/server.py
sudo wget https://git.shihaam.dev/shihaam/jsonnutserver/raw/branch/main/jsonnutserver.service -O /etc/systemd/system/jsonnutserver.service
sudo systemctl daemon-reload
sudo systemctl restart jsonnutserver.service
Uninstall
sudo systemctl disable --now jsonnutserver.service
sudo rm /etc/systemd/system/jsonnutserver.service
sudo systemctl daemon-reload
sudo rm -r /opt/jsonnutserver
If you set up the Apache proxy:
sudo a2disconf nut-json
sudo rm /etc/apache2/conf-available/nut-json.conf
sudo systemctl reload apache2
Languages
Python
100%