From c77f2b8a64ccb0d405cc48522d94f7129f431092 Mon Sep 17 00:00:00 2001 From: snailed Date: Thu, 28 Jul 2022 13:11:42 +0000 Subject: [PATCH] Make http_host_bind_port vars more useful (#1984) * if variable to bind an exporter container to a host port is set, have matrix-domain.conf (nginx) support this * manipulate some variables to account for just port numbers or 0.0.0.0 IPs * Make sure to use the right variable in the init.yml files * Update roles/matrix-prometheus-node-exporter/tasks/init.yml Co-authored-by: Slavi Pantaleev * Update roles/matrix-prometheus-postgres-exporter/tasks/init.yml Co-authored-by: Slavi Pantaleev * remove extraneous variables and whitespace Co-authored-by: Luca Bilke Co-authored-by: Slavi Pantaleev --- roles/matrix-prometheus-node-exporter/defaults/main.yml | 8 +++++++- roles/matrix-prometheus-node-exporter/tasks/init.yml | 6 +++--- roles/matrix-prometheus-node-exporter/vars/main.yml | 5 +++++ .../matrix-prometheus-postgres-exporter/defaults/main.yml | 8 +++++++- roles/matrix-prometheus-postgres-exporter/tasks/init.yml | 6 +++--- roles/matrix-prometheus-postgres-exporter/vars/main.yml | 5 +++++ 6 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 roles/matrix-prometheus-node-exporter/vars/main.yml create mode 100644 roles/matrix-prometheus-postgres-exporter/vars/main.yml diff --git a/roles/matrix-prometheus-node-exporter/defaults/main.yml b/roles/matrix-prometheus-node-exporter/defaults/main.yml index d9077697..c7d6512f 100644 --- a/roles/matrix-prometheus-node-exporter/defaults/main.yml +++ b/roles/matrix-prometheus-node-exporter/defaults/main.yml @@ -38,7 +38,7 @@ matrix_prometheus_node_exporter_metrics_proxying_enabled: false # Controls whether the matrix-prometheus container exposes its HTTP port (tcp/9100 in the container). # -# Takes an ":" value (e.g. "127.0.0.1:9100"), or empty string to not expose. +# Takes an ":" value (e.g. "127.0.0.1:9100"), just a port number or empty string to not expose. # # You likely don't need to do this. See `matrix_prometheus_node_exporter_metrics_proxying_enabled`. # @@ -54,3 +54,9 @@ matrix_prometheus_node_exporter_metrics_proxying_enabled: false # because node-exporter can't see all interfaces, etc. # For now, we'll live with that, until someone develops a better solution. matrix_prometheus_node_exporter_container_http_host_bind_port: '' + +# If you are supplying your own NGINX proxy but want to use the provided exporters you will have to supply an ":" value for the containers to bind to on your host. +# If matrix_prometheus_node_exporter_container_http_host_bind_port is set to just a port number, this will default to "127.0.0.1:" +# If matrix_prometheus_node_exporter_container_http_host_bind_port is set to an IP that is not 0.0.0.0 and a port, that ":" value will be used +# Otherwise this value will be empty and you will have to manually configure your NGINX config file. (If you are using the config files generated by this playbook, you will have to edit matrix-domain.conf) +matrix_prometheus_node_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host: "{{ '127.0.0.1' + matrix_prometheus_node_exporter_container_http_host_bind_port_number_raw if not ':' in matrix_prometheus_node_exporter_container_http_host_bind_port else (matrix_prometheus_node_exporter_container_http_host_bind_port if matrix_prometheus_node_exporter_container_http_host_bind_port.split(':')[0] != '0.0.0.0' else '') }}" diff --git a/roles/matrix-prometheus-node-exporter/tasks/init.yml b/roles/matrix-prometheus-node-exporter/tasks/init.yml index 51dd94f2..42f21667 100644 --- a/roles/matrix-prometheus-node-exporter/tasks/init.yml +++ b/roles/matrix-prometheus-node-exporter/tasks/init.yml @@ -23,10 +23,10 @@ resolver 127.0.0.11 valid=5s; set $backend "matrix-prometheus-node-exporter:9100"; proxy_pass http://$backend/metrics; + {% elif matrix_prometheus_node_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host != '' %} + proxy_pass http://{{ matrix_prometheus_node_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host }}/metrics; {% else %} - {# Generic configuration for use outside of our container setup #} - {# This may be implemented in the future. #} - return 404 "matrix-nginx-proxy is disabled, so metrics are unavailable"; + return 404 "matrix-nginx-proxy is disabled and no host port was bound to the container, so metrics are unavailable"; {% endif %} } diff --git a/roles/matrix-prometheus-node-exporter/vars/main.yml b/roles/matrix-prometheus-node-exporter/vars/main.yml new file mode 100644 index 00000000..952dc205 --- /dev/null +++ b/roles/matrix-prometheus-node-exporter/vars/main.yml @@ -0,0 +1,5 @@ +--- + +# `matrix_prometheus_node_exporter_container_http_host_bind_port_number_raw` contains the raw port number extracted from `matrix_prometheus_node_exporter_container_http_host_bind_port`, +# which can contain values like this: ('1234', '127.0.0.1:1234', '0.0.0.0:1234') +matrix_prometheus_node_exporter_container_http_host_bind_port_number_raw: "{{ '' if matrix_prometheus_node_exporter_container_http_host_bind_port == '' else (matrix_prometheus_node_exporter_container_http_host_bind_port.split(':')[1] if ':' in matrix_prometheus_node_exporter_container_http_host_bind_port else matrix_prometheus_node_exporter_container_http_host_bind_port) }}" diff --git a/roles/matrix-prometheus-postgres-exporter/defaults/main.yml b/roles/matrix-prometheus-postgres-exporter/defaults/main.yml index 82a12f42..b530df85 100644 --- a/roles/matrix-prometheus-postgres-exporter/defaults/main.yml +++ b/roles/matrix-prometheus-postgres-exporter/defaults/main.yml @@ -35,7 +35,7 @@ matrix_prometheus_postgres_exporter_metrics_proxying_enabled: false # Controls whether the matrix-prometheus container exposes its HTTP port (tcp/9187 in the container). # -# Takes an ":" value (e.g. "127.0.0.1:9187"), or empty string to not expose. +# Takes an ":" value (e.g. "127.0.0.1:9187"), just a port number or an empty string to not expose. # # You likely don't need to do this. See `matrix_prometheus_postgres_exporter_metrics_proxying_enabled`. # @@ -52,5 +52,11 @@ matrix_prometheus_postgres_exporter_metrics_proxying_enabled: false # For now, we'll live with that, until someone develops a better solution. matrix_prometheus_postgres_exporter_container_http_host_bind_port: '' +# If you are supplying your own NGINX proxy but want to use the provided exporters you will have to supply an ":" value for the containers to bind to on your host. +# If matrix_prometheus_postgres_exporter_container_http_host_bind_port is set to just a port number, this will default to "127.0.0.1:" +# If matrix_prometheus_postgres_exporter_container_http_host_bind_port is set to an IP that is not 0.0.0.0 and a port, that ":" value will be used +# Otherwise this value will be empty and you will have to manually configure your NGINX config file. (If you are using the config files generated by this playbook, you will have to edit matrix-domain.conf) +matrix_prometheus_postgres_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host: "{{ '127.0.0.1' + matrix_prometheus_postgres_exporter_container_http_host_bind_port_number_raw if not ':' in matrix_prometheus_postgres_exporter_container_http_host_bind_port else (matrix_prometheus_postgres_exporter_container_http_host_bind_port if matrix_prometheus_postgres_exporter_container_http_host_bind_port.split(':')[0] != '0.0.0.0' else '') }}" + matrix_prometheus_postgres_exporter_dashboard_urls: - "https://grafana.com/api/dashboards/9628/revisions/7/download" diff --git a/roles/matrix-prometheus-postgres-exporter/tasks/init.yml b/roles/matrix-prometheus-postgres-exporter/tasks/init.yml index 6da16937..03fe965c 100644 --- a/roles/matrix-prometheus-postgres-exporter/tasks/init.yml +++ b/roles/matrix-prometheus-postgres-exporter/tasks/init.yml @@ -23,10 +23,10 @@ resolver 127.0.0.11 valid=5s; set $backend "matrix-prometheus-postgres-exporter:9187"; proxy_pass http://$backend/metrics; + {% elif matrix_prometheus_postgres_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host != '' %} + proxy_pass http://{{ matrix_prometheus_postgres_exporter_matrix_nginx_proxy_not_enabled_proxy_pass_host }}/metrics; {% else %} - {# Generic configuration for use outside of our container setup #} - {# This may be implemented in the future. #} - return 404 "matrix-nginx-proxy is disabled, so metrics are unavailable"; + return 404 "matrix-nginx-proxy is disabled and no host port was bound to the container, so metrics are unavailable"; {% endif %} } diff --git a/roles/matrix-prometheus-postgres-exporter/vars/main.yml b/roles/matrix-prometheus-postgres-exporter/vars/main.yml new file mode 100644 index 00000000..aed3b216 --- /dev/null +++ b/roles/matrix-prometheus-postgres-exporter/vars/main.yml @@ -0,0 +1,5 @@ +--- + +# `matrix_prometheus_postgres_exporter_container_http_host_bind_port_number_raw` contains the raw port number extracted from `matrix_prometheus_postgres_exporter_container_http_host_bind_port`, +# which can contain values like this: ('1234', '127.0.0.1:1234', '0.0.0.0:1234') +matrix_prometheus_postgres_exporter_container_http_host_bind_port_number_raw: "{{ '' if matrix_prometheus_postgres_exporter_container_http_host_bind_port == '' else (matrix_prometheus_postgres_exporter_container_http_host_bind_port.split(':')[1] if ':' in matrix_prometheus_postgres_exporter_container_http_host_bind_port else matrix_prometheus_postgres_exporter_container_http_host_bind_port) }}"