Class: AiRootShield::Enterprise::AlertSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_root_shield/enterprise/alert_system.rb

Overview

Advanced reporting and alert system with multiple notification channels

Constant Summary collapse

ALERT_LEVELS =
%w[info warning critical emergency].freeze
SUPPORTED_CHANNELS =
%w[syslog webhook email slack teams].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ AlertSystem

Returns a new instance of AlertSystem.



18
19
20
21
22
23
24
25
26
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 18

def initialize(config = {})
  @config = default_config.merge(config)
  @channels = {}
  @alert_history = []
  @status = { active: false, alerts_sent: 0 }
  @rate_limiter = {}
  
  initialize_channels
end

Instance Attribute Details

#alert_history ⇒ Object (readonly)

Returns the value of attribute alert_history.



16
17
18
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 16

def alert_history
  @alert_history
end

#channels ⇒ Object (readonly)

Returns the value of attribute channels.



16
17
18
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 16

def channels
  @channels
end

#status ⇒ Object (readonly)

Returns the value of attribute status.



16
17
18
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 16

def status
  @status
end

Instance Method Details

#configure_channel(channel_name, config) ⇒ Object

Configure alert channel



141
142
143
144
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 141

def configure_channel(channel_name, config)
  @config[:channels][channel_name.to_sym] = config
  initialize_channel(channel_name, config)
end

#get_statistics ⇒ Object

Get alert statistics



129
130
131
132
133
134
135
136
137
138
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 129

def get_statistics
  {
    total_alerts: @alert_history.length,
    alerts_by_severity: count_by_severity,
    alerts_by_type: count_by_type,
    alerts_by_channel: count_by_channel,
    success_rate: calculate_success_rate,
    recent_alerts: @alert_history.last(10)
  }
end

#send_alert(alert_data) ⇒ Object

Send alert through configured channels



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
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 53

def send_alert(alert_data)
  return false unless @status[:active]
  return false if rate_limited?(alert_data)
  
  alert = prepare_alert(alert_data)
  results = {}
  
  @config[:channels].each do |channel_name, channel_config|
    next unless channel_config[:enabled]
    
    begin
      result = send_to_channel(channel_name, alert, channel_config)
      results[channel_name] = result
      
      log_channel_result(channel_name, result, alert[:id])
    rescue StandardError => e
      results[channel_name] = { success: false, error: e.message }
      log_system_event("Alert channel error", { channel: channel_name, error: e.message })
    end
  end
  
  # Store alert in history
  store_alert_history(alert, results)
  
  @status[:alerts_sent] += 1
  results
end

#send_compliance_alert(compliance_result, policy_type) ⇒ Object

Send compliance violation alert



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 97

def send_compliance_alert(compliance_result, policy_type)
  severity = compliance_result[:compliant] ? 'info' : 'critical'
  
  alert_data = {
    type: 'compliance_violation',
    severity: severity,
    title: "Compliance Status Update",
    message: format_compliance_message(compliance_result, policy_type),
    data: compliance_result,
    source: 'ai_root_shield_compliance',
    timestamp: Time.now.utc.iso8601
  }
  
  send_alert(alert_data)
end

#send_health_alert(health_data, severity = 'info') ⇒ Object

Send system health alert



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 114

def send_health_alert(health_data, severity = 'info')
  alert_data = {
    type: 'system_health',
    severity: severity,
    title: "System Health Update",
    message: format_health_message(health_data),
    data: health_data,
    source: 'ai_root_shield_system',
    timestamp: Time.now.utc.iso8601
  }
  
  send_alert(alert_data)
end

#send_security_alert(analysis_result, severity = 'warning') ⇒ Object

Send security incident alert



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 82

def send_security_alert(analysis_result, severity = 'warning')
  alert_data = {
    type: 'security_incident',
    severity: severity,
    title: "Security Threat Detected",
    message: format_security_message(analysis_result),
    data: analysis_result,
    source: 'ai_root_shield',
    timestamp: Time.now.utc.iso8601
  }
  
  send_alert(alert_data)
end

#start ⇒ Object

Start alert system



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 29

def start
  @status[:active] = true
  @status[:started_at] = Time.now.utc.iso8601
  
  # Initialize syslog if configured
  if @config[:syslog][:enabled]
    Syslog.open("ai_root_shield", Syslog::LOG_PID, Syslog::LOG_LOCAL0)
  end
  
  log_system_event("Alert system started")
  true
end

#stop ⇒ Object

Stop alert system



43
44
45
46
47
48
49
50
# File 'lib/ai_root_shield/enterprise/alert_system.rb', line 43

def stop
  @status[:active] = false
  @status[:stopped_at] = Time.now.utc.iso8601
  
  Syslog.close if @config[:syslog][:enabled] && Syslog.opened?
  log_system_event("Alert system stopped")
  true
end