hardware.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SystemReport::Plugin::Hardware < SystemReport::Plugin
  3. DESCRIPTION = __('Hardware (e.g. CPU cores, memory, disk space)').freeze
  4. def fetch
  5. {
  6. 'total_memory' => total_memory,
  7. 'cpu_cores' => Parallel.processor_count,
  8. 'app_disk_space' => %w[total used free].zip(df_zammad_root).to_h,
  9. }
  10. end
  11. def total_memory
  12. open3_data&.dig('children')&.find { |entry| entry['description'].downcase == 'motherboard' }&.dig('children')&.find { |entry| entry['description'].downcase == 'system memory' }&.dig('size')
  13. end
  14. def df_zammad_root
  15. `df #{Rails.root}`.lines.last.scan(%r{\d+}).map(&:to_i)[0..2]
  16. rescue
  17. []
  18. end
  19. def open3_data
  20. return {} if !binary_path
  21. data = execute
  22. return {} if data.blank?
  23. return data.first if data.is_a?(Array) # https://github.com/zammad/zammad/issues/5402
  24. data
  25. end
  26. private
  27. def execute
  28. stdout, stderr, status = Open3.capture3(binary_path, '-json', binmode: true)
  29. if !status.success?
  30. Rails.logger.error("lshw failed: #{stderr}")
  31. return {}
  32. end
  33. JSON.parse(stdout)
  34. rescue => e
  35. Rails.logger.error "lshw failed: #{e.message}"
  36. Rails.logger.error e
  37. {}
  38. end
  39. def binary_path
  40. return ENV['LSHW_PATH'] if ENV['LSHW_PATH'] && File.executable?(ENV['LSHW_PATH'])
  41. ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
  42. bin = File.join(path, 'lshw')
  43. return bin if File.executable?(bin)
  44. end
  45. nil
  46. end
  47. end