add cloudflare exporter

This commit is contained in:
Ludovic Cartier 2023-01-27 16:51:27 +01:00
parent 17e1e50a48
commit a7940658b1
7 changed files with 108 additions and 2 deletions

View File

@ -1,4 +1,8 @@
--- ---
docker_compose_path: "/opt/docker-compose"
prometheus_docker_network: prometheus
prometheus_docker_pull: "no" prometheus_docker_pull: "no"
exporter_upgrade: "" exporter_upgrade: ""

View File

@ -0,0 +1,46 @@
---
- name: '{{ exporter }} exporter | check if docker is installed'
shell:
cmd: {{ item }} --help
register: result
check_mode: false
changed_when: false
failed_when: result.rc != 0 and result.rc != 127
with_items:
- docker
- docker-compose
tags: [ 'prometheus_{{ exporter }}' ]
- name: '{{ exporter }} exporter | create docker-compose directory'
file:
path: "{{ docker_compose_directory }}"
state: directory
mode: '0755'
tags: [ 'prometheus_{{ exporter }}' ]
- name: '{{ exporter }} exporter | copy docker-compose file'
template:
src: compose/{{ exporter }}.yml.j2
dest: /opt/docker-compose/{{ exporter }}/docker-compose.yml
owner: root
group: root
mode: 0644
notify: "{{ exporter }}-restart"
tags: [ 'prometheus_{{ exporter }}' ]
- name: '{{ exporter }} exporter | systemd | install unit file'
template:
src: systemd/docker-compose.service.j2
dest: /etc/systemd/system/docker-compose@{{ exporter }}.service
owner: root
group: root
mode: 0600
tags: [ 'prometheus_{{ exporter }}' ]
- name: '{{ exporter }} exporter | systemd | enable service'
systemd:
daemon_reload: yes
name: docker-compose@{{ exporter }}
enabled: true
ignore_errors: '{{ ansible_check_mode }}'
tags: [ 'prometheus_{{ exporter }}' ]

View File

@ -0,0 +1,9 @@
---
- name: cloudflare | check vars are defined
assert:
that:
- cloudflare_api_key is defined
tags: ['prometheus_cloudflare_exporter']
- include_tasks: _docker.yml
tags: ['prometheus_cloudflare_exporter']

View File

@ -7,7 +7,7 @@
- name: 'prometheus | server | create docker network' - name: 'prometheus | server | create docker network'
docker_network: docker_network:
name: 'prometheus' name: '{{ prometheus_docker_network }}'
tags: ['prometheus_server'] tags: ['prometheus_server']
- name: 'prometheus | server | create docker volume data' - name: 'prometheus | server | create docker volume data'
@ -50,7 +50,7 @@
- '--web.console.templates=/usr/share/prometheus/consoles' - '--web.console.templates=/usr/share/prometheus/consoles'
- '--web.enable-admin-api' - '--web.enable-admin-api'
networks: networks:
- { name: prometheus } - { name: {{ prometheus_docker_network }} }
ports: ports:
- "9090:9090" - "9090:9090"
log_driver: syslog log_driver: syslog

View File

@ -0,0 +1,17 @@
version: '3.7'
networks:
{{ prometheus_docker_network }}:
external: true
services:
cloudflare-exporter:
container_name: cloudflare-exporter
image: ghcr.io/lablabs/cloudflare_exporter:latest
restart: unless-stopped
environment:
- CF_API_TOKEN={{ cloudflare_api_key }}
ports:
- 8082:8080
networks:
- {{ prometheus_docker_network }}

View File

@ -371,3 +371,19 @@ scrape_configs:
regex: '(.*):8080' regex: '(.*):8080'
replacement: '${1}' replacement: '${1}'
{% endif %} {% endif %}
{% if prometheus_cloudflare_exporter_targets is defined %}
# cloudflare #
- job_name: cloudflare
scrape_interval: 30s
static_configs:
- targets:
{% for target in prometheus_cloudflare_exporter_targets %}
- {{ target }}:8082
{% endfor %}
relabel_configs:
- source_labels: [ __address__ ]
target_label: instance
regex: '(.*):8082'
replacement: '${1}'
{% endif %}

View File

@ -0,0 +1,14 @@
[Unit]
Description=%i service with docker compose
PartOf=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/opt/docker-compose/{{ exporter }}
ExecStart=/usr/bin/docker-compose up -d
ExecStop=/usr/bin/docker-compose down
[Install]
WantedBy=multi-user.target