Skip to content

Commit

Permalink
add postgres exporter (voxpupuli#236)
Browse files Browse the repository at this point in the history
* add postgres exporter

* spec fixes

* spec fixes

* default to no env vars, and extra input checks

* add postgres exporter

* add pointer to postgres_exporter module

* stricter data typing, spectest enhanchements

* tuning spec tests

* change spec tests

* fix rubocop.

* update to follow suggestions of review

* add a comma, spec fixing..

* set required params in acceptance test.
  • Loading branch information
blupman authored and bastelfreak committed Jul 30, 2018
1 parent 9083f1c commit a295981
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 1 deletion.
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
10 changes: 10 additions & 0 deletions data/defaults.yaml
Expand Up @@ -251,6 +251,16 @@ 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::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
209 changes: 209 additions & 0 deletions manifests/postgres_exporter.pp
@@ -0,0 +1,209 @@
# 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)
#
# [*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[1] $download_extension,
String[1] $download_url_base,
Array[String[1]] $extra_groups,
String[1] $group,
String[1] $package_ensure,
String[1] $package_name,
String[1] $user,
String[1] $version,
String[1] $data_source_uri,
String[1] $postgres_pass,
String[1] $postgres_user,
Enum['env', 'file'] $postgres_auth_method,
Boolean $purge_config_dir = true,
Boolean $restart_on_change = true,
Boolean $service_enable = true,
String[1] $service_ensure = 'running',
String[1] $service_name = 'postgres_exporter',
Optional[String] $init_style = $prometheus::init_style,
String[1] $install_method = $prometheus::install_method,
Boolean $manage_group = true,
Boolean $manage_service = true,
Boolean $manage_user = true,
String[1] $os = $prometheus::os,
String $options = '',
Optional[String] $download_url = undef,
String[1] $arch = $prometheus::real_arch,
String[1] $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
# postgres_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 = "class{'prometheus::postgres_exporter': postgres_pass => 'password', postgres_user => 'username' }"
# 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
34 changes: 34 additions & 0 deletions spec/classes/postgres_exporter_spec.rb
@@ -0,0 +1,34 @@
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',
postgres_user: 'username',
postgres_pass: 'password'
}
end

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

0 comments on commit a295981

Please sign in to comment.