Skip to content

Commit

Permalink
feat: Add mysqld_exporter role
Browse files Browse the repository at this point in the history
Add a role for deploying the mysqld_exporter.

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ authored and prombot committed Mar 9, 2023
1 parent d389ad2 commit dab41e8
Show file tree
Hide file tree
Showing 29 changed files with 882 additions and 0 deletions.
99 changes: 99 additions & 0 deletions roles/mysqld_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<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>

# Ansible Role: mysqld exporter


Deploy prometheus [mysql exporter](/~https://github.com/prometheus/mysqld_exporter) using ansible.

## Requirements

- Ansible >= 2.7 (It might work on previous versions, but we cannot guarantee it)
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)

## Role Variables

All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) and are listed in the table below.

| Name | Default Value | Description |
| -------------- | ------------- | -----------------------------------|
| `mysqld_exporter_version` | 0.14.0 | mysqld exporter package version. Also accepts latest as parameter. |
| `mysqld_exporter_binary_local_dir` | "" | Enables the use of local packages instead of those distributed on github. The parameter may be set to a directory where the `mysqld_exporter` binary is stored on the host where ansible is run. This overrides the `mysqld_exporter_version` parameter |
| `mysqld_exporter_binary_url` | `/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz` | URL of the mysqld\_exporter binaries .tar.gz file |
| `mysqld_exporter_checksums_url` | `/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt` | URL of the mysqld\_exporter checksums file |
| `mysqld_exporter_web_listen_address` | "0.0.0.0:9104" | Address on which mysqld\_exporter will listen |
| `mysqld_exporter_web_telemetry_path` | "/metrics" | Path under which to expose metrics |
| `mysqld_exporter_enabled_collectors` | `[]` | List of dicts defining additionally enabled collectors and their configuration. It adds collectors to [those enabled by default](/~https://github.com/prometheus/mysqld_exporter#collector-flags). |
| `mysqld_exporter_disabled_collectors` | [] | List of disabled collectors. By default mysqld_exporter disables collectors listed [here](/~https://github.com/prometheus/mysqld_exporter#collector-flags). |
| `mysqld_exporter_tls_server_config` | {} | Configuration for TLS authentication. Keys and values are the same as in [mysqld_exporter docs](/~https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config). |
| `mysqld_exporter_http_server_config` | {} | Config for HTTP/2 support. Keys and values are the same as in [mysqld_exporter docs](/~https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config). |
| `mysqld_exporter_basic_auth_users` | {} | Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt. |

## Example

### Playbook

Use it in a playbook as follows:
```yaml
- hosts: all
collections:
- prometheus.prometheus
roles:
- prometheus.prometheus.mysqld_exporter
```
### TLS config
Before running mysqld_exporter role, the user needs to provision their own certificate and key.
```yaml
- hosts: all
pre_tasks:
- name: Create mysqld_exporter cert dir
ansible.builtin.file:
path: "/etc/mysqld_exporter"
state: directory
owner: root
group: root

- name: Create cert and key
community.crypto.x509_certificate:
path: /etc/mysqld_exporter/tls.cert
csr_path: /etc/mysqld_exporter/tls.csr
privatekey_path: /etc/mysqld_exporter/tls.key
provider: selfsigned
collections:
- prometheus.prometheus
roles:
- prometheus.prometheus.mysqld_exporter
vars:
mysqld_exporter_tls_server_config:
cert_file: /etc/mysqld_exporter/tls.cert
key_file: /etc/mysqld_exporter/tls.key
mysqld_exporter_basic_auth_users:
randomuser: examplepassword
```
### Demo site
We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](/~https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com).
## Local Testing
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 for your system. Running your tests is as simple as executing `molecule test`.

## Continuous Integration

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 quite a large test matrix which can take more time than local testing, so please be patient.

## Contributing

See [contributor guideline](CONTRIBUTING.md).

## Troubleshooting

See [troubleshooting](TROUBLESHOOTING.md).

## License

This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
20 changes: 20 additions & 0 deletions roles/mysqld_exporter/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Troubleshooting

## Bad requests (HTTP 400)

This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur.

This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers.

Unless netrc is needed by your playbook and ansible roles, please unset the var like so:

```
$ NETRC= ansible-playbook ...
```

Or:

```
$ export NETRC=
$ ansible-playbook ...
```
35 changes: 35 additions & 0 deletions roles/mysqld_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
mysqld_exporter_version: 0.14.0
mysqld_exporter_binary_local_dir: ""
mysqld_exporter_binary_url: "/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/\
mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz"
mysqld_exporter_checksums_url: "/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt"

mysqld_exporter_web_listen_address: "0.0.0.0:9104"
mysqld_exporter_web_telemetry_path: "/metrics"

mysqld_exporter_username: exporter
mysqld_exporter_password: secret

mysqld_exporter_config_file: "mysqld_exporter.cnf"
# mysqld_exporter_host: localhost
# mysqld_exporter_port: 3306
mysqld_exporter_socket: "/run/mysqld/mysqld.sock"

mysqld_exporter_tls_server_config: {}

mysqld_exporter_http_server_config: {}

mysqld_exporter_basic_auth_users: {}

mysqld_exporter_enabled_collectors: []
# - engine_innodb_status
# - perf_schema.eventsstatements

mysqld_exporter_disabled_collectors: []

# Internal variables.
_mysqld_exporter_binary_install_dir: "/usr/local/bin"
_mysqld_exporter_config_dir: "/etc/mysqld_exporter"
_mysqld_exporter_system_group: "mysqld-exp"
_mysqld_exporter_system_user: "{{ _mysqld_exporter_system_group }}"
10 changes: 10 additions & 0 deletions roles/mysqld_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart mysqld_exporter
listen: "restart mysqld_exporter"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: mysqld_exporter
state: restarted
when:
- not ansible_check_mode
55 changes: 55 additions & 0 deletions roles/mysqld_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus MySQLd Exporter"
description:
- "Deploy prometheus L(mysqld_exporter,/~https://github.com/prometheus/mysqld_exporter) using ansible"
author:
- "Prometheus Community"
options:
mysqld_exporter_version:
description: "MySQLd exporter package version. Also accepts latest as parameter."
default: "1.1.2"
mysqld_exporter_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on github."
- "The parameter may be set to a directory where the C(mysqld_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(mysqld_exporter_version) parameter"
mysqld_exporter_binary_url:
description: "URL of the mysqld_exporter binaries .tar.gz file"
default: "/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz"
mysqld_exporter_checksums_url:
description: "URL of the mysqld_exporter checksums file"
default: "/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt"
mysqld_exporter_web_listen_address:
description: "Address on which mysqld_exporter will listen"
default: "0.0.0.0:9104"
mysqld_exporter_web_telemetry_path:
description: "Path under which to expose metrics"
default: "/metrics"
mysqld_exporter_enabled_collectors:
description:
- "List of dicts defining additionally enabled collectors and their configuration."
- "It adds collectors to L(those enabled by default,/~https://github.com/prometheus/mysqld_exporter#enabled-by-default)."
type: "list"
default: []
mysqld_exporter_disabled_collectors:
description:
- "List of disabled collectors."
- "By default mysqld_exporter disables collectors listed L(here,/~https://github.com/prometheus/mysqld_exporter#disabled-by-default)."
type: "list"
elements: "str"
mysqld_exporter_tls_server_config:
description:
- "Configuration for TLS authentication."
- "Keys and values are the same as in L(mysqld_exporter docs,/~https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config)."
type: "dict"
mysqld_exporter_http_server_config:
description:
- "Config for HTTP/2 support."
- "Keys and values are the same as in L(mysqld_exporter docs,/~https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config)."
type: "dict"
mysqld_exporter_basic_auth_users:
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
type: "dict"
34 changes: 34 additions & 0 deletions roles/mysqld_exporter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
galaxy_info:
author: Prometheus Community
description: Prometheus MySQLd Exporter
license: Apache
company: none
min_ansible_version: "2.7"
platforms:
- name: Ubuntu
versions:
- bionic
- xenial
- name: Debian
versions:
- stretch
- buster
- name: EL
versions:
- '7'
- '8'
- name: Fedora
versions:
- '30'
- '31'
galaxy_tags:
- monitoring
- prometheus
- exporter
- metrics
- system
- mysql
- mariadb

dependencies: []
40 changes: 40 additions & 0 deletions roles/mysqld_exporter/molecule/alternative/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
- name: Run role
hosts: all
any_errors_fatal: true
roles:
- prometheus.prometheus.mysqld_exporter
pre_tasks:
- name: Create mysqld_exporter cert dir
ansible.builtin.file:
path: "{{ mysqld_exporter_tls_server_config.cert_file | dirname }}"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX

- name: Copy cert and key
ansible.builtin.copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode | default('0644') }}"
loop:
- src: "/tmp/tls.cert"
dest: "{{ mysqld_exporter_tls_server_config.cert_file }}"
- src: "/tmp/tls.key"
dest: "{{ mysqld_exporter_tls_server_config.key_file }}"
vars:
mysqld_exporter_binary_local_dir: "/tmp/mysqld_exporter-linux-amd64"
mysqld_exporter_web_listen_address: "127.0.0.1:8080"
mysqld_exporter_enabled_collectors:
- slave_hosts
mysqld_exporter_disabled_collectors:
- global_variables

mysqld_exporter_tls_server_config:
cert_file: /etc/mysqld_exporter/tls.cert
key_file: /etc/mysqld_exporter/tls.key
mysqld_exporter_http_server_config:
http2: true
mysqld_exporter_basic_auth_users:
randomuser: examplepassword
1 change: 1 addition & 0 deletions roles/mysqld_exporter/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
56 changes: 56 additions & 0 deletions roles/mysqld_exporter/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
- name: Prepare
hosts: localhost
gather_facts: false
vars:
go_arch: amd64
mysqld_exporter_version: 0.13.0
tasks:
- name: Download mysqld_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "/~https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/\
mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz"
mode: 0644
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
check_mode: false

- name: Unpack mysqld_exporter binary
become: false
ansible.builtin.unarchive:
src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "/tmp"
creates: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}/mysqld_exporter"
check_mode: false

- name: Link to mysqld_exporter binaries directory
become: false
ansible.builtin.file:
src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-amd64"
dest: "/tmp/mysqld_exporter-linux-amd64"
state: link
check_mode: false

- name: Install pyOpenSSL for certificate generation
ansible.builtin.pip:
name: "pyOpenSSL"

- name: Create private key
community.crypto.openssl_privatekey:
path: "/tmp/tls.key"

- name: Create CSR
community.crypto.openssl_csr:
path: "/tmp/tls.csr"
privatekey_path: "/tmp/tls.key"

- name: Create certificate
community.crypto.x509_certificate:
path: "/tmp/tls.cert"
csr_path: "/tmp/tls.csr"
privatekey_path: "/tmp/tls.key"
provider: selfsigned
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_service(host):
s = host.service("mysqld_exporter")
# assert s.is_enabled
assert s.is_running


def test_socket(host):
sockets = [
"tcp://127.0.0.1:8080"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
8 changes: 8 additions & 0 deletions roles/mysqld_exporter/molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
any_errors_fatal: true
roles:
- prometheus.prometheus.mysqld_exporter
vars:
mysqld_exporter_web_listen_address: "127.0.0.1:9104"
1 change: 1 addition & 0 deletions roles/mysqld_exporter/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Loading

0 comments on commit dab41e8

Please sign in to comment.