add smartctl_exporter role

This commit is contained in:
Sergey Malyuk
2025-12-16 11:40:16 +03:00
parent 7a41b9697c
commit d8d3c2df67
21 changed files with 341 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
---
profile: production
strict: true
# Enable checking of loop variable prefixes in roles
loop_var_prefix: "^(__|{role}_)"
skip_list:
- var-naming[no-role-prefix]
warn_list:
- role-name[path]
- var-naming[no-role-prefix]
exclude_paths:
- .github/

2
roles/smartctl_exporter/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.vscode
.idea

View File

@@ -0,0 +1,8 @@
---
rules:
brackets:
forbid: false
min-spaces-inside: 0
max-spaces-inside: 2
min-spaces-inside-empty: -1
max-spaces-inside-empty: 2

View File

@@ -0,0 +1,56 @@
genlab.smartctl-exporter
=========
```smartctl``` is a command-line utility used to control and monitor the Self-Monitoring, Analysis and Reporting Technology (SMART) system in hard disk drives (HDDs) and solid-state drives (SSDs). ```smartctl-exporter``` exports ```smartctl``` statistics to Prometheus.
Requirements
------------
smartctl
Role Variables
--------------
The exporter will scan the system for available devices if no ```--smartctl.device``` flags are used. The format of web.config file id described [here](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md). If ```smartctl_exp_source_web_config_dir``` is defined, role searches for ```web_smartctl.conf``` file and copy it to target host to ```smartctl_exp_web_config_dir``` directory
```yaml
---
smartctl_exp_port: "localhost:9633" # Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.
smartctl_exp_version: "0.14.0" # exporter version to install
smartctl_exp_dir: "/etc/exporters" # where to download and unarchive expoter
smartctl_exp_web_config_dir: "/etc/exporters/config" # Path to configuration file that can enable TLS or authentication
smartctl_exp_interval: "60s" # The interval between smartctl polls
smartctl_exp_rescan: "10m" # The interval between rescanning for new/disappeared devices. If the interval is smaller than 1s no rescanning takes place. If any devices are configured with smartctl.device also no rescanning takes place.
smartctl_exp_devices: [] # The device to monitor (repeatable)
smartctl_exp_device_exclude: "" #Regexp of devices to exclude from automatic scanning. (mutually exclusive to device-include)
smartctl_exp_device_include: "" # Regexp of devices to include in automatic scanning. (mutually exclusive to device-exclude)
smartctl_exp_web_telemetry_path: "/metrics" # Path under which to expose metrics
smartctl_exp_log_level: "info" # Only log messages with the given severity or above. One of: [debug, info, warn, error]
smartctl_exp_log_format: "logfmt" # Output format of log messages. One of: [logfmt, json]
smartctl_exp_args: # --version --web.systemd-socket
smartctl_exp_source_web_config_dir: ""
```
Dependencies
------------
None
Example Playbook
----------------
```yaml
roles:
- role: genlab.smartctl_exporter
smartctl_exp_version: "0.14.0"
smartctl_exp_dir: "/etc/exporters"
smartctl_exp_config_dir: "/etc/exporters/config"
```
License
-------
BSD
Author Information
------------------
corvus-migratorius@proton.me

View File

@@ -0,0 +1,11 @@
---
name: ansible-role-template
channels:
- conda-forge
dependencies:
- python~=3.12.0
- pip>=24.2
- actionlint
- pip:
- -r requirements.txt
- -r requirements.ci.txt

View File

@@ -0,0 +1,9 @@
---
name: ansible-role-template
channels:
- conda-forge
dependencies:
- python~=3.12.0
- pip>=24.2
- pip:
- -r requirements.txt

View File

@@ -0,0 +1,14 @@
---
smartctl_exp_web_listen_address: "localhost:9633"
smartctl_exp_args: ""
smartctl_exp_version: "0.14.0"
smartctl_exp_dir: "/etc/exporters"
smartctl_exp_web_config_dir: "/etc/exporters/config"
smartctl_exp_interval: "60s"
smartctl_exp_rescan: "10m"
smartctl_exp_devices: []
smartctl_exp_device_exclude: ""
smartctl_exp_device_include: ""
smartctl_exp_web_telemetry_path: "/metrics"
smartctl_exp_log_level: "info"
smartctl_exp_log_format: "logfmt"

View File

@@ -0,0 +1,7 @@
---
- name: "(Re)start and enable smartctl_exporter"
ansible.builtin.systemd_service:
name: smartctl_exporter.service
state: restarted
enabled: true
daemon_reload: true

View File

@@ -0,0 +1,17 @@
---
galaxy_info:
role_name: "smartctl_exporter"
namespace: genlab
author: "Alexander Gorelyshev"
company: "Genlab, LLC"
description: ""
license: "MIT"
min_ansible_version: "2.1"
platforms:
- name: "Ubuntu"
versions: [ "focal", "jammy", "noble" ]
galaxy_tags: [ ]
dependencies: []

View File

@@ -0,0 +1,7 @@
---
- name: Converge
hosts: all
roles:
- role: genlab.smartctl_exporter
smartctl_exp_dir: "/etc/exporters"
smartctl_exp_config_dir: "/etc/exporters/config"

View File

@@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2404}-ansible:latest
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
provisioner:
name: ansible
verifier:
name: ansible
lint: |
set -e
yamllint .
ansible-lint .

View File

@@ -0,0 +1,37 @@
---
- name: Verify
hosts: all
gather_facts: false
any_errors_fatal: true
tasks:
- name: "Include default vars"
ansible.builtin.include_vars:
dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/"
extensions: [ 'yml' ]
- name: "Check if smartctl_exporter is installed"
changed_when: false
ansible.builtin.command: "smartctl_exporter --version"
register: smartctl_exporter_installed_version
- name: "Check smartctl_exporter version"
ansible.builtin.assert:
that: "smartctl_exporter_installed_version.stderr is regex('{{ smartctl_exp_version }}')"
success_msg: "smartctl_exporter version {{ smartctl_exp_version }} is installed and working"
fail_msg: "smartctl_exporter version {{ smartctl_exp_version }} is not installed or not working correctly"
# kics-scan ignore-block
- name: "Check if /metrics endpoint is reachable"
ansible.builtin.uri:
url: "http://{{ smartctl_exp_web_listen_address }}/metrics"
return_content: true
status_code: 200
timeout: 5
register: metrics_check
- name: "Fail if /metrics doesn't contain smartctl_exporter_build_info line"
ansible.builtin.assert:
that: "'smartctl_exporter_build_info' in metrics_check.content"
fail_msg: "smartctl_exporter /metrics endpoint doesn't contain smartctl_exporter_build_info line!"
success_msg: "smartctl_exporter /metrics endpoint contains smartctl_exporter_build_info line!"

View File

@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
}

View File

@@ -0,0 +1,6 @@
ansible-lint
molecule==24.12.0
molecule-plugins[docker]
docker~=7.1.0
requests==2.31.0 # pinned to the latest version not breaking Docker SDK
yamllint

View File

@@ -0,0 +1 @@
ansible~=11.3.0

View File

@@ -0,0 +1,3 @@
# requirements file
---
collections: []

View File

@@ -0,0 +1,10 @@
---
- name: "Upload smartctl_exporter local configure file"
notify: "(Re)start and enable smartctl_exporter"
when: smartctl_exp_source_web_config_dir is defined and smartctl_exp_source_web_config_dir | length > 0
ansible.builtin.template:
src: "{{ smartctl_exp_source_web_config_dir }}/web_smartctl.conf"
dest: "{{ smartctl_exp_web_config_dir }}/web_smartctl.conf"
owner: root
group: root
mode: '0640'

View File

@@ -0,0 +1,51 @@
---
- name: "Install smartctl_exporter from binary"
block:
- name: "Check smartctl_exporter version"
changed_when: false
ansible.builtin.command:
cmd: "smartctl_exporter --version"
register: smartctl_exporter_ver
- name: "Assert version correctness"
ansible.builtin.assert:
that: "smartctl_exporter_ver.stderr is regex('{{ smartctl_exp_version }}')"
success_msg: "smartctl_exporter version {{ smartctl_exp_version }} is installed and working"
fail_msg: "smartctl_exporter version {{ smartctl_exp_version }} is not installed or not working correctly"
rescue:
- name: "Create smartctl_exporter directories"
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: "0755"
with_items:
- "{{ smartctl_exp_dir }}"
- "{{ smartctl_exp_config_dir }}"
- name: "Unarchive smartctl_exporter tar file"
notify: "(Re)start and enable smartctl_exporter"
ansible.builtin.unarchive:
src: "https://github.com/prometheus-community/smartctl_exporter/releases/\
download/v{{ smartctl_exp_version }}/smartctl_exporter-{{ smartctl_exp_version }}.linux-amd64.tar.gz"
dest: "{{ smartctl_exp_dir }}"
remote_src: true
- name: "Move smartctl_exporter binary"
ansible.builtin.copy:
src: "{{ smartctl_exp_dir }}/smartctl_exporter-{{ smartctl_exp_version }}.linux-amd64/smartctl_exporter"
dest: "/usr/local/bin/smartctl_exporter"
mode: "0755"
owner: root
group: root
remote_src: true
- name: Create systemd service file
ansible.builtin.template:
src: smartctl_exporter.service.j2
dest: /etc/systemd/system/smartctl_exporter.service
owner: root
group: root
mode: '0644'

View File

@@ -0,0 +1,6 @@
---
- name: "Include installation tasks"
ansible.builtin.include_tasks: "install.yml"
- name: "Including configuration tasks"
ansible.builtin.include_tasks: "configure.yml"

View File

@@ -0,0 +1,46 @@
[Unit]
Description=Smartctl Exporter
Documentation=https://github.com/prometheus-community/smartctl_exporter
After=network.target
StartLimitIntervalSec=120
StartLimitBurst=5
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/bin/smartctl_exporter \
--web.listen-address={{ smartctl_exp_web_listen_address }} \
--smartctl.interval={{ smartctl_exp_interval }} \
--smartctl.rescan={{ smartctl_exp_rescan }} \
{% if smartctl_exp_devices is defined and smartctl_exp_devices | length > 0 %}
{% for device in smartctl_exp_devices %}
--smartctl.device={{ device }} \
{% endfor %}
{% endif %}
--smartctl.device-exclude={{ smartctl_exp_device_exclude }} \
--smartctl.device-include={{ smartctl_exp_device_include }} \
--web.telemetry-path={{ smartctl_exp_web_telemetry_path }} \
--log.level={{ smartctl_exp_log_level }} \
--log.format={{ smartctl_exp_log_format }} \
{% if smartctl_exp_args is defined and smartctl_exp_args | length > 0 %}
{{ smartctl_exp_args }} \
{% endif %}
{% if smartctl_exp_source_web_config_dir is defined and smartctl_exp_source_web_config_dir | length > 0 %} \
--web.config.file={{ smartctl_exp_web_config_dir }}/web_smartctl.conf
{% endif %}
SyslogIdentifier=smartctl_exporter
Restart=on-failure
RestartSec=5
ProtectHome=yes
NoNewPrivileges=yes
ProtectSystem=strict
ProtectControlGroups=true
ProtectKernelModules=true
ProtectKernelTunables=yes
ProtectSystem=full
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1 @@
---