From bca97989420f254ed7d47d2f625086b0b15deb63 Mon Sep 17 00:00:00 2001 From: Camille Prugnard Date: Tue, 5 Oct 2021 16:52:47 +0200 Subject: [PATCH] add phpfpm exporter --- README.md | 44 ++++++++-------- defaults/main.yml | 2 + handlers/main.yml | 6 +++ tasks/main.yml | 5 ++ tasks/phpfpm_exporter.yml | 51 +++++++++++++++++++ templates/conf/prometheus.yml.j2 | 11 ++++ .../prometheus-phpfpm-exporter-script.sh.j2 | 2 + .../prometheus-phpfpm-exporter.service.j2 | 2 +- 8 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 tasks/phpfpm_exporter.yml create mode 100644 templates/scripts/prometheus-phpfpm-exporter-script.sh.j2 diff --git a/README.md b/README.md index 3c5dac8..ab11c39 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 | | | -| 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 | | | +| 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/defaults/main.yml b/defaults/main.yml index 51034c6..37bf178 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -10,6 +10,8 @@ prometheus_mongodb_exporter_database: "admin" prometheus_mongodb_exporter_host: "localhost" prometheus_mongodb_exporter_port: 27017 +prometheus_phpfpm_sockets_directory: "/run/php/" + prometheus_postgres_exporter_host: "/var/run/postgresql/" prometheus_postgres_exporter_sslmode: "disable" prometheus_postgres_exporter_user: "postgres" diff --git a/handlers/main.yml b/handlers/main.yml index a9f1d2d..66f0fd7 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -33,3 +33,9 @@ enabled: yes tags: ['prometheus'] +- name: 'prometheus | phpfpm exporter | restart service' + systemd: + name: prometheus-phpfpm-exporter.service + state: restarted + enabled: yes + tags: ['prometheus'] diff --git a/tasks/main.yml b/tasks/main.yml index 1e8934b..c25b758 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -29,3 +29,8 @@ include_tasks: 'postgresql_exporter.yml' when: - "'postgresql' in prometheus_exporter_packages" + +- name: 'prometheus | PHP Fpm exporter' + include_tasks: 'phpfpm_exporter.yml' + when: + - "'phpfpm' in prometheus_exporter_packages" diff --git a/tasks/phpfpm_exporter.yml b/tasks/phpfpm_exporter.yml new file mode 100644 index 0000000..ec30865 --- /dev/null +++ b/tasks/phpfpm_exporter.yml @@ -0,0 +1,51 @@ +--- +- name: 'prometheus | phpfpm exporter | get latest release from GitHub' + shell: curl --silent "https://api.github.com/repos/Lusitaniae/phpfpm_exporter/releases/latest" | jq -r .tag_name | sed "s/v//" + args: + warn: false + register: prometheus_phpfpm_exporter_latest_version + run_once: true + +- name: 'prometheus | phpfpm exporter | download' + get_url: + url: https://github.com/Lusitaniae/phpfpm_exporter/releases/download/v{{ prometheus_phpfpm_exporter_version|default(prometheus_phpfpm_exporter_latest_version.stdout) }}/phpfpm_exporter-{{ prometheus_phpfpm_exporter_version|default(prometheus_phpfpm_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | phpfpm exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/phpfpm_exporter-{{ prometheus_phpfpm_exporter_version|default(prometheus_phpfpm_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | phpfpm exporter | move to /usr/local/bin' + copy: + src: /tmp/phpfpm_exporter-{{ prometheus_phpfpm_exporter_version|default(prometheus_phpfpm_exporter_latest_version.stdout) }}.linux-amd64/phpfpm_exporter + dest: /usr/local/bin/prometheus-phpfpm-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + notify: + - 'prometheus | phpfpm exporter | restart service' + +- name: 'prometheus | phpfpm exporter | install startup script' + template: + src: scripts/prometheus-phpfpm-exporter-script.sh.j2 + dest: /usr/local/bin/prometheus-phpfpm-exporter-script.sh + owner: root + group: root + mode: 0600 + +- name: 'prometheus | phpfpm exporter | install unit file to systemd' + template: + src: systemd/prometheus-phpfpm-exporter.service.j2 + dest: /etc/systemd/system/prometheus-phpfpm-exporter.service + owner: root + group: root + mode: 0600 + +- name: 'prometheus | phpfpm exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + name: prometheus-phpfpm-exporter.service diff --git a/templates/conf/prometheus.yml.j2 b/templates/conf/prometheus.yml.j2 index 2b6ef93..1f52e96 100644 --- a/templates/conf/prometheus.yml.j2 +++ b/templates/conf/prometheus.yml.j2 @@ -74,3 +74,14 @@ scrape_configs: - {{ target }}:9187 {% endfor %} {% endif %} + +{% if prometheus_phpfpm_exporter_targets is defined %} + - job_name: phpfpm + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% for target in prometheus_phpfpm_exporter_targets %} + - {{ target }}:9253 +{% endfor %} +{% endif %} diff --git a/templates/scripts/prometheus-phpfpm-exporter-script.sh.j2 b/templates/scripts/prometheus-phpfpm-exporter-script.sh.j2 new file mode 100644 index 0000000..8634fbe --- /dev/null +++ b/templates/scripts/prometheus-phpfpm-exporter-script.sh.j2 @@ -0,0 +1,2 @@ +#!/bin/bash +/usr/local/bin/prometheus-phpfpm-exporter $(ls -1 {{ prometheus_phpfpm_sockets_directory }} | awk '{print "--phpfpm.socket-paths {{ prometheus_phpfpm_sockets_directory }}" $1}' |head -c -1) diff --git a/templates/systemd/prometheus-phpfpm-exporter.service.j2 b/templates/systemd/prometheus-phpfpm-exporter.service.j2 index f899eef..26f371e 100644 --- a/templates/systemd/prometheus-phpfpm-exporter.service.j2 +++ b/templates/systemd/prometheus-phpfpm-exporter.service.j2 @@ -4,7 +4,7 @@ After=network.target [Service] Type=simple -ExecStart=/usr/local/bin/prometheus-phpfpm-exporter +ExecStart=/bin/bash /usr/local/bin/prometheus-phpfpm-exporter-script.sh [Install] WantedBy=multi-user.target