commit 7879d6a13ce951b0230e43de5c3ea5e5b50274a0 Author: Ludovic Cartier Date: Thu Apr 29 18:53:56 2021 +0200 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..7422d7e --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +prometheus +========== + +The present role : + - installs prometheus server inside a Docker container + - installs various prometheus exporter + +It has been tested on : + - Debian 9 + - Debian 10 + +Role variables +-------------- + +| Variable | Type | Choices | Default | Comment | +|------------------------------------|---------|--------------|-------------------------------------------------------------------------------|----------------------------------------------------------------------------| + +Dependencies +------------ + +Docker must installed and running. + +Example Playbook +---------------- + + - hosts: prometheus + ignore_errors: "{{ ansible_check_mode }}" # ignore errors only in check mode ! + + roles: + - { role: brainsys.prometheus, tags: ['prometheus'] } + +Example variables +----------------- + + --- + prometheus_enable: 'true' + prometheus_node_exporter_enable: 'true' + prometheus_mysqld_exporter_enable: 'true' + prometheus_mysqld_exporter_user: 'foo' + prometheus_mysqld_exporter_password: 'bar' + +TODO +---- + +License +------- + +GPLv3 + +Author Information +------------------ + +Written by Ludovic Cartier diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..a7df715 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,5 @@ +--- +prometheus_node_exporter_version: '1.1.2' +prometheus_mysqld_exporter_version: '0.12.1' +prometheus_postgres_exporter_version: '0.9.0' +prometheus_mongodb_exporter_version: '0.20.4' diff --git a/tasks/asserts.yml b/tasks/asserts.yml new file mode 100644 index 0000000..eeb2c8f --- /dev/null +++ b/tasks/asserts.yml @@ -0,0 +1,19 @@ +--- +- name: 'prometheus | assert | check if ansible version >= 2.7' + assert: + that: ansible_version.full is version_compare(2.7, '>=') + msg: Ansible version 2.7 or later is required to use this version of the role + tags: + - prometheus + +- name: 'prometheus | assert | gather the package facts' + ansible.builtin.package_facts: + manager: auto + tags: + - prometheus + +- name: 'prometheus | assert | check if docker-ce is installed' + assert: + that: "'docker-ce' in ansible_facts.packages" + tags: + - prometheus diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..8e2e18b --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: 'prometheus | include asserts' + include_tasks: 'asserts.yml' + +- name: 'prometheus | install' + include_tasks: 'install.yml' diff --git a/tasks/mongodb_exporter.yml b/tasks/mongodb_exporter.yml new file mode 100644 index 0000000..b913bb7 --- /dev/null +++ b/tasks/mongodb_exporter.yml @@ -0,0 +1,40 @@ +--- +- name: 'prometheus | mongodb exporter | download' + get_url: + url: https://github.com/percona/mongodb_exporter/releases/download/v{{ prometheus_mongodb_exporter_version }}/mongodb_exporter-{{ prometheus_mongodb_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_mongodb_exporter_enable == "true" + +- name: 'prometheus | mongodb exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/mongodb_exporter-{{ prometheus_mongodb_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_mongodb_exporter_enable == "true" + +- name: 'prometheus | mongodb exporter | move to /usr/local/bin' + copy: + src: /tmp/mongodb_exporter-{{ prometheus_mongodb_exporter_version }}.linux-amd64/mongodb_exporter + dest: /usr/local/bin/prometheus-mongodb-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + when: prometheus_mongodb_exporter_enable == "true" + +- name: 'prometheus | mongodb exporter | install unit file to systemd' + template: + src: templates/systemd/prometheus-mongodb-exporter.service.j2 + dest: /etc/systemd/system/prometheus-mongodb-exporter.service + owner: root + group: root + mode: 0600 + when: prometheus_mongodb_exporter_enable == "true" + +- name: 'prometheus | mongodb exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + state: started + name: mongodb_exporter.service + when: prometheus_mongodb_exporter_enable == "true" diff --git a/tasks/mysqld_exporter.yml b/tasks/mysqld_exporter.yml new file mode 100644 index 0000000..844e942 --- /dev/null +++ b/tasks/mysqld_exporter.yml @@ -0,0 +1,56 @@ +--- +- name: 'prometheus | mysqld exporter | download' + get_url: + url: https://github.com/prometheus/mysqld_exporter/releases/download/v{{ prometheus_mysqld_exporter_version }}/mysqld_exporter-{{ prometheus_mysqld_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_mysqld_exporter_enable == "true" + +- name: 'prometheus | mysqld exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/mysqld_exporter-{{ prometheus_mysqld_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_mysqld_exporter_enable == "true" + +- name: 'prometheus | mysqld exporter | move to /usr/local/bin' + copy: + src: /tmp/mysqld_exporter-{{ prometheus_mysqld_exporter_version }}.linux-amd64/mysqld_exporter + dest: /usr/local/bin/prometheus-mysqld-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + when: prometheus_mysqld_exporter_enable == "true" + +- name: 'prometheus | mysqld exporter | configuration' + copy: + dest: '/var/lib/prometheus/.my.cnf' + owner: root + group: root + mode: 0644 + backup: yes + content: | + [client] + user={{ prometheus_mysqld_exporter_user }} + password={{ prometheus_mysqld_exporter_password }} + when: + - prometheus_mysqld_exporter_user is defined + - prometheus_mysqld_exporter_enable == "true" + register: prometheus_mysqld_exporter_configuration + +- name: 'prometheus | mysqld exporter | install unit file to systemd' + template: + src: templates/systemd/prometheus-mysqld-exporter.service.j2 + dest: /etc/systemd/system/prometheus-mysqld-exporter.service + owner: root + group: root + mode: 0600 + when: prometheus_mysqld_exporter_enable == "true" + +- name: 'prometheus | mysqld exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + state: started + name: mysqld_exporter.service + when: prometheus_mysqld_exporter_enable == "true" diff --git a/tasks/node_exporter.yml b/tasks/node_exporter.yml new file mode 100644 index 0000000..54c6700 --- /dev/null +++ b/tasks/node_exporter.yml @@ -0,0 +1,40 @@ +--- +- name: 'prometheus | node exporter | download' + get_url: + url: https://github.com/prometheus/node_exporter/releases/download/v{{ prometheus_node_exporter_version }}/node_exporter-{{ prometheus_node_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_node_exporter_enable == "true" + +- name: 'prometheus | node exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/node_exporter-{{ prometheus_node_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_node_exporter_enable == "true" + +- name: 'prometheus | node exporter | move to /usr/local/bin' + copy: + src: /tmp/node_exporter-{{ prometheus_node_exporter_version }}.linux-amd64/node_exporter + dest: /usr/local/bin/prometheus-node-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + when: prometheus_node_exporter_enable == "true" + +- 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 + owner: root + group: root + mode: 0600 + when: prometheus_node_exporter_enable == "true" + +- name: 'prometheus | node exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + state: started + name: node_exporter.service + when: prometheus_node_exporter_enable == "true" diff --git a/tasks/postgresql_exporter.yml b/tasks/postgresql_exporter.yml new file mode 100644 index 0000000..8e05715 --- /dev/null +++ b/tasks/postgresql_exporter.yml @@ -0,0 +1,67 @@ +--- +- name: 'prometheus | postgres exporter | download' + get_url: + url: https://github.com/prometheus-community/postgres_exporter/releases/download/v{{ prometheus_postgres_exporter_version }}/postgres_exporter-{{ prometheus_postgres_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_postgres_exporter_enable == "true" + +- name: 'prometheus | postgres exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/postgres_exporter-{{ prometheus_postgres_exporter_version }}.linux-amd64.tar.gz + dest: /tmp + when: prometheus_postgres_exporter_enable == "true" + +- name: 'prometheus | postgres exporter | move to /usr/local/bin' + copy: + src: /tmp/postgres_exporter-{{ prometheus_postgres_exporter_version }}.linux-amd64/postgres_exporter + dest: /usr/local/bin/prometheus-postgres-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + when: prometheus_postgres_exporter_enable == "true" + +- name: 'prometheus | mysql exporter | configuration' + copy: + dest: '/var/lib/prometheus/.my.cnf' + owner: root + group: root + mode: 0644 + backup: yes + content: | + [client] + user={{ prometheus_postgres_exporter_user }} + password={{ prometheus_postgres_exporter_password }} + when: + - prometheus_postgres_exporter_user is defined + - prometheus_postgres_exporter_enable == "true" + register: prometheus_postgres_exporter_configuration + +- name: 'prometheus | postgresql exporter | configuration' + copy: + dest: '/etc/default/prometheus-postgres-exporter' + owner: root + group: root + mode: 0644 + backup: yes + content: | + DATA_SOURCE_NAME='postgresql://{{ prometheus_exporter_postgresql_user }}:{{ prometheus_exporter_postgresql_password }}@{{ prometheus_exporter_postgresql_host }}:{{prometheus_exporter_postgresql_port }}/'' + ARGS='' + when: + - prometheus_postgres_exporter_user is defined + - prometheus_postgres_exporter_password is defined + - prometheus_postgres_exporter_host is defined + - prometheus_postgres_exporter_port is defined + - prometheus_postgres_exporter_enable == "true" + register: prometheu_postgres_exporter_configuration + +- name: 'prometheus | postgres exporter | install unit file to systemd' + template: + src: templates/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" + diff --git a/tasks/prometheus.yml b/tasks/prometheus.yml new file mode 100644 index 0000000..8d3f23f --- /dev/null +++ b/tasks/prometheus.yml @@ -0,0 +1,65 @@ +--- +- name: 'prometheus | create docker network' + docker_network: + name: 'prometheus' + tags: + - prometheus + when: prometheus_enable == "true" + +- name: 'prometheus | create docker volume data' + docker_volume: + name: prometheus_data + register: register_docker_volume_prometheus__prometheus_data + when: prometheus_enable == "true" + tags: + - prometheus + +- name: 'prometheus | create prometheus directory to /etc' + file: + path: /etc/prometheus + state: directory + mode: '0755' + when: prometheus_enable == "true" + tags: + - prometheus + +- name: 'prometheus | copy config prometheus yml' + template: + src: templates/prometheus.yml.j2 + dest: /etc/prometheus/prometheus.yml + owner: root + group: root + mode: 0644 + when: prometheus_enable == "true" + tags: + - prometheus + +- name: 'prometheus | deploy container' + docker_container: + name: prometheus + hostname: '{{ inventory_hostname }}' + image: prom/prometheus:{{ awh_services_prometheus_version|default("latest") }} + volumes: + - /etc/prometheus/:/etc/prometheus/ + - prometheus_data:/prometheus + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + - '--web.console.libraries=/usr/share/prometheus/console_libraries' + - '--web.console.templates=/usr/share/prometheus/consoles' + - '--web.enable-admin-api' + networks: + - { 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: + - prometheus diff --git a/templates/prometheus.yml b/templates/prometheus.yml new file mode 100644 index 0000000..d66d33c --- /dev/null +++ b/templates/prometheus.yml @@ -0,0 +1,74 @@ +# 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/systemd/prometheus_haproxy_exporter.service.j2 b/templates/systemd/prometheus_haproxy_exporter.service.j2 new file mode 100644 index 0000000..6f9c20f --- /dev/null +++ b/templates/systemd/prometheus_haproxy_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus Haproxy Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-haproxy-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_mongodb_exporter.service.j2 b/templates/systemd/prometheus_mongodb_exporter.service.j2 new file mode 100644 index 0000000..e422779 --- /dev/null +++ b/templates/systemd/prometheus_mongodb_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus MongoDB Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-mongodb-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_mysqld_exporter.service.j2 b/templates/systemd/prometheus_mysqld_exporter.service.j2 new file mode 100644 index 0000000..628335c --- /dev/null +++ b/templates/systemd/prometheus_mysqld_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus MySQL Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-mysqld-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_node_exporter.service.j2 b/templates/systemd/prometheus_node_exporter.service.j2 new file mode 100644 index 0000000..c5fa533 --- /dev/null +++ b/templates/systemd/prometheus_node_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus Node Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-node-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_phpfpm_exporter.service.j2 b/templates/systemd/prometheus_phpfpm_exporter.service.j2 new file mode 100644 index 0000000..f899eef --- /dev/null +++ b/templates/systemd/prometheus_phpfpm_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus PHP-FPM Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-phpfpm-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_postgres_exporter.service.j2 b/templates/systemd/prometheus_postgres_exporter.service.j2 new file mode 100644 index 0000000..53b577e --- /dev/null +++ b/templates/systemd/prometheus_postgres_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus PostgreSQL Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-postgres-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_redis_exporter.service.j2 b/templates/systemd/prometheus_redis_exporter.service.j2 new file mode 100644 index 0000000..d74df38 --- /dev/null +++ b/templates/systemd/prometheus_redis_exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus Redis Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-redis-exporter + +[Install] +WantedBy=multi-user.target diff --git a/templates/systemd/prometheus_varnish_exporter.service.j2 b/templates/systemd/prometheus_varnish_exporter.service.j2 new file mode 100644 index 0000000..4b944e7 --- /dev/null +++ b/templates/systemd/prometheus_varnish_exporter.service.j2 @@ -0,0 +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