PageRenderTime 51ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/facter/idrac.rb

https://github.com/askreet/dell_facts
Ruby | 96 lines | 71 code | 20 blank | 5 comment | 7 complexity | f7c3baae5aab6f0cceab00612d732f7c MD5 | raw file
  1. # vim: ts=2 sw=2 expandtab
  2. require 'facter'
  3. require 'rubygems'
  4. require 'time'
  5. # Run a command, unless it's already been run and the output is cached.
  6. def cached_command_output(command, cache_time = 86400)
  7. cache_directory = '/var/opt/lib/pe-puppet/facts/cached_command_output'
  8. Dir.mkdir cache_directory unless File.exist? cache_directory
  9. cache_file = cache_directory + '/' + command.gsub("/", "_")
  10. if File.exist?(cache_file) && File.mtime(cache_file) > (Time.now - cache_time) then
  11. command_output = File.read(cache_file).chomp
  12. else
  13. command_output = %x{#{command}}
  14. f = File.open(cache_file, 'w')
  15. f.puts command_output
  16. f.close
  17. end
  18. return command_output
  19. end
  20. # Are there alternate locations this could be installed?
  21. if File.exists? '/opt/dell/srvadmin/sbin/racadm'
  22. Facter.add('dell_has_racadm') do
  23. setcode do
  24. 'true'
  25. end
  26. end
  27. # What data points should we gather, and what should we call them as Facts?
  28. name_mapping = {
  29. 'NIC Enabled' => 'nic_enabled',
  30. 'IPv4 Enabled' => 'enabled',
  31. 'DHCP Enabled' => 'dhcp',
  32. 'IP Address' => 'ip',
  33. 'Subnet Mask' => 'netmask',
  34. 'Gateway' => 'gateway',
  35. 'IPv6 Enabled' => 'enabled',
  36. 'DHCP6 Enabled' => 'dhcp',
  37. 'IP Address 1' => 'ip',
  38. 'Gateway' => 'gateway',
  39. 'NIC Selection' => 'nic_type',
  40. 'Link Detected' => 'has_link',
  41. 'Speed' => 'speed',
  42. 'Duplex Mode' => 'duplex',
  43. }
  44. heading = ''
  45. cached_command_output('/opt/dell/srvadmin/sbin/racadm getniccfg', 3600).each_line do |line|
  46. # Are we in a new section?
  47. if line =~ /^IPv4 settings/
  48. heading = 'ipv4_'
  49. next
  50. elsif line =~ /^IPv6 settings/
  51. heading = 'ipv6_'
  52. next
  53. elsif line =~ /^LOM Status/
  54. heading = ''
  55. next
  56. end
  57. d = line.split("=")
  58. next if d.length != 2
  59. key = d[0].strip
  60. value = d[1].strip
  61. if name_mapping.has_key? key
  62. fact_name = 'dell_rac_' + heading + name_mapping[key]
  63. Facter.add(fact_name) do
  64. setcode do
  65. value
  66. end
  67. end
  68. end
  69. end
  70. else
  71. Facter.add('dell_has_racadm') do
  72. setcode do
  73. 'false'
  74. end
  75. end
  76. end