From 95994651bd5180366f4fd4993bab12399ab38974 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 16 Jan 2018 19:37:50 +0100 Subject: [PATCH] library/roles/prometheus-node-exporter: Role that installs and runs the prometheus node exporter. --- prometheus-node-exporter/defaults/main.yml | 16 ++++++ prometheus-node-exporter/handlers/main.yml | 3 ++ prometheus-node-exporter/tasks/main.yml | 53 +++++++++++++++++++ .../templates/node_exporter.systemd.j2 | 12 +++++ .../templates/node_exporter.upstart.j2 | 12 +++++ 5 files changed, 96 insertions(+) create mode 100644 prometheus-node-exporter/defaults/main.yml create mode 100644 prometheus-node-exporter/handlers/main.yml create mode 100644 prometheus-node-exporter/tasks/main.yml create mode 100644 prometheus-node-exporter/templates/node_exporter.systemd.j2 create mode 100644 prometheus-node-exporter/templates/node_exporter.upstart.j2 diff --git a/prometheus-node-exporter/defaults/main.yml b/prometheus-node-exporter/defaults/main.yml new file mode 100644 index 00000000..fa63a8cc --- /dev/null +++ b/prometheus-node-exporter/defaults/main.yml @@ -0,0 +1,16 @@ +--- +prometheus_n_e_install: True +prometheus_n_e_version: 0.15.2 +prometheus_n_e_dir: 'node_exporter-{{ prometheus_n_e_version }}.linux-amd64' +prometheus_n_e_file: '{{ prometheus_n_e_dir }}.tar.gz' +prometheus_n_e_download_url: 'https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_n_e_version }}/{{ prometheus_n_e_file }}' +prometheus_n_e_user: prometheus +prometheus_n_e_home: /opt/prometheus +prometheus_n_e_dist_dir: '{{ prometheus_n_e_home }}/dist' +prometheus_n_e_logdir: '/var/log/prometheus-node-exporter' +prometheus_n_e_cmd: '{{ prometheus_n_e_dist_dir }}/{{ prometheus_n_e_dir }}/node_exporter' +prometheus_n_e_port: 9100 +prometheus_n_e_loglevel: info +prometheus_n_e_opts: '--web.listen-address=":{{ prometheus_n_e_port }} --log.level={{ prometheus_n_e_loglevel }}' +# List the additional options here +prometheus_n_e_additional_opts: '' diff --git a/prometheus-node-exporter/handlers/main.yml b/prometheus-node-exporter/handlers/main.yml new file mode 100644 index 00000000..032ba816 --- /dev/null +++ b/prometheus-node-exporter/handlers/main.yml @@ -0,0 +1,3 @@ +--- +- name: systemd reload + command: systemctl daemon-reload diff --git a/prometheus-node-exporter/tasks/main.yml b/prometheus-node-exporter/tasks/main.yml new file mode 100644 index 00000000..db249be9 --- /dev/null +++ b/prometheus-node-exporter/tasks/main.yml @@ -0,0 +1,53 @@ +--- +- block: + - name: Create the user under the node exporter will run + user: name={{ prometheus_n_e_user }} home={{ prometheus_n_e_home }} createhome=no shell=/usr/sbin/nologin system=yes + + - name: Create the prometheus node exporter base directory + file: dest={{ item }} state=directory owner=root group=root + with_items: + - '{{ prometheus_n_e_home }}' + - '{{ prometheus_n_e_dist_dir }}' + + - name: Create the prometheus node exporter log directory + file: dest={{ prometheus_n_e_logdir }} state=directory owner={{ prometheus_n_e_user }} group={{ prometheus_n_e_user }} + + - name: Download the prometheus node exporter + get_url: url={{ prometheus_n_e_download_url }} dest=/srv/ + + - name: Unarchive the prometheus distribution + unarchive: src=/srv/{{ prometheus_n_e_file }} dest={{ prometheus_n_e_dist_dir }} remote_src=yes + args: + creates: '{{ prometheus_n_e_dist_dir }}/{{ prometheus_n_e_dir }}/node_exporter' + notify: Restart node exporter + + - name: Install the prometheus node exporter upstart script + template: src=node_exporter.upstart.j2 dest=/etc/init/node_exporter.conf mode=0644 owner=root group=root + when: ansible_service_mgr != 'systemd' + + - name: Install the prometheus node exporter systemd unit + template: src=node_exporter.systemd.j2 dest=/etc/systemd/system/node_exporter.service mode=0644 owner=root group=root + when: ansible_service_mgr == 'systemd' + notify: systemd reload + + - name: Ensure that prometheus node_exporter is started and enabled + service: name=node_exporter state=started enabled=yes + + tags: [ 'prometheus', 'node_exporter' ] + when: prometheus_n_e_install + +- block: + - name: Ensure that prometheus node_exporter is stopped and disabled + service: name=node_exporter state=stopped enabled=no + + - name: Remove prometheus node exporter upstart script + file: dest=/etc/init/node_exporter.conf state=absent + when: ansible_service_mgr != 'systemd' + + - name: Remove the prometheus node exporter systemd unit + file: dest=/etc/systemd/system/node_exporter.service state=absent + when: ansible_service_mgr == 'systemd' + notify: systemd reload + + tags: [ 'prometheus', 'node_exporter' ] + when: not prometheus_n_e_install diff --git a/prometheus-node-exporter/templates/node_exporter.systemd.j2 b/prometheus-node-exporter/templates/node_exporter.systemd.j2 new file mode 100644 index 00000000..6480623f --- /dev/null +++ b/prometheus-node-exporter/templates/node_exporter.systemd.j2 @@ -0,0 +1,12 @@ +[Unit] +Description=node_exporter - Prometheus exporter for machine metrics. +After=network.target + +[Service] +Type=simple + +User={{ prometheus_user }} +Group={{ prometheus_user }} + +ExecStart={{ prometheus_n_e_cmd }} {{ prometheus_n_e_opts }} {{ prometheus_n_e_additional_opts }} --collector.systemd + diff --git a/prometheus-node-exporter/templates/node_exporter.upstart.j2 b/prometheus-node-exporter/templates/node_exporter.upstart.j2 new file mode 100644 index 00000000..f7d8cb70 --- /dev/null +++ b/prometheus-node-exporter/templates/node_exporter.upstart.j2 @@ -0,0 +1,12 @@ +description "Prometheus node exporter" +start on (local-filesystems and net-device-up IFACE!=lo) +stop on runlevel [016] + +respawn +respawn limit 10 5 +setuid {{ prometheus_n_e_user }} +setgid {{ prometheus_n_e_user }} + +script + exec {{ prometheus_n_e_cmd }} {{ prometheus_n_e_opts }} {{ prometheus_n_e_additional_opts }} > {{ prometheus_n_e_logdir }}/node_exporter.log 2>&1 +end script