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

add postgres exporter #236

Merged
merged 16 commits into from Jul 30, 2018
Merged
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ This module automates the install and configuration of Prometheus monitoring too
* Installs a configuration file for prometheus daemon (/etc/prometheus/prometheus.yaml) or for alertmanager (/etc/prometheus/alert.rules)
* Manages the services via upstart, sysv, or systemd
* Optionally creates alert rules
* The following exporters are currently implemented: node_exporter, statsd_exporter, process_exporter, haproxy_exporter, mysqld_exporter, blackbox_exporter, consul_exporter, redis_exporter, varnish_exporter, graphite_exporter
* The following exporters are currently implemented: node_exporter, statsd_exporter, process_exporter, haproxy_exporter, mysqld_exporter, blackbox_exporter, consul_exporter, redis_exporter, varnish_exporter, graphite_exporter, postgres_exporter

## Usage

Expand Down
12 changes: 12 additions & 0 deletions data/defaults.yaml
Expand Up @@ -253,6 +253,18 @@ prometheus::blackbox_exporter::package_name: 'blackbox_exporter'
prometheus::blackbox_exporter::modules: {}
prometheus::blackbox_exporter::config_file: '/etc/blackbox-exporter.yaml'
prometheus::blackbox_exporter::version: '0.7.0'
prometheus::postgres_exporter::data_source_uri: 'host=/var/run/postgresql/ sslmode=disable'
prometheus::postgres_exporter::postgres_pass: 'postgres_exporter_password'
prometheus::postgres_exporter::postgres_user: 'postgres_exporter_username'
Copy link
Member

Choose a reason for hiding this comment

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

should postgres_user and postgres_pass default to undef? Might makes it more obvious to users that they need to change it.

Copy link
Author

Choose a reason for hiding this comment

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

agreed,
i had to add the postgres_pass and postgres_pass to the spec test, to make the daemon start

prometheus::postgres_exporter::download_extension: 'tar.gz'
prometheus::postgres_exporter::download_url_base: 'https://github.com/wrouesnel/postgres_exporter/releases'
prometheus::postgres_exporter::extra_groups: []
prometheus::postgres_exporter::group: 'postgres-exporter'
prometheus::postgres_exporter::package_ensure: 'latest'
prometheus::postgres_exporter::package_name: 'postgres_exporter'
prometheus::postgres_exporter::postgres_auth_method: 'env'
prometheus::postgres_exporter::user: 'postgres-exporter'
prometheus::postgres_exporter::version: '0.4.6'
prometheus::redis_exporter::addr:
- 'redis://localhost:6379'
prometheus::redis_exporter::download_extension: 'tar.gz'
Expand Down
210 changes: 210 additions & 0 deletions manifests/postgres_exporter.pp
@@ -0,0 +1,210 @@
# Class: prometheus::postgres_exporter
#
# This module manages prometheus node postgres_exporter
#
# Parameters:
# [*arch*]
# Architecture (amd64 or i386)
#

# [*bin_dir*]
# Directory where binaries are located
#
# [*addr*]
# Array of address of one or more redis nodes. Defaults to redis://localhost:6379
#
# [*download_extension*]
# Extension for the release binary archive
#
# [*download_url*]
# Complete URL corresponding to the where the release binary archive can be downloaded
#
# [*download_url_base*]
# Base URL for the binary archive
#
# [*extra_groups*]
# Extra groups to add the binary user to
#
# [*extra_options*]
# Extra options added to the startup command
# For a full list of the exporter's supported extra options
# please refer to https://github.com/wrouesnel/postgres_exporter
#
# [*group*]
# Group under which the binary is running
#
# [*init_style*]
# Service startup scripts style (e.g. rc, upstart or systemd)
#
# [*install_method*]
# Installation method: url or package (only url is supported currently)
Copy link
Member

Choose a reason for hiding this comment

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

only url is supported currently. Is that true?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not aware of a packaged version of postgres_exporter, if it exsist, its not tested.

#
# [*manage_group*]
# Whether to create a group for or rely on external code for that
#
# [*manage_service*]
# Should puppet manage the service? (default true)
#
# [*manage_user*]
# Whether to create user or rely on external code for that
#
# [*os*]
# Operating system (linux is the only one supported)
#
# [*package_ensure*]
# If package, then use this for package ensure default 'latest'
#
# [*package_name*]
# The binary package name - not available yet
#
# [*purge_config_dir*]
# Purge config files no longer generated by Puppet
#
# [*restart_on_change*]
# Should puppet restart the service on configuration change? (default true)
#
# [*service_enable*]
# Whether to enable the service from puppet (default true)
#
# [*service_ensure*]
# State ensured for the service (default 'running')
#
# [*service_name*]
# Name of the node exporter service (default 'postgres_exporter')
#
# [*user*]
# User which runs the service
#
# [*version*]
# The binary release version

# [*postgres_user*]
# User to authenticate against postgres
#
# [*postgres_pass*]
# Password to authenticate against postgres
#
# [*postgres_auth_method*]
# method for presenting username and password to the exporter
# This can be file, or env (default 'env')
#
# [*data_source_uri*]
# Uri on howto connect to the database

class prometheus::postgres_exporter (
String $download_extension,
String $download_url_base,
Array[String] $extra_groups,
Copy link
Member

Choose a reason for hiding this comment

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

Can you please update this to Array[String[1]]? That will enforce a minimum string length of 1.

Copy link
Author

Choose a reason for hiding this comment

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

ok. done

String $group,
Copy link
Member

Choose a reason for hiding this comment

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

can you update the String datatype to String[1] on all locations where it makes sense? (It enforces a minimum string length of 1).

Copy link
Author

Choose a reason for hiding this comment

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

I've added quite a few [1]'s to where it makes sense. also some minor cleanups, in de comments

String $package_ensure,
String $package_name,
String $user,
String $version,
String $data_source_uri,
String $postgres_pass,
String $postgres_user,
Enum['env', 'file'] $postgres_auth_method,
Boolean $purge_config_dir = true,
Boolean $restart_on_change = true,
Boolean $service_enable = true,
String $service_ensure = 'running',
Copy link
Member

Choose a reason for hiding this comment

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

for all the other String datatypes here: Please ensure a minimal String length of 1.

Copy link
Author

Choose a reason for hiding this comment

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

now its done for all non optional strings.

String $service_name = 'postgres_exporter',
Optional[String] $init_style = $prometheus::init_style,
String $install_method = $prometheus::install_method,
Boolean $manage_group = true,
Boolean $manage_service = true,
Boolean $manage_user = true,
String $os = $prometheus::os,
String $options = '',
Optional[String] $download_url = undef,
String $arch = $prometheus::real_arch,
String $bin_dir = $prometheus::bin_dir,
) inherits prometheus {

$release = "v${version}"

$real_download_url = pick($download_url, "${download_url_base}/download/${release}/${package_name}_${release}_${os}-${arch}.${download_extension}")

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

case $postgres_auth_method {
'env': {
$env_vars = {
'DATA_SOURCE_URI' => $data_source_uri,
'DATA_SOURCE_USER' => $postgres_user,
'DATA_SOURCE_PASS' => $postgres_pass,
}
}
'file': {
$env_vars = {
'DATA_SOURCE_URI' => $data_source_uri,
'DATA_SOURCE_USER_FILE' => $postgres_user,
'DATA_SOURCE_PASS_FILE' => $postgres_pass,
}
}
default: {
$env_vars = {}
}
}

if $install_method == 'url' {
# Not a big fan of copypasting but prometheus::daemon takes for granted
# a specific path embedded in the prometheus *_exporter tarball, which
# redis_exporter lacks.
# TODO: patch prometheus::daemon to support custom extract directories
$exporter_install_method = 'none'
$install_dir = "/opt/${service_name}-${version}.${os}-${arch}"
file { $install_dir:
ensure => 'directory',
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555',
}
-> archive { "/tmp/${service_name}-${version}.${download_extension}":
ensure => present,
extract => true,
extract_path => $install_dir,
extract_flags => '--strip-components=1 -xzf',
source => $real_download_url,
checksum_verify => false,
creates => "${install_dir}/${service_name}",
cleanup => true,
}
-> file { "${bin_dir}/${service_name}":
ensure => link,
notify => $notify_service,
target => "${install_dir}/${service_name}",
before => Prometheus::Daemon[$service_name],
}
} else {
$exporter_install_method = $install_method
}

prometheus::daemon { $service_name:
install_method => $exporter_install_method,
version => $version,
download_extension => $download_extension,
env_vars => $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,
}
}
19 changes: 19 additions & 0 deletions spec/acceptance/postgres_exporter_spec.rb
@@ -0,0 +1,19 @@
require 'spec_helper_acceptance'

describe 'prometheus postgres exporter' do
it 'postgres_exporter works idempotently with no errors' do
pp = 'include prometheus::postgres_exporter'
# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe service('postgres_exporter') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end
# the class installs an the postgres_exporter that listens on port 9187
describe port(9187) do
it { is_expected.to be_listening.with('tcp6') }
end
end
27 changes: 27 additions & 0 deletions spec/classes/postgres_exporter_spec.rb
@@ -0,0 +1,27 @@
require 'spec_helper'

describe 'prometheus::postgres_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.4.6',
arch: 'amd64',
os: 'linux',
bin_dir: '/usr/local/bin',
install_method: 'url'
}
end

describe 'install correct binary' do
it { is_expected.to contain_file('/usr/local/bin/postgres_exporter').with('target' => '/opt/postgres_exporter-0.4.6.linux-amd64/postgres_exporter') }
Copy link
Member

Choose a reason for hiding this comment

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

please also add something like this:

it { is_expected.to compile.with_all_deps }

Copy link
Author

Choose a reason for hiding this comment

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

added, a few tests.
I used redis_exporter as a starting point, which is about the only spec test that doesn't do this, shall i create an other pull request enhanching the redis_exporter?

Copy link
Member

Choose a reason for hiding this comment

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

That would be awesome!

end
end
end
end
end