Puppet Class: icinga2::feature::idomysql

Defined in:
manifests/feature/idomysql.pp

Overview

== Class: icinga2::feature::idomysql

This module configures the Icinga 2 feature ido-mysql.

=== Parameters

[*ensure*] Set to present enables the feature ido-mysql, absent disables it. Defaults to present.

[*host*] MySQL database host address. Defaults to 'localhost'.

[*port*] MySQL database port.

[*socket_path*] MySQL socket path.

[*user*] MySQL database user with read/write permission to the icinga database. Defaults to 'icinga'.

[*password*] MySQL database user's password.

[*database*] MySQL database name. Defaults to 'icinga'.

[*enable_ssl*] Either enable or disable SSL/TLS. Other SSL parameters are only affected if this is set to 'true'. Defaults to 'false'.

[*ssl_key_path*] Location of the private key. Only valid if ssl is enabled.

[*ssl_cert_path*] Location of the certificate. Only valid if ssl is enabled.

[*ssl_cacert_path*] Location of the CA certificate. Only valid if ssl is enabled.

[*ssl_key*] The private key in a base64 encoded string to store in spicified ssl_key_path file. Default depends on platform: /var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql.key on Linux C:/ProgramData/icinga2/var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql.key on Windows Only valid if ssl is enabled.

[*ssl_cert*] The certificate in a base64 encoded string to store in spicified ssl_cert_path file. Default depends on platform: /var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql.crt on Linux C:/ProgramData/icinga2/var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql.crt on Windows Only valid if ssl is enabled.

[*ssl_cacert*] The CA root certificate in a base64 encoded string to store in spicified ssl_cacert_path file. Default depends on platform: /var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql_ca.crt on Linux C:/ProgramData/icinga2/var/lib/icinga2/certs/IdoMysqlConnection_ido-mysql_ca.crt on Windows Only valid if ssl is enabled.

[*ssl_capath*] MySQL SSL trusted SSL CA certificates in PEM format directory path. Only valid if ssl is enabled.

[*ssl_cipher*] MySQL SSL list of allowed ciphers. Only valid if ssl is enabled.

[*table_prefix*] MySQL database table prefix. Icinga defaults to 'icinga_'.

[*instance_name*] Unique identifier for the local Icinga 2 instance. Icinga defaults to 'default'.

[*instance_description*] Description for the Icinga 2 instance.

[*enable_ha*] Enable the high availability functionality. Only valid in a cluster setup. Icinga defaults to 'true'.

[*failover_timeout*] Set the failover timeout in a HA cluster. Must not be lower than 60s. Icinga defaults to '60s'.

[*cleanup*] Hash with items for historical table cleanup.

[*categories*] Array of information types that should be written to the database.

[*import_schema*] Whether to import the MySQL schema or not. Defaults to 'false'.

=== Examples

The ido-mysql featue requires an existing database and a user with permissions. To install a database server, create databases and manage user permissions we recommend the puppetlabs/mysql module. Here's an example how you create a MySQL database with the corresponding user with permissions by usng the puppetlabs/mysql module:

include icinga2 include mysql::server

mysql::db { 'icinga2': user => 'icinga2', password => 'supersecret', host => 'localhost', grant => ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'DROP', 'CREATE VIEW', 'CREATE', 'INDEX', 'EXECUTE', 'ALTER'], }

class{ 'icinga2::feature::idomysql': user => "icinga2", password => "supersecret", database => "icinga2", import_schema => true, require => Mysql::Db['icinga2'] }

Parameters:

  • password (String)
  • ensure (Enum['absent', 'present']) (defaults to: present)
  • host (Stdlib::Host) (defaults to: 'localhost')
  • port (Optional[Stdlib::Port::Unprivileged]) (defaults to: undef)
  • socket_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • user (String) (defaults to: 'icinga')
  • database (String) (defaults to: 'icinga')
  • enable_ssl (Boolean) (defaults to: false)
  • ssl_key_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • ssl_cert_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • ssl_cacert_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • ssl_key (Optional[String]) (defaults to: undef)
  • ssl_cert (Optional[String]) (defaults to: undef)
  • ssl_cacert (Optional[String]) (defaults to: undef)
  • ssl_capath (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • ssl_cipher (Optional[String]) (defaults to: undef)
  • table_prefix (Optional[String]) (defaults to: undef)
  • instance_name (Optional[String]) (defaults to: undef)
  • instance_description (Optional[String]) (defaults to: undef)
  • enable_ha (Optional[Boolean]) (defaults to: undef)
  • failover_timeout (Optional[Icinga2::Interval]) (defaults to: undef)
  • cleanup (Optional[Hash[String,Icinga2::Interval]]) (defaults to: undef)
  • categories (Optional[Array]) (defaults to: undef)
  • import_schema (Boolean) (defaults to: false)


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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'manifests/feature/idomysql.pp', line 118

class icinga2::feature::idomysql(
  String                                      $password,
  Enum['absent', 'present']                   $ensure                 = present,
  Stdlib::Host                                $host                   = 'localhost',
  Optional[Stdlib::Port::Unprivileged]        $port                   = undef,
  Optional[Stdlib::Absolutepath]              $socket_path            = undef,
  String                                      $user                   = 'icinga',
  String                                      $database               = 'icinga',
  Boolean                                     $enable_ssl             = false,
  Optional[Stdlib::Absolutepath]              $ssl_key_path           = undef,
  Optional[Stdlib::Absolutepath]              $ssl_cert_path          = undef,
  Optional[Stdlib::Absolutepath]              $ssl_cacert_path        = undef,
  Optional[String]                            $ssl_key                = undef,
  Optional[String]                            $ssl_cert               = undef,
  Optional[String]                            $ssl_cacert             = undef,
  Optional[Stdlib::Absolutepath]              $ssl_capath             = undef,
  Optional[String]                            $ssl_cipher             = undef,
  Optional[String]                            $table_prefix           = undef,
  Optional[String]                            $instance_name          = undef,
  Optional[String]                            $instance_description   = undef,
  Optional[Boolean]                           $enable_ha              = undef,
  Optional[Icinga2::Interval]                 $failover_timeout       = undef,
  Optional[Hash[String,Icinga2::Interval]]    $cleanup                = undef,
  Optional[Array]                             $categories             = undef,
  Boolean                                     $import_schema          = false,
) {

  if ! defined(Class['::icinga2']) {
    fail('You must include the icinga2 base class before using any icinga2 feature class!')
  }

  $owner                  = $::icinga2::globals::user
  $group                  = $::icinga2::globals::group
  $conf_dir               = $::icinga2::globals::conf_dir
  $ssl_dir                = $::icinga2::globals::cert_dir
  $ido_mysql_package_name = $::icinga2::globals::ido_mysql_package_name
  $ido_mysql_schema       = $::icinga2::globals::ido_mysql_schema
  $manage_package         = $::icinga2::manage_package

  $_ssl_key_mode          = $::osfamily ? {
    'windows' => undef,
    default   => '0600',
  }

  $_notify           = $ensure ? {
    'present' => Class['::icinga2::service'],
    default   => undef,
  }

  # to build mysql exec command to import schema
  if $import_schema {
    $_mysql_options = join(any2array(delete_undef_values({
      '-h' => $host ? {
        /localhost/ => undef,
        default     => $host,
      },
      '-P' => $port,
      '-u' => $user,
    })), ' ')
  }

  File {
    owner   => $owner,
    group   => $group,
  }


  if $enable_ssl {
    # Set defaults for certificate stuff
    if $ssl_key {
      if $ssl_key_path {
        $_ssl_key_path = $ssl_key_path }
      else {
        $_ssl_key_path = "${ssl_dir}/IdoMysqlConnection_ido-mysql.key"
      }

      $_ssl_key = $::osfamily ? {
        'windows' => regsubst($ssl_key, '\n', "\r\n", 'EMG'),
        default   => $ssl_key,
      }

      file { $_ssl_key_path:
        ensure  => file,
        mode    => $_ssl_key_mode,
        content => $ssl_key,
        tag     => 'icinga2::config::file',
      }
    } else {
      $_ssl_key_path = $ssl_key_path
    }

    if $ssl_cert {
      if $ssl_cert_path {
        $_ssl_cert_path = $ssl_cert_path }
      else {
        $_ssl_cert_path = "${ssl_dir}/IdoMysqlConnection_ido-mysql.crt"
      }

      $_ssl_cert = $::osfamily ? {
        'windows' => regsubst($ssl_cert, '\n', "\r\n", 'EMG'),
        default   => $ssl_cert,
      }

      file { $_ssl_cert_path:
        ensure  => file,
        content => $ssl_cert,
        tag     => 'icinga2::config::file',
      }
    } else {
      $_ssl_cert_path = $ssl_cert_path
    }

    if $ssl_cacert {
      if $ssl_cacert_path {
        $_ssl_cacert_path = $ssl_cacert_path }
      else {
        $_ssl_cacert_path = "${ssl_dir}/IdoMysqlConnection_ido-mysql_ca.crt"
      }

      $_ssl_cacert = $::osfamily ? {
        'windows' => regsubst($ssl_cacert, '\n', "\r\n", 'EMG'),
        default   => $ssl_cacert,
      }

      file { $_ssl_cacert_path:
        ensure  => file,
        content => $ssl_cacert,
        tag     => 'icinga2::config::file',
      }
    } else {
      $_ssl_cacert_path = $ssl_cacert_path
    }

    if $import_schema {
      $_ssl_options = join(any2array(delete_undef_values({
        '--ssl-ca'     => $_ssl_cacert_path,
        '--ssl-cert'   => $_ssl_cert_path,
        '--ssl-key'    => $_ssl_key_path,
        '--ssl-capath' => $ssl_capath,
        '--ssl-cipher' => $ssl_cipher,
      })), ' ')

      # set cli options for mysql connection via tls
      $_mysql_command = "mysql ${_mysql_options} -p'${password}' ${_ssl_options} ${database}"
    }

    $attrs_ssl = {
      enable_ssl => $enable_ssl,
      ssl_ca     => $_ssl_cacert_path,
      ssl_cert   => $_ssl_cert_path,
      ssl_key    => $_ssl_key_path,
      ssl_capath => $ssl_capath,
      ssl_cipher => $ssl_cipher,
    }
  } # enable_ssl
  else {
    # set cli options for mysql connection
    if $import_schema {
      $_mysql_command = "mysql ${_mysql_options} -p'${password}' ${database}" }

    $attrs_ssl = { enable_ssl  => $enable_ssl }
  }

  $attrs = {
    host                  => $host,
    port                  => $port,
    socket_path           => $socket_path,
    user                  => $user,
    password              => $password,
    database              => $database,
    table_prefix          => $table_prefix,
    instance_name         => $instance_name,
    instance_description  => $instance_description,
    enable_ha             => $enable_ha,
    failover_timeout      => $failover_timeout,
    cleanup               => $cleanup,
    categories            => $categories,
  }

  # install additional package
  if $ido_mysql_package_name and $manage_package {
    if $::osfamily == 'debian' {
      ensure_resources('file', { '/etc/dbconfig-common' => { ensure => directory } })
      file { "/etc/dbconfig-common/${ido_mysql_package_name}.conf":
        ensure  => file,
        content => "dbc_install='false'\ndbc_upgrade='false'\ndbc_remove='false'\n",
        mode    => '0600',
        before  => Package[$ido_mysql_package_name],
      }
    } # Debian

    package { $ido_mysql_package_name:
      ensure => installed,
      before => Icinga2::Feature['ido-mysql'],
    }
  }

  # import db schema
  if $import_schema {
    if $ido_mysql_package_name and $manage_package {
      Package[$ido_mysql_package_name] -> Exec['idomysql-import-schema']
    }
    exec { 'idomysql-import-schema':
      user    => 'root',
      path    => $::path,
      command => "${_mysql_command} < \"${ido_mysql_schema}\"",
      unless  => "${_mysql_command} -Ns -e 'select version from icinga_dbversion'",
    }
  }

  # create object
  icinga2::object { 'icinga2::object::IdoMysqlConnection::ido-mysql':
    object_name => 'ido-mysql',
    object_type => 'IdoMysqlConnection',
    attrs       => delete_undef_values(merge($attrs, $attrs_ssl)),
    attrs_list  => concat(keys($attrs), keys($attrs_ssl)),
    target      => "${conf_dir}/features-available/ido-mysql.conf",
    order       => 10,
    notify      => $_notify,
  }

  # import library
  concat::fragment { 'icinga2::feature::ido-mysql':
    target  => "${conf_dir}/features-available/ido-mysql.conf",
    content => "library \"db_ido_mysql\"\n\n",
    order   => '05',
  }

  icinga2::feature { 'ido-mysql':
    ensure  => $ensure,
  }
}