Class: ActivePostgres::HealthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/active_postgres/health_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HealthChecker

Returns a new instance of HealthChecker.



5
6
7
8
# File 'lib/active_postgres/health_checker.rb', line 5

def initialize(config)
  @config = config
  @executor = create_executor
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/active_postgres/health_checker.rb', line 3

def config
  @config
end

#executorObject (readonly)

Returns the value of attribute executor.



3
4
5
# File 'lib/active_postgres/health_checker.rb', line 3

def executor
  @executor
end

Instance Method Details

#cluster_statusObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_postgres/health_checker.rb', line 108

def cluster_status
  status = {
    primary: {
      host: config.primary_host,
      status: check_postgres_running(config.primary_host) ? 'running' : 'down',
      connections: get_connection_count(config.primary_host),
      replication_lag: nil
    },
    standbys: []
  }

  config.standby_hosts.each do |host|
    standby_status = {
      host: host,
      status: check_postgres_running(host) ? 'streaming' : 'down',
      lag: get_replication_lag(host),
      sync_state: 'async'
    }
    status[:standbys] << standby_status
  end

  status
end

#run_health_checksObject



51
52
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_postgres/health_checker.rb', line 51

def run_health_checks
  puts '==> Running health checks...'
  puts

  all_ok = true

  # Check primary
  print "Primary (#{config.primary_host})... "
  if check_postgres_running(config.primary_host)
    puts ''
  else
    puts ''
    all_ok = false
  end

  # Check standbys
  config.standby_hosts.each do |host|
    print "Standby (#{host})... "
    if check_postgres_running(host) && check_replication_status(host)
      puts ''
    else
      puts ''
      all_ok = false
    end
  end

  # Check pgbouncer on all hosts
  if config.component_enabled?(:pgbouncer)
    puts
    puts '==> Checking PgBouncer...'
    config.all_hosts.each do |host|
      print "PgBouncer (#{host})... "
      pgbouncer_ok = check_pgbouncer_running(host)
      userlist_ok = check_pgbouncer_userlist(host)

      if pgbouncer_ok && userlist_ok
        puts ''
      elsif pgbouncer_ok && !userlist_ok
        puts '✗ (missing app user in userlist)'
        all_ok = false
      else
        puts ''
        all_ok = false
      end
    end
  end

  puts
  if all_ok
    puts '✓ All checks passed'
  else
    puts '✗ Some checks failed'
  end

  all_ok
end

#show_statusObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_postgres/health_checker.rb', line 33

def show_status
  puts
  puts "PostgreSQL Cluster Status (#{config.environment})"
  puts '=' * 70
  puts

  # Collect all node data first
  nodes = collect_node_status

  # Print table
  print_status_table(nodes)

  # Print components
  print_components

  puts
end

#ssh_executorObject

Backwards compatibility alias



11
12
13
# File 'lib/active_postgres/health_checker.rb', line 11

def ssh_executor
  @executor
end

#use_ssh_executor?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/active_postgres/health_checker.rb', line 23

def use_ssh_executor?
  mode = ENV['ACTIVE_POSTGRES_STATUS_MODE'].to_s.strip.downcase
  return true if mode == 'ssh'
  return false if mode == 'direct'

  return false if %w[localhost 127.0.0.1 ::1].include?(config.primary_host.to_s)

  true
end