121 lines
3.0 KiB
Markdown
121 lines
3.0 KiB
Markdown
# 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 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
|
|
|
|
```bash
|
|
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:
|
|
|
|
```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'
|
|
```
|
|
|
|
## 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.
|
|
|
|
```bash
|
|
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
|
|
```
|
|
|
|
```bash
|
|
curl 'http://localhost/jsonstatus?=nutdev1@localhost'
|
|
```
|
|
|
|
## Update
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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:
|
|
|
|
```bash
|
|
sudo a2disconf nut-json
|
|
sudo rm /etc/apache2/conf-available/nut-json.conf
|
|
sudo systemctl reload apache2
|
|
```
|