Class: BrainzLab::Sentinel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/sentinel/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/brainzlab/sentinel/client.rb', line 10

def initialize(config)
  @config = config
  @base_url = config.sentinel_url || 'https://sentinel.brainzlab.ai'
end

Instance Method Details

#get_alerts(host_id: nil, status: nil, severity: nil) ⇒ Object

Get alerts for a host



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/brainzlab/sentinel/client.rb', line 120

def get_alerts(host_id: nil, status: nil, severity: nil)
  params = {}
  params[:host_id] = host_id if host_id
  params[:status] = status if status
  params[:severity] = severity if severity

  response = request(:get, '/api/v1/alerts', params: params)

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:alerts] || []
rescue StandardError => e
  log_error('get_alerts', e)
  []
end

#get_container(container_id) ⇒ Object

Get container details



94
95
96
97
98
99
100
101
102
103
# File 'lib/brainzlab/sentinel/client.rb', line 94

def get_container(container_id)
  response = request(:get, "/api/v1/containers/#{container_id}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_container', e)
  nil
end

#get_container_metrics(container_id, period: '1h') ⇒ Object

Get container metrics



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/brainzlab/sentinel/client.rb', line 106

def get_container_metrics(container_id, period: '1h')
  params = { period: period }

  response = request(:get, "/api/v1/containers/#{container_id}/metrics", params: params)

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_container_metrics', e)
  nil
end

#get_host(host_id) ⇒ Object

Get host details



32
33
34
35
36
37
38
39
40
41
# File 'lib/brainzlab/sentinel/client.rb', line 32

def get_host(host_id)
  response = request(:get, "/api/v1/hosts/#{host_id}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_host', e)
  nil
end

#get_metrics(host_id, period: '1h', metrics: nil) ⇒ Object

Get host metrics

Parameters:

  • host_id (String)

    Host ID

  • period (String) (defaults to: '1h')

    Time period (1h, 6h, 24h, 7d, 30d)

  • metrics (Array<String>) (defaults to: nil)

    Specific metrics to fetch (cpu, memory, disk, network)



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brainzlab/sentinel/client.rb', line 47

def get_metrics(host_id, period: '1h', metrics: nil)
  params = { period: period }
  params[:metrics] = metrics.join(',') if metrics

  response = request(:get, "/api/v1/hosts/#{host_id}/metrics", params: params)

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_metrics', e)
  nil
end

#get_processes(host_id, sort_by: 'cpu', limit: 20) ⇒ Object

Get top processes for a host



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brainzlab/sentinel/client.rb', line 62

def get_processes(host_id, sort_by: 'cpu', limit: 20)
  params = { sort_by: sort_by, limit: limit }

  response = request(:get, "/api/v1/hosts/#{host_id}/processes", params: params)

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:processes] || []
rescue StandardError => e
  log_error('get_processes', e)
  []
end

#list_containers(host_id: nil, status: nil) ⇒ Object

List all containers



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/brainzlab/sentinel/client.rb', line 77

def list_containers(host_id: nil, status: nil)
  params = {}
  params[:host_id] = host_id if host_id
  params[:status] = status if status

  response = request(:get, '/api/v1/containers', params: params)

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:containers] || []
rescue StandardError => e
  log_error('list_containers', e)
  []
end

#list_hosts(status: nil, page: 1, per_page: 50) ⇒ Object

List all registered hosts



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brainzlab/sentinel/client.rb', line 16

def list_hosts(status: nil, page: 1, per_page: 50)
  params = { page: page, per_page: per_page }
  params[:status] = status if status

  response = request(:get, '/api/v1/hosts', params: params)

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:hosts] || []
rescue StandardError => e
  log_error('list_hosts', e)
  []
end

#provision(project_id:, app_name:) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/brainzlab/sentinel/client.rb', line 156

def provision(project_id:, app_name:)
  response = request(
    :post,
    '/api/v1/projects/provision',
    body: { project_id: project_id, app_name: app_name },
    use_service_key: true
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('provision', e)
  false
end

#report_metrics(host_id:, metrics:, timestamp: nil) ⇒ Object

Report metrics from agent (internal use)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/brainzlab/sentinel/client.rb', line 138

def report_metrics(host_id:, metrics:, timestamp: nil)
  response = request(
    :post,
    '/internal/agent/report',
    body: {
      host_id: host_id,
      metrics: metrics,
      timestamp: timestamp || Time.now.utc.iso8601
    },
    use_agent_key: true
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPAccepted)
rescue StandardError => e
  log_error('report_metrics', e)
  false
end