mirror of
https://github.com/prometheus-community/ansible
synced 2024-11-10 06:14:13 +00:00
feat: add smartctl_exporter role (#229)
Signed-off-by: Nikolay Asenov <asenov.nikolay98@gmail.com> Signed-off-by: Ben Kochie <superq@gmail.com> Co-authored-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
parent
2dac933659
commit
9503b96854
24 changed files with 848 additions and 0 deletions
44
roles/smartctl_exporter/README.md
Normal file
44
roles/smartctl_exporter/README.md
Normal file
|
@ -0,0 +1,44 @@
|
|||
<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: smartctl_exporter
|
||||
|
||||
## Description
|
||||
|
||||
Deploy prometheus [smartctl_exporter](https://github.com/prometheus-community/smartctl_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`)
|
||||
- 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) 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/smartctl_exporter_role.html) for description and default values of the variables.
|
||||
|
||||
## Example
|
||||
|
||||
### Demo site
|
||||
|
||||
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).
|
||||
|
||||
## 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 to for your system. Running your tests is as simple as executing `molecule test`.
|
||||
|
||||
## Continuous Intergation
|
||||
|
||||
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.
|
||||
|
||||
## 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.
|
30
roles/smartctl_exporter/defaults/main.yml
Normal file
30
roles/smartctl_exporter/defaults/main.yml
Normal file
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
smartctl_exporter_version: 0.11.0
|
||||
smartctl_exporter_binary_local_dir: ""
|
||||
smartctl_exporter_binary_url: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/\
|
||||
smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
|
||||
smartctl_exporter_checksums_url: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/sha256sums.txt"
|
||||
smartctl_exporter_skip_install: false
|
||||
|
||||
smartctl_exporter_smartctl_path: "/usr/sbin/smartctl"
|
||||
smartctl_exporter_smartctl_interval: "60s"
|
||||
smartctl_exporter_smartctl_rescan: "10m"
|
||||
smartctl_exporter_smartctl_devices: []
|
||||
smartctl_exporter_smartctl_device_exclude: ""
|
||||
smartctl_exporter_smartctl_device_include: ""
|
||||
|
||||
smartctl_exporter_web_listen_address: "0.0.0.0:9633"
|
||||
smartctl_exporter_web_telemetry_path: "/metrics"
|
||||
|
||||
smartctl_exporter_tls_server_config: {}
|
||||
|
||||
smartctl_exporter_http_server_config: {}
|
||||
|
||||
smartctl_exporter_basic_auth_users: {}
|
||||
|
||||
smartctl_exporter_log_level: "info"
|
||||
smartctl_exporter_log_format: "logfmt"
|
||||
|
||||
smartctl_exporter_binary_install_dir: "/usr/local/bin"
|
||||
smartctl_exporter_system_group: "smartctl-exp"
|
||||
smartctl_exporter_system_user: "{{ smartctl_exporter_system_group }}"
|
10
roles/smartctl_exporter/handlers/main.yml
Normal file
10
roles/smartctl_exporter/handlers/main.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
- name: Restart smartctl_exporter
|
||||
listen: "restart smartctl_exporter"
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
name: smartctl_exporter
|
||||
state: restarted
|
||||
when:
|
||||
- not ansible_check_mode
|
92
roles/smartctl_exporter/meta/argument_specs.yml
Normal file
92
roles/smartctl_exporter/meta/argument_specs.yml
Normal file
|
@ -0,0 +1,92 @@
|
|||
---
|
||||
# yamllint disable rule:line-length
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: "Prometheus Smartctl Exporter"
|
||||
description:
|
||||
- "Deploy prometheus L(smartctl exporter,https://github.com/prometheus-community/smartctl_exporter) using ansible"
|
||||
author:
|
||||
- "Prometheus Community"
|
||||
options:
|
||||
smartctl_exporter_version:
|
||||
description: "Smartctl exporter package version. Also accepts latest as parameter."
|
||||
default: "0.11.0"
|
||||
smartctl_exporter_skip_install:
|
||||
description: "Smartctl exporter installation tasks gets skipped when set to true."
|
||||
type: bool
|
||||
default: false
|
||||
smartctl_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(smartctl_exporter) binary is stored on the host where ansible is run."
|
||||
- "This overrides the I(smartctl_exporter_version) parameter"
|
||||
smartctl_exporter_binary_url:
|
||||
description: "URL of the Smartctl exporter binaries .tar.gz file"
|
||||
default: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
|
||||
smartctl_exporter_checksums_url:
|
||||
description: "URL of the Smartctl exporter checksums file"
|
||||
default: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/sha256sums.txt"
|
||||
smartctl_exporter_smartctl_interval:
|
||||
description: "The interval between smartctl polls"
|
||||
default: "60s"
|
||||
smartctl_exporter_smartctl_rescan:
|
||||
description:
|
||||
- "The interval between rescanning for new/disappeared devices."
|
||||
- "If the interval is smaller than 1s norescanning takes place."
|
||||
- "If any devices are configured with smartctl_exporter_smartctl_device also no rescanning takes place."
|
||||
default: "10m"
|
||||
smartctl_exporter_smartctl_devices:
|
||||
description: "List of devices to be monitored by smartctl_exporter. Disables auto scan for available devices."
|
||||
type: "list"
|
||||
elements: "str"
|
||||
smartctl_exporter_smartctl_device_exclude:
|
||||
description: "Regexp of devices to exclude from automatic scanning. (mutually exclusive to smartctl_exporter_smartctl_device_include)"
|
||||
default: ""
|
||||
smartctl_exporter_smartctl_device_include:
|
||||
description: "Regexp of devices to include in automatic scanning. (mutually exclusive to smartctl_exporter_smartctl_device_exclude)"
|
||||
default: ""
|
||||
smartctl_exporter_web_listen_address:
|
||||
description: "Address on which smartctl exporter will listen"
|
||||
default: "0.0.0.0:9633"
|
||||
smartctl_exporter_web_telemetry_path:
|
||||
description: "Path under which to expose metrics"
|
||||
default: "/metrics"
|
||||
smartctl_exporter_tls_server_config:
|
||||
description:
|
||||
- "Configuration for TLS authentication."
|
||||
- "Keys and values are the same as in L(smartctl_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
|
||||
type: "dict"
|
||||
smartctl_exporter_http_server_config:
|
||||
description:
|
||||
- "Config for HTTP/2 support."
|
||||
- "Keys and values are the same as in L(smartctl_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
|
||||
type: "dict"
|
||||
smartctl_exporter_basic_auth_users:
|
||||
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
|
||||
type: "dict"
|
||||
smartctl_exporter_log_level:
|
||||
description: "Only log messages with the given severity or above. One of: [debug, info, warn, error]"
|
||||
default: "info"
|
||||
smartctl_exporter_log_format:
|
||||
description: "Output format of log messages. One of: [logfmt, json]"
|
||||
default: "logfmt"
|
||||
smartctl_exporter_smartctl_path:
|
||||
description:
|
||||
- "I(Advanced)"
|
||||
- "The path to the smartctl binary"
|
||||
default: "/usr/sbin/smartctl"
|
||||
smartctl_exporter_binary_install_dir:
|
||||
description:
|
||||
- "I(Advanced)"
|
||||
- "Directory to install smartctl_exporter binary"
|
||||
default: "/usr/local/bin"
|
||||
smartctl_exporter_system_group:
|
||||
description:
|
||||
- "I(Advanced)"
|
||||
- "System group for Smartctl exporter"
|
||||
default: "smartctl-exp"
|
||||
smartctl_exporter_system_user:
|
||||
description:
|
||||
- "I(Advanced)"
|
||||
- "Smartctl exporter user"
|
||||
default: "smartctl-exp"
|
30
roles/smartctl_exporter/meta/main.yml
Normal file
30
roles/smartctl_exporter/meta/main.yml
Normal file
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
galaxy_info:
|
||||
author: "Prometheus Community"
|
||||
description: "Prometheus Smartctl Exporter"
|
||||
license: "Apache"
|
||||
min_ansible_version: "2.9"
|
||||
platforms:
|
||||
- name: "Ubuntu"
|
||||
versions:
|
||||
- "focal"
|
||||
- "jammy"
|
||||
- name: "Debian"
|
||||
versions:
|
||||
- "bullseye"
|
||||
- "buster"
|
||||
- name: "EL"
|
||||
versions:
|
||||
- "7"
|
||||
- "8"
|
||||
- "9"
|
||||
- name: "Fedora"
|
||||
versions:
|
||||
- "37"
|
||||
- '38'
|
||||
galaxy_tags:
|
||||
- "monitoring"
|
||||
- "prometheus"
|
||||
- "exporter"
|
||||
- "metrics"
|
||||
- "system"
|
21
roles/smartctl_exporter/molecule/alternative/molecule.yml
Normal file
21
roles/smartctl_exporter/molecule/alternative/molecule.yml
Normal file
|
@ -0,0 +1,21 @@
|
|||
---
|
||||
provisioner:
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
smartctl_exporter_binary_local_dir: "/tmp/smartctl_exporter-linux-amd64"
|
||||
smartctl_exporter_web_listen_address: "127.0.0.1:8080"
|
||||
smartctl_exporter_smartctl_interval: "30s"
|
||||
smartctl_exporter_smartctl_rescan: "5m"
|
||||
smartctl_exporter_smartctl_device_include: "/dev/sda"
|
||||
smartctl_exporter_smartctl_device_exclude: "/dev/sdb"
|
||||
|
||||
smartctl_exporter_tls_server_config:
|
||||
cert_file: /etc/smartctl_exporter/tls.cert
|
||||
key_file: /etc/smartctl_exporter/tls.key
|
||||
smartctl_exporter_http_server_config:
|
||||
http2: true
|
||||
smartctl_exporter_basic_auth_users:
|
||||
randomuser: examplepassword
|
||||
go_arch: amd64
|
||||
smartctl_exporter_version: 0.11.0
|
78
roles/smartctl_exporter/molecule/alternative/prepare.yml
Normal file
78
roles/smartctl_exporter/molecule/alternative/prepare.yml
Normal file
|
@ -0,0 +1,78 @@
|
|||
---
|
||||
- name: Run local preparation
|
||||
hosts: localhost
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Download smartctl_exporter binary to local folder
|
||||
become: false
|
||||
ansible.builtin.get_url:
|
||||
url: "https://github.com/prometheus-community/smartctl_exporter/releases/download/v{{\
|
||||
\ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{\
|
||||
\ go_arch }}.tar.gz"
|
||||
dest: "/tmp/smartctl_exporter-{{ smartctl_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 smartctl_exporter binary
|
||||
become: false
|
||||
ansible.builtin.unarchive:
|
||||
src: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
|
||||
dest: "/tmp"
|
||||
creates: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch\
|
||||
\ }}/smartctl_exporter"
|
||||
check_mode: false
|
||||
|
||||
- name: Link to smartctl_exporter binaries directory
|
||||
become: false
|
||||
ansible.builtin.file:
|
||||
src: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-amd64"
|
||||
dest: "/tmp/smartctl_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
|
||||
|
||||
- name: Run target preparation
|
||||
hosts: all
|
||||
any_errors_fatal: true
|
||||
tasks:
|
||||
- name: Create smartctl_exporter cert dir
|
||||
ansible.builtin.file:
|
||||
path: "{{ smartctl_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: "{{ smartctl_exporter_tls_server_config.cert_file }}"
|
||||
- src: "/tmp/tls.key"
|
||||
dest: "{{ smartctl_exporter_tls_server_config.key_file }}"
|
|
@ -0,0 +1,46 @@
|
|||
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 = [
|
||||
"/etc/smartctl_exporter"
|
||||
]
|
||||
for dir in dirs:
|
||||
d = host.file(dir)
|
||||
assert d.is_directory
|
||||
assert d.exists
|
||||
|
||||
|
||||
def test_service(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
try:
|
||||
assert s.is_running
|
||||
except AssertionError:
|
||||
# Capture service logs
|
||||
journal_output = host.run('journalctl -u smartctl_exporter --since "1 hour ago"')
|
||||
print("\n==== journalctl -u smartctl_exporter Output ====\n")
|
||||
print(journal_output)
|
||||
print("\n============================================\n")
|
||||
raise # Re-raise the original assertion error
|
||||
|
||||
|
||||
def test_protecthome_property(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
p = s.systemd_properties
|
||||
assert p.get("ProtectHome") == "yes"
|
||||
|
||||
|
||||
def test_socket(host):
|
||||
sockets = [
|
||||
"tcp://127.0.0.1:8080"
|
||||
]
|
||||
for socket in sockets:
|
||||
s = host.socket(socket)
|
||||
assert s.is_listening
|
6
roles/smartctl_exporter/molecule/default/molecule.yml
Normal file
6
roles/smartctl_exporter/molecule/default/molecule.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
provisioner:
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
smartctl_exporter_web_listen_address: "127.0.0.1:9633"
|
|
@ -0,0 +1,64 @@
|
|||
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 = [
|
||||
"/etc/smartctl_exporter"
|
||||
]
|
||||
for dir in dirs:
|
||||
d = host.file(dir)
|
||||
assert d.is_directory
|
||||
assert d.exists
|
||||
|
||||
|
||||
def test_files(host):
|
||||
files = [
|
||||
"/etc/systemd/system/smartctl_exporter.service",
|
||||
"/usr/local/bin/smartctl_exporter"
|
||||
]
|
||||
for file in files:
|
||||
f = host.file(file)
|
||||
assert f.exists
|
||||
assert f.is_file
|
||||
|
||||
|
||||
def test_user(host):
|
||||
assert host.group("smartctl-exp").exists
|
||||
assert "smartctl-exp" in host.user("smartctl-exp").groups
|
||||
assert host.user("smartctl-exp").shell == "/usr/sbin/nologin"
|
||||
assert host.user("smartctl-exp").home == "/"
|
||||
|
||||
|
||||
def test_service(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
try:
|
||||
assert s.is_running
|
||||
except AssertionError:
|
||||
# Capture service logs
|
||||
journal_output = host.run('journalctl -u smartctl_exporter --since "1 hour ago"')
|
||||
print("\n==== journalctl -u smartctl_exporter Output ====\n")
|
||||
print(journal_output)
|
||||
print("\n============================================\n")
|
||||
raise # Re-raise the original assertion error
|
||||
|
||||
|
||||
def test_protecthome_property(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
p = s.systemd_properties
|
||||
assert p.get("ProtectHome") == "yes"
|
||||
|
||||
|
||||
def test_socket(host):
|
||||
sockets = [
|
||||
"tcp://127.0.0.1:9633"
|
||||
]
|
||||
for socket in sockets:
|
||||
s = host.socket(socket)
|
||||
assert s.is_listening
|
6
roles/smartctl_exporter/molecule/latest/molecule.yml
Normal file
6
roles/smartctl_exporter/molecule/latest/molecule.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
provisioner:
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
smartctl_exporter_version: latest
|
43
roles/smartctl_exporter/molecule/latest/tests/test_latest.py
Normal file
43
roles/smartctl_exporter/molecule/latest/tests/test_latest.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
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/smartctl_exporter.service",
|
||||
"/usr/local/bin/smartctl_exporter"
|
||||
])
|
||||
def test_files(host, files):
|
||||
f = host.file(files)
|
||||
assert f.exists
|
||||
assert f.is_file
|
||||
|
||||
|
||||
def test_service(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
try:
|
||||
assert s.is_running
|
||||
except AssertionError:
|
||||
# Capture service logs
|
||||
journal_output = host.run('journalctl -u smartctl_exporter --since "1 hour ago"')
|
||||
print("\n==== journalctl -u smartctl_exporter Output ====\n")
|
||||
print(journal_output)
|
||||
print("\n============================================\n")
|
||||
raise # Re-raise the original assertion error
|
||||
|
||||
|
||||
def test_protecthome_property(host):
|
||||
s = host.service("smartctl_exporter")
|
||||
p = s.systemd_properties
|
||||
assert p.get("ProtectHome") == "yes"
|
||||
|
||||
|
||||
def test_socket(host):
|
||||
s = host.socket("tcp://0.0.0.0:9633")
|
||||
assert s.is_listening
|
36
roles/smartctl_exporter/tasks/configure.yml
Normal file
36
roles/smartctl_exporter/tasks/configure.yml
Normal file
|
@ -0,0 +1,36 @@
|
|||
---
|
||||
- name: Copy the smartctl_exporter systemd service file
|
||||
ansible.builtin.template:
|
||||
src: smartctl_exporter.service.j2
|
||||
dest: /etc/systemd/system/smartctl_exporter.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
notify: restart smartctl_exporter
|
||||
|
||||
- name: Create smartctl_exporter config directory
|
||||
ansible.builtin.file:
|
||||
path: "/etc/smartctl_exporter"
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: u+rwX,g+rwX,o=rX
|
||||
|
||||
- name: Copy the smartctl_exporter web config file
|
||||
ansible.builtin.template:
|
||||
src: web_config.yaml.j2
|
||||
dest: /etc/smartctl_exporter/web_config.yaml
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
notify: restart smartctl_exporter
|
||||
|
||||
- name: Allow smartctl_exporter port in SELinux on RedHat OS family
|
||||
community.general.seport:
|
||||
ports: "{{ smartctl_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"
|
69
roles/smartctl_exporter/tasks/install.yml
Normal file
69
roles/smartctl_exporter/tasks/install.yml
Normal file
|
@ -0,0 +1,69 @@
|
|||
---
|
||||
- name: Create the smartctl_exporter group
|
||||
ansible.builtin.group:
|
||||
name: "{{ smartctl_exporter_system_group }}"
|
||||
state: present
|
||||
system: true
|
||||
when: smartctl_exporter_system_group != "root"
|
||||
|
||||
- name: Create the smartctl_exporter user
|
||||
ansible.builtin.user:
|
||||
name: "{{ smartctl_exporter_system_user }}"
|
||||
groups: "{{ smartctl_exporter_system_group }}"
|
||||
append: true
|
||||
shell: /usr/sbin/nologin
|
||||
system: true
|
||||
create_home: false
|
||||
home: /
|
||||
when: smartctl_exporter_system_user != "root"
|
||||
|
||||
- name: Get binary
|
||||
when:
|
||||
- smartctl_exporter_binary_local_dir | length == 0
|
||||
- not smartctl_exporter_skip_install
|
||||
block:
|
||||
|
||||
- name: Download smartctl_exporter binary to local folder
|
||||
become: false
|
||||
ansible.builtin.get_url:
|
||||
url: "{{ smartctl_exporter_binary_url }}"
|
||||
dest: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
|
||||
checksum: "sha256:{{ __smartctl_exporter_checksum }}"
|
||||
mode: '0644'
|
||||
register: _download_binary
|
||||
until: _download_binary is succeeded
|
||||
retries: 5
|
||||
delay: 2
|
||||
delegate_to: localhost
|
||||
check_mode: false
|
||||
|
||||
- name: Unpack smartctl_exporter binary
|
||||
become: false
|
||||
ansible.builtin.unarchive:
|
||||
src: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
|
||||
dest: "/tmp"
|
||||
creates: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}/smartctl_exporter"
|
||||
delegate_to: localhost
|
||||
check_mode: false
|
||||
|
||||
- name: Propagate smartctl_exporter binaries
|
||||
ansible.builtin.copy:
|
||||
src: "/tmp/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}/smartctl_exporter"
|
||||
dest: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter"
|
||||
mode: 0755
|
||||
owner: root
|
||||
group: root
|
||||
notify: restart smartctl_exporter
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Propagate locally distributed smartctl_exporter binary
|
||||
ansible.builtin.copy:
|
||||
src: "{{ smartctl_exporter_binary_local_dir }}/smartctl_exporter"
|
||||
dest: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter"
|
||||
mode: 0755
|
||||
owner: root
|
||||
group: root
|
||||
when:
|
||||
- smartctl_exporter_binary_local_dir | length > 0
|
||||
- not smartctl_exporter_skip_install
|
||||
notify: restart smartctl_exporter
|
63
roles/smartctl_exporter/tasks/main.yml
Normal file
63
roles/smartctl_exporter/tasks/main.yml
Normal file
|
@ -0,0 +1,63 @@
|
|||
---
|
||||
- name: Preflight
|
||||
ansible.builtin.include_tasks:
|
||||
file: preflight.yml
|
||||
apply:
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
- smartctl_exporter_configure
|
||||
- smartctl_exporter_run
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
- smartctl_exporter_configure
|
||||
- smartctl_exporter_run
|
||||
|
||||
- name: Install
|
||||
ansible.builtin.include_tasks:
|
||||
file: install.yml
|
||||
apply:
|
||||
become: true
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
when:
|
||||
( not __smartctl_exporter_is_installed.stat.exists ) or
|
||||
( (__smartctl_exporter_current_version_output.stderr_lines | length > 0)
|
||||
and (__smartctl_exporter_current_version_output.stderr_lines[0].split(" ")[2] != smartctl_exporter_version) ) or
|
||||
( (__smartctl_exporter_current_version_output.stdout_lines | length > 0)
|
||||
and (__smartctl_exporter_current_version_output.stdout_lines[0].split(" ")[2] != smartctl_exporter_version) ) or
|
||||
( smartctl_exporter_binary_local_dir | length > 0 )
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
|
||||
- name: SELinux
|
||||
ansible.builtin.include_tasks:
|
||||
file: selinux.yml
|
||||
apply:
|
||||
become: true
|
||||
tags:
|
||||
- smartctl_exporter_configure
|
||||
when: ansible_selinux.status == "enabled"
|
||||
tags:
|
||||
- smartctl_exporter_configure
|
||||
|
||||
- name: Configure
|
||||
ansible.builtin.include_tasks:
|
||||
file: configure.yml
|
||||
apply:
|
||||
become: true
|
||||
tags:
|
||||
- smartctl_exporter_configure
|
||||
tags:
|
||||
- smartctl_exporter_configure
|
||||
|
||||
- name: Ensure Smartctl Exporter is enabled on boot
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
name: smartctl_exporter
|
||||
enabled: true
|
||||
state: started
|
||||
when:
|
||||
- not ansible_check_mode
|
||||
tags:
|
||||
- smartctl_exporter_run
|
99
roles/smartctl_exporter/tasks/preflight.yml
Normal file
99
roles/smartctl_exporter/tasks/preflight.yml
Normal file
|
@ -0,0 +1,99 @@
|
|||
---
|
||||
- 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: Install package fact dependencies
|
||||
become: true
|
||||
ansible.builtin.package:
|
||||
name: "{{ _pkg_fact_req }}"
|
||||
state: present
|
||||
when: (_pkg_fact_req)
|
||||
vars:
|
||||
_pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\
|
||||
{{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }}
|
||||
{% else %}\
|
||||
{% endif %}"
|
||||
|
||||
- name: Gather package facts
|
||||
ansible.builtin.package_facts:
|
||||
when: "not 'packages' in ansible_facts"
|
||||
|
||||
- name: Naive assertion of proper listen address
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "':' in smartctl_exporter_web_listen_address"
|
||||
|
||||
- name: Assert that TLS config is correct
|
||||
when: smartctl_exporter_tls_server_config | length > 0
|
||||
block:
|
||||
- name: Assert that TLS key and cert path are set
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "smartctl_exporter_tls_server_config.cert_file is defined"
|
||||
- "smartctl_exporter_tls_server_config.key_file is defined"
|
||||
|
||||
- name: Check existence of TLS cert file
|
||||
ansible.builtin.stat:
|
||||
path: "{{ smartctl_exporter_tls_server_config.cert_file }}"
|
||||
register: __smartctl_exporter_cert_file
|
||||
|
||||
- name: Check existence of TLS key file
|
||||
ansible.builtin.stat:
|
||||
path: "{{ smartctl_exporter_tls_server_config.key_file }}"
|
||||
register: __smartctl_exporter_key_file
|
||||
|
||||
- name: Assert that TLS key and cert are present
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "{{ __smartctl_exporter_cert_file.stat.exists }}"
|
||||
- "{{ __smartctl_exporter_key_file.stat.exists }}"
|
||||
|
||||
- name: Check if smartctl_exporter is installed
|
||||
ansible.builtin.stat:
|
||||
path: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter"
|
||||
register: __smartctl_exporter_is_installed
|
||||
check_mode: false
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
|
||||
- name: Gather currently installed smartctl_exporter version (if any)
|
||||
ansible.builtin.command: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter --version"
|
||||
changed_when: false
|
||||
register: __smartctl_exporter_current_version_output
|
||||
check_mode: false
|
||||
when: __smartctl_exporter_is_installed.stat.exists
|
||||
tags:
|
||||
- smartctl_exporter_install
|
||||
|
||||
- name: Discover latest version
|
||||
ansible.builtin.set_fact:
|
||||
smartctl_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/prometheus-community/smartctl_exporter/releases/latest', headers=_github_api_headers,
|
||||
split_lines=False) | from_json).get('tag_name') | replace('v', '') }}"
|
||||
run_once: true
|
||||
until: smartctl_exporter_version is version('0.0.0', '>=')
|
||||
retries: 10
|
||||
when:
|
||||
- smartctl_exporter_version == "latest"
|
||||
- smartctl_exporter_binary_local_dir | length == 0
|
||||
- not smartctl_exporter_skip_install
|
||||
|
||||
- name: Get smartctl_exporter binary checksum
|
||||
when:
|
||||
- smartctl_exporter_binary_local_dir | length == 0
|
||||
- not smartctl_exporter_skip_install
|
||||
block:
|
||||
- name: Get checksum list from github
|
||||
ansible.builtin.set_fact:
|
||||
__smartctl_exporter_checksums: "{{ lookup('url', smartctl_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
|
||||
run_once: true
|
||||
until: __smartctl_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
|
||||
retries: 10
|
||||
|
||||
- name: "Get checksum for {{ go_arch }}"
|
||||
ansible.builtin.set_fact:
|
||||
__smartctl_exporter_checksum: "{{ item.split(' ')[0] }}"
|
||||
with_items: "{{ __smartctl_exporter_checksums }}"
|
||||
when:
|
||||
- "('linux-' + go_arch + '.tar.gz') in item"
|
23
roles/smartctl_exporter/tasks/selinux.yml
Normal file
23
roles/smartctl_exporter/tasks/selinux.yml
Normal file
|
@ -0,0 +1,23 @@
|
|||
---
|
||||
- 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"
|
||||
|
||||
- name: Install selinux python packages [clearlinux]
|
||||
ansible.builtin.package:
|
||||
name: sysadmin-basic
|
||||
state: present
|
||||
register: _install_selinux_packages
|
||||
until: _install_selinux_packages is success
|
||||
retries: 5
|
||||
delay: 2
|
||||
when:
|
||||
- ansible_distribution | lower == "clearlinux"
|
|
@ -0,0 +1,46 @@
|
|||
{{ ansible_managed | comment }}
|
||||
|
||||
[Unit]
|
||||
Description=Prometheus Smartctl Exporter
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User={{ smartctl_exporter_system_user }}
|
||||
Group={{ smartctl_exporter_system_group }}
|
||||
ExecStart={{ smartctl_exporter_binary_install_dir }}/smartctl_exporter \
|
||||
'--smartctl.path={{ smartctl_exporter_smartctl_path }}' \
|
||||
'--smartctl.interval={{ smartctl_exporter_smartctl_interval }}' \
|
||||
'--smartctl.rescan={{ smartctl_exporter_smartctl_rescan }}' \
|
||||
{% for device in smartctl_exporter_smartctl_devices %}
|
||||
'--smartctl.device={{ device }}' \
|
||||
{% endfor %}
|
||||
'--smartctl.device-exclude={{ smartctl_exporter_smartctl_device_exclude }}' \
|
||||
'--smartctl.device-include={{ smartctl_exporter_smartctl_device_include }}' \
|
||||
'--web.listen-address={{ smartctl_exporter_web_listen_address }}' \
|
||||
'--web.telemetry-path={{ smartctl_exporter_web_telemetry_path }}' \
|
||||
{% if smartctl_exporter_tls_server_config | length > 0 or smartctl_exporter_http_server_config | length > 0 or smartctl_exporter_basic_auth_users | length > 0 %}
|
||||
'--web.config.file=/etc/smartctl_exporter/web_config.yaml' \
|
||||
{% endif %}
|
||||
'--log.level={{ smartctl_exporter_log_level }}' \
|
||||
'--log.format={{ smartctl_exporter_log_format }}'
|
||||
|
||||
SyslogIdentifier=smartctl_exporter
|
||||
Restart=always
|
||||
RestartSec=1
|
||||
StartLimitInterval=0
|
||||
|
||||
ProtectHome=yes
|
||||
NoNewPrivileges=yes
|
||||
|
||||
{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %}
|
||||
ProtectSystem=strict
|
||||
ProtectControlGroups=true
|
||||
ProtectKernelModules=true
|
||||
ProtectKernelTunables=yes
|
||||
{% else %}
|
||||
ProtectSystem=full
|
||||
{% endif %}
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
18
roles/smartctl_exporter/templates/web_config.yaml.j2
Normal file
18
roles/smartctl_exporter/templates/web_config.yaml.j2
Normal file
|
@ -0,0 +1,18 @@
|
|||
---
|
||||
{{ ansible_managed | comment }}
|
||||
{% if smartctl_exporter_tls_server_config | length > 0 %}
|
||||
tls_server_config:
|
||||
{{ smartctl_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
|
||||
{% endif %}
|
||||
|
||||
{% if smartctl_exporter_http_server_config | length > 0 %}
|
||||
http_server_config:
|
||||
{{ smartctl_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
|
||||
{% endif %}
|
||||
|
||||
{% if smartctl_exporter_basic_auth_users | length > 0 %}
|
||||
basic_auth_users:
|
||||
{% for k, v in smartctl_exporter_basic_auth_users.items() %}
|
||||
{{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
1
roles/smartctl_exporter/test-requirements.txt
Normal file
1
roles/smartctl_exporter/test-requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
bcrypt
|
11
roles/smartctl_exporter/vars/main.yml
Normal file
11
roles/smartctl_exporter/vars/main.yml
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
go_arch_map:
|
||||
i386: '386'
|
||||
x86_64: 'amd64'
|
||||
aarch64: 'arm64'
|
||||
armv7l: 'armv7'
|
||||
armv6l: 'armv6'
|
||||
|
||||
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
|
||||
_smartctl_exporter_repo: "prometheus-community/smartctl_exporter"
|
||||
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
|
||||
source "$collection_root/tests/integration/molecule.sh"
|
4
tests/integration/targets/molecule-smartctl_exporter-default/runme.sh
Executable file
4
tests/integration/targets/molecule-smartctl_exporter-default/runme.sh
Executable file
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
|
||||
source "$collection_root/tests/integration/molecule.sh"
|
4
tests/integration/targets/molecule-smartctl_exporter-latest/runme.sh
Executable file
4
tests/integration/targets/molecule-smartctl_exporter-latest/runme.sh
Executable file
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
|
||||
source "$collection_root/tests/integration/molecule.sh"
|
Loading…
Reference in a new issue