monitoring_controller.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. diff = Time.zone.now - channel.preferences['last_fetch']
  39. issues.push "#{message} channel is active but not fetched for #{helpers.time_ago_in_words(Time.zone.now - diff.seconds)} hour"
  40. end
  41. # outbound channel
  42. next if channel.status_out != 'error'
  43. message = "Channel: #{channel.area} out "
  44. %w[host user uid].each do |key|
  45. next if channel.options[key].blank?
  46. message += "key:#{channel.options[key]};"
  47. end
  48. issues.push "#{message} #{channel.last_log_out}"
  49. end
  50. # unprocessable mail check
  51. directory = Rails.root.join('tmp', 'unprocessable_mail').to_s
  52. if File.exist?(directory)
  53. count = 0
  54. Dir.glob("#{directory}/*.eml") do |_entry|
  55. count += 1
  56. end
  57. if count.nonzero?
  58. issues.push "unprocessable mails: #{count}"
  59. end
  60. end
  61. # scheduler running check
  62. Scheduler.where('active = ? AND period > 300', true).where.not(last_run: nil).order(last_run: :asc, period: :asc).each do |scheduler|
  63. diff = Time.zone.now - (scheduler.last_run + scheduler.period.seconds)
  64. next if diff < 8.minutes
  65. issues.push "scheduler may not run (last execution of #{scheduler.method} #{helpers.time_ago_in_words(Time.zone.now - diff.seconds)} over) - please contact your system administrator"
  66. break
  67. end
  68. if Scheduler.where(active: true, last_run: nil).count == Scheduler.where(active: true).count
  69. issues.push 'scheduler not running'
  70. end
  71. Scheduler.failed_jobs.each do |job|
  72. issues.push "Failed to run scheduled job '#{job.name}'. Cause: #{job.error_message}"
  73. actions.add(:restart_failed_jobs)
  74. end
  75. # failed jobs check
  76. failed_jobs = Delayed::Job.where('attempts > 0')
  77. count_failed_jobs = failed_jobs.count
  78. if count_failed_jobs > 10
  79. issues.push "#{count_failed_jobs} failing background jobs"
  80. end
  81. listed_failed_jobs = failed_jobs.select(:handler, :attempts).limit(10)
  82. sorted_failed_jobs = listed_failed_jobs.group_by(&:name).sort_by { |_handler, entries| entries.length }.reverse.to_h
  83. sorted_failed_jobs.each_with_index do |(name, jobs), index|
  84. attempts = jobs.map(&:attempts).sum
  85. issues.push "Failed to run background job ##{index += 1} '#{name}' #{jobs.count} time(s) with #{attempts} attempt(s)."
  86. end
  87. # job count check
  88. total_jobs = Delayed::Job.where('created_at < ?', Time.zone.now - 15.minutes).count
  89. if total_jobs > 8000
  90. issues.push "#{total_jobs} background jobs in queue"
  91. end
  92. # import jobs
  93. import_backends = ImportJob.backends
  94. # failed import jobs
  95. import_backends.each do |backend|
  96. job = ImportJob.where(
  97. name: backend,
  98. dry_run: false,
  99. ).where('finished_at >= ?', 5.minutes.ago).limit(1).first
  100. next if job.blank?
  101. next if !job.result.is_a?(Hash)
  102. error_message = job.result[:error]
  103. next if error_message.blank?
  104. issues.push "Failed to run import backend '#{backend}'. Cause: #{error_message}"
  105. end
  106. # stuck import jobs
  107. import_backends.each do |backend|
  108. job = ImportJob.where(
  109. name: backend,
  110. dry_run: false,
  111. finished_at: nil,
  112. ).where('updated_at <= ?', 5.minutes.ago).limit(1).first
  113. next if job.blank?
  114. issues.push "Stuck import backend '#{backend}' detected. Last update: #{job.updated_at}"
  115. end
  116. token = Setting.get('monitoring_token')
  117. if issues.blank?
  118. result = {
  119. healthy: true,
  120. message: 'success',
  121. token: token,
  122. }
  123. render json: result
  124. return
  125. end
  126. result = {
  127. healthy: false,
  128. message: issues.join(';'),
  129. issues: issues,
  130. actions: actions,
  131. token: token,
  132. }
  133. render json: result
  134. end
  135. =begin
  136. Resource:
  137. GET /api/v1/monitoring/status?token=XXX
  138. Response:
  139. {
  140. "agents": 8123,
  141. "last_login": "2016-11-21T14:14:14Z",
  142. "counts": {
  143. "users": 12313,
  144. "tickets": 23123,
  145. "ticket_articles": 131451,
  146. },
  147. "last_created_at": {
  148. "users": "2016-11-21T14:14:14Z",
  149. "tickets": "2016-11-21T14:14:14Z",
  150. "ticket_articles": "2016-11-21T14:14:14Z",
  151. },
  152. }
  153. Test:
  154. curl http://localhost/api/v1/monitoring/status?token=XXX
  155. =end
  156. def status
  157. token_or_permission_check
  158. last_login = nil
  159. last_login_user = User.where('last_login IS NOT NULL').order(last_login: :desc).limit(1).first
  160. if last_login_user
  161. last_login = last_login_user.last_login
  162. end
  163. status = {
  164. counts: {},
  165. last_created_at: {},
  166. last_login: last_login,
  167. agents: User.with_permissions('ticket.agent').count,
  168. }
  169. map = {
  170. users: User,
  171. groups: Group,
  172. overviews: Overview,
  173. tickets: Ticket,
  174. ticket_articles: Ticket::Article,
  175. text_modules: TextModule,
  176. }
  177. map.each do |key, class_name|
  178. status[:counts][key] = class_name.count
  179. last = class_name.last
  180. status[:last_created_at][key] = last&.created_at
  181. end
  182. if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
  183. sql = 'SELECT SUM(CAST(coalesce(size, \'0\') AS INTEGER)) FROM stores WHERE id IN (SELECT DISTINCT(store_file_id) FROM stores)'
  184. records_array = ActiveRecord::Base.connection.exec_query(sql)
  185. if records_array[0] && records_array[0]['sum']
  186. sum = records_array[0]['sum']
  187. status[:storage] = {
  188. kB: sum / 1024,
  189. MB: sum / 1024 / 1024,
  190. GB: sum / 1024 / 1024 / 1024,
  191. }
  192. end
  193. end
  194. render json: status
  195. end
  196. def token
  197. access_check
  198. token = SecureRandom.urlsafe_base64(40)
  199. Setting.set('monitoring_token', token)
  200. result = {
  201. token: token,
  202. }
  203. render json: result, status: :created
  204. end
  205. def restart_failed_jobs
  206. access_check
  207. Scheduler.restart_failed_jobs
  208. render json: {}, status: :ok
  209. end
  210. private
  211. def token_or_permission_check
  212. user = authentication_check_only(permission: 'admin.monitoring')
  213. return if user
  214. return if Setting.get('monitoring_token') == params[:token]
  215. raise Exceptions::NotAuthorized
  216. end
  217. def access_check
  218. return if Permission.find_by(name: 'admin.monitoring', active: true)
  219. raise Exceptions::NotAuthorized
  220. end
  221. end