monitoring_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class MonitoringController < ApplicationController
  3. prepend_before_action { authorize! }
  4. prepend_before_action -> { authentication_check }, except: %i[health_check status amount_check]
  5. prepend_before_action -> { authentication_check_only }, only: %i[health_check status amount_check]
  6. skip_before_action :verify_csrf_token
  7. =begin
  8. Resource:
  9. GET /api/v1/monitoring/health_check?token=XXX
  10. Response:
  11. {
  12. "healthy": true,
  13. "message": "success",
  14. }
  15. {
  16. "healthy": false,
  17. "message": "authentication of XXX failed; issue #2",
  18. "issues": ["authentication of XXX failed", "issue #2"],
  19. }
  20. Test:
  21. curl http://localhost/api/v1/monitoring/health_check?token=XXX
  22. =end
  23. def health_check
  24. health_status = MonitoringHelper::HealthChecker.new
  25. health_status.check_health
  26. result = {
  27. healthy: health_status.healthy?,
  28. message: health_status.message,
  29. issues: health_status.response.issues,
  30. actions: health_status.response.actions,
  31. }
  32. # Send the token if the request came from the admin GUI which needs it.
  33. if authorized? policy_record, :token?
  34. result[:token] = Setting.get('monitoring_token')
  35. end
  36. render json: result
  37. end
  38. =begin
  39. Resource:
  40. GET /api/v1/monitoring/status?token=XXX
  41. Response:
  42. {
  43. "agents": 8123,
  44. "last_login": "2016-11-21T14:14:14Z",
  45. "counts": {
  46. "users": 12313,
  47. "tickets": 23123,
  48. "ticket_articles": 131451,
  49. },
  50. "last_created_at": {
  51. "users": "2016-11-21T14:14:14Z",
  52. "tickets": "2016-11-21T14:14:14Z",
  53. "ticket_articles": "2016-11-21T14:14:14Z",
  54. },
  55. }
  56. Test:
  57. curl http://localhost/api/v1/monitoring/status?token=XXX
  58. =end
  59. def status
  60. render json: MonitoringHelper::Status.new.fetch_status
  61. end
  62. =begin
  63. get counts about created ticket in certain time slot. s, m, h and d possible.
  64. Resource:
  65. GET /api/v1/monitoring/amount_check?token=XXX&max_warning=2000&max_critical=3000&periode=1h
  66. GET /api/v1/monitoring/amount_check?token=XXX&min_warning=2000&min_critical=3000&periode=1h
  67. GET /api/v1/monitoring/amount_check?token=XXX&periode=1h
  68. Response:
  69. {
  70. "state": "ok",
  71. "message": "",
  72. "count": 123,
  73. }
  74. {
  75. "state": "warning",
  76. "message": "limit of 2000 tickets in 1h reached",
  77. "count": 123,
  78. }
  79. {
  80. "state": "critical",
  81. "message": "limit of 3000 tickets in 1h reached",
  82. "count": 123,
  83. }
  84. Test:
  85. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&max_warning=2000&max_critical=3000&periode=1h
  86. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&min_warning=2000&min_critical=3000&periode=1h
  87. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h
  88. =end
  89. def amount_check
  90. render json: MonitoringHelper::AmountCheck.new(params).check_amount
  91. end
  92. def token
  93. token = SecureRandom.urlsafe_base64(40)
  94. Setting.set('monitoring_token', token)
  95. result = {
  96. token: token,
  97. }
  98. render json: result, status: :created
  99. end
  100. def restart_failed_jobs
  101. Scheduler.restart_failed_jobs
  102. render json: {}, status: :ok
  103. end
  104. end