Module: PEStatusCheck

Defined in:
lib/shared/pe_status_check.rb

Overview

PEStatusCheck - Shared code for pe_status_check facts

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.facter_timeoutObject

Returns the value of attribute facter_timeout.



9
10
11
# File 'lib/shared/pe_status_check.rb', line 9

def facter_timeout
  @facter_timeout
end

.infra_profilesObject

Returns the value of attribute infra_profiles.



9
10
11
# File 'lib/shared/pe_status_check.rb', line 9

def infra_profiles
  @infra_profiles
end

.pup_pathsObject

Returns the value of attribute pup_paths.



9
10
11
# File 'lib/shared/pe_status_check.rb', line 9

def pup_paths
  @pup_paths
end

Class Method Details

.ca_bootstrap?Boolean

checks puppetlabs.services.ca.certificate-authority-service/certificate-authority-service exists in puppetserver bootstrap

Returns:

  • (Boolean)


40
41
42
# File 'lib/shared/pe_status_check.rb', line 40

def ca_bootstrap?
  return true if File.exist?('/etc/puppetlabs/puppetserver/bootstrap.cfg') && File.foreach('/etc/puppetlabs/puppetserver/bootstrap.cfg').grep(%r{certificate-authority-service}).any?
end

.cur_connectionsObject



149
150
151
152
153
154
155
# File 'lib/shared/pe_status_check.rb', line 149

def cur_connections
  sql = %(
  select count(*) used from pg_stat_activity;
)
  psql_options = '-qtAX'
  psql_return_result(sql, psql_options)
end

.enabled?Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
# File 'lib/shared/pe_status_check.rb', line 171

def enabled?
  enabled_file = '/opt/puppetlabs/puppet/cache/state/status_check_enable'
  if Facter.value('os')['name'] == 'windows'
    enabled_file = File.join(Facter.value('common_appdata'),
                             'PuppetLabs/puppet/cache/state/status_check_enable')
  end
  File.exist?(enabled_file)
end

.filesystem_free(path) ⇒ Integer

Get the free disk percentage from a path

Parameters:

  • path (String)

    The path on the file system

Returns:

  • (Integer)

    The percentage of free disk space on the mount



160
161
162
163
164
165
166
167
168
169
# File 'lib/shared/pe_status_check.rb', line 160

def filesystem_free(path)
  require 'sys/filesystem'

  stat = Sys::Filesystem.stat(path)
  (stat.blocks_available.to_f / stat.blocks.to_f * 100).to_i
rescue LoadError => e
  Facter.warn("Error in fact 'pe_status_check': #{e.message}")
  Facter.debug(e.backtrace)
  0
end

.get_resource(resource, name) ⇒ Puppet::Resource

Gets the resource object by name

Parameters:

  • resource (String)

    The resource type to get

  • name (String)

    The name of the resource

Returns:

  • (Puppet::Resource)

    The instance of the resource or nil



30
31
32
33
34
35
36
37
# File 'lib/shared/pe_status_check.rb', line 30

def get_resource(resource, name)
  name += '.service' if (resource == 'service') && !name.include?('.')
  Puppet::Indirector::Indirection.instance(:resource).find("#{resource}/#{name}")
rescue ScriptError, StandardError => e
  Facter.debug("Error when finding resource #{resource}: #{e.message}")
  Facter.debug(e.backtrace)
  nil
end

.http_get(path, port, host = ) ⇒ Hash

Module method to make a GET request to an api specified by path and port params

Parameters:

  • path (String)

    The API path to query. Should include a ‘/’ prefix and query parameters

  • port (Integer)

    The port to use

  • host (String) (defaults to: )

    The FQDN to use in making the connection. Defaults to the Puppet certname

Returns:

  • (Hash)

    Response body of the API call



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
# File 'lib/shared/pe_status_check.rb', line 104

def http_get(path, port, host = Puppet[:certname])
  # Use an instance variable to only create an SSLContext once
  @ssl_context ||= Puppet::SSL::SSLContext.new(
    cacerts: Puppet[:localcacert],
    private_key: OpenSSL::PKey::RSA.new(File.read(Puppet[:hostprivkey])),
    client_cert: OpenSSL::X509::Certificate.new(File.open(Puppet[:hostcert])),
  )

  client = Net::HTTP.new(host, port)
  # The main reason to use this approach is to set open and read timeouts to a small value
  # Puppet's HTTP client does not allow access to these
  client.open_timeout = 2
  client.read_timeout = 2
  client.use_ssl = true
  client.verify_mode = OpenSSL::SSL::VERIFY_PEER
  client.cert = @ssl_context.client_cert
  client.key = @ssl_context.private_key
  client.ca_file = @ssl_context.cacerts

  response = client.request_get(Puppet::Util.uri_encode(path))
  if response.is_a? Net::HTTPSuccess
    JSON.parse(response.body)
  else
    false
  end
rescue StandardError => e
  Facter.debug("Error in fact 'pe_status_check' when querying #{path}: #{e.message}")
  Facter.debug(e.backtrace)
  false
end

.max_connectionsObject



141
142
143
144
145
146
147
# File 'lib/shared/pe_status_check.rb', line 141

def max_connections
  sql = %(
  SELECT current_setting('max_connections');
)
  psql_options = '-qtAX'
  psql_return_result(sql, psql_options)
end

.pe_postgres_service_nameString

Return the name of the pe-postgresql service for the current OS

Returns:

  • (String)

    The name of the pe-postgresql service



79
80
81
82
83
84
85
# File 'lib/shared/pe_status_check.rb', line 79

def pe_postgres_service_name
  if Facter.value(:os)['family'].eql?('Debian')
    "pe-postgresql#{Facter.value(:pe_postgresql_info)['installed_server_version']}"
  else
    'pe-postgresql'
  end
end

.psql_return_result(sql, psql_options = '') ⇒ Object

Get the maximum defined and current connections to Postgres



136
137
138
139
# File 'lib/shared/pe_status_check.rb', line 136

def psql_return_result(sql, psql_options = '')
  command = %(su pe-postgres --shell /bin/bash --command "cd /tmp && #{pup_paths[:server_bin]}/psql #{psql_options} --command \\"#{sql}\\"")
  Facter::Core::Execution.execute(command, { timeout: facter_timeout })
end

.service_enabled(name, service = nil) ⇒ Boolean

Check if the service is enabled

Parameters:

  • name (String)

    The name of the service

  • service (Puppet::Resource) (defaults to: nil)

    An optional service resource to use

Returns:

  • (Boolean)

    True if the service is enabled



59
60
61
62
63
64
# File 'lib/shared/pe_status_check.rb', line 59

def service_enabled(name, service = nil)
  service ||= get_resource('service', name)
  return false if service.nil?

  service[:enable].to_s.casecmp('true').zero?
end

.service_file_exist?(configfile) ⇒ Boolean

Checks if passed service file exists in correct directory for the OS

Parameters:

  • configfile (String)

    The name of the pe service to be tested

Returns:

  • (Boolean)

    true if file exists



90
91
92
93
94
95
96
97
# File 'lib/shared/pe_status_check.rb', line 90

def service_file_exist?(configfile)
  configdir = if Facter.value(:os)['family'].eql?('RedHat') || Facter.value(:os)['family'].eql?('Suse')
                '/etc/sysconfig'
              else
                '/etc/default'
              end
  File.exist?("#{configdir}/#{configfile}")
end

.service_running(name, service = nil) ⇒ Boolean

Check if the service is running

Parameters:

  • name (String)

    The name of the service

  • service (Puppet::Resource) (defaults to: nil)

    An optional service resource to use

Returns:

  • (Boolean)

    True if the service is running



48
49
50
51
52
53
# File 'lib/shared/pe_status_check.rb', line 48

def service_running(name, service = nil)
  service ||= get_resource('service', name)
  return false if service.nil?

  service[:ensure] == :running
end

.service_running_enabled(name, service = nil) ⇒ Boolean

Check if the service is running and enabled

Parameters:

  • name (String)

    The name of the service

  • service (Puppet::Resource) (defaults to: nil)

    An optional service resource to use

Returns:

  • (Boolean)

    True if the service is running and enabled



70
71
72
73
74
75
# File 'lib/shared/pe_status_check.rb', line 70

def service_running_enabled(name, service = nil)
  service ||= get_resource('service', name)
  return false if service.nil?

  service_running(name, service) and service_enabled(name, service)
end