check_mk_controller.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Integration::CheckMkController < ApplicationController
  3. skip_before_action :verify_csrf_token
  4. before_action :check_configured
  5. def update
  6. # check params
  7. raise Exceptions::UnprocessableEntity, 'event_id is missing!' if params[:event_id].blank?
  8. raise Exceptions::UnprocessableEntity, 'state is missing!' if params[:state].blank?
  9. raise Exceptions::UnprocessableEntity, 'host is missing!' if params[:host].blank?
  10. # search for open ticket
  11. auto_close = Setting.get('check_mk_auto_close')
  12. auto_close_state_id = Setting.get('check_mk_auto_close_state_id')
  13. group_id = Setting.get('check_mk_group_id')
  14. state_recovery_match = '(OK|UP)'
  15. # check if ticket with host is open
  16. customer = User.lookup(id: 1)
  17. # follow up detection by meta data
  18. integration = 'check_mk'
  19. open_states = Ticket::State.by_category(:open)
  20. ticket_ids = Ticket.where(state: open_states).order(created_at: :desc).limit(5000).pluck(:id)
  21. ticket_ids_found = []
  22. ticket_ids.each do |ticket_id|
  23. ticket = Ticket.find_by(id: ticket_id)
  24. next if !ticket
  25. next if !ticket.preferences
  26. next if !ticket.preferences[integration]
  27. next if !ticket.preferences[integration]['host']
  28. next if ticket.preferences[integration]['host'] != params[:host]
  29. next if ticket.preferences[integration]['service'] != params[:service]
  30. # found open ticket for service+host
  31. ticket_ids_found.push ticket.id
  32. end
  33. # new ticket, set meta data
  34. title = "#{params[:host]} is #{params[:state]}"
  35. body = "EventID: #{params[:event_id]}
  36. Host: #{params[:host]}
  37. Service: #{params[:service]}
  38. State: #{params[:state]}
  39. Text: #{params[:text]}
  40. RemoteIP: #{request.remote_ip}
  41. UserAgent: #{request.env['HTTP_USER_AGENT']}
  42. "
  43. # add article
  44. if params[:state].present? && ticket_ids_found.present?
  45. ticket_ids_found.each do |ticket_id|
  46. ticket = Ticket.find_by(id: ticket_id)
  47. next if !ticket
  48. article = Ticket::Article.create!(
  49. ticket_id: ticket_id,
  50. type_id: Ticket::Article::Type.find_by(name: 'web').id,
  51. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  52. body: body,
  53. subject: title,
  54. internal: false,
  55. )
  56. end
  57. if (!auto_close && params[:state].match(/#{state_recovery_match}/i)) || !params[:state].match(/#{state_recovery_match}/i)
  58. render json: {
  59. result: 'ticket already open, added note',
  60. ticket_ids: ticket_ids_found,
  61. }
  62. return
  63. end
  64. end
  65. # check if service is recovered
  66. if auto_close && params[:state].present? && params[:state].match(/#{state_recovery_match}/i)
  67. if ticket_ids_found.blank?
  68. render json: {
  69. result: 'no open tickets found, ignore action',
  70. }
  71. return
  72. end
  73. state = Ticket::State.lookup(id: auto_close_state_id)
  74. ticket_ids_found.each do |ticket_id|
  75. ticket = Ticket.find_by(id: ticket_id)
  76. next if !ticket
  77. ticket.state_id = auto_close_state_id
  78. ticket.save!
  79. end
  80. render json: {
  81. result: "closed tickets with ids #{ticket_ids_found.join(',')}",
  82. ticket_ids: ticket_ids_found,
  83. }
  84. return
  85. end
  86. ticket = Ticket.create!(
  87. group_id: group_id,
  88. customer_id: customer.id,
  89. title: title,
  90. preferences: {
  91. check_mk: {
  92. host: params[:host],
  93. service: params[:service],
  94. },
  95. }
  96. )
  97. article = Ticket::Article.create!(
  98. ticket_id: ticket.id,
  99. type_id: Ticket::Article::Type.find_by(name: 'web').id,
  100. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  101. body: body,
  102. subject: title,
  103. internal: false,
  104. )
  105. render json: {
  106. result: "new ticket created (ticket id: #{ticket.id})",
  107. ticket_id: ticket.id,
  108. ticket_number: ticket.number,
  109. }
  110. end
  111. private
  112. def check_configured
  113. http_log_config facility: 'check_mk'
  114. if !Setting.get('check_mk_integration')
  115. raise Exceptions::UnprocessableEntity, 'Feature is disable, please contact your admin to enable it!'
  116. end
  117. if Setting.get('check_mk_token') != params[:token]
  118. raise Exceptions::UnprocessableEntity, 'Invalid token!'
  119. end
  120. end
  121. end