Skip to content

Commit 3e4ff17

Browse files
authored
feat: Add mongodb_exporter (#293)
* feat: Add mongodb_exporter --------- Signed-off-by: anviar <oleg.kluchkin+github@gmail.com>
1 parent 945f393 commit 3e4ff17

File tree

24 files changed

+924
-0
lines changed

24 files changed

+924
-0
lines changed

roles/mongodb_exporter/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>
2+
3+
# Ansible Role: mongodb_exporter
4+
5+
## Description
6+
7+
Deploy prometheus [mongodb_exporter](https://github.com/percona/mongodb_exporter/) using ansible.
8+
9+
## Requirements
10+
11+
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
12+
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
13+
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)
14+
15+
## Role Variables
16+
17+
All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
18+
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/mongodb_exporter_role.html) for description and default values of the variables.
19+
20+
## Example
21+
22+
### Demo site
23+
24+
We provide demo site for full monitoring solution based on prometheus and grafana. Repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and site is hosted on [DigitalOcean](https://digitalocean.com).
25+
26+
## Local Testing
27+
28+
The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable to for your system. Running your tests is as simple as executing `molecule test`.
29+
30+
## Continuous Intergation
31+
32+
Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have a quite large test matrix which can take more time than local testing, so please be patient.
33+
34+
## Contributing
35+
36+
See [contributor guideline](CONTRIBUTING.md).
37+
38+
## Troubleshooting
39+
40+
See [troubleshooting](TROUBLESHOOTING.md).
41+
42+
## License
43+
44+
This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
mongodb_exporter_version: 0.40.0
3+
mongodb_exporter_binary_local_dir: ""
4+
mongodb_exporter_binary_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\
5+
mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
6+
mongodb_exporter_checksums_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\
7+
mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt"
8+
mongodb_exporter_skip_install: false
9+
10+
mongodb_exporter_web_listen_address: "0.0.0.0:9216"
11+
mongodb_exporter_web_telemetry_path: "/metrics"
12+
13+
mongodb_exporter_uri: "mongodb://127.0.0.1:27017/admin?ssl=false"
14+
mongodb_exporter_collectors: []
15+
mongodb_exporter_collstats_colls: []
16+
mongodb_exporter_indexstats_colls: []
17+
mongodb_exporter_collstats_limit: 0
18+
mongodb_exporter_timeout_offset: 1
19+
mongodb_exporter_profile_time_ts: 30
20+
21+
mongodb_exporter_compatible_mode: false
22+
mongodb_exporter_discovering_mode: false
23+
mongodb_exporter_direct_connect: false
24+
mongodb_exporter_global_conn_pool: false
25+
mongodb_exporter_metrics_overridedescendingindex: false
26+
27+
mongodb_exporter_tls_server_config: {}
28+
mongodb_exporter_http_server_config: {}
29+
mongodb_exporter_basic_auth_users: {}
30+
31+
mongodb_exporter_log_level: "error"
32+
33+
mongodb_exporter_binary_install_dir: "/usr/local/bin"
34+
mongodb_exporter_system_group: "{{ mongodb_exporter_system_user }}"
35+
mongodb_exporter_system_user: "mongodb-exp"
36+
mongodb_exporter_config_dir: "/etc/mongodb_exporter"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Restart mongodb_exporter
3+
listen: "restart mongodb_exporter"
4+
become: true
5+
ansible.builtin.systemd:
6+
daemon_reload: true
7+
name: mongodb_exporter
8+
state: restarted
9+
when:
10+
- not ansible_check_mode
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
# yamllint disable rule:line-length
3+
argument_specs:
4+
main:
5+
short_description: "Prometheus mongodb_exporter"
6+
description:
7+
- "Deploy prometheus L(mongodb exporter,https://github.com/percona/mongodb_exporter) using ansible"
8+
author:
9+
- "Prometheus Community"
10+
options:
11+
mongodb_exporter_version:
12+
description: "mongodb_exporter package version. Also accepts latest as parameter."
13+
default: "0.40.0"
14+
mongodb_exporter_skip_install:
15+
description: "mongodb_exporter installation tasks gets skipped when set to true."
16+
type: bool
17+
default: false
18+
mongodb_exporter_binary_local_dir:
19+
description:
20+
- "Enables the use of local packages instead of those distributed on github."
21+
- "The parameter may be set to a directory where the C(mongodb_exporter) binary is stored on the host where ansible is run."
22+
- "This overrides the I(mongodb_exporter_version) parameter"
23+
mongodb_exporter_binary_url:
24+
description: "URL of the mongodb_exporter binaries .tar.gz file"
25+
default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
26+
mongodb_exporter_checksums_url:
27+
description: "URL of the mongodb_exporter checksums file"
28+
default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt"
29+
mongodb_exporter_web_listen_address:
30+
description: "Address on which mongodb exporter will listen"
31+
default: "0.0.0.0:9216"
32+
mongodb_exporter_web_telemetry_path:
33+
description: "Path under which to expose metrics"
34+
default: "/metrics"
35+
mongodb_exporter_config_dir:
36+
description: "The path where exporter configuration is stored"
37+
default: "/etc/mongodb_exporter"
38+
mongodb_exporter_tls_server_config:
39+
description:
40+
- "Configuration for TLS authentication."
41+
- "Keys and values are the same as in L(mongodb_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
42+
type: "dict"
43+
mongodb_exporter_http_server_config:
44+
description:
45+
- "Config for HTTP/2 support."
46+
- "Keys and values are the same as in L(mongodb_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
47+
type: "dict"
48+
mongodb_exporter_basic_auth_users:
49+
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
50+
type: "dict"
51+
mongodb_exporter_log_level:
52+
description: "Only log messages with the given severity or above."
53+
default: "error"
54+
choices:
55+
- "debug"
56+
- "info"
57+
- "warn"
58+
- "error"
59+
- "fatal"
60+
mongodb_exporter_uri:
61+
description: "MongoDB connection URI"
62+
required: true
63+
type: "str"
64+
mongodb_exporter_collectors:
65+
description:
66+
- "List collectors from L(documantation, https://github.com/percona/mongodb_exporter/blob/main/REFERENCE.md)."
67+
- "You can specify 'all' to enable all collectors"
68+
mongodb_exporter_collstats_colls:
69+
description: "List of databases.collections to get $collStats"
70+
type: "list"
71+
mongodb_exporter_indexstats_colls:
72+
description: "List of databases.collections to get $indexStats"
73+
type: "list"
74+
mongodb_exporter_collstats_limit:
75+
description: "Disable collstats, dbstats, topmetrics and indexstats collector if there are more than <n> collections. 0=No limit"
76+
type: "int"
77+
default: 0
78+
mongodb_exporter_timeout_offset:
79+
description: "Offset to subtract from the timeout in seconds"
80+
type: "int"
81+
default: 1
82+
mongodb_exporter_profile_time_ts:
83+
description: "Set time for scrape slow queries"
84+
type: "int"
85+
default: 30
86+
mongodb_exporter_compatible_mode:
87+
description: "Enable old mongodb-exporter compatible metrics"
88+
type: bool
89+
default: false
90+
mongodb_exporter_discovering_mode:
91+
description: "Enable autodiscover collections"
92+
type: bool
93+
default: false
94+
mongodb_exporter_direct_connect:
95+
description: "Whether or not a direct connect should be made. Direct connections are not valid if multiple hosts are specified or an SRV URI is used"
96+
type: bool
97+
default: false
98+
mongodb_exporter_global_conn_pool:
99+
description: "Use global connection pool instead of creating new pool for each http request"
100+
type: bool
101+
default: false
102+
mongodb_exporter_metrics_overridedescendingindex:
103+
description: "Enable descending index name override to replace -1 with _DESC"
104+
type: bool
105+
default: false
106+
mongodb_exporter_binary_install_dir:
107+
description:
108+
- "I(Advanced)"
109+
- "Directory to install mongodb_exporter binary"
110+
default: "/usr/local/bin"
111+
mongodb_exporter_system_group:
112+
description:
113+
- "I(Advanced)"
114+
- "System group for mongodb_exporter"
115+
default: "mongodb-exp"
116+
mongodb_exporter_system_user:
117+
description:
118+
- "I(Advanced)"
119+
- "mongodb_exporter user"
120+
default: "mongodb-exp"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
galaxy_info:
3+
author: "Prometheus Community"
4+
description: "Prometheus mongodb_exporter"
5+
license: "Apache"
6+
min_ansible_version: "2.9"
7+
platforms:
8+
- name: "Ubuntu"
9+
versions:
10+
- "focal"
11+
- "jammy"
12+
- name: "Debian"
13+
versions:
14+
- "bullseye"
15+
- "buster"
16+
- name: "EL"
17+
versions:
18+
- "7"
19+
- "8"
20+
- "9"
21+
- name: "Fedora"
22+
versions:
23+
- "37"
24+
- '38'
25+
galaxy_tags:
26+
- "monitoring"
27+
- "prometheus"
28+
- "exporter"
29+
- "metrics"
30+
- "system"
31+
- "mongodb"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
provisioner:
3+
inventory:
4+
group_vars:
5+
all:
6+
mongodb_exporter_binary_local_dir: "/tmp/mongodb_exporter-linux-amd64_local"
7+
mongodb_exporter_web_listen_address: "127.0.1.1:9216"
8+
9+
mongodb_exporter_tls_server_config:
10+
cert_file: /etc/mongodb_exporter/tls.cert
11+
key_file: /etc/mongodb_exporter/tls.key
12+
mongodb_exporter_http_server_config:
13+
http2: true
14+
mongodb_exporter_basic_auth_users:
15+
randomuser: examplepassword
16+
go_arch: amd64
17+
mongodb_exporter_version: 0.40.0
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
- name: Run local preparation
3+
hosts: localhost
4+
gather_facts: false
5+
tasks:
6+
- name: Download mongodb_exporter binary to local folder
7+
become: false
8+
ansible.builtin.get_url:
9+
url: "https://github.com/percona/mongodb_exporter/releases/download/v{{ mongodb_exporter_version }}/\
10+
mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
11+
dest: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
12+
mode: 0644
13+
register: _download_binary
14+
until: _download_binary is succeeded
15+
retries: 5
16+
delay: 2
17+
check_mode: false
18+
19+
- name: Unpack mongodb_exporter binary
20+
become: false
21+
ansible.builtin.unarchive:
22+
src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
23+
dest: "/tmp"
24+
creates: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter"
25+
check_mode: false
26+
27+
- name: Link to mongodb_exporter binaries directory
28+
become: false
29+
ansible.builtin.file:
30+
src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}"
31+
dest: "{{ mongodb_exporter_binary_local_dir }}"
32+
state: link
33+
check_mode: false
34+
35+
- name: Install pyOpenSSL for certificate generation
36+
ansible.builtin.pip:
37+
name: "pyOpenSSL"
38+
39+
- name: Create private key
40+
community.crypto.openssl_privatekey:
41+
path: "/tmp/tls.key"
42+
43+
- name: Create CSR
44+
community.crypto.openssl_csr:
45+
path: "/tmp/tls.csr"
46+
privatekey_path: "/tmp/tls.key"
47+
48+
- name: Create certificate
49+
community.crypto.x509_certificate:
50+
path: "/tmp/tls.cert"
51+
csr_path: "/tmp/tls.csr"
52+
privatekey_path: "/tmp/tls.key"
53+
provider: selfsigned
54+
55+
- name: Run target preparation
56+
hosts: all
57+
any_errors_fatal: true
58+
tasks:
59+
- name: Create mongodb_exporter cert dir
60+
ansible.builtin.file:
61+
path: "{{ mongodb_exporter_tls_server_config.cert_file | dirname }}"
62+
state: directory
63+
owner: root
64+
group: root
65+
mode: u+rwX,g+rwX,o=rX
66+
67+
- name: Copy cert and key
68+
ansible.builtin.copy:
69+
src: "{{ item.src }}"
70+
dest: "{{ item.dest }}"
71+
mode: "{{ item.mode | default('0644') }}"
72+
loop:
73+
- src: "/tmp/tls.cert"
74+
dest: "{{ mongodb_exporter_tls_server_config.cert_file }}"
75+
- src: "/tmp/tls.key"
76+
dest: "{{ mongodb_exporter_tls_server_config.key_file }}"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import (absolute_import, division, print_function)
2+
__metaclass__ = type
3+
4+
import os
5+
import testinfra.utils.ansible_runner
6+
7+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
8+
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
9+
10+
11+
def test_directories(host):
12+
dirs = [
13+
"/etc/mongodb_exporter"
14+
]
15+
for dir in dirs:
16+
d = host.file(dir)
17+
assert d.is_directory
18+
assert d.exists
19+
20+
21+
def test_service(host):
22+
s = host.service("mongodb_exporter")
23+
try:
24+
assert s.is_running
25+
except AssertionError:
26+
# Capture service logs
27+
journal_output = host.run('journalctl -u mongodb_exporter --since "1 hour ago"')
28+
print("\n==== journalctl -u mongodb_exporter Output ====\n")
29+
print(journal_output)
30+
print("\n============================================\n")
31+
raise # Re-raise the original assertion error
32+
33+
34+
def test_protecthome_property(host):
35+
s = host.service("mongodb_exporter")
36+
p = s.systemd_properties
37+
assert p.get("ProtectHome") == "yes"
38+
39+
40+
def test_socket(host):
41+
sockets = [
42+
"tcp://127.0.1.1:9216"
43+
]
44+
for socket in sockets:
45+
s = host.socket(socket)
46+
assert s.is_listening
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
provisioner:
3+
inventory:
4+
group_vars:
5+
all:
6+
mongodb_exporter_web_listen_address: "127.0.0.1:9216"

0 commit comments

Comments
 (0)