Puppet Class: sensu::agent
- Defined in:
- manifests/agent.pp
Summary
Manage Sensu agentOverview
Class to manage the Sensu agent.
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 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 |
# File 'manifests/agent.pp', line 81
class sensu::agent (
Optional[String] $version = undef,
Optional[String[1]] $package_source = undef,
Optional[Stdlib::Absolutepath] $package_download_path = undef,
String $package_name = 'sensu-go-agent',
Optional[Stdlib::Absolutepath] $service_env_vars_file = undef,
Hash $service_env_vars = {},
String $service_name = 'sensu-agent',
String $service_ensure = 'running',
Boolean $service_enable = true,
Stdlib::Absolutepath $service_path = '/usr/sbin/sensu-agent',
Hash $config_hash = {},
Boolean $agent_managed_entity = false,
Optional[Array[Sensu::Backend_URL]] $backends = undef,
String[1] $entity_name = $facts['networking']['fqdn'],
Optional[Array[String[1]]] $subscriptions = undef,
Optional[Hash[String[1],String]] $annotations = undef,
Optional[Hash[String[1],String]] $labels = undef,
String[1] $namespace = 'default',
Array[String[1]] $redact = ['password','passwd','pass','api_key','api_token','access_key','secret_key','private_key','secret'],
Boolean $show_diff = true,
Optional[Stdlib::Absolutepath] $log_file = undef,
Enum['sensuctl','sensu_api'] $agent_entity_config_provider = 'sensu_api',
Boolean $validate_entity = true,
) {
include sensu
include sensu::common
include sensu::api
if $agent_entity_config_provider == 'sensuctl' {
warning('Class[sensu::agent]: Using agent_entity_config_provider is deprecated at this time, forcing use of sensu_api provider')
}
$_version = pick($version, $sensu::version)
$_backends = pick($backends, ["${sensu::api_host}:8081"])
if $sensu::use_ssl {
$backend_protocol = 'wss'
$ssl_config = {
'trusted-ca-file' => $sensu::trusted_ca_file_path,
}
$service_subscribe = Class['sensu::ssl']
} else {
$backend_protocol = 'ws'
$ssl_config = {}
$service_subscribe = undef
}
$backend_urls = $_backends.map |$backend| {
if 'ws://' in $backend or 'wss://' in $backend {
$backend
} else {
"${backend_protocol}://${backend}"
}
}
$default_config = {
'backend-url' => $backend_urls,
'name' => $entity_name,
'agent-managed-entity' => $agent_managed_entity,
'subscriptions' => $subscriptions,
'annotations' => $annotations,
'labels' => $labels,
'namespace' => $namespace,
'redact' => $redact,
'password' => $sensu::agent_password,
}
$config = filter($default_config + $ssl_config + $config_hash) |$key, $value| { $value =~ NotUndef }
if $config['subscriptions'] {
$config['subscriptions'].each |$s| {
sensu::agent::subscription { $s: }
}
}
if $config['labels'] {
$config['labels'].each |$key, $value| {
sensu::agent::label { $key:
value => $value,
}
}
}
if $config['annotations'] {
$config['annotations'].each |$key, $value| {
sensu::agent::annotation { $key:
value => $value,
}
}
}
sensu_agent_entity_setup { 'puppet':
url => $sensu::api_url,
username => 'puppet-agent_entity_config',
password => $sensu::_agent_entity_config_password,
agent_managed_entity => $config['agent-managed-entity'],
}
$_service_env_vars = $service_env_vars.map |$key,$value| {
"${key}=\"${value}\""
}
$_service_env_vars_lines = ['# This file is being maintained by Puppet.','# DO NOT EDIT'] + $_service_env_vars
if $facts['os']['family'] == 'windows' {
$sensu_agent_exe = "C:\\Program Files\\sensu\\sensu-agent\\bin\\sensu-agent.exe"
exec { 'install-agent-service':
command => "C:\\windows\\system32\\cmd.exe /c \"\"${sensu_agent_exe}\" service install --config-file \"${sensu::agent_config_path}\" --log-file \"${log_file}\"\"", # lint:ignore:140chars
unless => "C:\\windows\\system32\\sc.exe query SensuAgent",
before => Service['sensu-agent'],
require => [
Package['sensu-go-agent'],
File['sensu_agent_config'],
],
}
if $package_source and ($package_source =~ Stdlib::HTTPSUrl or $package_source =~ Stdlib::HTTPUrl) {
$package_provider = undef
$package_source_basename = basename($package_source)
$_package_source = "${package_download_path}\\${package_source_basename}"
archive { 'sensu-go-agent.msi':
source => $package_source,
path => $_package_source,
extract => false,
cleanup => false,
before => Package['sensu-go-agent'],
}
} elsif $package_source and $package_source =~ /^puppet:/ {
$package_provider = undef
$package_source_basename = basename($package_source)
$_package_source = "${package_download_path}\\${package_source_basename}"
file { 'sensu-go-agent.msi':
ensure => 'file',
path => $_package_source,
source => $package_source,
before => Package['sensu-go-agent'],
}
} elsif $package_source {
$package_provider = undef
$_package_source = $package_source
} else {
include chocolatey
$package_provider = 'chocolatey'
$_package_source = $package_source
}
} else {
$package_provider = undef
$_package_source = undef
}
package { 'sensu-go-agent':
ensure => $_version,
name => $package_name,
source => $_package_source,
provider => $package_provider,
before => File['sensu_etc_dir'],
require => $sensu::package_require,
notify => Service['sensu-agent'],
}
datacat_collector { 'sensu_agent_config':
template => 'sensu/agent.yml.erb',
template_body => template_body('sensu/agent.yml.erb'),
target_resource => File['sensu_agent_config'],
target_field => 'content',
}
datacat_fragment { 'sensu_agent_config-main':
target => 'sensu_agent_config',
data => $config,
order => '01',
}
file { 'sensu_agent_config':
ensure => 'file',
path => $sensu::agent_config_path,
owner => $sensu::sensu_user,
group => $sensu::sensu_group,
mode => $sensu::file_mode,
show_diff => $show_diff,
require => Package['sensu-go-agent'],
notify => Service['sensu-agent'],
}
if $service_env_vars_file {
$_service_env_vars_content = join($_service_env_vars_lines, "\n")
file { 'sensu-agent_env_vars':
ensure => 'file',
path => $service_env_vars_file,
content => "${_service_env_vars_content}\n",
owner => $sensu::sensu_user,
group => $sensu::sensu_group,
mode => $sensu::file_mode,
show_diff => $show_diff,
require => Package['sensu-go-agent'],
notify => Service['sensu-agent'],
}
}
# No built in way to read environment variables from a file for Windows
if $facts['os']['family'] == 'windows' {
$service_env_vars.each |$key,$value| {
windows_env { $key:
ensure => 'present',
value => $value,
mergemode => 'clobber',
notify => Service['sensu-agent'],
}
}
}
if $facts['service_provider'] == 'systemd' {
systemd::dropin_file { 'sensu-agent-start.conf':
unit => 'sensu-agent.service',
content => join([
'[Service]',
'ExecStart=',
"ExecStart=${service_path} start -c ${sensu::agent_config_path}",
], "\n"),
notify => Service['sensu-agent'],
}
}
service { 'sensu-agent':
ensure => $service_ensure,
enable => $service_enable,
name => $service_name,
subscribe => $service_subscribe,
}
if $validate_entity {
sensu_agent_entity_validator { $config['name']:
ensure => 'present',
namespace => $config['namespace'],
provider => 'sensu_api',
}
}
}
|