diff --git a/ansible/playbook.yml b/ansible/playbook.yml index af75d23..d15f74b 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -15,6 +15,7 @@ - watchtower - docker-gc - traefik + - prometheus - hosts: dchi roles: diff --git a/ansible/roles/prometheus/defaults/main.yml b/ansible/roles/prometheus/defaults/main.yml new file mode 100644 index 0000000..70b3760 --- /dev/null +++ b/ansible/roles/prometheus/defaults/main.yml @@ -0,0 +1 @@ +prometheus_container: prom/prometheus:v1.5.1 diff --git a/ansible/roles/prometheus/handlers/main.yml b/ansible/roles/prometheus/handlers/main.yml new file mode 100644 index 0000000..cae8a82 --- /dev/null +++ b/ansible/roles/prometheus/handlers/main.yml @@ -0,0 +1,5 @@ +- name: restart prometheus + systemd: + name: prometheus + state: restarted + daemon_reload: yes diff --git a/ansible/roles/prometheus/tasks/main.yml b/ansible/roles/prometheus/tasks/main.yml new file mode 100644 index 0000000..97ef165 --- /dev/null +++ b/ansible/roles/prometheus/tasks/main.yml @@ -0,0 +1,28 @@ +- name: prometheus + notify: + - restart prometheus + template: + src: prometheus.j2 + dest: /etc/prometheus.yml + +- name: service + notify: + - restart prometheus + template: + src: service.j2 + dest: /etc/systemd/system/prometheus.service + +- name: default + notify: + - restart prometheus + template: + src: default.j2 + dest: /etc/default/prometheus + +- name: start + systemd: + name: prometheus + state: started + daemon_reload: yes + masked: no + enabled: yes diff --git a/ansible/roles/prometheus/templates/default.j2 b/ansible/roles/prometheus/templates/default.j2 new file mode 100644 index 0000000..e69de29 diff --git a/ansible/roles/prometheus/templates/prometheus.j2 b/ansible/roles/prometheus/templates/prometheus.j2 new file mode 100644 index 0000000..6d98f93 --- /dev/null +++ b/ansible/roles/prometheus/templates/prometheus.j2 @@ -0,0 +1,32 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + external_labels: + monitor: 'exporter-metrics' + +# rule_files: TODO: Add rules for alertmanager +# - "prometheus.rules" + +scrape_configs: +- job_name: 'node-exporter' + dns_sd_configs: + - names: + - node-exporter + refresh_interval: 15s + type: A + port: 9100 + +- job_name: 'prometheus' + static_configs: + - targets: + - '127.0.0.1:9090' + +- job_name: 'alertmanager' + static_configs: + - targets: + - 'alertmanager:9093' + +- job_name: 'traefik-exporter' + static_configs: + - targets: + - 'traefik-exporter.traefik.rancher.internal:9000' diff --git a/ansible/roles/prometheus/templates/service.j2 b/ansible/roles/prometheus/templates/service.j2 new file mode 100644 index 0000000..09de5a0 --- /dev/null +++ b/ansible/roles/prometheus/templates/service.j2 @@ -0,0 +1,25 @@ +[Unit] +Description=Prometheus + +Requires=docker.service +After=docker.service + +[Service] +Restart=always + +EnvironmentFile=/etc/default/prometheus +ExecStop=/bin/sh -c '/usr/bin/docker ps | /bin/grep %p 1> /dev/null && /usr/bin/docker stop %p || true' +ExecStartPre=/bin/sh -c '/usr/bin/docker ps | /bin/grep %p 1> /dev/null && /usr/bin/docker kill %p || true' +ExecStartPre=/bin/sh -c '/usr/bin/docker ps -a | /bin/grep %p 1> /dev/null && /usr/bin/docker rm %p || true' +ExecStartPre=/usr/bin/docker pull {{ prometheus_container }} + +ExecStart=/usr/bin/docker run --rm \ + --name %p \ + --volume /var/lib/prometheus:/prometheus \ + --publish 127.0.0.1:9090:9090 \ + {{ prometheus_container }} \ + -config.file=/etc/prometheus.yml \ + -alertmanager.url=http://alertmanager:9093 + +[Install] +WantedBy=multi-user.target