Puppet Class: prometheus::cgroup_exporter

Inherits:
prometheus
Defined in:
manifests/cgroup_exporter.pp

Summary

This module manages prometheus cgroup_exporter (https://github.com/treydock/cgroup_exporter)

Overview

Parameters:

  • arch (String[1]) (defaults to: $prometheus::real_arch)

    Architecture (x86_64)

  • bin_dir (Stdlib::Absolutepath) (defaults to: $prometheus::bin_dir)

    Directory where binaries are located

  • download_extension (String) (defaults to: 'tar.gz')

    Extension for the release binary archive

  • download_url (Optional[Prometheus::Uri]) (defaults to: undef)

    Complete URL corresponding to the where the release binary archive can be downloaded

  • download_url_base (Prometheus::Uri) (defaults to: 'https://github.com/treydock/cgroup_exporter/releases')

    Base URL for the binary archive

  • extra_groups (Array[String]) (defaults to: [])

    Extra groups to add the binary user to

  • extra_options (Optional[String[1]]) (defaults to: undef)

    Extra options added to the startup command

  • group (String[1]) (defaults to: 'cgroup-exporter')

    Group under which the binary is running

  • init_style (Prometheus::Initstyle) (defaults to: $prometheus::init_style)

    Service startup scripts style (e.g. rc or systemd)

  • install_method (Prometheus::Install) (defaults to: $prometheus::install_method)

    Installation method: url or package (only url is supported currently)

  • manage_group (Boolean) (defaults to: true)

    Whether to create a group for or rely on external code for that

  • manage_service (Boolean) (defaults to: true)

    Should puppet manage the service? (default true)

  • manage_user (Boolean) (defaults to: true)

    Whether to create user or rely on external code for that

  • modules (Hash) (defaults to: {})

    Structured, array of blackbox module definitions for different probe types

  • export_scrape_job (Boolean) (defaults to: false)

    Whether to export a scrape job for this service

  • scrape_host (Optional[Stdlib::Host]) (defaults to: undef)

    Hostname or IP address to scrape

  • scrape_port (Stdlib::Port) (defaults to: 9306)

    Host port to scrape

  • scrape_job_name (String[1]) (defaults to: 'cgroup')

    Name of the scrape job to export, if export_scrape_job is true

  • scrape_job_labels (Optional[Hash]) (defaults to: undef)

    Labels to add to the scrape job, if export_scrape_job is true

  • os (String[1]) (defaults to: downcase(fact('kernel')))

    Operating system (linux is the only one supported)

  • package_ensure (String[1]) (defaults to: 'latest')

    If package, then use this for package ensure default ‘latest’

  • package_name (String[1]) (defaults to: 'cgroup_exporter')

    The binary package name - not available yet

  • purge_config_dir (Boolean) (defaults to: true)

    Purge config files no longer generated by Puppet

  • restart_on_change (Boolean) (defaults to: true)

    Should puppet restart the service on configuration change? (default true)

  • service_enable (Boolean) (defaults to: true)

    Whether to enable the service from puppet (default true)

  • service_ensure (Stdlib::Ensure::Service) (defaults to: 'running')

    State ensured for the service (default ‘running’)

  • service_name (String[1]) (defaults to: 'cgroup_exporter')

    Name of the node exporter service (default ‘cgroup_exporter’)

  • user (String[1]) (defaults to: 'cgroup-exporter')

    User which runs the service

  • version (String[1]) (defaults to: '1.0.1')

    The binary release version

  • proxy_server (Optional[String[1]]) (defaults to: undef)

    Optional proxy server, with port number if needed. ie: example.com:8080

  • proxy_type (Optional[Enum['none', 'http', 'https', 'ftp']]) (defaults to: undef)

    Optional proxy server type (none|http|https|ftp)

  • cgroup_paths (Enum['slurm', 'user.slice']) (defaults to: 'slurm')

    cgroup paths (slurm|user.slice)

  • unprivileged (Boolean) (defaults to: false)

    If true, run the exporter as an unprivileged user and add sudoers entrie to manage the binary exporter

  • archive_bin_path (Stdlib::Absolutepath) (defaults to: "/opt/${package_name}-${version}.${os}-${arch}/${package_name}")

    Path to the binary in the downloaded archive.

  • env_file_path (Stdlib::Absolutepath) (defaults to: $prometheus::env_file_path)

    The path to the file with the environmetn variable that is read from the init script/systemd unit



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'manifests/cgroup_exporter.pp', line 72

class prometheus::cgroup_exporter (
  String[1]                                      $package_name       = 'cgroup_exporter',
  String                                         $download_extension = 'tar.gz',
  # renovate: depName=treydock/cgroup_exporter
  String[1]                                      $version            = '1.0.1',
  String[1]                                      $package_ensure     = 'latest',
  String[1]                                      $user               = 'cgroup-exporter',
  String[1]                                      $group              = 'cgroup-exporter',
  Prometheus::Uri                                $download_url_base  = 'https://github.com/treydock/cgroup_exporter/releases',
  Array[String]                                  $extra_groups       = [],
  Prometheus::Initstyle                          $init_style         = $prometheus::init_style,
  Boolean                                        $purge_config_dir   = true,
  String[1]                                      $arch               = $prometheus::real_arch,
  Stdlib::Absolutepath                           $bin_dir            = $prometheus::bin_dir,
  Boolean                                        $restart_on_change  = true,
  Boolean                                        $service_enable     = true,
  Stdlib::Ensure::Service                        $service_ensure     = 'running',
  String[1]                                      $service_name       = 'cgroup_exporter',
  Prometheus::Install                            $install_method     = $prometheus::install_method,
  Boolean                                        $manage_group       = true,
  Boolean                                        $manage_service     = true,
  Boolean                                        $manage_user        = true,
  String[1]                                      $os                 = downcase(fact('kernel')),
  Stdlib::Absolutepath                           $archive_bin_path   = "/opt/${package_name}-${version}.${os}-${arch}/${package_name}",
  Optional[String[1]]                            $extra_options      = undef,
  Optional[Prometheus::Uri]                      $download_url       = undef,
  Optional[Stdlib::Host]                         $scrape_host        = undef,
  Stdlib::Absolutepath                           $env_file_path      = $prometheus::env_file_path,
  Boolean                                        $export_scrape_job  = false,
  Stdlib::Port                                   $scrape_port        = 9306,
  String[1]                                      $scrape_job_name    = 'cgroup',
  Optional[Hash]                                 $scrape_job_labels  = undef,
  Hash                                           $modules            = {},
  Optional[String[1]]                            $proxy_server       = undef,
  Boolean                                        $unprivileged       = false,
  Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type         = undef,
  Enum['slurm', 'user.slice']                    $cgroup_paths       = 'slurm',
) inherits prometheus {
  $real_download_url = pick(
    $download_url,
    "${download_url_base}/download/v${version}/${package_name}-${version}.${os}-${arch}.${download_extension}"
  )

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

  $paths_option = "--config.paths=/${cgroup_paths}"

  $listen_address = $scrape_port ? {
    9306    => undef,
    default => "--web.listen-address=':${scrape_port}'",
  }

  case fact('os.name') {
    'Debian':  { $unprivileged_os = versioncmp ("${fact('os.release.full')}", '12') == -1 }
    'Ubuntu':  { $unprivileged_os = versioncmp ("${fact('os.release.full')}", '22.04') == -1 }
    default:  { $unprivileged_os = false }
  }

  $bin_name = $package_name

  $options = [
    $extra_options,
    $listen_address,
    $paths_option,
  ].filter |$x| { !$x.empty }.join(' ')

  if $unprivileged or $unprivileged_os {
    # workaround from the ipmi_exporter class
    sudo::conf { $service_name:
      ensure         => 'present',
      content        => join([
        "${user} ALL = NOPASSWD: ${archive_bin_path}",
      ], "\n"),
      sudo_file_name => $service_name,
    }

    file { "${bin_dir}/${bin_name}":
      owner   => $user,
      group   => $group,
      mode    => '0750',
      content => join([
        '#!/bin/bash',
        "sudo ${archive_bin_path} \"$@\"",
      ], "\n"),
    }

    # necessary to ensure that all processes are killed properly with Killmode=mixed
    # if not we encounter those error "cgroup_exporter.service: Unit process XXXXX (sudo) remains running after unit stopped."
    include 'systemd'
    systemd::manage_unit { "${service_name}.service":
      unit_entry    => {
        'Description' => "Prometheus ${package_name}",
        'Wants'       => 'network-online.target',
        'After'       => 'network-online.target',
      },
      service_entry => {
        'User'            => $user,
        'Group'           => $group,
        'EnvironmentFile' => "-${env_file_path}/${package_name}",
        'ExecStart'       => sprintf('%s/%s %s', $bin_dir, $bin_name, $options),
        'ExecReload'      => '/bin/kill -HUP $MAINPID',
        'KillMode'        => 'mixed',
        'Restart'         => 'always',
      },
      install_entry => {
        'WantedBy' => 'multi-user.target',
      },
      notify        => $notify_service,
    }
    $init_style_real = 'none'
  } else {
    exec { 'setcap cgroup_exporter':
      command => "/usr/sbin/setcap cap_sys_ptrace=eip ${archive_bin_path}",
      unless  => "/usr/sbin/getcap ${archive_bin_path} | grep -q cap_sys_ptrace=eip",
      require => Prometheus::Daemon[$service_name],
    }
    $init_style_real = $init_style
  }

  prometheus::daemon { $service_name:
    install_method     => $install_method,
    version            => $version,
    download_extension => $download_extension,
    os                 => $os,
    arch               => $arch,
    real_download_url  => $real_download_url,
    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,
    group              => $group,
    manage_group       => $manage_group,
    purge              => $purge_config_dir,
    options            => $options,
    init_style         => $init_style_real,
    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,
    bin_name           => $bin_name,
    proxy_server       => $proxy_server,
    proxy_type         => $proxy_type,
    archive_bin_path   => $archive_bin_path,
    manage_bin_link    => !($unprivileged or $unprivileged_os),
  }
}