feat: include influxdb exporter role

Include the [Influxdb exporter](https://github.com/prometheus/influxdb_exporter) role.

Signed-off-by: dobbi84 <davide.obbi@e4company.com>
This commit is contained in:
dobbi84 2024-08-06 15:57:53 +02:00 committed by gardar
parent 7cebe5bd9e
commit ad51f0b088
20 changed files with 648 additions and 0 deletions

View file

@ -0,0 +1,54 @@
# Ansible Role: influxdb exporter
## Description
Deploy prometheus [influxdb exporter](https://github.com/prometheus/influxdb_exporter) using ansible.
## Requirements
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
## Role Variables
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).
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/influxdb_exporter_role.html) for description and default values of the variables.
## Example
### Playbook
Use it in a playbook as follows:
```yaml
- hosts: all
roles:
- prometheus.prometheus.influxdb_exporter
```
### 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.

View file

@ -0,0 +1,23 @@
---
influxdb_exporter_version: "0.11.5"
influxdb_exporter_binary_local_dir: ""
influxdb_exporter_binary_url: "https://github.com/{{ _influxdb_exporter_repo }}/releases/download/v{{ influxdb_exporter_version }}/\
influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
influxdb_exporter_checksums_url: "https://github.com/{{ _influxdb_exporter_repo }}/releases/download/v{{ influxdb_exporter_version }}/sha256sums.txt"
influxdb_exporter_skip_install: false
influxdb_exporter_web_listen_address: "0.0.0.0:9122"
influxdb_exporter_udp_bind_address: "{{ influxdb_exporter_web_listen_address }}"
influxdb_exporter_web_telemetry_path: "/metrics"
influxdb_exporter_exporter_web_telemetry_path: "/metrics/exporter"
influxdb_exporter_influxdb_sample_expiry: "5m"
influxdb_exporter_log_level: "info"
influxdb_exporter_log_format: "logfmt"
influxdb_exporter_export_timestamps: true
influxdb_exporter_binary_install_dir: "/usr/local/bin"
influxdb_exporter_system_group: "influxdb-exp"
influxdb_exporter_system_user: "{{ influxdb_exporter_system_group }}"
# Local path to stash the archive and its extraction
influxdb_exporter_archive_path: /tmp

View file

@ -0,0 +1,10 @@
---
- name: Restart influxdb_exporter
listen: "restart influxdb_exporter"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: influxdb_exporter
state: restarted
when:
- not ansible_check_mode

View file

@ -0,0 +1,72 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus Influxdb Exporter"
description:
- "Deploy prometheus L(influxdb exporter,https://github.com/prometheus/influxdb_exporter) using ansible"
author:
- "Prometheus Community"
options:
influxdb_exporter_version:
description: "influxdb exporter package version. Also accepts latest as parameter."
default: "0.11.5"
influxdb_exporter_skip_install:
description: "influxdb exporter installation tasks gets skipped when set to true."
type: bool
default: false
influxdb_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(influxdb_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(influxdb_exporter_version) parameter"
influxdb_exporter_binary_url:
description: "URL of the influxdb exporter binaries .tar.gz file"
default: "https://github.com/{{ _influxdb_exporter_repo }}/releases/download/v{{ influxdb_exporter_version }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
influxdb_exporter_checksums_url:
description: "URL of the influxdb exporter checksums file"
default: "https://github.com/{{ _influxdb_exporter_repo }}/releases/download/v{{ influxdb_exporter_version }}/sha256sums.txt"
influxdb_exporter_web_listen_address:
description: "Address on which influxdb exporter will listen"
default: "0.0.0.0:9122"
influxdb_exporter_web_telemetry_path:
description: "Path under which to expose metrics"
default: "/metrics"
influxdb_exporter_influxdb_sample_expiry:
description: "How long a sample is valid for"
default: "5m"
influxdb_exporter_udp_bind_address:
description: "Address on which to listen for udp packets"
default: "0.0.0.0:9122"
influxdb_exporter_exporter_web_telemetry_path:
description: "Path under which to expose metrics"
default: "/metrics"
influxdb_exporter_log_level:
default: "info"
description: "Only log messages with the given severity or above"
choices: ["debug", "info", "warn", "error"]
influxdb_exporter_log_format:
default: "logfmt"
description: "Output format of log messages"
choices: ["logfmt", "json"]
influxdb_exporter_export_timestamps:
default: true
description: "Export timestamps of points"
influxdb_exporter_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install influxdb_exporter binary"
default: "/usr/local/bin"
influxdb_exporter_system_group:
description:
- "I(Advanced)"
- "System group for influxdb exporter"
default: "influxdb-exp"
influxdb_exporter_system_user:
description:
- "I(Advanced)"
- "influxdb exporter user"
default: "influxdb-exp"
influxdb_exporter_archive_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"

View file

@ -0,0 +1,25 @@
---
galaxy_info:
author: "Prometheus Community"
description: "Prometheus Influxdb Exporter"
license: "Apache"
min_ansible_version: "2.9"
platforms:
- name: "Ubuntu"
versions:
- "focal"
- "jammy"
- name: "Debian"
versions:
- "bullseye"
- "buster"
- name: "EL"
versions:
- "8"
- "9"
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"

View file

@ -0,0 +1,12 @@
---
provisioner:
inventory:
group_vars:
all:
influxdb_exporter_binary_local_dir: "/tmp/influxdb_exporter-linux-amd64"
influxdb_exporter_web_listen_address:
- '127.0.0.1:8080'
- '127.0.1.1:8080'
go_arch: amd64
influxdb_exporter_version: 0.11.2

View file

@ -0,0 +1,35 @@
---
- name: Run local preparation
hosts: localhost
gather_facts: false
tasks:
- name: Download influxdb_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "https://github.com/prometheus/influxdb_exporter/releases/download/v{{\
\ influxdb_exporter_version }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{\
\ go_arch }}.tar.gz"
dest: "/tmp/influxdb_exporter-{{ influxdb_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 influxdb_exporter binary
become: false
ansible.builtin.unarchive:
src: "/tmp/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "/tmp"
creates: "/tmp/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch\
\ }}/influxdb_exporter"
check_mode: false
- name: Link to influxdb_exporter binaries directory
become: false
ansible.builtin.file:
src: "/tmp/influxdb_exporter-{{ influxdb_exporter_version }}.linux-amd64"
dest: "/tmp/influxdb_exporter-linux-amd64"
state: link
check_mode: false

View file

@ -0,0 +1,39 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import testinfra.utils.ansible_runner
import pytest
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_directories(host):
dirs = [
"/var/lib/influxdb_exporter"
]
for dir in dirs:
d = host.file(dir)
assert not d.exists
def test_service(host):
s = host.service("influxdb_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u influxdb_exporter --since "1 hour ago"')
print("\n==== journalctl -u influxdb_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error
@pytest.mark.parametrize("sockets", [
"tcp://127.0.0.1:8080",
"tcp://127.0.1.1:8080",
])
def test_socket(host, sockets):
assert host.socket(sockets).is_listening

View file

@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
influxdb_exporter_web_listen_address: "127.0.0.1:9122"

View file

@ -0,0 +1,73 @@
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_directories(host):
dirs = [
"/var/lib/influxdb_exporter"
]
for dir in dirs:
d = host.file(dir)
assert d.is_directory
assert d.exists
def test_files(host):
files = [
"/etc/systemd/system/influxdb_exporter.service",
"/usr/local/bin/influxdb_exporter"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file
def test_permissions_didnt_change(host):
dirs = [
"/etc",
"/root",
"/usr",
"/var"
]
for file in dirs:
f = host.file(file)
assert f.exists
assert f.is_directory
assert f.user == "root"
assert f.group == "root"
def test_user(host):
assert host.group("influxdb-exp").exists
assert "influxdb-exp" in host.user("influxdb-exp").groups
assert host.user("influxdb-exp").shell == "/usr/sbin/nologin"
assert host.user("influxdb-exp").home == "/"
def test_service(host):
s = host.service("influxdb_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u influxdb_exporter --since "1 hour ago"')
print("\n==== journalctl -u influxdb_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error
def test_socket(host):
sockets = [
"tcp://127.0.0.1:9122"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening

View file

@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
influxdb_exporter_version: latest

View file

@ -0,0 +1,37 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import testinfra.utils.ansible_runner
import pytest
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
@pytest.mark.parametrize("files", [
"/etc/systemd/system/influxdb_exporter.service",
"/usr/local/bin/influxdb_exporter"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file
def test_service(host):
s = host.service("influxdb_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u influxdb_exporter --since "1 hour ago"')
print("\n==== journalctl -u influxdb_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error
def test_socket(host):
s = host.socket("tcp://0.0.0.0:9122")
assert s.is_listening

View file

@ -0,0 +1,27 @@
---
- name: Copy the influxdb_exporter systemd service file
ansible.builtin.template:
src: influxdb_exporter.service.j2
dest: /etc/systemd/system/influxdb_exporter.service
owner: root
group: root
mode: 0644
notify: restart influxdb_exporter
- name: Create influxdb_exporter config directory
ansible.builtin.file:
path: "/etc/influxdb_exporter"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX
- name: Allow influxdb_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ influxdb_exporter_web_listen_address.split(':')[-1] }}"
proto: tcp
setype: http_port_t
state: present
when:
- ansible_version.full is version_compare('2.4', '>=')
- ansible_selinux.status == "enabled"

View file

@ -0,0 +1,69 @@
---
- name: Create the influxdb_exporter group
ansible.builtin.group:
name: "{{ influxdb_exporter_system_group }}"
state: present
system: true
when: influxdb_exporter_system_group != "root"
- name: Create the influxdb_exporter user
ansible.builtin.user:
name: "{{ influxdb_exporter_system_user }}"
groups: "{{ influxdb_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: influxdb_exporter_system_user != "root"
- name: Get binary
when:
- influxdb_exporter_binary_local_dir | length == 0
- not influxdb_exporter_skip_install
block:
- name: Download influxdb_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ influxdb_exporter_binary_url }}"
dest: "{{ influxdb_exporter_archive_path }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __influxdb_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack influxdb_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ influxdb_exporter_archive_path }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ influxdb_exporter_archive_path }}"
creates: "{{ influxdb_exporter_archive_path }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}/influxdb_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate influxdb_exporter binaries
ansible.builtin.copy:
src: "{{ influxdb_exporter_archive_path }}/influxdb_exporter-{{ influxdb_exporter_version }}.linux-{{ go_arch }}/influxdb_exporter"
dest: "{{ influxdb_exporter_binary_install_dir }}/influxdb_exporter"
mode: 0755
owner: root
group: root
notify: restart influxdb_exporter
when: not ansible_check_mode
- name: Propagate locally distributed influxdb_exporter binary
ansible.builtin.copy:
src: "{{ influxdb_exporter_binary_local_dir }}/influxdb_exporter"
dest: "{{ influxdb_exporter_binary_install_dir }}/influxdb_exporter"
mode: 0755
owner: root
group: root
when:
- influxdb_exporter_binary_local_dir | length > 0
- not influxdb_exporter_skip_install
notify: restart influxdb_exporter

View file

@ -0,0 +1,59 @@
---
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- influxdb_exporter_install
- influxdb_exporter_configure
- influxdb_exporter_run
tags:
- influxdb_exporter_install
- influxdb_exporter_configure
- influxdb_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- influxdb_exporter_install
when:
( not __influxdb_exporter_is_installed.stat.exists ) or
( influxdb_exporter_binary_local_dir | length > 0 )
tags:
- influxdb_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- influxdb_exporter_configure
when: ansible_selinux.status == "enabled"
tags:
- influxdb_exporter_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- influxdb_exporter_configure
tags:
- influxdb_exporter_configure
- name: Ensure Influxdb Exporter is enabled on boot
become: true
ansible.builtin.systemd:
daemon_reload: true
name: influxdb_exporter
enabled: true
state: started
when:
- not ansible_check_mode
tags:
- influxdb_exporter_run

View file

@ -0,0 +1,54 @@
---
- name: Assert usage of systemd as an init system
ansible.builtin.assert:
that: ansible_service_mgr == 'systemd'
msg: "This role only works with systemd"
- name: Naive assertion of proper listen address
ansible.builtin.assert:
that:
- >-
[influxdb_exporter_web_listen_address] |
flatten |
reject('match', '.+:\\d+$') |
list |
length == 0
- name: Check if influxdb_exporter is installed
ansible.builtin.stat:
path: "{{ influxdb_exporter_binary_install_dir }}/influxdb_exporter"
register: __influxdb_exporter_is_installed
check_mode: false
tags:
- influxdb_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
influxdb_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _influxdb_exporter_repo }}/releases/latest', headers=_influxdb_exporter_github_api_headers,
split_lines=False) | from_json).get('tag_name') | replace('v', '') }}"
run_once: true
until: influxdb_exporter_version is version('0.0.0', '>=')
retries: 10
when:
- influxdb_exporter_version == "latest"
- influxdb_exporter_binary_local_dir | length == 0
- not influxdb_exporter_skip_install
- name: Get influxdb_exporter binary checksum
when:
- influxdb_exporter_binary_local_dir | length == 0
- not influxdb_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__influxdb_exporter_checksums: "{{ lookup('url', influxdb_exporter_checksums_url, headers=_influxdb_exporter_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __influxdb_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__influxdb_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __influxdb_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"

View file

@ -0,0 +1,12 @@
---
- name: Install selinux python packages [RedHat]
ansible.builtin.package:
name: "{{ ['libselinux-python', 'policycoreutils-python']
if ansible_python_version is version('3', '<') else
['python3-libselinux', 'python3-policycoreutils'] }}"
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when: ansible_os_family | lower == "redhat"

View file

@ -0,0 +1,31 @@
{{ ansible_managed | comment }}
[Unit]
Description=Influxdb to Prometheus Exporter Service
After=network-online.target
[Service]
Type=simple
User={{ influxdb_exporter_system_user }}
Group={{ influxdb_exporter_system_group }}
ExecStart={{ influxdb_exporter_binary_install_dir }}/influxdb_exporter \
'--web.listen-address={{ influxdb_exporter_web_listen_address }}' \
'--udp.bind-address={{ influxdb_exporter_udp_bind_address }}' \
'--web.telemetry-path={{ influxdb_exporter_web_telemetry_path }}' \
'--web.exporter-telemetry-path={{ influxdb_exporter_exporter_web_telemetry_path }}' \
'--influxdb.sample-expiry={{ influxdb_exporter_influxdb_sample_expiry }}' \
'--log.level={{ influxdb_exporter_log_level }}' \
'--log.format={{ influxdb_exporter_log_format }}' \
{% if influxdb_exporter_export_timestamps %}
'--timestamps'
{% else %}
'--no-timestamps'
{% endif %}
SyslogIdentifier=influxdb_exporter
Restart=always
RestartSec=1
StartLimitInterval=0
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1 @@
bcrypt

View file

@ -0,0 +1,3 @@
---
_influxdb_exporter_repo: "prometheus/influxdb_exporter"
_influxdb_exporter_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"