From 221442693706b443d7220e432701976b3957e9f1 Mon Sep 17 00:00:00 2001 From: Ludovic Cartier Date: Tue, 2 Nov 2021 15:51:47 +0100 Subject: [PATCH] first implement of blackbox --- handlers/main.yml | 7 +++ tasks/blackbox.yml | 56 +++++++++++++++++++ tasks/main.yml | 5 ++ .../conf/prometheus-blackbox-exporter.yml.j2 | 37 ++++++++++++ templates/conf/prometheus.yml.j2 | 49 ++++++++++++++++ .../prometheus-blackbox-exporter.service.j2 | 10 ++++ 6 files changed, 164 insertions(+) create mode 100644 tasks/blackbox.yml create mode 100644 templates/conf/prometheus-blackbox-exporter.yml.j2 create mode 100644 templates/systemd/prometheus-blackbox-exporter.service.j2 diff --git a/handlers/main.yml b/handlers/main.yml index bc7d932..0605da0 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -53,3 +53,10 @@ state: restarted enabled: yes tags: ['prometheus'] + +- name: 'prometheus | blackbox exporter | restart service' + systemd: + name: prometheus-blackbox-exporter.service + state: restarted + enabled: yes + tags: ['prometheus'] diff --git a/tasks/blackbox.yml b/tasks/blackbox.yml new file mode 100644 index 0000000..2676cd9 --- /dev/null +++ b/tasks/blackbox.yml @@ -0,0 +1,56 @@ +--- +- name: 'prometheus | blackbox exporter | get latest release from GitHub' + shell: curl --silent "https://api.github.com/repos/prometheus/blackbox_exporter/releases/latest" | jq -r .tag_name | sed "s/v//" + args: + warn: false + register: prometheus_blackbox_exporter_latest_version + run_once: true + +- name: 'prometheus | blackbox exporter | download' + get_url: + url: https://github.com/prometheus/blackbox_exporter/releases/download/v{{ prometheus_blackbox_exporter_version|default(prometheus_blackbox_exporter_latest_version.stdout) }}/blackbox_exporter-{{ prometheus_blackbox_exporter_version|default(prometheus_blackbox_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | blackbox exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/blackbox_exporter-{{ prometheus_blackbox_exporter_version|default(prometheus_blackbox_exporter_latest_version.stdout) }}.linux-amd64.tar.gz + dest: /tmp + +- name: 'prometheus | blackbox exporter | move to /usr/local/bin' + copy: + src: /tmp/blackbox_exporter-{{ prometheus_blackbox_exporter_version|default(prometheus_blackbox_exporter_latest_version.stdout) }}.linux-amd64/blackbox_exporter + dest: /usr/local/bin/prometheus-blackbox-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + notify: + - 'prometheus | blackbox exporter | restart service' + +- name: 'prometheus | blackbox exporter | configuration' + template: + src: conf/prometheus-blackbox-exporter.j2 + dest: /etc/prometheus/prometheus-blackbox-exporter.yml + owner: root + group: root + mode: 0600 + when: + - prometheus_blackbox_exporter_user is defined + register: prometheus_blackbox_exporter_configuration + notify: + - 'prometheus | blackbox exporter | restart service' + +- name: 'prometheus | blackbox exporter | install unit file to systemd' + template: + src: systemd/prometheus-blackbox-exporter.service.j2 + dest: /etc/systemd/system/prometheus-blackbox-exporter.service + owner: root + group: root + mode: 0600 + +- name: 'prometheus | blackbox exporter | configure systemd to use service' + systemd: + daemon_reload: yes + enabled: yes + name: prometheus-blackbox-exporter.service diff --git a/tasks/main.yml b/tasks/main.yml index bda4924..65de31a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -44,3 +44,8 @@ include_tasks: 'varnish_exporter.yml' when: - "'varnish' in prometheus_exporter_packages" + +- name: 'prometheus | Blackbox exporter' + include_tasks: 'varnish_exporter.yml' + when: + - "'blackbox' in prometheus_exporter_packages" diff --git a/templates/conf/prometheus-blackbox-exporter.yml.j2 b/templates/conf/prometheus-blackbox-exporter.yml.j2 new file mode 100644 index 0000000..09d909c --- /dev/null +++ b/templates/conf/prometheus-blackbox-exporter.yml.j2 @@ -0,0 +1,37 @@ +modules: + http_2xx: + prober: http + timeout: 30s + http: + preferred_ip_protocol: "ipv4" + http_post_2xx: + prober: http + http: + preferred_ip_protocol: "ipv4" + method: POST + tcp_connect: + prober: tcp + pop3s_banner: + prober: tcp + tcp: + query_response: + - expect: "^+OK" + tls: true + tls_config: + insecure_skip_verify: false + ssh_banner: + prober: tcp + tcp: + query_response: + - expect: "^SSH-2.0-" + irc_banner: + prober: tcp + tcp: + query_response: + - send: "NICK prober" + - send: "USER prober prober prober :prober" + - expect: "PING :([^ ]+)" + send: "PONG ${1}" + - expect: "^:[^ ]+ 001" + icmp: + prober: icmp diff --git a/templates/conf/prometheus.yml.j2 b/templates/conf/prometheus.yml.j2 index 8ccf28d..c9f166c 100644 --- a/templates/conf/prometheus.yml.j2 +++ b/templates/conf/prometheus.yml.j2 @@ -32,6 +32,7 @@ scrape_configs: - targets: ['localhost:9090'] {% if prometheus_node_exporter_targets is defined %} + # node exporter # - job_name: node scrape_interval: 30s scrape_timeout: 10s @@ -43,6 +44,7 @@ scrape_configs: {% endif %} {% if prometheus_mysqld_exporter_targets is defined %} + # mysql exporter # - job_name: mysql scrape_interval: 30s scrape_timeout: 10s @@ -65,6 +67,7 @@ scrape_configs: {% endif %} {% if prometheus_mongodb_exporter_targets is defined %} + # mongoDB exporter # - job_name: mongodb scrape_interval: 30s scrape_timeout: 10s @@ -87,6 +90,7 @@ scrape_configs: {% endif %} {% if prometheus_postgres_exporter_targets is defined %} + # postgresql exporter # - job_name: postgresql scrape_interval: 30s scrape_timeout: 10s @@ -109,6 +113,7 @@ scrape_configs: {% endif %} {% if prometheus_phpfpm_exporter_targets is defined %} + # PHP-FPM exporter # - job_name: phpfpm scrape_interval: 30s scrape_timeout: 10s @@ -120,6 +125,7 @@ scrape_configs: {% endif %} {% if prometheus_apache_exporter_targets is defined %} + # apache exporter # - job_name: apache scrape_interval: 30s scrape_timeout: 10s @@ -131,6 +137,7 @@ scrape_configs: {% endif %} {% if prometheus_haproxy_exporter_targets is defined %} + # haproxy exporter # - job_name: haproxy scrape_interval: 30s scrape_timeout: 10s @@ -151,3 +158,45 @@ scrape_configs: - {{ target }}:9131 {% endfor %} {% endif %} + +// +{% if prometheus_blackbox_exporter_targets is defined %} + # blacbox # +{% for target in prometheus_blackbox_exporter_http.values() %} + ## http check ## + - job_name: blackbox - url {{ target.name }} + scrape_interval: 30s + scrape_timeout: 10s + metrics_path: {{ target.metrics_path }} + params: + module: [http_2xx] + static_configs: + - targets: + - {{target.host }} + relabel_configs: + - source_labels: [__address__] + target_label: __param_target + - source_labels: [__param_target] + target_label: instance + - target_label: __address__ + replacement: localhost:9115 +{% endfor %} +{% for target in prometheus_blackbox_exporter_ping.values() %} + ## ping ## + - job_name: blackbox - ping {{ target.name }} + scrape_interval: 30s + scrape_timeout: 10s + params: + module: [icmp] + static_configs: + - targets: + - {{ target.host }} + relabel_configs: + - source_labels: [__address__] + target_label: __param_target + - source_labels: [__param_target] + target_label: instance + - target_label: __address__ + replacement: localhost:9115 +{% endfor %} +{% endif %} diff --git a/templates/systemd/prometheus-blackbox-exporter.service.j2 b/templates/systemd/prometheus-blackbox-exporter.service.j2 new file mode 100644 index 0000000..5082b5f --- /dev/null +++ b/templates/systemd/prometheus-blackbox-exporter.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Prometheus Blacbox Exporter +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/prometheus-blackbox-exporter --config.file /etc/prometheus/prometheus-blackbox-exporter.yml + +[Install] +WantedBy=multi-user.target