initial commit

This commit is contained in:
Ludovic Cartier 2021-04-29 18:53:56 +02:00
commit 7879d6a13c
18 changed files with 505 additions and 0 deletions

53
README.md Normal file
View File

@ -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 <ludovic.cartier@brainsys.io>

5
defaults/main.yml Normal file
View File

@ -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'

19
tasks/asserts.yml Normal file
View File

@ -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

6
tasks/main.yml Normal file
View File

@ -0,0 +1,6 @@
---
- name: 'prometheus | include asserts'
include_tasks: 'asserts.yml'
- name: 'prometheus | install'
include_tasks: 'install.yml'

View File

@ -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"

56
tasks/mysqld_exporter.yml Normal file
View File

@ -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"

40
tasks/node_exporter.yml Normal file
View File

@ -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"

View File

@ -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"

65
tasks/prometheus.yml Normal file
View File

@ -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

74
templates/prometheus.yml Normal file
View File

@ -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=<job_name>` 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 %}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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