From 7e91930523a354db07e673a89c208a7aca2e796a Mon Sep 17 00:00:00 2001 From: Ludovic Cartier Date: Tue, 25 May 2021 18:25:22 +0200 Subject: [PATCH] many corrections --- tasks/asserts.yml | 8 ++ tasks/main.yml | 32 ++++++- tasks/mongodb_exporter.yml | 4 +- tasks/mysqld_exporter.yml | 4 +- tasks/node_exporter.yml | 6 +- tasks/postgresql_exporter.yml | 12 ++- tasks/prometheus.yml | 37 ++++---- templates/prometheus.yml | 74 ---------------- templates/prometheus.yml.j2 | 85 +++++++++++++++++++ ...=> prometheus-haproxy-exporter.service.j2} | 0 ...=> prometheus-mongodb-exporter.service.j2} | 0 ... => prometheus-mysqld-exporter.service.j2} | 0 ...j2 => prometheus-node-exporter.service.j2} | 0 ... => prometheus-phpfpm-exporter.service.j2} | 0 ...> prometheus-postgres-exporter.service.j2} | 0 ...2 => prometheus-redis-exporter.service.j2} | 0 ...=> prometheus-varnish-exporter.service.j2} | 0 17 files changed, 162 insertions(+), 100 deletions(-) delete mode 100644 templates/prometheus.yml create mode 100644 templates/prometheus.yml.j2 rename templates/systemd/{prometheus_haproxy_exporter.service.j2 => prometheus-haproxy-exporter.service.j2} (100%) rename templates/systemd/{prometheus_mongodb_exporter.service.j2 => prometheus-mongodb-exporter.service.j2} (100%) rename templates/systemd/{prometheus_mysqld_exporter.service.j2 => prometheus-mysqld-exporter.service.j2} (100%) rename templates/systemd/{prometheus_node_exporter.service.j2 => prometheus-node-exporter.service.j2} (100%) rename templates/systemd/{prometheus_phpfpm_exporter.service.j2 => prometheus-phpfpm-exporter.service.j2} (100%) rename templates/systemd/{prometheus_postgres_exporter.service.j2 => prometheus-postgres-exporter.service.j2} (100%) rename templates/systemd/{prometheus_redis_exporter.service.j2 => prometheus-redis-exporter.service.j2} (100%) rename templates/systemd/{prometheus_varnish_exporter.service.j2 => prometheus-varnish-exporter.service.j2} (100%) diff --git a/tasks/asserts.yml b/tasks/asserts.yml index eeb2c8f..e0adabd 100644 --- a/tasks/asserts.yml +++ b/tasks/asserts.yml @@ -17,3 +17,11 @@ that: "'docker-ce' in ansible_facts.packages" tags: - prometheus + +- name: 'prometheus | assert | check if docker pyhon libraries are installed' + assert: + that: + - "'python-docker' in ansible_facts.packages" + - "'python3-docker' in ansible_facts.packages" + tags: + - prometheus diff --git a/tasks/main.yml b/tasks/main.yml index 8e2e18b..5036055 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,5 +2,33 @@ - name: 'prometheus | include asserts' include_tasks: 'asserts.yml' -- name: 'prometheus | install' - include_tasks: 'install.yml' +- name: 'prometheus | server install' + include_tasks: 'prometheus.yml' + when: + - prometheus_server_enable is defined + - prometheus_server_enable == "true" + +- name: 'prometheus | Node exporter' + include_tasks: 'node_exporter.yml' + when: + - prometheus_node_exporter_enable is defined + - prometheus_node_exporter_enable == "true" + +- name: 'prometheus | MongoDB exporter' + include_tasks: 'mongodb_exporter.yml' + when: + - prometheus_mongodb_exporter_enable is defined + - prometheus_mongodb_exporter_enable == "true" + +- name: 'prometheus | MySQL exporter' + include_tasks: 'mysqld_exporter.yml' + when: + - prometheus_mysqld_exporter_enable is defined + - prometheus_mysqld_exporter_enable == "true" + +- name: 'prometheus | PostgreSQL exporter' + include_tasks: 'postgresql_exporter.yml' + when: + - prometheus_postgres_exporter_enable is defined + - prometheus_postgres_exporter_enable == "true" + diff --git a/tasks/mongodb_exporter.yml b/tasks/mongodb_exporter.yml index b913bb7..afeb897 100644 --- a/tasks/mongodb_exporter.yml +++ b/tasks/mongodb_exporter.yml @@ -24,7 +24,7 @@ - name: 'prometheus | mongodb exporter | install unit file to systemd' template: - src: templates/systemd/prometheus-mongodb-exporter.service.j2 + src: systemd/prometheus-mongodb-exporter.service.j2 dest: /etc/systemd/system/prometheus-mongodb-exporter.service owner: root group: root @@ -36,5 +36,5 @@ daemon_reload: yes enabled: yes state: started - name: mongodb_exporter.service + name: prometheus-mongodb-exporter.service when: prometheus_mongodb_exporter_enable == "true" diff --git a/tasks/mysqld_exporter.yml b/tasks/mysqld_exporter.yml index 844e942..6dac399 100644 --- a/tasks/mysqld_exporter.yml +++ b/tasks/mysqld_exporter.yml @@ -40,7 +40,7 @@ - name: 'prometheus | mysqld exporter | install unit file to systemd' template: - src: templates/systemd/prometheus-mysqld-exporter.service.j2 + src: systemd/prometheus-mysqld-exporter.service.j2 dest: /etc/systemd/system/prometheus-mysqld-exporter.service owner: root group: root @@ -52,5 +52,5 @@ daemon_reload: yes enabled: yes state: started - name: mysqld_exporter.service + name: prometheus-mysqld-exporter.service when: prometheus_mysqld_exporter_enable == "true" diff --git a/tasks/node_exporter.yml b/tasks/node_exporter.yml index 54c6700..754d318 100644 --- a/tasks/node_exporter.yml +++ b/tasks/node_exporter.yml @@ -24,8 +24,8 @@ - name: 'prometheus | node exporter | install unit file to systemd' template: - src: templates/systemd/prometheus_node_exporter.service.j2 - dest: /etc/systemd/system/prometheus_node_exporter.service + src: systemd/prometheus-node-exporter.service.j2 + dest: /etc/systemd/system/prometheus-node-exporter.service owner: root group: root mode: 0600 @@ -36,5 +36,5 @@ daemon_reload: yes enabled: yes state: started - name: node_exporter.service + name: prometheus-node-exporter.service when: prometheus_node_exporter_enable == "true" diff --git a/tasks/postgresql_exporter.yml b/tasks/postgresql_exporter.yml index 8e05715..2aef4b7 100644 --- a/tasks/postgresql_exporter.yml +++ b/tasks/postgresql_exporter.yml @@ -22,7 +22,7 @@ mode: 0755 when: prometheus_postgres_exporter_enable == "true" -- name: 'prometheus | mysql exporter | configuration' +- name: 'prometheus | postgres exporter | configuration' copy: dest: '/var/lib/prometheus/.my.cnf' owner: root @@ -58,10 +58,18 @@ - name: 'prometheus | postgres exporter | install unit file to systemd' template: - src: templates/systemd/prometheus-postgres-exporter.service.j2 + src: systemd/prometheus-postgres-exporter.service.j2 dest: /etc/systemd/system/prometheus-postgres-exporter.service owner: root group: root mode: 0600 when: prometheus_postgres_exporter_enable == "true" + +- name: 'prometheus | postgres exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + state: started + name: prometheus-postgres-exporter.service + when: prometheus_postgres_exporter_enable == "true" diff --git a/tasks/prometheus.yml b/tasks/prometheus.yml index 8d3f23f..3762a2f 100644 --- a/tasks/prometheus.yml +++ b/tasks/prometheus.yml @@ -1,40 +1,49 @@ --- -- name: 'prometheus | create docker network' +- name: 'prometheus | server | assert | check mandatory vars are defined' + assert: + that: + - "prometheus_server_monitor is defined" + tags: + - prometheus + +- name: 'prometheus | server | create docker network' docker_network: name: 'prometheus' tags: - prometheus - when: prometheus_enable == "true" + when: prometheus_server_enable == "true" -- name: 'prometheus | create docker volume data' +- name: 'prometheus | server | create docker volume data' docker_volume: name: prometheus_data register: register_docker_volume_prometheus__prometheus_data - when: prometheus_enable == "true" + when: prometheus_server_enable == "true" tags: - prometheus -- name: 'prometheus | create prometheus directory to /etc' +- name: 'prometheus | server | create prometheus directory to /etc' file: path: /etc/prometheus state: directory mode: '0755' - when: prometheus_enable == "true" + when: prometheus_server_enable == "true" tags: - prometheus -- name: 'prometheus | copy config prometheus yml' +- name: 'prometheus | server | copy config prometheus yml' template: - src: templates/prometheus.yml.j2 + src: prometheus.yml.j2 dest: /etc/prometheus/prometheus.yml owner: root group: root mode: 0644 - when: prometheus_enable == "true" + when: + - prometheus_server_enable == "true" + - prometheus_server_monitor is defined tags: - prometheus -- name: 'prometheus | deploy container' +- name: 'prometheus | server | deploy container' docker_container: name: prometheus hostname: '{{ inventory_hostname }}' @@ -52,14 +61,12 @@ - { name: prometheus } ports: - "9090:9090" - etc_hosts: - foo: 10.1.1.1 - bar: 10.1.1.2 log_driver: syslog log_options: tag: docker_prometheus restart_policy: 'unless-stopped' pull: '{{ awh_services_docker_pull|default("no") }}' - when: prometheus_enable == "true" - tags: + when: prometheus_server_enable == "true" + tags: - prometheus + diff --git a/templates/prometheus.yml b/templates/prometheus.yml deleted file mode 100644 index d66d33c..0000000 --- a/templates/prometheus.yml +++ /dev/null @@ -1,74 +0,0 @@ -# Sample config for Prometheus. - -global: - scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. - evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. - # scrape_timeout is set to the global default (10s). - - # Attach these labels to any time series or alerts when communicating with - # external systems (federation, remote storage, Alertmanager). - external_labels: - monitor: '{{ prometheus_monitor }}' - -{% if prometheus_rules is defined %} -# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. -rule_files: - - 'rules.yml' -{% endif %} - -# A scrape configuration containing exactly one endpoint to scrape: -# Here it's Prometheus itself. -scrape_configs: - # The job name is added as a label `job=` to any timeseries scraped from this config. - - job_name: 'prometheus' - - # Override the global default and scrape targets from this job every 5 seconds. - scrape_interval: 5s - scrape_timeout: 5s - - # metrics_path defaults to '/metrics' - # scheme defaults to 'http'. - - static_configs: - - targets: ['localhost:9090'] - -{% if prometheus_node_exporter_enable == "true" %} - - job_name: node - scrape_interval: 30s - scrape_timeout: 10s - static_configs: - - targets: - - foo:9100 - - bar:9100 -{% endif %} - -{% if prometheus_mysqld_exporter_enable == "true" %} - - job_name: mysql - scrape_interval: 30s - scrape_timeout: 10s - static_configs: - - targets: - - foo:9104 - - bar:9104 -{% endif %} - -{% if prometheus_mongodb_exporter_enable == "true" %} - - job_name: mongodb - scrape_interval: 30s - scrape_timeout: 10s - static_configs: - - targets: - - foo:9001 - - bar:9001 -{% endif %} - -{% if prometheus_postgres_exporter_enable == "true" %} - - job_name: postgresql - scrape_interval: 30s - scrape_timeout: 10s - static_configs: - - targets: - - foo:9001 - - bar:9001 -{% endif %} - diff --git a/templates/prometheus.yml.j2 b/templates/prometheus.yml.j2 new file mode 100644 index 0000000..4f15dad --- /dev/null +++ b/templates/prometheus.yml.j2 @@ -0,0 +1,85 @@ +# {{ ansible_managed }} + +global: + scrape_interval: '{{ prometheus_server_global_scrape_interval | default(15) }}'s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: '{{ prometheus_server_global_evaluation_interval | default(15) }}'s # Evaluate rules every 15 seconds. The default is every 1 minute. + + # Attach these labels to any time series or alerts when communicating with + # external systems (federation, remote storage, Alertmanager). + external_labels: + monitor: '{{ prometheus_server_monitor }}' + +{% if prometheus_server_rules is defined %} +# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. +rule_files: + - 'rules.yml' +{% endif %} + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: '{{ prometheus_server_job_name | default("prometheus") }}' + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: '{{ prometheus_server_scrape_interval | default(5) }}'s + scrape_timeout: '{{ prometheus_server_scrape_timeout | default(5) }}'s + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ['localhost:9090'] + +{% if (prometheus_node_exporter_enable is defined) and (prometheus_node_exporter_enable == "true") %} + - job_name: node + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% if prometheus_node_exporter_targets is defined %} +{% for target in prometheus_node_exporter_targets %} + - {{ target }}:9100 +{% endfor %} +{% endif %} +{% endif %} + +{% if (prometheus_mysqld_exporter_enable is defined) and (prometheus_mysqld_exporter_enable == "true") %} + - job_name: mysql + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% if prometheus_mysql_exporter_targets is defined %} +{% for target in prometheus_mysqld_exporter_targets %} + - {{ target }}:9104 +{% endfor %} +{% endif %} +{% endif %} + +{% if (prometheus_mongodb_exporter_enable is defined) and (prometheus_mongodb_exporter_enable == "true") %} + - job_name: mongodb + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets +{% if prometheus_mongodb_exporter_targets is defined %} +{% for target in prometheus_mongodb_exporter_targets %} + - {{ target }}:9001 +{% endfor %} +{% endif %} +{% endif %} + +{% if (prometheus_postgres_exporter_enable is defined) and (prometheus_postgres_exporter_enable == "true") %} + - job_name: postgresql + scrape_interval: 30s + scrape_timeout: 10s + static_configs: + - targets: +{% if prometheus_postgres_exporter_targets is defined %} +{% for target in prometheus_postgres_exporter_targets %} + - {{ target }}:9187 +{% endfor %} +{% endif %} +{% endif %} + diff --git a/templates/systemd/prometheus_haproxy_exporter.service.j2 b/templates/systemd/prometheus-haproxy-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_haproxy_exporter.service.j2 rename to templates/systemd/prometheus-haproxy-exporter.service.j2 diff --git a/templates/systemd/prometheus_mongodb_exporter.service.j2 b/templates/systemd/prometheus-mongodb-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_mongodb_exporter.service.j2 rename to templates/systemd/prometheus-mongodb-exporter.service.j2 diff --git a/templates/systemd/prometheus_mysqld_exporter.service.j2 b/templates/systemd/prometheus-mysqld-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_mysqld_exporter.service.j2 rename to templates/systemd/prometheus-mysqld-exporter.service.j2 diff --git a/templates/systemd/prometheus_node_exporter.service.j2 b/templates/systemd/prometheus-node-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_node_exporter.service.j2 rename to templates/systemd/prometheus-node-exporter.service.j2 diff --git a/templates/systemd/prometheus_phpfpm_exporter.service.j2 b/templates/systemd/prometheus-phpfpm-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_phpfpm_exporter.service.j2 rename to templates/systemd/prometheus-phpfpm-exporter.service.j2 diff --git a/templates/systemd/prometheus_postgres_exporter.service.j2 b/templates/systemd/prometheus-postgres-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_postgres_exporter.service.j2 rename to templates/systemd/prometheus-postgres-exporter.service.j2 diff --git a/templates/systemd/prometheus_redis_exporter.service.j2 b/templates/systemd/prometheus-redis-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_redis_exporter.service.j2 rename to templates/systemd/prometheus-redis-exporter.service.j2 diff --git a/templates/systemd/prometheus_varnish_exporter.service.j2 b/templates/systemd/prometheus-varnish-exporter.service.j2 similarity index 100% rename from templates/systemd/prometheus_varnish_exporter.service.j2 rename to templates/systemd/prometheus-varnish-exporter.service.j2