monitoring_controller.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class MonitoringController < ApplicationController
  3. prepend_before_action -> { authentication_check(permission: 'admin.monitoring') }, except: %i[health_check status]
  4. skip_before_action :verify_csrf_token
  5. =begin
  6. Resource:
  7. GET /api/v1/monitoring/health_check?token=XXX
  8. Response:
  9. {
  10. "healthy": true,
  11. "message": "success",
  12. }
  13. {
  14. "healthy": false,
  15. "message": "authentication of XXX failed; issue #2",
  16. "issues": ["authentication of XXX failed", "issue #2"],
  17. }
  18. Test:
  19. curl http://localhost/api/v1/monitoring/health_check?token=XXX
  20. =end
  21. def health_check
  22. token_or_permission_check
  23. issues = []
  24. actions = Set.new
  25. # channel check
  26. last_run_tolerance = Time.zone.now - 1.hour
  27. Channel.where(active: true).each do |channel|
  28. # inbound channel
  29. if channel.status_in == 'error'
  30. message = "Channel: #{channel.area} in "
  31. %w[host user uid].each do |key|
  32. next if channel.options[key].blank?
  33. message += "key:#{channel.options[key]};"
  34. end
  35. issues.push "#{message} #{channel.last_log_in}"
  36. end
  37. if channel.preferences && channel.preferences['last_fetch'] && channel.preferences['last_fetch'] < last_run_tolerance
  38. issues.push "#{message} channel is active but not fetched for 1 hour"
  39. end
  40. # outbound channel
  41. next if channel.status_out != 'error'
  42. message = "Channel: #{channel.area} out "
  43. %w[host user uid].each do |key|
  44. next if channel.options[key].blank?
  45. message += "key:#{channel.options[key]};"
  46. end
  47. issues.push "#{message} #{channel.last_log_out}"
  48. end
  49. # unprocessable mail check
  50. directory = Rails.root.join('tmp', 'unprocessable_mail').to_s
  51. if File.exist?(directory)
  52. count = 0
  53. Dir.glob("#{directory}/*.eml") do |_entry|
  54. count += 1
  55. end
  56. if count.nonzero?
  57. issues.push "unprocessable mails: #{count}"
  58. end
  59. end
  60. # scheduler check
  61. Scheduler.where(active: true).where.not(last_run: nil).each do |scheduler|
  62. next if scheduler.period <= 300
  63. next if scheduler.last_run + scheduler.period.seconds > Time.zone.now - 5.minutes
  64. issues.push 'scheduler not running'
  65. break
  66. end
  67. if Scheduler.where(active: true, last_run: nil).count == Scheduler.where(active: true).count
  68. issues.push 'scheduler not running'
  69. end
  70. Scheduler.failed_jobs.each do |job|
  71. issues.push "Failed to run scheduled job '#{job.name}'. Cause: #{job.error_message}"
  72. actions.add(:restart_failed_jobs)
  73. end
  74. token = Setting.get('monitoring_token')
  75. if issues.blank?
  76. result = {
  77. healthy: true,
  78. message: 'success',
  79. token: token,
  80. }
  81. render json: result
  82. return
  83. end
  84. result = {
  85. healthy: false,
  86. message: issues.join(';'),
  87. issues: issues,
  88. actions: actions,
  89. token: token,
  90. }
  91. render json: result
  92. end
  93. =begin
  94. Resource:
  95. GET /api/v1/monitoring/status?token=XXX
  96. Response:
  97. {
  98. "agents": 8123,
  99. "last_login": "2016-11-21T14:14:14Z",
  100. "counts": {
  101. "users": 12313,
  102. "tickets": 23123,
  103. "ticket_articles": 131451,
  104. },
  105. "last_created_at": {
  106. "users": "2016-11-21T14:14:14Z",
  107. "tickets": "2016-11-21T14:14:14Z",
  108. "ticket_articles": "2016-11-21T14:14:14Z",
  109. },
  110. }
  111. Test:
  112. curl http://localhost/api/v1/monitoring/status?token=XXX
  113. =end
  114. def status
  115. token_or_permission_check
  116. last_login = nil
  117. last_login_user = User.where('last_login IS NOT NULL').order(last_login: :desc).limit(1).first
  118. if last_login_user
  119. last_login = last_login_user.last_login
  120. end
  121. status = {
  122. counts: {},
  123. last_created_at: {},
  124. last_login: last_login,
  125. agents: User.with_permissions('ticket.agent').count,
  126. }
  127. map = {
  128. users: User,
  129. groups: Group,
  130. overviews: Overview,
  131. tickets: Ticket,
  132. ticket_articles: Ticket::Article,
  133. }
  134. map.each do |key, class_name|
  135. status[:counts][key] = class_name.count
  136. last = class_name.last
  137. status[:last_created_at][key] = last&.created_at
  138. end
  139. render json: status
  140. end
  141. def token
  142. access_check
  143. token = SecureRandom.urlsafe_base64(40)
  144. Setting.set('monitoring_token', token)
  145. result = {
  146. token: token,
  147. }
  148. render json: result, status: :created
  149. end
  150. def restart_failed_jobs
  151. access_check
  152. Scheduler.restart_failed_jobs
  153. render json: {}, status: :ok
  154. end
  155. private
  156. def token_or_permission_check
  157. user = authentication_check_only(permission: 'admin.monitoring')
  158. return if user
  159. return if Setting.get('monitoring_token') == params[:token]
  160. raise Exceptions::NotAuthorized
  161. end
  162. def access_check
  163. return if Permission.find_by(name: 'admin.monitoring', active: true)
  164. raise Exceptions::NotAuthorized
  165. end
  166. end