Class: Contrast::Config::Diagnostics::Monitor

Inherits:
Object
  • Object
show all
Includes:
Contrast::Components::Logger::InstanceMethods, Utils::LogUtils
Defined in:
lib/contrast/config/diagnostics/monitor.rb

Overview

This class is responsible for logging to file the effective Agent configurations after startup.

Constant Summary collapse

DEFAULT_PATH =
File.join(Dir.pwd).cs__freeze
ERROR_MESSAGE =
'[Configuration Diagnostics] Could not write effective Agent configuration to file'
FILE_NAME =
'contrast_connection.json'
WRITE =
'w+'

Constants included from Utils::LogUtils

Utils::LogUtils::DATE_TIME_FORMAT, Utils::LogUtils::DEFAULT_LEVEL, Utils::LogUtils::DEFAULT_NAME, Utils::LogUtils::PROGNAME, Utils::LogUtils::STDERR_STR, Utils::LogUtils::STDOUT_STR, Utils::LogUtils::VALID_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::LogUtils

#write_permission?

Methods included from Contrast::Components::Logger::InstanceMethods

#cef_logger, #logger

Constructor Details

#initialize(path) ⇒ Monitor

Returns a new instance of Monitor.

Parameters:

  • path (String)

    path to write to file.



32
33
34
# File 'lib/contrast/config/diagnostics/monitor.rb', line 32

def initialize path
  @path = path unless Contrast::Utils::DuckUtils.empty_duck?(path)
end

Instance Attribute Details

#pathString (readonly)

Returns path to write the file.

Returns:

  • (String)

    path to write the file.



24
25
26
# File 'lib/contrast/config/diagnostics/monitor.rb', line 24

def path
  @path
end

Instance Method Details

#configContrast::Config::Diagnostics::Config

Returns effective configurations of the agent.



75
76
77
# File 'lib/contrast/config/diagnostics/monitor.rb', line 75

def config
  @_config ||= Contrast::Config::Diagnostics::Config.new
end

#dir_nameString

Determine the path of the current logger and permissions required for writing to path.

Returns:



62
63
64
65
66
67
68
69
70
# File 'lib/contrast/config/diagnostics/monitor.rb', line 62

def dir_name
  @_dir_name ||= if write_permission?(@path)
                   File.dirname(File.absolute_path(@path))
                 else
                   DEFAULT_PATH
                 end
rescue Errno::EROFS => e
  logger.warn(ERROR_MESSAGE, e)
end

#extract_settingsObject



49
50
51
52
53
54
55
56
# File 'lib/contrast/config/diagnostics/monitor.rb', line 49

def extract_settings
  Contrast::AGENT.to_effective_config(config.effective_config)
  Contrast::API.to_effective_config(config.effective_config)
  Contrast::APP_CONTEXT.to_effective_config(config.effective_config)
  Contrast::ASSESS.to_effective_config(config.effective_config)
  Contrast::INVENTORY.to_effective_config(config.effective_config)
  Contrast::PROTECT.to_effective_config(config.effective_config)
end

#reset_configObject

Reset the state of the current effective config.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/contrast/config/diagnostics/monitor.rb', line 80

def reset_config
  # salvage status if status:
  status = if Contrast::Utils::DuckUtils.empty_duck?(@_config.config_status)
             nil
           else
             @_config.config_status
           end
  @_config = Contrast::Config::Diagnostics::Config.new
  @_config.config_status = status if status
  @_config
end

#to_controlled_hashObject



92
93
94
95
96
97
# File 'lib/contrast/config/diagnostics/monitor.rb', line 92

def to_controlled_hash
  {
      report_create: report_create_time,
      config: config.to_controlled_hash
  }
end

#write_to_file(reset: true) ⇒ Boolean

Write current settings to file.

Parameters:

  • reset (Boolean) (defaults to: true)

    should we reset the config we have

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/contrast/config/diagnostics/monitor.rb', line 40

def write_to_file reset: true
  logger&.info('[Configuration Diagnostics] Writing Effective Configurations to file', path: dir_name)
  status = false
  write_to_file_logic(status, reset: reset)
rescue IOError => e
  logger&.warn(ERROR_MESSAGE, e)
  false
end

#write_to_file_logic(status, reset: true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/contrast/config/diagnostics/monitor.rb', line 99

def write_to_file_logic status, reset: true
  # We need to reset the config before updating it's values
  reset_config if reset
  extract_settings
  File.open(File.join(dir_name, FILE_NAME), WRITE) do |file|
    file.truncate(0)
    file.write(JSON.pretty_generate(to_controlled_hash, { space: Contrast::Utils::ObjectShare::SPACE }))
    status = true if file
    file.close
  end
  status
end