Class: Net::TNSPing
- Inherits:
-
PingTCP
- Object
- PingTCP
- Net::TNSPing
- Defined in:
- lib/net/tnsping.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
-
#driver ⇒ Object
Returns the value of attribute driver.
-
#dsn ⇒ Object
Returns the value of attribute dsn.
-
#host ⇒ Object
Returns the value of attribute host.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#oracle_home ⇒ Object
Returns the value of attribute oracle_home.
-
#port ⇒ Object
Returns the value of attribute port.
-
#ports ⇒ Object
readonly
Returns the value of attribute ports.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#tns_admin ⇒ Object
Returns the value of attribute tns_admin.
-
#tns_file ⇒ Object
Returns the value of attribute tns_file.
Instance Method Summary collapse
-
#initialize(db, driver = "OCI8", host = nil, port = 1521, timeout = 5) {|_self| ... } ⇒ TNSPing
constructor
Creates and returns a new TNSPing object.
-
#ping? ⇒ Boolean
(also: #ping_listener?)
Performs a TCP ping on the listener.
-
#ping_all? ⇒ Boolean
Simple wrapper for ping_listener? + ping_database?.
-
#ping_database?(dsn = @dsn, timeout = @timeout, user = @sid, passwd = "xz949") ⇒ Boolean
Attempts to make a connection using a bogus login and password via the DBI class.
Constructor Details
#initialize(db, driver = "OCI8", host = nil, port = 1521, timeout = 5) {|_self| ... } ⇒ TNSPing
Creates and returns a new TNSPing object. If the db specified cannot be found in the tnsnames.ora file, then a TNSPingError is raised.
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 |
# File 'lib/net/tnsping.rb', line 17 def initialize(db, driver="OCI8", host=nil, port=1521, timeout=5) @db = db @dsn = "dbi:#{driver}:" << db @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"] @oracle_home = ENV["ORACLE_HOME"] || ENV["ORA_HOME"] if @tns_admin @tns_file = @tns_admin + "/tnsnames.ora" elsif @oracle_home @tns_file = @oracle_home + "/network/admin/tnsnames.ora" else @tns_file = 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 TNSPingError, err_msg unless File.exists?(@tns_file) parse_tns_file else @hosts.push(host) @ports.push(port) end end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
10 11 12 |
# File 'lib/net/tnsping.rb', line 10 def db @db end |
#driver ⇒ Object
Returns the value of attribute driver.
11 12 13 |
# File 'lib/net/tnsping.rb', line 11 def driver @driver end |
#dsn ⇒ Object
Returns the value of attribute dsn.
10 11 12 |
# File 'lib/net/tnsping.rb', line 10 def dsn @dsn end |
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/net/tnsping.rb', line 10 def host @host end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
12 13 14 |
# File 'lib/net/tnsping.rb', line 12 def hosts @hosts end |
#oracle_home ⇒ Object
Returns the value of attribute oracle_home.
11 12 13 |
# File 'lib/net/tnsping.rb', line 11 def oracle_home @oracle_home end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/net/tnsping.rb', line 12 def port @port end |
#ports ⇒ Object (readonly)
Returns the value of attribute ports.
12 13 14 |
# File 'lib/net/tnsping.rb', line 12 def ports @ports end |
#timeout ⇒ Object
Returns the value of attribute timeout.
11 12 13 |
# File 'lib/net/tnsping.rb', line 11 def timeout @timeout end |
#tns_admin ⇒ Object
Returns the value of attribute tns_admin.
10 11 12 |
# File 'lib/net/tnsping.rb', line 10 def tns_admin @tns_admin end |
#tns_file ⇒ Object
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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/net/tnsping.rb', line 70 def ping? if @hosts.empty? raise TNSPingError, "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?
136 137 138 139 140 |
# File 'lib/net/tnsping.rb', line 136 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 = "xz949") ⇒ 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 TNSPing#exception. – I have intentionally set the user and password to something zany in order to avoid the possibility of accidentally guessing them. In case of cosmic coincidence, set them yourself.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/net/tnsping.rb', line 109 def ping_database?(dsn=@dsn, timeout=@timeout, user=@sid, passwd="xz949") re = /ORA-01017/ dbh = nil user ||= "pzoyadf244" 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 |