This commit is contained in:
2026-09-24 13:16:47 +05:00
commit 538b71596c
3 changed files with 202 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
# jsonnutserver
Small HTTP server that runs `upsc <ups>` and returns the result as JSON.
```
GET /jsonstatus?=nutdev1@localhost
```
## Requirements
- Python 3 (standard library only)
- `upsc` from the NUT client package, and a reachable `upsd`
```bash
# Debian/Ubuntu
sudo apt install python3 nut-client
# Fedora/RHEL
sudo dnf install python3 nut-client
```
Check that `upsc` works before going further:
```bash
upsc nutdev1@localhost
```
## Install to /opt
```bash
sudo mkdir -p /opt/jsonnutserver
sudo install -m 0755 server.py /opt/jsonnutserver/server.py
```
## systemd service
```bash
sudo install -m 0644 jsonnutserver.service /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:
```bash
sudo systemctl edit jsonnutserver.service
```
```ini
[Service]
Environment=LISTEN_HOST=127.0.0.1
Environment=LISTEN_PORT=9000
```
```bash
sudo systemctl restart jsonnutserver.service
```
## Check it
```bash
systemctl status jsonnutserver.service
journalctl -u jsonnutserver.service -f
curl 'http://localhost:8000/jsonstatus?=nutdev1@localhost'
```
## Update
```bash
sudo install -m 0755 server.py /opt/jsonnutserver/server.py
sudo systemctl restart jsonnutserver.service
```
## Uninstall
```bash
sudo systemctl disable --now jsonnutserver.service
sudo rm /etc/systemd/system/jsonnutserver.service
sudo systemctl daemon-reload
sudo rm -r /opt/jsonnutserver
```