From 4c9be50c296a1d809c49b637650ceee5c290b436 Mon Sep 17 00:00:00 2001 From: Camille Prugnard Date: Fri, 8 Oct 2021 15:22:59 +0200 Subject: [PATCH] add varnish exporter --- README.md | 44 +++++++++---------- handlers/main.yml | 7 +++ tasks/main.yml | 5 +++ tasks/varnish_exporter.yml | 43 ++++++++++++++++++ templates/conf/prometheus.yml.j2 | 11 +++++ .../prometheus-varnish-exporter.service.j2 | 4 +- 6 files changed, 90 insertions(+), 24 deletions(-) create mode 100644 tasks/varnish_exporter.yml diff --git a/README.md b/README.md index 118bd58..d489eda 100644 --- a/README.md +++ b/README.md @@ -12,28 +12,28 @@ It has been tested on : Role variables -------------- -| Variable | Type | Choices | Default | Comment | -|----------------------------------------------|---------|------------------------------------------------------------------|-------------|-----------------| -| prometheus_server_enable | string | true / false | | | -| prometheus_server_version | string | | latest | | -| prometheus_server_monitor | string | | | | -| prometheus_server_global_scrape_interval | string | | 15 | | -| prometheus_server_global_evaluation_interval | string | | 15 | | -| prometheus_server_job_name | string | | prometheus | | -| prometheus_server_scrape_interval | string | | 5 | | -| prometheus_server_scrape_timeout | string | | 5 | | -| prometheus_exporter_packages | list | node / mysqld / postgresql / mongodb / phpfpm / apache / haproxy | | | -| prometheus_node_exporter_targets | list | | | | -| prometheus_mysqld_exporter_targets | list | | | | -| prometheus_mongodb_exporter_targets | list | | | | -| prometheus_postgres_exporter_targets | list | | | | -| prometheus_mysqld_exporter_user | string | | | | -| prometheus_mysqld_exporter_password | string | | | | -| prometheus_mongodb_exporter_host | string | | localhost | | -| prometheus_mongodb_exporter_port | string | | 27017 | | -| prometheus_mongodb_exporter_user | string | | | | -| prometheus_mongodb_exporter_pass | string | | | | -| prometheus_server_version | string | | latest | | +| Variable | Type | Choices | Default | Comment | +|----------------------------------------------|---------|----------------------------------------------------------------------------|-------------|-----------------| +| prometheus_server_enable | string | true / false | | | +| prometheus_server_version | string | | latest | | +| prometheus_server_monitor | string | | | | +| prometheus_server_global_scrape_interval | string | | 15 | | +| prometheus_server_global_evaluation_interval | string | | 15 | | +| prometheus_server_job_name | string | | prometheus | | +| prometheus_server_scrape_interval | string | | 5 | | +| prometheus_server_scrape_timeout | string | | 5 | | +| prometheus_exporter_packages | list | node / mysqld / postgresql / mongodb / phpfpm / apache / haproxy / varnish | | | +| prometheus_node_exporter_targets | list | | | | +| prometheus_mysqld_exporter_targets | list | | | | +| prometheus_mongodb_exporter_targets | list | | | | +| prometheus_postgres_exporter_targets | list | | | | +| prometheus_mysqld_exporter_user | string | | | | +| prometheus_mysqld_exporter_password | string | | | | +| prometheus_mongodb_exporter_host | string | | localhost | | +| prometheus_mongodb_exporter_port | string | | 27017 | | +| prometheus_mongodb_exporter_user | string | | | | +| prometheus_mongodb_exporter_pass | string | | | | +| prometheus_server_version | string | | latest | | Dependencies ------------ diff --git a/handlers/main.yml b/handlers/main.yml index 3c12cb6..bc7d932 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -46,3 +46,10 @@ state: restarted enabled: yes tags: ['prometheus'] + +- name: 'prometheus | varnish exporter | restart service' + systemd: + name: prometheus-varnish-exporter.service + state: restarted + enabled: yes + tags: ['prometheus'] diff --git a/tasks/main.yml b/tasks/main.yml index 906ad34..bda4924 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -39,3 +39,8 @@ include_tasks: 'apache_exporter.yml' when: - "'apache' in prometheus_exporter_packages" + +- name: 'prometheus | Varnish exporter' + include_tasks: 'varnish_exporter.yml' + when: + - "'varnish' in prometheus_exporter_packages" diff --git a/tasks/varnish_exporter.yml b/tasks/varnish_exporter.yml new file mode 100644 index 0000000..54acd3f --- /dev/null +++ b/tasks/varnish_exporter.yml @@ -0,0 +1,43 @@ +--- +- name: 'prometheus | varnish exporter | get latest release from GitHub' + shell: curl --silent "https://api.github.com/repos/jonnenauha/prometheus_varnish_exporter/releases/latest" | jq -r .tag_name | sed "s/v//" + args: + warn: false + register: prometheus_varnish_exporter_latest_version + run_once: true + +- name: 'prometheus | varnish exporter | download' + get_url: + url: https://github.com/jonnenauha/prometheus_varnish_exporter/releases/download/v{{ prometheus_varnish_exporter_version|default(prometheus_varnish_exporter_latest_version.stdout) }}/varnish_exporter-{{ prometheus_varnish_exporter_version|default(prometheus_varnish_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | varnish exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/varnish_exporter-{{ prometheus_varnish_exporter_version|default(prometheus_varnish_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | varnish exporter | move to /usr/local/bin' + copy: + src: /tmp/varnish_exporter-{{ prometheus_varnish_exporter_version|default(prometheus_varnish_exporter_latest_version.stdout) }}.linux-amd64/varnish_exporter + dest: /usr/local/bin/prometheus-varnish-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + notify: + - 'prometheus | varnish exporter | restart service' + +- name: 'prometheus | varnish exporter | install unit file to systemd' + template: + src: systemd/prometheus-varnish-exporter.service.j2 + dest: /etc/systemd/system/prometheus-varnish-exporter.service + owner: root + group: root + mode: 0600 + +- name: 'prometheus | varnish exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + name: prometheus-varnish-exporter.service diff --git a/templates/conf/prometheus.yml.j2 b/templates/conf/prometheus.yml.j2 index dd20d51..d78619f 100644 --- a/templates/conf/prometheus.yml.j2 +++ b/templates/conf/prometheus.yml.j2 @@ -107,3 +107,14 @@ scrape_configs: - {{ target }}:8404 {% endfor %} {% endif %} + +{% if prometheus_varnish_exporter_targets is defined %} + - job_name: varnish + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% for target in prometheus_varnish_exporter_targets %} + - {{ target }}:9131 +{% endfor %} +{% endif %} diff --git a/templates/systemd/prometheus-varnish-exporter.service.j2 b/templates/systemd/prometheus-varnish-exporter.service.j2 index 4b944e7..a1c994a 100644 --- a/templates/systemd/prometheus-varnish-exporter.service.j2 +++ b/templates/systemd/prometheus-varnish-exporter.service.j2 @@ -1,10 +1,10 @@ [Unit] Description=Prometheus Varnish Exporter After=network.target - + [Service] Type=simple ExecStart=/usr/local/bin/prometheus-varnish-exporter - + [Install] WantedBy=multi-user.target