Module: Central::Cli::Stacks::Common

Instance Method Summary collapse

Instance Method Details

#calculate_filesystem_stats(nodes) ⇒ Hash

Parameters:

  • nodes (Array<Hash>)

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/central/cli/stacks/common.rb', line 70

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)


45
46
47
48
49
50
51
52
53
# File 'lib/central/cli/stacks/common.rb', line 45

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)


57
58
59
60
61
62
63
64
65
66
# File 'lib/central/cli/stacks/common.rb', line 57

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

#find_stack_by_name(name) ⇒ Object



86
87
88
# File 'lib/central/cli/stacks/common.rb', line 86

def find_stack_by_name(name)
  stacks['stacks'].find { |stack| stack['name'] == name }
end

Parameters:

  • stack (Hash)


5
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
# File 'lib/central/cli/stacks/common.rb', line 5

def print_stack(stack)
  puts "#{stack['name']}:"
  puts "  uri: #{current_master['url'].sub('http', 'ws')}"
  puts "  token: #{stack['token']}"
  root_dir = stack['engine_root_dir']
  nodes = client(require_token).get("stacks/#{stack['name']}/nodes")
  nodes = nodes['nodes'].select { |n| n['connected'] == true }
  node_count = nodes.size
  puts '  stats:'
  puts "    nodes: #{nodes.size} of #{stack['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: #{stack['user_count']}"
  puts "    services: #{stack['service_count']}"
  puts "    containers: #{stack['container_count']}"
  if statsd = stack.dig('stats', 'statsd')
    puts '  exports:'
    puts "    statsd: #{statsd['server']}:#{statsd['port']}"
  end
end

#stacksObject



38
39
40
# File 'lib/central/cli/stacks/common.rb', line 38

def stacks
  @stacks ||= client(require_token).get('stacks')
end

#to_gigabytes(amount) ⇒ Object



90
91
92
93
# File 'lib/central/cli/stacks/common.rb', line 90

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