hardware.rb 1.4 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'] == 'Motherboard' }&.dig('children')&.find { |entry| entry['description'] == '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. data
  24. end
  25. private
  26. def execute
  27. stdout, stderr, status = Open3.capture3(binary_path, '-json', binmode: true)
  28. if !status.success?
  29. Rails.logger.error("lshw failed: #{stderr}")
  30. return {}
  31. end
  32. begin
  33. JSON.parse(stdout)
  34. rescue
  35. Rails.logger.error("lshw failed: #{stdout}")
  36. {}
  37. end
  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