Sha256: 67af2f533643eb2a7d4b1edfd43404ed12fd6538a67ef4951dd805e702f07361

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 KB

Contents

module Marty::Diagnostic::Node
  def self.my_ip
    begin
      Socket.ip_address_list.detect{|intf| intf.ipv4_private?}.ip_address
    rescue => e
      e.message
    end
  end

  def self.get_postgres_connections
    conn = ActiveRecord::Base.connection.execute('SELECT datname,'\
                                                 'application_name,'\
                                                 'state,'\
                                                 'pid,'\
                                                 'client_addr '\
                                                 'FROM pg_stat_activity')
    conn.each_with_object({}) do |conn, h|
      h[conn['datname']] ||= []
      h[conn['datname']] << conn.except('datname')
    end
  end

  def self.get_target_connections target
    get_postgres_connections[Marty::Diagnostic::Database.db_name].select{|conn|
      conn['application_name'].include?(target)
    }.map{|conn|
      conn['client_addr'] == '127.0.0.1' ? my_ip : conn['client_addr']
    }.uniq.compact
  end

  def self.get_nodes
    nodes = get_target_connections('Passenger')
    nodes.empty? ? [my_ip] : nodes
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
marty-1.2.9 other/marty/diagnostic/node.rb
marty-1.2.8 other/marty/diagnostic/node.rb
marty-1.2.7 other/marty/diagnostic/node.rb
marty-1.2.6 other/marty/diagnostic/node.rb
marty-1.2.5 other/marty/diagnostic/node.rb
marty-1.2.4 other/marty/diagnostic/node.rb
marty-1.2.3 other/marty/diagnostic/node.rb
marty-1.2.2 other/marty/diagnostic/node.rb
marty-1.2.1 lib/marty/diagnostic/node.rb
marty-1.2.0 lib/marty/diagnostic/node.rb
marty-1.1.9 lib/marty/diagnostic/node.rb