2017-08-21 23:13:19 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Integration::CheckMkController < ApplicationController
|
|
|
|
skip_before_action :verify_csrf_token
|
|
|
|
before_action :check_configured
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
|
|
|
# check params
|
|
|
|
raise Exceptions::UnprocessableEntity, 'event_id is missing!' if params[:event_id].blank?
|
|
|
|
raise Exceptions::UnprocessableEntity, 'state is missing!' if params[:state].blank?
|
|
|
|
raise Exceptions::UnprocessableEntity, 'host is missing!' if params[:host].blank?
|
|
|
|
|
|
|
|
# search for open ticket
|
|
|
|
auto_close = Setting.get('check_mk_auto_close')
|
|
|
|
auto_close_state_id = Setting.get('check_mk_auto_close_state_id')
|
|
|
|
group_id = Setting.get('check_mk_group_id')
|
|
|
|
state_recovery_match = '(OK|UP)'
|
|
|
|
|
2019-08-16 15:39:31 +00:00
|
|
|
# follow-up detection by meta data
|
2017-08-21 23:13:19 +00:00
|
|
|
integration = 'check_mk'
|
|
|
|
open_states = Ticket::State.by_category(:open)
|
|
|
|
ticket_ids = Ticket.where(state: open_states).order(created_at: :desc).limit(5000).pluck(:id)
|
|
|
|
ticket_ids_found = []
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket_ids.each do |ticket_id|
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket = Ticket.find_by(id: ticket_id)
|
|
|
|
next if !ticket
|
|
|
|
next if !ticket.preferences
|
|
|
|
next if !ticket.preferences[integration]
|
|
|
|
next if !ticket.preferences[integration]['host']
|
|
|
|
next if ticket.preferences[integration]['host'] != params[:host]
|
|
|
|
next if ticket.preferences[integration]['service'] != params[:service]
|
|
|
|
|
|
|
|
# found open ticket for service+host
|
|
|
|
ticket_ids_found.push ticket.id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-08-21 23:13:19 +00:00
|
|
|
|
|
|
|
# new ticket, set meta data
|
|
|
|
title = "#{params[:host]} is #{params[:state]}"
|
|
|
|
body = "EventID: #{params[:event_id]}
|
|
|
|
Host: #{params[:host]}
|
2020-07-13 08:44:33 +00:00
|
|
|
Service: #{params[:service] || '-'}
|
2017-08-21 23:13:19 +00:00
|
|
|
State: #{params[:state]}
|
2020-07-13 08:44:33 +00:00
|
|
|
Text: #{params[:text] || '-'}
|
2017-08-21 23:13:19 +00:00
|
|
|
RemoteIP: #{request.remote_ip}
|
2020-07-13 08:44:33 +00:00
|
|
|
UserAgent: #{request.env['HTTP_USER_AGENT'] || '-'}
|
2017-08-21 23:13:19 +00:00
|
|
|
"
|
|
|
|
|
|
|
|
# add article
|
|
|
|
if params[:state].present? && ticket_ids_found.present?
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket_ids_found.each do |ticket_id|
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket = Ticket.find_by(id: ticket_id)
|
|
|
|
next if !ticket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-06-28 11:38:49 +00:00
|
|
|
Ticket::Article.create!(
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket_id: ticket_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
type_id: Ticket::Article::Type.find_by(name: 'web').id,
|
2017-08-21 23:13:19 +00:00
|
|
|
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
2018-12-19 17:31:51 +00:00
|
|
|
body: body,
|
|
|
|
subject: title,
|
|
|
|
internal: false,
|
2017-08-21 23:13:19 +00:00
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-08-21 23:13:19 +00:00
|
|
|
if (!auto_close && params[:state].match(/#{state_recovery_match}/i)) || !params[:state].match(/#{state_recovery_match}/i)
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'ticket already open, added note',
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket_ids: ticket_ids_found,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if service is recovered
|
|
|
|
if auto_close && params[:state].present? && params[:state].match(/#{state_recovery_match}/i)
|
|
|
|
if ticket_ids_found.blank?
|
|
|
|
render json: {
|
|
|
|
result: 'no open tickets found, ignore action',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket_ids_found.each do |ticket_id|
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket = Ticket.find_by(id: ticket_id)
|
|
|
|
next if !ticket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket.state_id = auto_close_state_id
|
|
|
|
ticket.save!
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-08-21 23:13:19 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: "closed tickets with ids #{ticket_ids_found.join(',')}",
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket_ids: ticket_ids_found,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-02 11:28:18 +00:00
|
|
|
# define customer of ticket
|
|
|
|
customer = nil
|
|
|
|
if params[:customer].present?
|
|
|
|
customer = User.find_by(login: params[:customer].downcase)
|
|
|
|
if !customer
|
|
|
|
customer = User.find_by(email: params[:customer].downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !customer
|
|
|
|
customer = User.lookup(id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:state] = nil
|
|
|
|
params[:customer] = nil
|
|
|
|
ticket = Ticket.new(Ticket.param_cleanup(Ticket.association_name_to_id_convert(params)))
|
|
|
|
ticket.group_id ||= group_id
|
|
|
|
ticket.customer_id = customer.id
|
|
|
|
ticket.title = title
|
|
|
|
ticket.preferences = {
|
|
|
|
check_mk: {
|
|
|
|
host: params[:host],
|
|
|
|
service: params[:service],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ticket.save!
|
|
|
|
|
2019-06-28 11:38:49 +00:00
|
|
|
Ticket::Article.create!(
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket_id: ticket.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
type_id: Ticket::Article::Type.find_by(name: 'web').id,
|
2017-08-21 23:13:19 +00:00
|
|
|
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
2018-12-19 17:31:51 +00:00
|
|
|
body: body,
|
|
|
|
subject: title,
|
|
|
|
internal: false,
|
2017-08-21 23:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: "new ticket created (ticket id: #{ticket.id})",
|
|
|
|
ticket_id: ticket.id,
|
2017-08-21 23:13:19 +00:00
|
|
|
ticket_number: ticket.number,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_configured
|
|
|
|
http_log_config facility: 'check_mk'
|
|
|
|
|
|
|
|
if !Setting.get('check_mk_integration')
|
|
|
|
raise Exceptions::UnprocessableEntity, 'Feature is disable, please contact your admin to enable it!'
|
|
|
|
end
|
|
|
|
|
|
|
|
if Setting.get('check_mk_token') != params[:token]
|
|
|
|
raise Exceptions::UnprocessableEntity, 'Invalid token!'
|
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
|
|
|
|
true
|
2017-08-21 23:13:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|