Browse Source

Manage the prometheus exporter.

feauture/20970
Andrea Dell'Amico 4 months ago
parent
commit
a7434a7eee
Signed by: andrea.dellamico
GPG Key ID: 147ABE6CEB9E20FF
  1. 23
      defaults/main.yml
  2. 8
      handlers/main.yml
  3. 1
      tasks/main.yml
  4. 80
      tasks/mongodb-prometheus-exporter.yml
  5. 15
      templates/mongodb_exporter.service.j2
  6. 12
      templates/mongodb_exporter.upstart.j2

23
defaults/main.yml

@ -82,5 +82,24 @@ mongodb_authorization_enabled: False
mongodb_replicaset: storagedev
mongodb_replica_keyfile: '{{ mongodb_dbpath }}/replica_keyfile'
# User and password are stored in the variables mongo_monitoring_u and mongo_monitoring_pwd
# Prometheus metrics
mongodb_prometheus_exporter_enabled: False
mongodb_prometheus_exporter_version: '0.30.0'
mongodb_prometheus_exporter_dir: 'mongodb_exporter-{{ mongodb_prometheus_exporter_version }}.linux-amd64'
mongodb_prometheus_exporter_file: '{{ mongodb_prometheus_exporter_dir }}.tar.gz'
mongodb_prometheus_exporter_url: 'https://github.com/percona/mongodb_exporter/releases/download/v{{ mongodb_prometheus_exporter_version }}/{{ mongodb_prometheus_exporter_file }}'
mongodb_prometheus_exporter_port: 9216
mongodb_prometheus_exporter_loglevel: error
mongodb_prometheus_exporter_opts: '--web.listen-address=":{{ mongodb_prometheus_exporter_port }}" --log.level={{ mongodb_prometheus_exporter_loglevel }}'
mongodb_prometheus_exporter_connection_opts: '--mongodb.uri="mongodb://{{ mongodb_prometheus_exporter_connection_user }}:{{ mongodb_prometheus_exporter_connection_password }}@{{ mongodb_prometheus_exporter_connection_host }}/{{ mongodb_prometheus_exporter_connection_params }}"'
mongodb_prometheus_exporter_collect_opts: '--mongodb.global-conn-pool --mongodb.direct-connect=true --collector.topmetrics --discovering-mode --collector.dbstats'
# List the additional options here
mongodb_prometheus_exporter_additional_opts: ''
mongodb_prometheus_exporter_user: prometheus_mongodb
mongodb_prometheus_exporter_home: '/opt/{{ mongodb_prometheus_exporter_user }}'
mongodb_prometheus_exporter_dist_dir: '{{ prometheus_n_e_home }}/dist'
mongodb_prometheus_exporter_cmd: '{{ mongodb_prometheus_exporter_dist_dir }}/{{ mongodb_prometheus_exporter_dir }}/mongodb_exporter'
mongodb_prometheus_exporter_connection_host: 'localhost:27017'
mongodb_prometheus_exporter_connection_user: monitor
#mongodb_prometheus_exporter_connection_password: 'use a vault'
mongodb_prometheus_exporter_connection_params: 'admin?ssl=true'

8
handlers/main.yml

@ -7,3 +7,11 @@
service: name=mongodb state=restarted
when: "'{{ mongodb_start_server }}' == 'yes'"
- name: Restart mongodb exporter
ansible.builtin.service:
name: mongodb_exporter
state: restarted
- name: Reload the systemd data
systemd:
daemon_reload: yes

1
tasks/main.yml

@ -2,3 +2,4 @@
- import_tasks: mongodb.yml
- import_tasks: mongodb-letsencrypt-acmetool.yml
when: mongodb_ssl_letsencrypt_managed
- import_tasks: mongodb-prometheus-exporter.yml

80
tasks/mongodb-prometheus-exporter.yml

@ -0,0 +1,80 @@
---
- name: Mongodb exporter
block:
- name: Create the user under the mongodb exporter will run
ansible.builtin.user:
name: '{{ mongodb_prometheus_exporter_user }}'
home: '{{ mongodb_prometheus_exporter_home }}'
createhome: no
shell: /usr/sbin/nologin
system: yes
- name: Create the prometheus mongodb exporter base directory
ansible.builtin.file:
dest: '{{ item }}'
state: directory
owner: root
group: root
loop:
- '{{ mongodb_prometheus_exporter_home }}'
- '{{ mongodb_prometheus_exporter_dist_dir }}'
- name: Download the prometheus mongodb exporter
ansible.builtin.get_url:
url: '{{ mongodb_prometheus_exporter_download_url }}'
dest: /srv/
- name: Unarchive the prometheus distribution
ansible.builtin.unarchive:
src: '/srv/{{ mongodb_prometheus_exporter_file }}'
dest: '{{ mongodb_prometheus_exporter_dist_dir }}'
remote_src: yes
owner: root
group: root
args:
creates: '{{ mongodb_prometheus_exporter_dist_dir }}/{{ mongodb_prometheus_exporter_dir }}/mongodb_exporter'
notify: Restart mongodb exporter
- name: Install the prometheus node exporter upstart script
ansible.builtin.template:
src: mongodb_exporter.upstart.j2
dest: /etc/init/mongodb_exporter.conf
mode: 0644
owner: root
group: root
when: ansible_service_mgr != 'systemd'
notify: Restart mongodb exporter
- name: Install the prometheus mongodb exporter systemd unit
ansible.builtin.template:
src: mongodb_exporter.service.j2
dest: /etc/systemd/system/mongodb_exporter.service
mode: 0644
owner: root
group: root
when: ansible_service_mgr == 'systemd'
notify:
- Reload the systemd data
- Restart mongodb exporter
- ansible.builtin.meta: flush_handlers
- name: Ensure that prometheus mongodb_exporter is started and enabled
ansible.builtin.service:
name: mongodb_exporter
state: started
enabled: yes
when: mongodb_prometheus_exporter_enabled
tags: [ 'mongodb', 'prometheus', 'mongodb_prometheus' ]
- name: No mongodb exporter
block:
- name: Ensure that prometheus mongodb_exporter is stopped and disabled
ansible.builtin.service:
name: mongodb_exporter
state: stopped
enabled: no
when: not mongodb_prometheus_exporter_enabled
tags: [ 'mongodb', 'prometheus', 'mongodb_prometheus' ]

15
templates/mongodb_exporter.service.j2

@ -0,0 +1,15 @@
[Unit]
Description=mongodb_exporter - Prometheus exporter for mongodb.
After=network.target
[Service]
Type=simple
Restart=on-failure
User={{ mongodb_prometheus_exporter_user }}
Group={{ mongodb_prometheus_exporter_user }}
ExecStart={{ mongodb_prometheus_exporter_cmd }} {{ mongodb_prometheus_exporter_opts }} {{ mongodb_prometheus_exporter_additional_opts }} {{ mongodb_prometheus_exporter_collect_opts }} {{ mongodb_prometheus_exporter_connection_opts }}
[Install]
WantedBy=multi-user.target

12
templates/mongodb_exporter.upstart.j2

@ -0,0 +1,12 @@
description "Prometheus mongodb exporter"
start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [016]
respawn
respawn limit 10 5
setuid {{ mongodb_prometheus_exporter_user }}
setgid {{ mongodb_prometheus_exporter_user }}
script
exec {{ mongodb_prometheus_exporter_cmd }} {{ mongodb_prometheus_exporter_opts }} {{ mongodb_prometheus_exporter_connection_opts }} {{ mongodb_prometheus_exporter_additional_opts }} > {{ mongodb_prometheus_exporter_logdir }}/mongodb_exporter.log 2>&1
end script
Loading…
Cancel
Save