channels.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module MonitoringHelper
  3. class HealthChecker
  4. class Channels < Backend
  5. include ActionView::Helpers::DateHelper
  6. LAST_RUN_TOLERANCE = 1.hour
  7. OPTIONS_KEYS = %w[host user uid].freeze
  8. def run_health_check
  9. scope.each { |channel| single_channel_check(channel) }
  10. end
  11. private
  12. def scope
  13. Channel.where(active: true)
  14. end
  15. def single_channel_check(channel)
  16. status_in(channel)
  17. status_out(channel)
  18. last_fetch(channel)
  19. end
  20. def status_in(channel)
  21. return if channel.status_in != 'error'
  22. message = status_message(channel, :in)
  23. response.issues.push "#{message} #{channel.last_log_in}"
  24. end
  25. def status_out(channel)
  26. return if channel.status_out != 'error'
  27. message = status_message(channel, :out)
  28. response.issues.push "#{message} #{channel.last_log_out}"
  29. end
  30. def status_message(channel, direction)
  31. message = "Channel: #{channel.area} #{direction} "
  32. OPTIONS_KEYS.each do |key|
  33. next if channel.options[key].blank?
  34. message += "#{key}:#{channel.options[key]};"
  35. end
  36. message
  37. end
  38. def last_fetch(channel)
  39. last_fetch = channel.preferences&.dig('last_fetch')
  40. return if !last_fetch
  41. return if last_fetch >= LAST_RUN_TOLERANCE.ago
  42. message = status_message(channel, :in)
  43. response.issues.push "#{message} is active but not fetched for #{time_ago_in_words(last_fetch)}"
  44. end
  45. end
  46. end
  47. end