Module: PuppetX::EnterpriseModules::OraInstall::Facts

Includes:
GetHomes, WindowsCore
Defined in:
lib/puppet_x/enterprisemodules/ora_install/facts.rb

Instance Method Summary collapse

Methods included from WindowsCore

#execute_sql, #powershell_script, #sql_script

Methods included from GetHomes

#default_database_sid, #get_homes, #get_orainst_loc, #group_for_file, #installed_patches, #ora_invdir, #oracle_home_for, #oracle_homes, #oratab_content, #oratab_exists?, #oratab_file, #os

Instance Method Details

#cleanup_opatch_residue(home) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 143

def cleanup_opatch_residue(home)
  # Some older versions of Opatch leave files and directories arround
  # When running opatch often, this causes some issues. This is to make
  # sure all files created by running Opatch as part of the fact, are remopved
  basename = Time.now.strftime('%Y-%m-%d_%H-%M-*%p')
  Puppet.debug `rm -rf /tmp/OraInstall*`
  Puppet.debug `rm -rf /tmp/opatch`
  FileUtils.rm_f("#{home}/cfgtoollogs/opatch/lsinv/lsinventory#{basename}.txt")
end

#first_non_root_file(dir) ⇒ Object



139
140
141
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 139

def first_non_root_file(dir)
  Dir.new(dir).each { |file| break file if File.stat("#{dir}/#{file}").uid.nonzero? }
end

#get_database_user(home) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 15

def get_database_user(home)
  database_user = Facter.value('override_database_user')
  return database_user unless database_user.nil?

  # puts "database user is oracle"
  if File.directory?("#{home}/bin")
    Facter::Util::Resolution.exec("/bin/stat --format=%U #{home}/bin/oracle")
  else
    'oracle'
  end
end

#get_defined_sids(home) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 153

def get_defined_sids(home)
  if os == 'windows'
    home_name = get_home_name(home)
    regkey = Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\Oracle\\KEY_#{home_name}")
    regkey.reject { |k, _v1, _v| k.scan(/ORA_(.*)_AUTOSTART/) == [] }.flatten.collect { |k| k.to_s.scan(/ORA_(.*)_AUTOSTART/) }.flatten.uniq
  else
    quoted_home = Regexp.quote(home)
    oratab_content.scan(/^([+-_A-Za-z].*):#{quoted_home}:[YN].*$/).flatten.uniq
  end
end

#get_home_name(home) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 74

def get_home_name(home)
  inventory_loc = get_orainst_loc
  return 'Invalid' unless File.exist?("#{inventory_loc}/ContentsXML/inventory.xml")

  file = File.read("#{inventory_loc}/ContentsXML/inventory.xml")
  doc = REXML::Document.new(file)
  doc.elements.each('/INVENTORY/HOME_LIST/HOME') do |element|
    return element.attributes['NAME'] if element.attributes['LOC'] == home
  end
end

#get_installed_patches(oracle_home) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 90

def get_installed_patches(oracle_home)
  if File.directory?(oracle_home)
    ora_user = user_for_file(oracle_home)
    installed_patches = patches_in_home(oracle_home, ora_user).flatten.sort
  else
    installed_patches = ['Invalid']
  end
  cleanup_opatch_residue(oracle_home) if Facter.value(:kernel) == 'Linux'
  installed_patches
end

#get_mode_role(sid, home) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 264

def get_mode_role(sid, home)
  if os == 'windows'
    service_state = `sc query OracleService#{sid}`.scan(/STATE *:.*(RUNNING)/).flatten.first
    if service_state == 'RUNNING'
      sql = %(select replace(open_mode, chr(32), chr(95)) as open_mode
                   , replace(database_role, chr(32), chr(95)) as database_role
                from v$database)
      execute_sql(sid, sql)
    else
      'Invalid Invalid'
    end
  else
    command     = %(sqlplus -S /nolog << EOF
      connect / as sysdba
      set heading off
      select replace(open_mode, chr(32), chr(95)) as open_mode
           , replace(database_role, chr(32), chr(95)) as database_role
        from v\\$database;
EOF)
    `su #{user_for_file(home)} -c 'export ORACLE_HOME="#{home}"; \
                                  export PATH="#{home}/bin:$PATH"; \
                                  export ORACLE_SID="#{sid}"; \
                                  unset TNS_ADMIN; \
                                  unset TWO_TASK; \
                                  export LD_LIBRARY_PATH="#{home}/lib"; \
                                  #{command}'`
  end
end

#get_opatch_version(home) ⇒ Object



37
38
39
40
41
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 37

def get_opatch_version(home)
  version = lsinventory(home).scan(/OPatch [V|v]ersion\s*:\s*(.*)/).flatten.first
  Puppet.debug "ora_install opatch version #{home}/OPatch/opatch: #{version}"
  version.nil? ? 'Invalid' : version
end

#get_product_version(oracle_home) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 293

def get_product_version(oracle_home)
  product_version = {
    'name'    => get_home_name(oracle_home),
    'os_user' => user_for_file(oracle_home),
    'version' => get_version(oracle_home)
  }
  cleanup_opatch_residue(oracle_home) if Facter.value(:kernel) == 'Linux'
  product_version
end

#get_running_connmgrs(home) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 254

def get_running_connmgrs(home)
  command = %Q(ps -eo pid,cmd | grep "#{home}/bin/cmadmin" | grep -v grep | awk '{print $3}')
  if os == 'windows'
    raw_info = ""
  else
    raw_info = Facter::Util::Resolution.exec(command)
  end
  raw_info.split("\n").compact
end

#get_running_listeners(home) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 211

def get_running_listeners(home)
  if os == 'windows'
    listener_services = `for /f "tokens=3" %i in ('tasklist /svc /fi "imagename eq tnslsnr.exe" ^| findstr "tnslsnr.exe"') do @echo %i`.split("\n")
    listeners = listener_services.map do |svc|
      svc_home_name, lsnr_name = svc.scan(/Oracle(.*)TNSListener(.*)/).flatten
      if svc_home_name == get_home_name(home)
        if lsnr_name.empty?
          'LISTENER'
        else
          lsnr_name
        end
      end
    end
    listeners.compact
  else
    command = %Q(ps -eo pid,cmd | grep "#{home}/bin/tns[l]snr" | grep -v cman | sed 's/-inherit//' | sed 's/-no_crs_notify//' | sed 's/-mode proxy//' |
                while read pid command listener; do
                    printf "%-9s %-80s\n" $listener  $(ls -l /proc/$pid/exe | awk '{ print $NF }' | sort | uniq)
                done)
    raw_info = Facter::Util::Resolution.exec(command)
    if $?.success?
      return [] if raw_info.empty?
      raw_info.split("\n").select{|e| e.include?(home)}.collect {|e| e.split.first}.sort
    else
      command = %Q(ps -eo pid,cmd | grep "#{home}/bin/tns[l]snr" | sed 's/-inherit//' | sed 's/-no_crs_notify//' |
                  while read pid command listener; do
                      printf "%-9s %-80s\n" $listener  $(ls -l /proc/$pid/exe | awk '{ print $NF }' | sort | uniq)
                  done)
      raw_info = Facter::Util::Resolution.exec(command)
      if $?.success?
        return [] if raw_info.empty?

        raw_info.split("\n").select { |e| e.include?(home) }.collect { |e| e.split.first }.sort
      else
        puts 'Fetching running listeners, needs access to /proc/pid/exe file system.'
        puts 'Probably using docker in non-privileged mode.'
        puts 'Run docker in privileged mode to fix the issue.'
        nil
      end
    end
  end
end

#get_running_processes(home) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 164

def get_running_processes(home)
  {
    'sids'        => get_running_sids(home),
    'listeners'   => get_running_listeners(home),
    'conn_mgrs'   => get_running_connmgrs(home)
  }
end

#get_running_sids(home) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 172

def get_running_sids(home)
  if os == 'windows'
    raw_array = []
    services = `for /f "tokens=3" %i in ('tasklist /svc /fi "imagename eq oracle.exe" ^| findstr "oracle.exe"') do @echo %i`
    services.split("\n").each do |service|
      db = service.gsub('OracleService', '')
      regkey = Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\#{service}")
      oracle_home = regkey['ImagePath'].gsub("\\bin\\ORACLE.EXE #{db}", '')
      raw_array << "#{db} ,#{oracle_home}"
    end
    raw_info = raw_array.join("\n")
  else
    command = %Q(ps aux | egrep "(ora|asm)_pmon" | awk '{gsub("(ora|asm)_pmon_", ""); print $2, $NF}' |
                 while read pid sid; do
                     printf "%-20s,%-80s\n" $sid  $(ls -l /proc/$pid/exe | awk '{ print $NF }' | sed 's/\\/bin\\/oracle$//' | sort | uniq)
                 done)
    raw_info = Facter::Util::Resolution.exec(command)
    unless $?.success?
      puts 'Fetching running SIDS, needs access to /proc/pid/exe file system.'
      puts 'Probably using docker in non-privileged mode.'
      puts 'Run docker in privileged mode to fix the issue.'
      nil
    end
  end
  return {} if raw_info.empty?

  databases = raw_info.split("\n").select { |e| e.downcase.include?(home.downcase)}.collect { |e| e.split(',').first.strip }.sort
  db_hash = databases.collect do |db|
    db_mode_role = db =~ /ASM/ ? ['n/a', 'n/a'] : get_mode_role(db, home).split
    {
      db => {
        'open_mode' => db_mode_role[0],
        'database_role' => db_mode_role[1]
      }
    }
  end
  Hash[*db_hash.collect { |h| h.to_a }.flatten]
end

#get_su_commandObject



27
28
29
30
31
32
33
34
35
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 27

def get_su_command
  if os == 'Linux'
    return 'su -l '
  elsif os == 'SunOS'
    return 'su - '
  end

  'su -l '
end

#get_version(home) ⇒ Object



85
86
87
88
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 85

def get_version(home)
  version = opatch_installed?(home) ? lsinventory(home).scan(/^(?:Oracle Database|Oracle Client|Oracle Grid Infrastructure|Oracle GoldenGate)\s.+?\s+(.*)$/).flatten.first : 'Invalid'
  version.nil? ? 'Invalid' : version
end

#jdk_clause(home) ⇒ Object



43
44
45
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 43

def jdk_clause(home)
  Dir.exist?("#{home}/jdk") ? "-jdk #{home}/jdk" : ''
end

#lsinventory(home) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 47

def lsinventory(home)
  @lsinventory ||= {}
  if os == 'windows'
    @lsinventory[home] ||= `#{home}\\OPatch\\opatch.bat lsinventory -jdk #{home}\\jdk`
  elsif supports_nolog?(home)
    @lsinventory[home] ||= Facter::Util::Resolution.exec("su #{user_for_file(home)} -c 'ORACLE_HOME=#{home} #{home}/OPatch/opatch lsinventory -jdk #{home}/jdk -customLogDir /tmp'")
    # We remove the created directory here, because it sometimes creates a dir with an other owner, which causes troubles for the next oracle home
    Puppet.debug `rm -rf /tmp/opatch`
    @lsinventory[home]
  else
    if supports_nolog?(home)
      @lsinventory[home] ||= Facter::Util::Resolution.exec("su #{user_for_file(home)} -c 'ORACLE_HOME=#{home} #{home}/OPatch/opatch lsinventory #{jdk_clause(home)} -customLogDir /tmp'")
      # We remove the created directory here, because it sometimes creates a dir with an other owner, which causes troubles for the next oracle home
      Puppet.debug `rm -rf /tmp/opatch`
      @lsinventory[home]
    else
      @lsinventory[home] ||= Facter::Util::Resolution.exec("su #{user_for_file(home)} -c 'ORACLE_HOME=#{home} #{home}/OPatch/opatch lsinventory #{jdk_clause(home)}'")
    end
  end
end

#opatch_installed?(oracle_home) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 112

def opatch_installed?(oracle_home)
  if os == 'windows'
    File.exist?("#{oracle_home}\\OPatch\\opatch.bat")
  else
    File.exist?("#{oracle_home}/OPatch/opatch")
  end
end

#patches_in_home(oracle_product_home_dir, os_user) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 101

def patches_in_home(oracle_product_home_dir, os_user)
  if opatch_installed?(oracle_product_home_dir)
    lsinventory(oracle_product_home_dir).scan(/Patch\s.(\d+)\s.*:\sapplied on/).flatten
  else
    #
    # When Opatch is not installed, we don't report any patches
    #
    ['Invalid']
  end
end

#supports_nolog?(home) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 68

def supports_nolog?(home)
  @lsinventory_help ||= {}
  @lsinventory_help[home] ||= Facter::Util::Resolution.exec("su #{user_for_file(home)} -c 'ORACLE_HOME=#{home} #{home}/OPatch/opatch lsinventory #{jdk_clause(home)} -help'")
  @lsinventory_help[home] =~ /customLogDir/
end

#user_for_file(home) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet_x/enterprisemodules/ora_install/facts.rb', line 120

def user_for_file(home)
  #
  # To run opatch, we need the owner of the grid and oracle directory. The mechanism
  # we use to get this user, is to fetch the owner of the bin directory in the grid or
  # oracle home. Sometimes however, these directories also contain files with root as owner.
  # since root can never be the user of the grid or oracle processen, we skip those.
  #
  if os == 'windows'
    group = group_for_file(home)
    users_in_group = `net localgroup #{group}`
    users = users_in_group.scan(/(^[a-zA-Z]*$)/).flatten
    users.reject!(&:empty?).reject! { |u| u == 'Members' }.first
  else
    a_file = first_non_root_file(home)
    uid = File.stat("#{home}/#{a_file}").uid
    Etc.getpwuid(uid).name
  end
end