d5b5819ba084f47df8fe8b9c0140179012f4d634
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'
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
Languages
Python
100%