Skip to content

Commit ac62c4f

Browse files
authored
Merge pull request #5 from ntk148v/feat/nginx_exporter
Feat/nginx exporter
2 parents a981b9c + 485a9e3 commit ac62c4f

File tree

19 files changed

+305
-1
lines changed

19 files changed

+305
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Ansitheus allows users to configure & deploy the following components:
5858
- [Prometheus Mysqld-exporter](https://github.com/prometheus/mysqld_exporter)
5959
- [Prometheus Openstack-exporter](https://github.com/openstack-exporter/openstack-exporter)
6060
- [Google Cadvisor](https://github.com/google/cadvisor)
61+
- [Prometheus Nginx-exporter](https://github.com/nginx/nginx-prometheus-exporter)
6162
- [Haproxy](http://www.haproxy.org/)
6263
- [Keepalived](https://www.keepalived.org/)
6364
- [Grafana](https://github.com/grafana/grafana)

ansible/group_vars/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ enable_haproxy: "yes"
7272
enable_mysqld_exporter: "no"
7373
enable_fluentd: "no"
7474
enable_openstack_exporter: "no"
75+
enable_nginx_exporter: "no"
7576

7677
# Special variable to handle case, you want to only ONE Prometheus running instance at time.
7778
prometheus_active_passive_mode: "no"

ansible/inventory/all-in-one

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ localhost ansible_connection=local
3434

3535
[openstack_exporter]
3636
localhost ansible_connection=local
37+
38+
[nginx_exporter]
39+
localhost ansible_connection=local

ansible/inventory/multinode

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ fluentd-host-01
3838

3939
[openstack_exporter]
4040
openstack-host-01
41+
42+
[nginx_exporter]
43+
nginx-host-01
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Ansible Role: nginx_exporter
2+
3+
Deploy [nginx_exporter](https://github.com/nginxinc/nginx-prometheus-exporter) using Ansible and Docker.
4+
5+
## Requirements
6+
7+
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it).
8+
9+
## Role variables
10+
11+
All variables which can be overridden are stored in [defaults/main.yml](./defaults/main.yml) file as well as in [meta/argument_specs.yml](./meta/argument_specs.yml).
12+
13+
## Example playbook
14+
15+
```yaml
16+
- hosts: all
17+
roles:
18+
- { role: nginx_exporter }
19+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
nginx_exporter_version: "1.4.1"
3+
nginx_exporter_port: 9113
4+
nginx_exporter_container_name: "nginx_exporter"
5+
nginx_exporter_docker_namespace: "{{ docker_namespace if docker_namespace else 'nginx' }}"
6+
nginx_exporter_docker_log_driver: "{{ docker_log_driver }}"
7+
nginx_exporter_docker_log_opts: "{{ docker_log_opts }}"
8+
nginx_exporter_image: "{{ docker_registry ~ '/' if docker_registry else '' }}{{ nginx_exporter_docker_namespace }}/nginx-prometheus-exporter:{{ nginx_exporter_version }}"
9+
# Docker resource limit
10+
nginx_exporter_docker_memory_limit: "{{ docker_memory_limit }}"
11+
nginx_exporter_docker_memory_swap_limit: "{{ docker_memory_swap_limit }}"
12+
nginx_exporter_docker_cpus_limit: "{{ docker_cpus_limit }}"
13+
14+
# Nginx-exporter arguments
15+
# -----------------------
16+
nginx_exporter_plus: false
17+
nginx_exporter_scrape_uri: "http://{{ api_interface_address }}/stub_status"
18+
nginx_exporter_web_listen_address: "{{ api_interface_address }}:{{ nginx_exporter_port }}"
19+
nginx_exporter_config_dir: "{{ ansitheus_config_dir }}/nginx_exporter"
20+
nginx_exporter_web_telemetry_path: "/metrics"
21+
22+
nginx_exporter_tls_server_config: {}
23+
nginx_exporter_http_server_config: {}
24+
nginx_exporter_basic_auth_users: {}
25+
26+
# Nginx-exporter environment variables
27+
# -----------------------------------
28+
nginx_exporter_env: "{{ docker_container_env }}"
29+
30+
nginx_exporter_services:
31+
nginx_exporter:
32+
container_name: "{{ nginx_exporter_container_name }}"
33+
group: "nginx_exporter"
34+
enabled: "{{ enable_nginx_exporter }}"
35+
image: "{{ nginx_exporter_image }}"
36+
privileged: "no"
37+
state: "started"
38+
port: "{{ nginx_exporter_port }}"
39+
volumes:
40+
- "{{ nginx_exporter_config_dir }}:/etc/nginx_exporter"
41+
command: >
42+
'--nginx.scrape-uri={{ nginx_exporter_scrape_uri }}'
43+
{% if nginx_exporter_tls_server_config | length > 0 or nginx_exporter_http_server_config | length > 0 or nginx_exporter_basic_auth_users | length > 0 %}
44+
'--web.config.file=/etc/nginx_exporter/config.yml'
45+
{% endif %}
46+
{% if nginx_exporter_web_listen_address is iterable and
47+
nginx_exporter_web_listen_address is not mapping and
48+
nginx_exporter_web_listen_address is not string %}
49+
{% for address in nginx_exporter_web_listen_address %}
50+
'--web.listen-address={{ address }}'
51+
{% endfor %}
52+
{% else %}
53+
'--web.listen-address={{ nginx_exporter_web_listen_address }}'
54+
{% endif %}
55+
'--web.telemetry-path={{ nginx_exporter_web_telemetry_path }}'
56+
{% if nginx_exporter_plus %}
57+
'--nginx.plus'
58+
{% endif %}
59+
restart_policy: "unless-stopped"
60+
network_mode: "host"
61+
log_driver: "{{ nginx_exporter_docker_log_driver }}"
62+
log_options: "{{ nginx_exporter_docker_log_opts }}"
63+
memory: "{{ nginx_exporter_docker_memory_limit }}"
64+
memory_swap: "{{ nginx_exporter_docker_memory_swap_limit }}"
65+
cpus: "{{ nginx_exporter_docker_cpus_limit }}"
66+
env: "{{ nginx_exporter_env }}"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: Restart nginx_exporter container
3+
vars:
4+
service_name: "nginx_exporter"
5+
service: "{{ nginx_exporter_services[service_name] }}"
6+
become: true
7+
community.general.docker_container:
8+
name: "{{ service.container_name }}"
9+
image: "{{ service.image }}"
10+
volumes: "{{ service.volumes }}"
11+
command: "{{ service.command }}"
12+
state: "{{ service.state }}"
13+
restart: "yes"
14+
restart_policy: "{{ service.restart_policy }}"
15+
privileged: "{{ service.privileged }}"
16+
network_mode: "{{ service.network_mode }}"
17+
log_driver: "{{ service.log_driver }}"
18+
log_options: "{{ service.log_options }}"
19+
when:
20+
- inventory_hostname in groups[service.group]
21+
- service.enabled | bool
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# yamllint disable rule:line-length
3+
argument_specs:
4+
main:
5+
short_description: "Prometheus nginx_exporter"
6+
description:
7+
- "Deploy prometheus L(nginx exporter,https://github.com/nginxinc/nginx-prometheus-exporter) using ansible"
8+
author:
9+
- "Prometheus Community"
10+
options:
11+
nginx_exporter_version:
12+
description: "nginx_exporter package version. Also accepts latest as parameter."
13+
default: "1.4.1"
14+
nginx_exporter_plus:
15+
description: "Start the exporter for NGINX Plus."
16+
type: bool
17+
default: false
18+
nginx_exporter_web_listen_address:
19+
description: "Address on which nginx exporter will listen"
20+
default: "0.0.0.0:9113"
21+
nginx_exporter_web_telemetry_path:
22+
description: "Path under which to expose metrics"
23+
default: "/metrics"
24+
nginx_exporter_tls_server_config:
25+
description:
26+
- "Configuration for TLS authentication."
27+
- "Keys and values are the same as in L(nginx_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
28+
type: "dict"
29+
nginx_exporter_http_server_config:
30+
description:
31+
- "Config for HTTP/2 support."
32+
- "Keys and values are the same as in L(nginx_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
33+
type: "dict"
34+
nginx_exporter_basic_auth_users:
35+
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
36+
type: "dict"
37+
nginx_exporter_scrape_uri:
38+
description: "A URI or unix domain socket path for scraping NGINX or NGINX Plus metrics. For NGINX, the stub_status page must be available through the URI."
39+
default: "http://127.0.0.1/stub_status"
40+
nginx_exporter_config_dir:
41+
description: "Path to directory with nginx_exporter configuration"
42+
default: "/etc/nginx_exporter"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
galaxy_info:
3+
author: "Kien Nguyen Tuan"
4+
description: "Prometheus nginx_exporter"
5+
license: "Apache"
6+
min_ansible_version: "2.9"
7+
platforms:
8+
- name: "Ubuntu"
9+
versions:
10+
- "focal"
11+
- "jammy"
12+
- "noble"
13+
- name: "Debian"
14+
versions:
15+
- "bullseye"
16+
- name: "EL"
17+
versions:
18+
- "8"
19+
- "9"
20+
galaxy_tags:
21+
- "monitoring"
22+
- "prometheus"
23+
- "exporter"
24+
- "metrics"
25+
- "system"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
- name: Ensuring nginx_exporter config directory exist
3+
ansible.builtin.file:
4+
path: "{{ nginx_exporter_config_dir }}"
5+
state: "directory"
6+
owner: "{{ config_owner_user }}"
7+
group: "{{ config_owner_group }}"
8+
mode: 0755
9+
10+
- name: Recreating if not exist running containers
11+
community.general.docker_container:
12+
name: "{{ item.value.container_name }}"
13+
image: "{{ item.value.image }}"
14+
volumes: "{{ item.value.volumes }}"
15+
command: "{{ item.value.command }}"
16+
state: "{{ item.value.state }}"
17+
restart_policy: "{{ item.value.restart_policy }}"
18+
privileged: "{{ item.value.privileged }}"
19+
network_mode: "{{ item.value.network_mode }}"
20+
log_driver: "{{ item.value.log_driver }}"
21+
log_options: "{{ item.value.log_options }}"
22+
memory: "{{ item.value.memory }}"
23+
memory_swap: "{{ item.value.memory_swap }}"
24+
cpus: "{{ item.value.cpus }}"
25+
env: "{{ item.value.env }}"
26+
when:
27+
- inventory_hostname in groups[item.value.group]
28+
- item.value.enabled | bool
29+
with_dict: "{{ nginx_exporter_services }}"

0 commit comments

Comments
 (0)