From 290e1c9f5e6977c0cd1c45233e1d91b842a8c019 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 7 Feb 2018 19:06:21 +0100 Subject: [PATCH] library/roles/prometheus-haproxy-exporter: Playbook that installs and runs the prometheus haproxy exporter. --- prometheus-haproxy-exporter/defaults/main.yml | 18 +++++++ prometheus-haproxy-exporter/handlers/main.yml | 7 +++ prometheus-haproxy-exporter/tasks/main.yml | 53 +++++++++++++++++++ .../templates/haproxy_exporter.systemd.j2 | 12 +++++ .../templates/haproxy_exporter.upstart.j2 | 12 +++++ 5 files changed, 102 insertions(+) create mode 100644 prometheus-haproxy-exporter/defaults/main.yml create mode 100644 prometheus-haproxy-exporter/handlers/main.yml create mode 100644 prometheus-haproxy-exporter/tasks/main.yml create mode 100644 prometheus-haproxy-exporter/templates/haproxy_exporter.systemd.j2 create mode 100644 prometheus-haproxy-exporter/templates/haproxy_exporter.upstart.j2 diff --git a/prometheus-haproxy-exporter/defaults/main.yml b/prometheus-haproxy-exporter/defaults/main.yml new file mode 100644 index 00000000..ca053ce9 --- /dev/null +++ b/prometheus-haproxy-exporter/defaults/main.yml @@ -0,0 +1,18 @@ +--- +prometheus_h_e_install: True +prometheus_h_e_version: 0.9.0 +prometheus_h_e_dir: 'haproxy_exporter-{{ prometheus_h_e_version }}.linux-amd64' +prometheus_h_e_file: '{{ prometheus_h_e_dir }}.tar.gz' +prometheus_h_e_download_url: 'https://github.com/prometheus/haproxy_exporter/releases/download/v{{ prometheus_h_e_version }}/{{ prometheus_h_e_file }}' +prometheus_h_e_user: prometheus +prometheus_h_e_home: /opt/prometheus +prometheus_h_e_dist_dir: '{{ prometheus_h_e_home }}/dist' +prometheus_h_e_logdir: '/var/log/prometheus-haproxy-exporter' +prometheus_h_e_cmd: '{{ prometheus_h_e_dist_dir }}/{{ prometheus_h_e_dir }}/haproxy_exporter' +prometheus_h_e_port: 9101 +prometheus_h_e_loglevel: info +prometheus_h_e_haproxy_pid: '/run/haproxy.pid' +prometheus_h_e_haproxy_stats_port: 8881 +prometheus_h_e_opts: '--web.listen-address=":{{ prometheus_h_e_port }}" --log.level={{ prometheus_h_e_loglevel }} --haproxy.pid-file="{{ prometheus_h_e_haproxy_pid }}" --haproxy.scrape-uri="http://localhost:{{ prometheus_h_e_haproxy_stats_port }}/;csv"' +# List the additional options here +prometheus_h_e_additional_opts: '' diff --git a/prometheus-haproxy-exporter/handlers/main.yml b/prometheus-haproxy-exporter/handlers/main.yml new file mode 100644 index 00000000..a07bf565 --- /dev/null +++ b/prometheus-haproxy-exporter/handlers/main.yml @@ -0,0 +1,7 @@ +--- +- name: systemd reload + command: systemctl daemon-reload + +- name: Restart haproxy exporter + service: name=haproxy_exporter state=restarted + diff --git a/prometheus-haproxy-exporter/tasks/main.yml b/prometheus-haproxy-exporter/tasks/main.yml new file mode 100644 index 00000000..6c33256a --- /dev/null +++ b/prometheus-haproxy-exporter/tasks/main.yml @@ -0,0 +1,53 @@ +--- +- block: + - name: Create the user under the haproxy exporter will run + user: name={{ prometheus_h_e_user }} home={{ prometheus_h_e_home }} createhome=no shell=/usr/sbin/nologin system=yes + + - name: Create the prometheus haproxy exporter base directory + file: dest={{ item }} state=directory owner=root group=root + with_items: + - '{{ prometheus_h_e_home }}' + - '{{ prometheus_h_e_dist_dir }}' + + - name: Create the prometheus haproxy exporter log directory + file: dest={{ prometheus_h_e_logdir }} state=directory owner={{ prometheus_h_e_user }} group={{ prometheus_h_e_user }} + + - name: Download the prometheus haproxy exporter + get_url: url={{ prometheus_h_e_download_url }} dest=/srv/ + + - name: Unarchive the prometheus distribution + unarchive: src=/srv/{{ prometheus_h_e_file }} dest={{ prometheus_h_e_dist_dir }} remote_src=yes owner=root group=root + args: + creates: '{{ prometheus_h_e_dist_dir }}/{{ prometheus_h_e_dir }}/haproxy_exporter' + notify: Restart haproxy exporter + + - name: Install the prometheus haproxy exporter upstart script + template: src=haproxy_exporter.upstart.j2 dest=/etc/init/haproxy_exporter.conf mode=0644 owner=root group=root + when: ansible_service_mgr != 'systemd' + + - name: Install the prometheus haproxy exporter systemd unit + template: src=haproxy_exporter.systemd.j2 dest=/etc/systemd/system/haproxy_exporter.service mode=0644 owner=root group=root + when: ansible_service_mgr == 'systemd' + notify: systemd reload + + - name: Ensure that prometheus haproxy_exporter is started and enabled + service: name=haproxy_exporter state=started enabled=yes + + tags: [ 'prometheus', 'haproxy_exporter' ] + when: prometheus_h_e_install + +- block: + - name: Ensure that prometheus haproxy_exporter is stopped and disabled + service: name=haproxy_exporter state=stopped enabled=no + + - name: Remove prometheus haproxy exporter upstart script + file: dest=/etc/init/haproxy_exporter.conf state=absent + when: ansible_service_mgr != 'systemd' + + - name: Remove the prometheus haproxy exporter systemd unit + file: dest=/etc/systemd/system/haproxy_exporter.service state=absent + when: ansible_service_mgr == 'systemd' + notify: systemd reload + + tags: [ 'prometheus', 'haproxy_exporter' ] + when: not prometheus_h_e_install diff --git a/prometheus-haproxy-exporter/templates/haproxy_exporter.systemd.j2 b/prometheus-haproxy-exporter/templates/haproxy_exporter.systemd.j2 new file mode 100644 index 00000000..04ef1c28 --- /dev/null +++ b/prometheus-haproxy-exporter/templates/haproxy_exporter.systemd.j2 @@ -0,0 +1,12 @@ +[Unit] +Description=haproxy_exporter - Prometheus exporter for haproxy metrics and stats. +After=network.target + +[Service] +Type=simple + +User={{ prometheus_user }} +Group={{ prometheus_user }} + +ExecStart={{ prometheus_h_e_cmd }} {{ prometheus_h_e_opts }} {{ prometheus_h_e_additional_opts }} --collector.systemd + diff --git a/prometheus-haproxy-exporter/templates/haproxy_exporter.upstart.j2 b/prometheus-haproxy-exporter/templates/haproxy_exporter.upstart.j2 new file mode 100644 index 00000000..f95929f2 --- /dev/null +++ b/prometheus-haproxy-exporter/templates/haproxy_exporter.upstart.j2 @@ -0,0 +1,12 @@ +description "Prometheus haproxy exporter" +start on (local-filesystems and net-device-up IFACE!=lo) +stop on runlevel [016] + +respawn +respawn limit 10 5 +setuid {{ prometheus_h_e_user }} +setgid {{ prometheus_h_e_user }} + +script + exec {{ prometheus_h_e_cmd }} {{ prometheus_h_e_opts }} {{ prometheus_h_e_additional_opts }} > {{ prometheus_h_e_logdir }}/haproxy_exporter.log 2>&1 +end script