monitoring_controller.rb 4.6 KB

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