The NGINX Unit control API includes a /status
endpoint for usage statistics. This is a solution for exposing these metrics in Prometheus format.
The application (run by Unit) queries the /status
URI on the control socket and converts the JSON response into Prometheus text format. PHP and Python implementations are available.
These instructions assume an existing Unit installation with a working configuration. We will add a new listener on the default prometheus port (9090) and route it directly to the Prometheus app.
Step 0. Install the preferred Unit language module (unit-php
or unit-python
)
Step 1. Create a new group that has access to read and write to the Unit control socket (run these commands as root)
groupadd unitsock
chgrp unitsock /var/run/control.unit.sock
chmod g+rw /var/run/control.unit.sock
Note: Ownership and permissions of the control socket will be reset on restart/reboot.
Step 2a. Copy your preferred exporter implementation (prometheus.php or prometheus.py) to a suitable location on your Unit instance.
Step 2b. Create a JSON object as prom.json
for your preferred exporter implementation, paying special attention to the location of the control socket.
{
"type": "php",
"root": "/path/to/app",
"script": "prometheus.php",
"group": "unitsock",
"environment": {
"control_socket": "/var/run/control.unit.sock"
}
}
{
"type": "python",
"path": "/path/to/app",
"module": "prometheus",
"group": "unitsock",
"environment": {
"control_socket": "/var/run/control.unit.sock"
}
}
Step 2b. Create the prom
application
unitc /config/applications/prom < prom.json
Step 3. Create the listener and route it directly to the application
echo '{"pass": "applications/prom"}' | unitc /config/listeners/\*:9090
Step 4. Test the Prometheus endpoint
$ curl http://localhost:9090/
unit_connections_accepted_total 38
unit_connections_active 1
unit_connections_idle 0
unit_connections_closed_total 37
unit_requests_total 38
unit_application_prom_processes_running 1
unit_application_prom_processes_starting 0
unit_application_prom_processes_idle 0
unit_application_prom_requests_active 1