Module: Kontena::Cli::Grids::Common

Defined Under Namespace

Modules: Parameters

Instance Method Summary collapse

Instance Method Details

#calculate_filesystem_stats(nodes) ⇒ Hash

Parameters:

  • nodes (Array<Hash>)

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kontena/cli/grids/common.rb', line 85

def calculate_filesystem_stats(nodes)
  total_fs = {
    'used' => 0.0,
    'total' => 0.0
  }
  nodes.each do |node|
    root_dir = node['engine_root_dir']
    filesystems = node.dig('resource_usage', 'filesystem') || []
    root_fs = filesystems.find{|fs| fs['name'] == root_dir}
    total_fs['used'] += root_fs['used']
    total_fs['total'] += root_fs['total']
  end

  total_fs
end

#calculate_loads(nodes, node_count) ⇒ Hash

Parameters:

  • nodes (Array<Hash>)
  • node_count (Fixnum)

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
# File 'lib/kontena/cli/grids/common.rb', line 60

def calculate_loads(nodes, node_count)
  loads = {:'1m' => 0.0, :'5m' => 0.0, :'15m' => 0.0}
  return loads if node_count == 0

  loads[:'1m'] = nodes.map{|n| n.dig('resource_usage', 'load', '1m').to_f }.inject(:+) / node_count
  loads[:'5m'] = nodes.map{|n| n.dig('resource_usage', 'load', '5m').to_f }.inject(:+) / node_count
  loads[:'15m'] = nodes.map{|n| n.dig('resource_usage', 'load', '15m').to_f }.inject(:+) / node_count
  loads
end

#calculate_mem_used(nodes) ⇒ Float

Parameters:

  • nodes (Array<Hash>)

Returns:

  • (Float)


72
73
74
75
76
77
78
79
80
81
# File 'lib/kontena/cli/grids/common.rb', line 72

def calculate_mem_used(nodes)
  nodes.map{|n|
    mem = n.dig('resource_usage', 'memory')
    if mem
      mem['used'] - (mem['cached'] + mem['buffers'])
    else
      0.0
    end
  }.inject(:+)
end

#find_grid_by_name(name) ⇒ Object



101
102
103
# File 'lib/kontena/cli/grids/common.rb', line 101

def find_grid_by_name(name)
  grids['grids'].find {|grid| grid['name'] == name }
end

#get_grid(name = nil) ⇒ Hash

Returns /v1/grids/:grid JSON { … }.

Returns:

  • (Hash)

    /v1/grids/:grid JSON { … }



111
112
113
114
115
116
117
118
119
# File 'lib/kontena/cli/grids/common.rb', line 111

def get_grid(name = nil)
  if name
    client(require_token).get("grids/#{name}")
  elsif current_grid
    client(require_token).get("grids/#{current_grid}")
  else
    exit_with_error "No grid given or selected"
  end
end

#gridsObject



53
54
55
# File 'lib/kontena/cli/grids/common.rb', line 53

def grids
  @grids ||= client.get('grids')
end

Parameters:

  • grid (Hash)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kontena/cli/grids/common.rb', line 6

def print_grid(grid)
  host = ENV['KONTENA_URL'] || self.current_master['url']
  puts "#{grid['name']}:"
  puts "  uri: #{host.sub('http', 'ws')}"
  puts "  initial_size: #{grid['initial_size']}"
  puts "  default_affinity: "
  grid['default_affinity'].to_a.each do |a|
    puts "    - #{a}"
  end
  puts "  subnet: #{grid['subnet']}"
  puts "  supernet: #{grid['supernet']}"
  root_dir = grid['engine_root_dir']
  nodes = client(require_token).get("grids/#{grid['name']}/nodes")
  nodes = nodes['nodes'].select{|n| n['connected'] == true }
  node_count = nodes.size
  puts "  stats:"
  puts "    nodes: #{nodes.size} of #{grid['node_count']}"

  cpu_total = nodes.map{|n| n['cpus'].to_i}.inject(:+)
  puts "    cpus: #{cpu_total || 0}"

  loads = calculate_loads(nodes, node_count)
  puts "    load: #{(loads[:'1m'] || 0.0).round(2)} #{(loads[:'5m'] || 0.0).round(2)} #{(loads[:'15m'] || 0.0).round(2)}"

  mem_total = nodes.map{|n| n['mem_total'].to_i}.inject(:+)
  mem_used = calculate_mem_used(nodes)
  puts "    memory: #{to_gigabytes(mem_used)} of #{to_gigabytes(mem_total)} GB"

  total_fs = calculate_filesystem_stats(nodes)
  puts "    filesystem: #{to_gigabytes(total_fs['used'])} of #{to_gigabytes(total_fs['total'])} GB"

  puts "    users: #{grid['user_count']}"
  puts "    services: #{grid['service_count']}"
  puts "    containers: #{grid['container_count']}"
  if statsd = grid.dig('stats', 'statsd')
    puts "  exports:"
    puts "    statsd: #{statsd['server']}:#{statsd['port']}"
  end
  if logs = grid.dig('logs')
    puts "logs:"
    puts "  forwarder: #{logs['forwarder']}"
    logs['opts'].each do |k,v|
      puts "  #{k}: #{v}"
    end
  end
end

#to_gigabytes(amount) ⇒ Object



105
106
107
108
# File 'lib/kontena/cli/grids/common.rb', line 105

def to_gigabytes(amount)
  return 0.0 if amount.nil?
  (amount.to_f / 1024 / 1024 / 1024).to_f.round(2)
end