monitoring_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2012-2023 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. token = Setting.get('monitoring_token')
  27. result = {
  28. healthy: health_status.healthy?,
  29. message: health_status.message,
  30. issues: health_status.response.issues,
  31. actions: health_status.response.actions,
  32. token: token,
  33. }
  34. render json: result
  35. end
  36. =begin
  37. Resource:
  38. GET /api/v1/monitoring/status?token=XXX
  39. Response:
  40. {
  41. "agents": 8123,
  42. "last_login": "2016-11-21T14:14:14Z",
  43. "counts": {
  44. "users": 12313,
  45. "tickets": 23123,
  46. "ticket_articles": 131451,
  47. },
  48. "last_created_at": {
  49. "users": "2016-11-21T14:14:14Z",
  50. "tickets": "2016-11-21T14:14:14Z",
  51. "ticket_articles": "2016-11-21T14:14:14Z",
  52. },
  53. }
  54. Test:
  55. curl http://localhost/api/v1/monitoring/status?token=XXX
  56. =end
  57. def status
  58. render json: MonitoringHelper::Status.new.fetch_status
  59. end
  60. =begin
  61. get counts about created ticket in certain time slot. s, m, h and d possible.
  62. Resource:
  63. GET /api/v1/monitoring/amount_check?token=XXX&max_warning=2000&max_critical=3000&periode=1h
  64. GET /api/v1/monitoring/amount_check?token=XXX&min_warning=2000&min_critical=3000&periode=1h
  65. GET /api/v1/monitoring/amount_check?token=XXX&periode=1h
  66. Response:
  67. {
  68. "state": "ok",
  69. "message": "",
  70. "count": 123,
  71. }
  72. {
  73. "state": "warning",
  74. "message": "limit of 2000 tickets in 1h reached",
  75. "count": 123,
  76. }
  77. {
  78. "state": "critical",
  79. "message": "limit of 3000 tickets in 1h reached",
  80. "count": 123,
  81. }
  82. Test:
  83. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&max_warning=2000&max_critical=3000&periode=1h
  84. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&min_warning=2000&min_critical=3000&periode=1h
  85. curl http://localhost/api/v1/monitoring/amount_check?token=XXX&periode=1h
  86. =end
  87. def amount_check
  88. render json: MonitoringHelper::AmountCheck.new(params).check_amount
  89. end
  90. def token
  91. token = SecureRandom.urlsafe_base64(40)
  92. Setting.set('monitoring_token', token)
  93. result = {
  94. token: token,
  95. }
  96. render json: result, status: :created
  97. end
  98. def restart_failed_jobs
  99. Scheduler.restart_failed_jobs
  100. render json: {}, status: :ok
  101. end
  102. end