Puppet Plan: puppet_operational_dashboards::load_metrics

Defined in:
plans/load_metrics.pp

Summary

A plan created with bolt plan new.

Overview

The summary sets the description of the plan that will appear in ‘bolt plan show’ output. Bolt uses puppet-strings to parse the summary and parameters from the plan.

Parameters:

  • targets (TargetSpec)

    The targets to run on.

  • support_script_file (Optional[String]) (defaults to: undef)

    Path to a support script tarball

  • metrics_dir (Optional[String]) (defaults to: undef)

    Path to the ‘metrics’ directory from a PE support script

  • dest_dir (String) (defaults to: '/tmp')

    Directory to upload $metrics_dir to

  • cleanup_metrics (String) (defaults to: 'true')

    Whether to delete metrics after processing

  • influxdb_org (String) (defaults to: 'puppetlabs')

    Name of the InfluxDB organization to configure. Defaults to ‘puppetlabs’

  • influxdb_bucket (String) (defaults to: 'influxdb_puppet')

    Name of the InfluxDB bucket to configure and query. Defaults to ‘puppet_data’

  • influxdb_port (Integer) (defaults to: 8086)

    Port used by the InfluxDB service. Defaults to the value of influxdb::port, or 8086 if unset

  • grafana_datasource (String) (defaults to: $influxdb_bucket)

    Name of the Grafana datasource. Must match the name of the InfluxDB bucket

  • telegraf_token (String) (defaults to: 'puppet telegraf token')

    Name of the token to retrieve from InfluxDB. Defaults to ‘puppet telegraf token’

  • token_file (String) (defaults to: '/root/.influxdb_token')

    Location on disk of an InfluxDB admin token. This file is written to by the influxdb class during installation and read by the type and providers, as well Deferred functions in this module.

  • conf_dir (String) (defaults to: '/tmp/telegraf')

    Directory to upload Telegraf configuration files to

  • retention_rules (Array[Hash]) (defaults to: [{ 'type' => 'expire', 'everySeconds' => 3456000, 'shardGroupDurationSeconds' => 604800, }])
  • telegraf_process (Enum['local', 'remote']) (defaults to: 'remote')
  • token (Optional[String]) (defaults to: undef)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
# File 'plans/load_metrics.pp', line 33

plan puppet_operational_dashboards::load_metrics (
  TargetSpec $targets,
  Optional[String] $metrics_dir = undef,
  Optional[String] $support_script_file = undef,
  String $influxdb_org = 'puppetlabs',
  String $influxdb_bucket = 'influxdb_puppet',
  Integer $influxdb_port = 8086,
  String $grafana_datasource = $influxdb_bucket,
  String $telegraf_token = 'puppet telegraf token',
  String $token_file = '/root/.influxdb_token',
  String $conf_dir = '/tmp/telegraf',
  # 40 day default for bucket retention
  Array[Hash] $retention_rules = [{
      'type' => 'expire',
      'everySeconds' => 3456000,
      'shardGroupDurationSeconds' => 604800,
  }],
#TODO
  Enum['local', 'remote'] $telegraf_process = 'remote',
  String $dest_dir = '/tmp',
#TODO
  Optional[String] $token = undef,
  String $cleanup_metrics = 'true',
) {
  unless get_targets($targets).size == 1 {
    fail_plan('This plan only accepts a single target.')
  }

  unless $metrics_dir or $support_script_file {
    fail_plan('Must specify one of $metrics_dir or $support_script_file')
  }
  if $metrics_dir and $support_script_file {
    fail_plan('$metrics_dir and $support_script_file are mutually exclusive')
  }

  # Handle being passed a String or a Target
  $target = get_targets($targets)[0].name
  $target.apply_prep

  apply ($target) {
    $token_vars = {
      name     => $grafana_datasource,
      token    => Sensitive(Deferred('influxdb::retrieve_token', ["http://${target}:8086", $telegraf_token, $token_file])),
      database => $influxdb_bucket,
      url      => "http://${target}:8086",
    }

    influxdb_org { $influxdb_org:
      ensure  => present,
      use_ssl => false,
    }

    influxdb_bucket { $influxdb_bucket:
      ensure          => present,
      use_ssl         => false,
      org             => $influxdb_org,
      token_file      => $token_file,
      retention_rules => $retention_rules,
      require         => Influxdb_org[$influxdb_org],
    }

    service { 'grafana-server':
      ensure => running,
    }

    file { "/etc/grafana/provisioning/datasources/${influxdb_bucket}.yaml":
      ensure  => file,
      mode    => '0600',
      owner   => 'grafana',
      content => Deferred('inline_epp',
      [file('puppet_operational_dashboards/datasource.epp'), $token_vars]),
      notify  => Service['grafana-server'],
    }

    $telegraf_vars = {
      bucket => $influxdb_bucket,
      org => $influxdb_org,
      port => $influxdb_port,
      host => $target,
      token => Sensitive(Deferred('influxdb::retrieve_token', ["http://${target}:8086", $telegraf_token, $token_file])),
    }

    file { $conf_dir:
      ensure => directory,
    }
    file { "${conf_dir}/telegraf.conf":
      ensure  => file,
      content => Deferred('inline_epp', [file('puppet_operational_dashboards/telegraf.conf'), $telegraf_vars]),
    }
    file { "${conf_dir}/telegraf.conf.d":
      ensure => directory,
    }
    file { "${conf_dir}/sar2influx.rb":
      ensure => file,
      mode   => '0775',
      source => 'puppet:///modules/puppet_operational_dashboards/plan_files/sar2influx.rb',
    }

    # These are special because we don't want to load both and therefore don't write them to conf.d
    [
      'sar.conf',
      'system_sar.conf',
    ].each |$script| {
      file { "${conf_dir}/${script}":
        ensure => file,
        source => "puppet:///modules/puppet_operational_dashboards/plan_files/${script}",
      }
    }

    [
      'postgres.conf',
      'puppetdb.conf',
      'puppetserver.conf',
      'system_cpu.conf',
      'system_memory.conf',
      'system_procs.conf',
      'orchestrator.conf',
    ].each |$script| {
      file { "${conf_dir}/telegraf.conf.d/${script}":
        ensure => file,
        source => "puppet:///modules/puppet_operational_dashboards/plan_files/${script}",
      }
    }
  }

  if $support_script_file {
    $sup_script = split($support_script_file, '/')[-1]
    upload_file($support_script_file, $dest_dir, $target)

    return run_script(
      'puppet_operational_dashboards/plan_files/import_archives.sh',
      $target,
      'arguments' => ['-t', $conf_dir, '-s', "${dest_dir}/${sup_script}", '-c', $cleanup_metrics]
    )
  }
  else {
    upload_file($metrics_dir, $dest_dir, $targets)
    return run_script(
      'puppet_operational_dashboards/plan_files/import_archives.sh',
      $targets,
      'arguments' => ['-t', $conf_dir, '-m', $dest_dir, '-c', $cleanup_metrics]
    )
  }
}