Class: Ping::TNS

Inherits:
TCP
  • Object
show all
Defined in:
lib/net/tnsping.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'1.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, driver = "OCI8", host = nil, port = 1521, timeout = 5) {|_self| ... } ⇒ TNS

Creates and returns a new Ping::TNS object. If the db specified cannot be found in the tnsnames.ora file, then a Ping::TNS::Error is raised.

Yields:

  • (_self)

Yield Parameters:

  • _self (Ping::TNS)

    the object that the method was called on



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
# File 'lib/net/tnsping.rb', line 17

def initialize(db, driver="OCI8", host=nil, port=1521, timeout=5)
   @db        = db
   @dsn       = "dbi:#{driver}:" << db
   @host      = host
   @timeout   = timeout
   @port      = port
   @tns_admin = tns_admin
   @ports     = []  # There can be more than one host/port
   @hosts     = []  # for each dsn.  Try them in order.
   @sid       = nil

   @tns_admin = ENV['TNS_ADMIN']
   @ora_home  = ENV['ORACLE_HOME'] || ENV['ORA_HOME']

   if @tns_admin
      @tns_file = File.join(@tns_admin, 'tnsnames.ora')
   elsif @ora_home
      @tns_file = File.join(@ora_home, 'network', 'admin', 'tnsnames.ora')
   else
      @tns_file = File.join(ENV["HOME"] + 'tnsnames.ora')
   end

   yield self if block_given?

   # If the host is not specified, look for it in the tnsnames.or file
   if host.nil?
      err_msg = "tnsnames.ora file could not be found"
      raise Error, err_msg unless File.exists?(@tns_file)
      parse_tns_file
   else
      @hosts.push(host)
      @ports.push(port)
   end
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



10
11
12
# File 'lib/net/tnsping.rb', line 10

def db
  @db
end

#driverObject

Returns the value of attribute driver.



11
12
13
# File 'lib/net/tnsping.rb', line 11

def driver
  @driver
end

#dsnObject

Returns the value of attribute dsn.



10
11
12
# File 'lib/net/tnsping.rb', line 10

def dsn
  @dsn
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/net/tnsping.rb', line 10

def host
  @host
end

#hostsObject (readonly)

Returns the value of attribute hosts.



12
13
14
# File 'lib/net/tnsping.rb', line 12

def hosts
  @hosts
end

#ora_homeObject Also known as: oracle_home

Returns the value of attribute ora_home.



11
12
13
# File 'lib/net/tnsping.rb', line 11

def ora_home
  @ora_home
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/net/tnsping.rb', line 12

def port
  @port
end

#portsObject (readonly)

Returns the value of attribute ports.



12
13
14
# File 'lib/net/tnsping.rb', line 12

def ports
  @ports
end

#timeoutObject

Returns the value of attribute timeout.



11
12
13
# File 'lib/net/tnsping.rb', line 11

def timeout
  @timeout
end

#tns_adminObject

Returns the value of attribute tns_admin.



10
11
12
# File 'lib/net/tnsping.rb', line 10

def tns_admin
  @tns_admin
end

#tns_fileObject

Returns the value of attribute tns_file.



10
11
12
# File 'lib/net/tnsping.rb', line 10

def tns_file
  @tns_file
end

Instance Method Details

#ping?Boolean Also known as: ping_listener?

Performs a TCP ping on the listener. The host and port are determined from your tnsnames.ora file. If more than one host and/or port are found in the tnsnames.ora file, then each will be tried. So long as at least one of them connects successfully, true is returned.

If you specify a host and port in the constructor, then the attempt will only be made against that host on the given port. – Try each host/port listed for a given entry. Return a true result if any one of them succeeds and break out of the loop.

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/net/tnsping.rb', line 71

def ping?
   if @hosts.empty?
      raise Error, "No hosts found"
   end

   # Use 1521 if no ports were found in the tnsnames.ora file.
   if @ports.empty?
      @ports.push(@port)
   end

   # If the host is provided, only ping that host
   if @host
      0.upto(@ports.length-1){ |n|
         @port = @ports[n]
         return super
      }
   else
      0.upto(@ports.length-1){ |n|
         @port = @ports[n]
         @host = @hosts[n]
         return super
      }
   end
end

#ping_all?Boolean

Simple wrapper for ping_listener? + ping_database?

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/net/tnsping.rb', line 137

def ping_all?
   return false unless self.ping_listener?
   return false unless self.ping_database?
   true
end

#ping_database?(dsn = @dsn, timeout = @timeout, user = @sid, passwd = Time.now.to_s) ⇒ Boolean

Attempts to make a connection using a bogus login and password via the DBI class. If an ORA-01017 Oracle error is returned, that means the database is up and running and true is returned.

Note that each of the arguments for this method use the defaults passed to the constructor (or have a default otherwise set). You generally should not pass any arguments to this method. In the event that this method fails, false is returned and the error can be viewed via Ping::TNS#exception. – I have intentionally set the user and password to something random in order to avoid the possibility of accidentally guessing them. In case of cosmic coincidence, set them yourself.

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/net/tnsping.rb', line 110

def ping_database?(dsn=@dsn, timeout=@timeout, user=@sid, passwd=Time.now.to_s)
   re   = /ORA-01017/
   dbh  = nil
   user ||= Time.now.to_s
   rv = false
   begin
      Timeout.timeout(timeout){
         dbh = DBI.connect(dsn,user,passwd)
      }
   rescue DBI::DatabaseError => e
      if re.match(e.to_s)
         rv = true
      else
         @exception = e
      end
   rescue Timeout::Error, StandardError => e
      @exception = e
   ensure
      if dbh
         dbh.disconnect if dbh.connected?
      end
   end
   rv
end