Class: Garrison::Checks::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/garrison/agents/check.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Check

Returns a new instance of Check.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/garrison/agents/check.rb', line 14

def initialize(options = {})
  @source      = ENV['GARRISON_ALERT_SOURCE']
  @severity    = ENV['GARRISON_ALERT_SEVERITY']
  @type        = ENV['GARRISON_ALERT_TYPE']
  @family      = ENV['GARRISON_ALERT_FAMILY']
  @departments = ENV['GARRISON_ALERT_DEPARTMENTS'] ? ENV['GARRISON_ALERT_DEPARTMENTS'].split(',') : []
  @options     = options
  @state       = :initial

  Logging.logger.progname = Api.configuration.uuid
  Logging.info "Starting... #{self.class.name}"
  inherit_settings
  Logging.info "Agent Settings (uuid=#{Api.configuration.uuid} source=#{self.source} severity=#{self.severity || 'dynamic'} type=#{self.type} family=#{self.family} departments=#{self.departments.join(',')})"

  options_log = options.map do |key, value|
    value = value.is_a?(Array) ? value.join(',') : value
    "#{key}=#{value}"
  end
  Logging.info "Check Settings (#{options_log.join(' ')})" if options.any?
end

Instance Attribute Details

#departmentsObject

Returns the value of attribute departments.



9
10
11
# File 'lib/garrison/agents/check.rb', line 9

def departments
  @departments
end

#familyObject

Returns the value of attribute family.



8
9
10
# File 'lib/garrison/agents/check.rb', line 8

def family
  @family
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/garrison/agents/check.rb', line 10

def options
  @options
end

#run_uuidObject

Returns the value of attribute run_uuid.



11
12
13
# File 'lib/garrison/agents/check.rb', line 11

def run_uuid
  @run_uuid
end

#severityObject

Returns the value of attribute severity.



6
7
8
# File 'lib/garrison/agents/check.rb', line 6

def severity
  @severity
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/garrison/agents/check.rb', line 5

def source
  @source
end

#stateObject

Returns the value of attribute state.



12
13
14
# File 'lib/garrison/agents/check.rb', line 12

def state
  @state
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/garrison/agents/check.rb', line 7

def type
  @type
end

Instance Method Details

#after_performObject



54
55
56
57
58
# File 'lib/garrison/agents/check.rb', line 54

def after_perform
  self.state = :complete
  Api::Run.update(self)
  Api::Alert.obsolete_previous_runs(self) if ENV["GARRISON_AUTO_OBSOLETE"]
end

#alert(params = {}) ⇒ Object



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
# File 'lib/garrison/agents/check.rb', line 64

def alert(params = {})
  Logging.info "Raising alert for '#{params[:target]}'"
  utc_time_now = Time.now.utc

  alert = Api::Alert.new
  alert.run_uuid = self.run_uuid
  alert.type = type
  alert.family = family
  alert.source = source
  alert.departments = departments

  alert.name = params[:name]
  alert.target = params[:target]
  alert.detail = params[:detail]
  alert.severity = params[:external_severity] || severity
  alert.count = params[:count] || 1

  alert.finding = params[:finding]
  alert.finding_id = params[:finding_id]
  alert.first_detected_at = params[:first_detected_at] || utc_time_now
  alert.last_detected_at = params[:last_detected_at] || utc_time_now

  alert.urls = params[:urls]
  alert.key_values = (self.key_values + params[:key_values]).uniq { |h| h[:key] }

  alert.no_repeat = params[:no_repeat]

  alert.save
end

#before_performObject



50
51
52
# File 'lib/garrison/agents/check.rb', line 50

def before_perform
  self.run_uuid = Api::Run.create(self)
end

#key_valuesObject



60
61
62
# File 'lib/garrison/agents/check.rb', line 60

def key_values
  []
end

#performObject



46
47
48
# File 'lib/garrison/agents/check.rb', line 46

def perform
  raise 'You must provide a perform method in your check class'
end

#runObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/garrison/agents/check.rb', line 35

def run
  before_perform
  begin
    perform
  rescue Exception => e
    Logging.fatal "#{e} - #{e.message}"
    self.state = :failed
  end
  after_perform
end