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

Install prometheus server via own class #194

Merged
merged 4 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ This module automates the install and configuration of Prometheus monitoring too

## Usage

**Notice about breaking changes**

Version 5.0.0 and older of this module allowed you to deploy the prometheus server by doing a simple `include prometheus`.
We introduced a [new class layout](https://github.com/voxpupuli/puppet-prometheus/pull/194) in
version 6. By default, including the `prometheus` class won't deploy the server now.
You need to include the `prometheus::server` class for this (which has the same
parameters as `prometheus` had). An alterntive approach is to set the
`manage_prometheus_server` parameter to true in the `prometheus` class. Backgroud information about this change are available in the related [pull request](https://github.com/voxpupuli/puppet-prometheus/pull/187) and the [issue](https://github.com/voxpupuli/puppet-prometheus/issues/184)

To set up a prometheus daemon:
On the server (for prometheus version < 1.0.0):

```puppet
class { '::prometheus':
class { 'prometheus::server':
global_config => { 'scrape_interval'=> '15s', 'evaluation_interval'=> '15s', 'external_labels'=> { 'monitor'=>'master'}},
rule_files => [ "/etc/prometheus/alert.rules" ],
scrape_configs => [
Expand All @@ -69,7 +78,7 @@ class { '::prometheus':
On the server (for prometheus version >= 1.0.0):

```puppet
class { 'prometheus':
class { 'prometheus::server':
version => '1.0.0',
scrape_configs => [ {'job_name'=>'prometheus','scrape_interval'=> '30s','scrape_timeout'=>'30s','static_configs'=> [{'targets'=>['localhost:9090'], 'labels'=> { 'alias'=>'Prometheus'}}]}],
extra_options => '-alertmanager.url http://localhost:9093 -web.console.templates=/opt/prometheus-1.0.0.linux-amd64/consoles -web.console.libraries=/opt/prometheus-1.0.0.linux-amd64/console_libraries',
Expand All @@ -79,7 +88,7 @@ class { 'prometheus':
On the server (for prometheus version >= 2.0.0):

```puppet
class { '::prometheus':
class { 'prometheus::server':
version => '2.0.0',
alerts => { 'groups' => [{ 'name' => 'alert.rules', 'rules' => [{ 'alert' => 'InstanceDown', 'expr' => 'up == 0', 'for' => '5m', 'labels' => { 'severity' => 'page', }, 'annotations' => { 'summary' => 'Instance {{ $labels.instance }} down', 'description' => '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.' }}]}]},
scrape_configs => [
Expand All @@ -98,7 +107,7 @@ class { '::prometheus':

or simply:
```puppet
include ::prometheus
include prometheus::server
```

To add alert rules, add the following to the class prometheus in case you are using prometheus < 2.0:
Expand Down Expand Up @@ -173,7 +182,8 @@ Real Prometheus >=2.0.0 setup example including alertmanager and slack_configs.
```puppet
include profiles::prometheus

class { '::prometheus':
class { 'prometheus':
manage_prometheus_server => true,
version => '2.0.0',
alerts => { 'groups' => [{ 'name' => 'alert.rules', 'rules' => [{ 'alert' => 'InstanceDown', 'expr' => 'up == 0', 'for' => '5m', 'labels' => { 'severity' => 'page', }, 'annotations' => { 'summary' => 'Instance {{ $labels.instance }} down', 'description' => '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.' } }]}]},
scrape_configs => [
Expand All @@ -198,7 +208,7 @@ class { '::prometheus':
],
alertmanagers_config => [{ 'static_configs' => [{'targets' => [ 'localhost:9093' ]}]}],
}
class { '::prometheus::alertmanager':
class { 'prometheus::alertmanager':
version => '0.13.0',
route => { 'group_by' => [ 'alertname', 'cluster', 'service' ], 'group_wait'=> '30s', 'group_interval'=> '5m', 'repeat_interval'=> '3h', 'receiver'=> 'slack' },
receivers => [ { 'name' => 'slack', 'slack_configs'=> [ { 'api_url'=> 'https://hooks.slack.com/services/ABCDEFG123456', 'channel' => '#channel', 'send_resolved' => true, 'username' => 'username'}] }]
Expand All @@ -207,7 +217,8 @@ class { '::prometheus::alertmanager':

the same in hiera

```puppet
```yaml
prometheus::manage_prometheus_server: true
prometheus::version: '2.0.0'
prometheus::scrape_configs:
- job_name: 'nodexporter'
Expand Down
1 change: 1 addition & 0 deletions data/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
prometheus::bin_dir: '/usr/local/bin'
prometheus::version: '1.5.2'
prometheus::install_method: 'url'
prometheus::manage_prometheus_server: false
prometheus::alert_relabel_config: []
prometheus::alertmanagers_config: []
prometheus::alertmanager::config_dir: '/etc/alertmanager'
Expand Down
32 changes: 16 additions & 16 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
Array $scrape_configs,
Array $remote_read_configs,
Array $remote_write_configs,
String $config_template = $prometheus::config_template,
String $storage_retention = $prometheus::storage_retention,
String $config_template = $prometheus::server::config_template,
String $storage_retention = $prometheus::server::storage_retention,
) {

if $prometheus::init_style {
Expand All @@ -21,8 +21,8 @@
fail('remote_write_configs requires prometheus 2.X')
}
$daemon_flags = [
"-config.file=${prometheus::config_dir}/prometheus.yaml",
"-storage.local.path=${prometheus::localstorage}",
"-config.file=${prometheus::server::config_dir}/prometheus.yaml",
"-storage.local.path=${prometheus::server::localstorage}",
"-storage.local.retention=${storage_retention}",
"-web.console.templates=${prometheus::shared_dir}/consoles",
"-web.console.libraries=${prometheus::shared_dir}/console_libraries",
Expand All @@ -31,11 +31,11 @@
# helper variable indicating prometheus version, so we can use on this information in the template
$prometheus_v2 = true
$daemon_flags = [
"--config.file=${prometheus::config_dir}/prometheus.yaml",
"--storage.tsdb.path=${prometheus::localstorage}",
"--config.file=${prometheus::server::config_dir}/prometheus.yaml",
"--storage.tsdb.path=${prometheus::server::localstorage}",
"--storage.tsdb.retention=${storage_retention}",
"--web.console.templates=${prometheus::shared_dir}/consoles",
"--web.console.libraries=${prometheus::shared_dir}/console_libraries",
"--web.console.templates=${prometheus::server::shared_dir}/consoles",
"--web.console.libraries=${prometheus::server::shared_dir}/console_libraries",
]
}

Expand All @@ -50,7 +50,7 @@
$systemd_notify = Exec['prometheus-systemd-reload']
}

case $prometheus::init_style {
case $prometheus::server::init_style {
'upstart' : {
file { '/etc/init/prometheus.conf':
mode => '0444',
Expand Down Expand Up @@ -105,26 +105,26 @@
}
}
default : {
fail("I don't know how to create an init script for style ${prometheus::init_style}")
fail("I don't know how to create an init script for style ${prometheus::server::init_style}")
}
}
}

if versioncmp($prometheus::version, '2.0.0') >= 0 {
if versioncmp($prometheus::server::version, '2.0.0') >= 0 {
$cfg_verify_cmd = 'check config'
} else {
$cfg_verify_cmd = 'check-config'
}

file { 'prometheus.yaml':
ensure => present,
path => "${prometheus::config_dir}/prometheus.yaml",
owner => $prometheus::user,
group => $prometheus::group,
mode => $prometheus::config_mode,
path => "${prometheus::server::config_dir}/prometheus.yaml",
owner => $prometheus::server::user,
group => $prometheus::server::group,
mode => $prometheus::server::config_mode,
notify => Class['prometheus::service_reload'],
content => template($config_template),
validate_cmd => "${prometheus::bin_dir}/promtool ${cfg_verify_cmd} %",
validate_cmd => "${prometheus::server::bin_dir}/promtool ${cfg_verify_cmd} %",
}

}
57 changes: 3 additions & 54 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
Array $alertmanagers_config,
String $storage_retention,
Stdlib::Absolutepath $env_file_path,
Boolean $manage_prometheus_server,
Hash $extra_alerts = {},
Boolean $service_enable = true,
String $service_ensure = 'running',
Expand All @@ -179,59 +180,7 @@
}
}

if( versioncmp($prometheus::version, '1.0.0') == -1 ){
$real_download_url = pick($download_url,
"${download_url_base}/download/${version}/${package_name}-${version}.${os}-${real_arch}.${download_extension}")
} else {
$real_download_url = pick($download_url,
"${download_url_base}/download/v${version}/${package_name}-${version}.${os}-${real_arch}.${download_extension}")
if $manage_prometheus_server {
include prometheus::server
}
$notify_service = $restart_on_change ? {
true => Service['prometheus'],
default => undef,
}

$config_hash_real = assert_type(Hash, deep_merge($config_defaults, $config_hash))

file { "${::prometheus::config_dir}/rules":
ensure => 'directory',
owner => $prometheus::user,
group => $prometheus::group,
mode => $prometheus::config_mode,
}

$extra_alerts.each | String $alerts_file_name, Hash $alerts_config | {
prometheus::alerts { $alerts_file_name:
alerts => $alerts_config,
}
}
$extra_rule_files = suffix(prefix(keys($extra_alerts), "${config_dir}/rules/"), '.rules')

if ! empty($alerts) {
prometheus::alerts { 'alert':
alerts => $alerts,
location => $config_dir,
}
$_rule_files = concat(["${config_dir}/alert.rules"], $extra_rule_files)
}
else {
$_rule_files = $extra_rule_files
}

anchor {'prometheus_first': }
-> class { 'prometheus::install':
purge_config_dir => $purge_config_dir,
}
-> class { 'prometheus::config':
global_config => $global_config,
rule_files => $_rule_files,
scrape_configs => $scrape_configs,
remote_read_configs => $remote_read_configs,
remote_write_configs => $remote_write_configs,
config_template => $config_template,
storage_retention => $storage_retention,
}
-> class { 'prometheus::run_service': }
-> class { 'prometheus::service_reload': }
-> anchor {'prometheus_last': }
}
81 changes: 42 additions & 39 deletions manifests/install.pp
Original file line number Diff line number Diff line change
@@ -1,91 +1,94 @@
# Class prometheus::install
# Class prometheus::server::install
# Install prometheus via different methods with parameters from init
# Currently only the install from url is implemented, when Prometheus will deliver packages for some Linux distros I will
# implement the package install method as well
# The package method needs specific yum or apt repo settings which are not made yet by the module
class prometheus::install (
Boolean $purge_config_dir = true,
) {
if $prometheus::localstorage {
file { $prometheus::localstorage:

assert_private()

if $prometheus::server::localstorage {
file { $prometheus::server::localstorage:
ensure => 'directory',
owner => $prometheus::user,
group => $prometheus::group,
owner => $prometheus::server::user,
group => $prometheus::server::group,
mode => '0755',
}
}
case $prometheus::install_method {
case $prometheus::server::install_method {
'url': {
archive { "/tmp/prometheus-${prometheus::version}.${prometheus::download_extension}":
archive { "/tmp/prometheus-${prometheus::server::version}.${prometheus::server::download_extension}":
ensure => present,
extract => true,
extract_path => '/opt',
source => $prometheus::real_download_url,
source => $prometheus::server::real_download_url,
checksum_verify => false,
creates => "/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/prometheus",
creates => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus",
cleanup => true,
}
-> file {
"/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/prometheus":
"/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus":
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555';
"${prometheus::bin_dir}/prometheus":
"${prometheus::server::bin_dir}/prometheus":
ensure => link,
notify => $prometheus::notify_service,
target => "/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/prometheus";
"${prometheus::bin_dir}/promtool":
notify => $prometheus::server::notify_service,
target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus";
"${prometheus::server::bin_dir}/promtool":
ensure => link,
target => "/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/promtool";
$prometheus::shared_dir:
target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/promtool";
$prometheus::server::shared_dir:
ensure => directory,
owner => $prometheus::user,
group => $prometheus::group,
owner => $prometheus::server::user,
group => $prometheus::server::group,
mode => '0755';
"${prometheus::shared_dir}/consoles":
"${prometheus::server::shared_dir}/consoles":
ensure => link,
notify => $prometheus::notify_service,
target => "/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/consoles";
"${prometheus::shared_dir}/console_libraries":
notify => $prometheus::server::notify_service,
target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/consoles";
"${prometheus::server::shared_dir}/console_libraries":
ensure => link,
notify => $prometheus::notify_service,
target => "/opt/prometheus-${prometheus::version}.${prometheus::os}-${prometheus::real_arch}/console_libraries";
notify => $prometheus::server::notify_service,
target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/console_libraries";
}
}
'package': {
package { $prometheus::package_name:
ensure => $prometheus::package_ensure,
package { $prometheus::server::package_name:
ensure => $prometheus::server::package_ensure,
}
if $prometheus::manage_user {
User[$prometheus::user] -> Package[$prometheus::package_name]
if $prometheus::server::manage_user {
User[$prometheus::server::user] -> Package[$prometheus::server::package_name]
}
}
'none': {}
default: {
fail("The provided install method ${prometheus::install_method} is invalid")
fail("The provided install method ${prometheus::server::install_method} is invalid")
}
}
if $prometheus::manage_user {
ensure_resource('user', [ $prometheus::user ], {
if $prometheus::server::manage_user {
ensure_resource('user', [ $prometheus::server::user ], {
ensure => 'present',
system => true,
groups => $prometheus::extra_groups,
groups => $prometheus::server::extra_groups,
})

if $prometheus::manage_group {
Group[$prometheus::group] -> User[$prometheus::user]
if $prometheus::server::manage_group {
Group[$prometheus::server::group] -> User[$prometheus::server::user]
}
}
if $prometheus::manage_group {
ensure_resource('group', [ $prometheus::group ],{
if $prometheus::server::manage_group {
ensure_resource('group', [ $prometheus::server::group ],{
ensure => 'present',
system => true,
})
}
file { $prometheus::config_dir:
file { $prometheus::server::config_dir:
ensure => 'directory',
owner => $prometheus::user,
group => $prometheus::group,
owner => $prometheus::server::user,
group => $prometheus::server::group,
purge => $purge_config_dir,
recurse => $purge_config_dir,
}
Expand Down
8 changes: 4 additions & 4 deletions manifests/run_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
#
class prometheus::run_service {

$init_selector = $prometheus::init_style ? {
$init_selector = $prometheus::server::init_style ? {
'launchd' => 'io.prometheus.daemon',
default => 'prometheus',
}

if $prometheus::manage_service == true {
if $prometheus::server::manage_service == true {
service { 'prometheus':
ensure => $prometheus::service_ensure,
ensure => $prometheus::server::service_ensure,
name => $init_selector,
enable => $prometheus::service_enable,
enable => $prometheus::server::service_enable,
hasrestart => true,
}
}
Expand Down
Loading