diff --git a/defaults/main.yml b/defaults/main.yml index 36583d3..c837d91 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,8 @@ --- +docker_compose_path: "/opt/docker-compose" + +prometheus_docker_network: prometheus + prometheus_docker_pull: "no" exporter_upgrade: "" diff --git a/tasks/exporter/_docker.yml b/tasks/exporter/_docker.yml new file mode 100644 index 0000000..e2a73ca --- /dev/null +++ b/tasks/exporter/_docker.yml @@ -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 }}' ] diff --git a/tasks/exporter/cloudflare.yml b/tasks/exporter/cloudflare.yml new file mode 100644 index 0000000..8d4976f --- /dev/null +++ b/tasks/exporter/cloudflare.yml @@ -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'] diff --git a/tasks/server/prometheus.yml b/tasks/server/prometheus.yml index 82f58a7..0374699 100644 --- a/tasks/server/prometheus.yml +++ b/tasks/server/prometheus.yml @@ -7,7 +7,7 @@ - name: 'prometheus | server | create docker network' docker_network: - name: 'prometheus' + name: '{{ prometheus_docker_network }}' tags: ['prometheus_server'] - name: 'prometheus | server | create docker volume data' @@ -50,7 +50,7 @@ - '--web.console.templates=/usr/share/prometheus/consoles' - '--web.enable-admin-api' networks: - - { name: prometheus } + - { name: {{ prometheus_docker_network }} } ports: - "9090:9090" log_driver: syslog diff --git a/templates/compose/cloudflare.yml.j2 b/templates/compose/cloudflare.yml.j2 new file mode 100644 index 0000000..5b6af39 --- /dev/null +++ b/templates/compose/cloudflare.yml.j2 @@ -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 }} diff --git a/templates/conf/prometheus.yml.j2 b/templates/conf/prometheus.yml.j2 index 3390397..1d84781 100644 --- a/templates/conf/prometheus.yml.j2 +++ b/templates/conf/prometheus.yml.j2 @@ -371,3 +371,19 @@ scrape_configs: regex: '(.*):8080' replacement: '${1}' {% 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 %} diff --git a/templates/systemd/docker-compose.service.j2 b/templates/systemd/docker-compose.service.j2 new file mode 100644 index 0000000..dea48d9 --- /dev/null +++ b/templates/systemd/docker-compose.service.j2 @@ -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