From 14baee3d400b142031a2ce76ce1065a8089dc5d6 Mon Sep 17 00:00:00 2001 From: "jean-yves.fournier" Date: Wed, 5 Jan 2022 15:44:39 +0100 Subject: [PATCH] add opcache --- defaults/main.yml | 2 + handlers/main.yml | 7 +++ tasks/main.yml | 5 +++ tasks/php-opcache_exporter.yml | 44 +++++++++++++++++++ .../conf/prometheus-php-opcache-exporter.j2 | 4 ++ templates/conf/prometheus.yml.j2 | 12 +++++ ...prometheus-php-opcache-exporter.service.j2 | 11 +++++ 7 files changed, 85 insertions(+) create mode 100644 tasks/php-opcache_exporter.yml create mode 100644 templates/conf/prometheus-php-opcache-exporter.j2 create mode 100644 templates/systemd/prometheus-php-opcache-exporter.service.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 72a025c..131ce93 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -15,6 +15,8 @@ prometheus_mongodb_exporter_port: 27017 prometheus_phpfpm_sockets_directory: "/run/php/" prometheus_phpfpm_sockets_directory_additional: "/var/run/php*.sock" +prometheus_php-opcache_exporter_fcgi-uri: "unix:///var/run/php-000-default.sock" + prometheus_postgres_exporter_systemd_user: "postgres" prometheus_postgres_exporter_host: "/var/run/postgresql/" prometheus_postgres_exporter_sslmode: "disable" diff --git a/handlers/main.yml b/handlers/main.yml index 431d005..a518a1d 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -40,6 +40,13 @@ enabled: yes tags: ['prometheus'] +- name: 'prometheus | php-opcache exporter | restart service' + systemd: + name: prometheus-php-opcache-exporter.service + state: restarted + enabled: yes + tags: ['prometheus'] + - name: 'prometheus | apache exporter | restart service' systemd: name: prometheus-apache-exporter.service diff --git a/tasks/main.yml b/tasks/main.yml index f6b0af7..c23e7d3 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -35,6 +35,11 @@ when: - "'phpfpm' in prometheus_exporter_packages" +- name: 'prometheus | PHP opcache exporter' + include_tasks: 'php-opcache_exporter.yml' + when: + - "'php-opcache' in prometheus_exporter_packages" + - name: 'prometheus | Apache exporter' include_tasks: 'apache_exporter.yml' when: diff --git a/tasks/php-opcache_exporter.yml b/tasks/php-opcache_exporter.yml new file mode 100644 index 0000000..83cf5db --- /dev/null +++ b/tasks/php-opcache_exporter.yml @@ -0,0 +1,44 @@ +--- +- name: 'prometheus | php-opcache exporter | get latest release from GitHub' + shell: curl --silent "https://api.github.com/repos/MyOnlineStore/opcache-exporter/releases/latest" | jq -r .tag_name | sed "s/v//" + args: + warn: false + register: prometheus_php-opcache_exporter_latest_version + run_once: true + +- name: 'prometheus | php-opcache exporter | download' + get_url: + url: https://github.com/MyOnlineStore/opcache-exporter/releases/download/v{{ prometheus_php-opcache_exporter_version|default(prometheus_php-opcache_exporter_latest_version.stdout) }}/opcache_exporter-{{ prometheus_php-opcache_exporter_version|default(prometheus_php-opcache_exporter_latest_version.stdout) }}-linux-amd64 + dest: /tmp + +- name: 'prometheus | php-opcache exporter | move to /usr/local/bin' + copy: + src: /tmp/opcache_exporter-{{ prometheus_php-opcache_exporter_version|default(prometheus_php-opcache_exporter_latest_version.stdout) }}-linux-amd64 + dest: /usr/local/bin/prometheus-php-opcache-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + +- name: 'prometheus | php-opcache exporter | configuration' + template: + src: conf/prometheus-php-opcache-exporter.j2 + dest: /etc/default/prometheus-php-opcache-exporter + owner: root + group: root + mode: 0600 + register: prometheus_php-opcache_exporter_configuration + +- name: 'prometheus | php-opcache exporter | install unit file to systemd' + template: + src: systemd/prometheus-php-opcache-exporter.service.j2 + dest: /etc/systemd/system/prometheus-php-opcache-exporter.service + owner: root + group: root + mode: 0600 + +- name: 'prometheus | php-opcache exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + name: prometheus-php-opcache-exporter.service diff --git a/templates/conf/prometheus-php-opcache-exporter.j2 b/templates/conf/prometheus-php-opcache-exporter.j2 new file mode 100644 index 0000000..93babce --- /dev/null +++ b/templates/conf/prometheus-php-opcache-exporter.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% if (prometheus_php-opcache_exporter_fcgi-uri is defined) %} +ARGS='"--opcache.fcgi-uri="{{ prometheus_php-opcache_exporter_fcgi-uri }}"' +{% endif %} diff --git a/templates/conf/prometheus.yml.j2 b/templates/conf/prometheus.yml.j2 index 13a3ce2..7b44a42 100644 --- a/templates/conf/prometheus.yml.j2 +++ b/templates/conf/prometheus.yml.j2 @@ -124,6 +124,18 @@ scrape_configs: {% endfor %} {% endif %} +{% if prometheus_php-opcache_exporter_targets is defined %} + # PHP-FPM exporter # + - job_name: php-opcache + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% for target in prometheus_php-opcache_exporter_targets %} + - {{ target }}:9101 +{% endfor %} +{% endif %} + {% if prometheus_apache_exporter_targets is defined %} # apache exporter # - job_name: apache diff --git a/templates/systemd/prometheus-php-opcache-exporter.service.j2 b/templates/systemd/prometheus-php-opcache-exporter.service.j2 new file mode 100644 index 0000000..3e76aa2 --- /dev/null +++ b/templates/systemd/prometheus-php-opcache-exporter.service.j2 @@ -0,0 +1,11 @@ +[Unit] +Description=Prometheus PHP Opcache Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-php-opcache-exporter +EnvironmentFile=/etc/default/prometheus-php-opcache-exporter + +[Install] +WantedBy=multi-user.target