Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smartctl Exporter #713

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 251 additions & 0 deletions manifests/smartctl_exporter.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
# @summary This module manages prometheus node smartctl_exporter
# @param arch
# Architecture (See supported archictectures at https://github.com/prometheus-community/smartctl_exporter/releases)
# @param bin_dir
# Directory where binaries are located
# @param smartctl_path
# Directory where smartctl binary is located, default /usr/sbin/smartctl
# @param smartctl_interval
# The interval between smartctl polls, default 60s
# @param smartctl_rescan
# 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. Default 10m
# @param smartctl_device
# The list of devices to monitor. If not supplied, the exporter will scan the host for available devices.
# @param smartctl_device_exclude
# Regexp of devices to exclude from automatic scanning. (mutually exclusive to device-include)
# @param smartctl_device_include
# Regexp of devices to include for automatic scanning. (mutually exclusive to device-exclude)
# @param web_telemetry_path
# Path under which to expose metrics. Default is /metrics
# @param web_systemd_socket
# Use systemd socket activation listeners instead of port listeners (Linux only).
# @param web_listen_address
# Addresses on which to expose metrics and web interface. Repeatable for multiple addresses. Defaults to :9633
# @param web_config_file
# [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.
# @param web_config_content
# Unless empty the content of the web-config yaml which will handed over as option to the exporter
# @param log_level
# Only log messages with the given severity or above. One of: [debug, info, warn, error]. Default info
# @param log_format
# Output format of log messages. One of: [logfmt, json]. Default logfmt
# @param download_extension
# Extension for the release binary archive
# @param download_url
# Complete URL corresponding to the where the release binary archive can be downloaded
# @param download_url_base
# Base URL for the binary archive
# @param extra_groups
# Extra groups to add the binary user to
# @param group
# Group under which the binary is running. Default 'root' (or 'wheel' on Darwin), unlikely to work without root.
# @param init_style
# Service startup scripts style (e.g. rc, upstart or systemd)
# @param install_method
# Installation method: url or package (only url is supported currently)
# @param manage_group
# Whether to create a group for or rely on external code for that. Default false due to script needing root.
# @param manage_service
# Should puppet manage the service? (default true)
# @param manage_user
# Whether to create user or rely on external code for that. Default false due to script needing root.
# @param os
# Operating system
# @param package_ensure
# If package, then use this for package ensure default 'latest'
# @param package_name
# The binary package name - not available yet
# @param purge_config_dir
# Purge config files no longer generated by Puppet
# @param restart_on_change
# Should puppet restart the service on configuration change? (default true)
# @param service_enable
# Whether to enable the service from puppet (default true)
# @param service_ensure
# State ensured for the service (default 'running')
# @param service_name
# Name of the node exporter service (default 'smartctl_exporter')
# @param user
# User which runs the service (default 'root', unlikely to work without root)
# @param version
# The binary release version
# @param proxy_server
# Optional proxy server, with port number if needed. ie: https://example.com:8080
# @param proxy_type
# Optional proxy server type (none|http|https|ftp)
class prometheus::smartctl_exporter (
String $download_extension = 'tar.gz',
Prometheus::Uri $download_url_base = 'https://github.com/prometheus-community/smartctl_exporter/releases',
Array[String[1]] $extra_groups = [],
String[1] $group = 'root',
String[1] $package_ensure = 'latest',
String[1] $package_name = 'smartctl_exporter',
String[1] $user = 'root',
String[1] $version = '0.12.0',
Optional[String[1]] $smartctl_path = undef,
Optional[String[1]] $smartctl_interval = undef,
Optional[String[1]] $smartctl_rescan = undef,
Array[String[1]] $smartctl_device = [],
Optional[String[1]] $smartctl_device_exclude = undef,
Optional[String[1]] $smartctl_device_include = undef,
Optional[String[1]] $web_telemetry_path = undef,
Optional[String[1]] $web_systemd_socket = undef,
Array[String[1]] $web_listen_address = [],
Optional[Enum['debug','info','warn','error']] $log_level = undef,
Optional[Enum['logfmt','json']] $log_format = undef,
Boolean $purge_config_dir = true,
Boolean $restart_on_change = true,
Boolean $service_enable = true,
Stdlib::Ensure::Service $service_ensure = 'running',
String[1] $service_name = 'smartctl_exporter',
Prometheus::Initstyle $init_style = $prometheus::init_style,
Prometheus::Install $install_method = $prometheus::install_method,
Boolean $manage_group = false,
Boolean $manage_service = true,
Boolean $manage_user = false,
String[1] $os = downcase($facts['kernel']),
String $options = '', # lint:ignore:params_empty_string_assignment
Optional[Prometheus::Uri] $download_url = undef,
String[1] $arch = $prometheus::real_arch,
Stdlib::Absolutepath $bin_dir = $prometheus::bin_dir,
Boolean $export_scrape_job = false,
Optional[Stdlib::Host] $scrape_host = undef,
Stdlib::Port $scrape_port = 9633,
String[1] $scrape_job_name = 'smartctl',
Optional[Hash] $scrape_job_labels = undef,
Stdlib::Absolutepath $web_config_file = '/etc/smartctl_exporter_web-config.yml',
Optional[Hash] $web_config_content = undef,
Optional[String[1]] $proxy_server = undef,
Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type = undef,
) inherits prometheus {
$release = "v${version}"

$real_download_url = pick($download_url, "${download_url_base}/download/${release}/${package_name}-${version}.${os}-${arch}.${download_extension}") # lint:ignore:140chars

$notify_service = $restart_on_change ? {
true => Service[$service_name],
default => undef,
}

$_web_config_ensure = $web_config_content.empty ? {
true => absent,
default => file,
}

file { $web_config_file:
ensure => $_web_config_ensure,
owner => $user,
group => $group,
mode => '0640',
content => $web_config_content.stdlib::to_yaml,
notify => $notify_service,
}

$_web_config = if $web_config_content.empty {
''
} else {
"--web.config.file=${$web_config_file}"
}

$_cmd_smartctl_device = $smartctl_device.map |$device| {
"--smartctl.device=${device}"
}

$_cmd_web_listen_address = $web_listen_address.map |$address| {
"--web.listen-address=${address}"
}

$_smartctl_path = $smartctl_path ? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why set the defaults for all these parameters to undef and then override them here. Instead remove the Optional and set the default value directly in the parameter definition.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not quite what's happening here. The intent here was to support smartctl's optional parameters that, when passed, require them to be passed with specific named flags.

To be clear about the section you highlighted:

If the user sets a smartctl_path, then pass it using --smartctl.path=
If the user does not set a smartctl_path, then don't pass anything.

In other words, smartctl_path remains an optional flag. If I removed Optional and set the default value in the parameter definition per your suggestion, I would be sending invalid (empty) flags to the exporter.

I tried to follow the pattern set in other exporters in this project. See e.g. this:

$_status_paths = $status_paths ? {
undef => '',
default => "-openvpn.status_paths=${join(unique($status_paths), ',')}",
}

If you have another preferred pattern to use in instances like this, let me know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, i overlooked that.

The usual pattern is to just pass in extra_options just search for it in the code multiple example

That being said, I already started a discussion about moving all the exporters out of this module. I am quite reluctant to to the ever growing list of exporters in this module.

So if you want to have exporter specific parameters, maybe a dedicated module is a better option. It could still be depending on puppet-prometheus and re-use all of the shared bits.
But I that's just my opinion at the moment

undef => '',
default => "--smartctl.path=${smartctl_path}"
}

$_smartctl_interval = $smartctl_interval ? {
undef => '',
default => "--smartctl.interval=${smartctl_interval}"
}

$_smartctl_rescan = $smartctl_rescan ? {
undef => '',
default => "--smartctl.rescan=${smartctl_rescan}"
}

$_smartctl_device_exclude = $smartctl_device_exclude ? {
undef => '',
default => "--smartctl.device-exclude=${smartctl_device_exclude}"
}

$_smartctl_device_include = $smartctl_device_include ? {
undef => '',
default => "--smartctl.device-include=${smartctl_device_include}"
}

$_web_telemetry_path = $web_telemetry_path ? {
undef => '',
default => "--web.telemetry-path=${web_telemetry_path}"
}

$_web_systemd_socket = $web_systemd_socket ? {
undef => '',
default => '--web.systemd-socket'
}

$_log_level = $log_level ? {
undef => '',
default => "--log.level=${log_level}"
}

$_log_format = $log_format ? {
undef => '',
default => "--log.format=${log_format}"
}

$_options = [
$options,
$_web_config,
$_cmd_smartctl_device,
$_cmd_web_listen_address,
$_smartctl_path,
$_smartctl_interval,
$_smartctl_rescan,
$_smartctl_device_exclude,
$_smartctl_device_include,
$_web_telemetry_path,
$_web_systemd_socket,
$_log_level,
$_log_format,
].filter |$x| { !$x.empty }.join(' ')

prometheus::daemon { $service_name:
install_method => $install_method,
version => $version,
download_extension => $download_extension,
env_vars => {},
os => $os,
arch => $arch,
bin_dir => $bin_dir,
notify_service => $notify_service,
package_name => $package_name,
package_ensure => $package_ensure,
manage_user => $manage_user,
user => $user,
extra_groups => $extra_groups,
real_download_url => $real_download_url,
group => $group,
manage_group => $manage_group,
purge => $purge_config_dir,
options => $_options,
init_style => $init_style,
service_ensure => $service_ensure,
service_enable => $service_enable,
manage_service => $manage_service,
export_scrape_job => $export_scrape_job,
scrape_host => $scrape_host,
scrape_port => $scrape_port,
scrape_job_name => $scrape_job_name,
scrape_job_labels => $scrape_job_labels,
proxy_server => $proxy_server,
proxy_type => $proxy_type,
}
}
51 changes: 51 additions & 0 deletions spec/classes/smartctl_exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'prometheus::smartctl_exporter' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts.merge(os_specific_facts(facts))
end

context 'with version specified' do
let(:params) do
{
version: '0.12.0',
arch: 'amd64',
os: 'linux',
bin_dir: '/usr/local/bin',
install_method: 'url',
}
end

describe 'with all defaults' do
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_file('/usr/local/bin/smartctl_exporter').with('target' => '/opt/smartctl_exporter-0.12.0.linux-amd64/smartctl_exporter') }
it { is_expected.to contain_prometheus__daemon('smartctl_exporter') }
it { is_expected.to contain_user('root') }
it { is_expected.to contain_group('root') }
it { is_expected.to contain_service('smartctl_exporter') }
end

context 'with tls set in web-config.yml' do
let(:params) do
super().merge(
web_config_content: {
tls_server_config: {
cert_file: '/etc/smartctl_exporter/foo.cert',
key_file: '/etc/smartctl_exporter/foo.key'
}
}
)
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_file('/etc/smartctl_exporter_web-config.yml').with(ensure: 'file') }
it { is_expected.to contain_prometheus__daemon('smartctl_exporter').with(options: '--web.config.file=/etc/smartctl_exporter_web-config.yml') }
end
end
end
end
end
Loading