forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
780 additions
and
76 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
app/assets/javascripts/app/controllers/_integration/check_mk.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Index extends App.ControllerIntegrationBase | ||
featureIntegration: 'check_mk_integration' | ||
featureName: 'Check_MK' | ||
featureConfig: 'check_mk_config' | ||
description: [ | ||
['This service receives http requests from %s and creates tickets with host and service.', 'Check_MK'] | ||
['If the host and service is recovered again, the ticket will be closed automatically.'] | ||
] | ||
|
||
render: => | ||
super | ||
new App.SettingsForm( | ||
area: 'Integration::CheckMK' | ||
el: @$('.js-form') | ||
) | ||
|
||
new App.ScriptSnipped( | ||
el: @$('.js-scriptSnipped') | ||
facility: 'check_mk' | ||
style: 'bash' | ||
content: "#!/bin/bash\n\ncurl -X POST -F 'event_id=123' -F 'host=host1' -F 'service=http' -F 'state=down' #{App.Config.get('http_type')}://#{App.Config.get('fqdn')}/api/v1/integration/check_mk/#{App.Setting.get('check_mk_token')}" | ||
description: [ | ||
['To enable %s for sending http requests to %s, you need create "%s" in the admin interface if %s.', 'Check_MK', 'Zammad', 'Event Actions', 'Check_MK'] | ||
] | ||
) | ||
|
||
new App.HttpLog( | ||
el: @$('.js-log') | ||
facility: 'check_mk' | ||
) | ||
|
||
class State | ||
@current: -> | ||
App.Setting.get('check_mk_integration') | ||
|
||
App.Config.set( | ||
'IntegrationCheckMk' | ||
{ | ||
name: 'Check_MK' | ||
target: '#system/integration/check_mk' | ||
description: 'An open source monitoring tool.' | ||
controller: Index | ||
state: State | ||
} | ||
'NavBarIntegrations' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/assets/javascripts/app/controllers/widget/script_snipped.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class App.ScriptSnipped extends App.Controller | ||
#events: | ||
# 'click .js-record': 'show' | ||
|
||
elements: | ||
'.js-code': 'code' | ||
|
||
|
||
constructor: -> | ||
super | ||
#@fetch() | ||
@records = [] | ||
@render() | ||
|
||
render: => | ||
@html App.view('widget/script_snipped')( | ||
records: @records | ||
description: @description | ||
style: @style | ||
content: @content | ||
) | ||
|
||
@code.each (i, block) -> | ||
hljs.highlightBlock block |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
<hr> | ||
|
||
<h2><%- @T('Recent logs') %></h2> | ||
<div class="settings-entry"> | ||
<h2><%- @T('Recent logs') %></h2> | ||
<% if @description: %> | ||
<% for item in @description: %> | ||
<p><%- @T(item...) %></p> | ||
<% end %> | ||
<% end %> | ||
<div class="settings-entry"> | ||
<% if [email protected]: %> | ||
<table class="settings-list settings-list--stretch settings-list--placeholder"> | ||
<thead><tr><th><%- @T('No Entries') %> | ||
</table> | ||
<table class="settings-list settings-list--stretch settings-list--placeholder"> | ||
<thead><tr><th><%- @T('No Entries') %> | ||
</table> | ||
<% else: %> | ||
<table class="settings-list settings-list--stretch"> | ||
<thead> | ||
<tr> | ||
<th width="10%"><%- @T('Direction') %> | ||
<th><%- @T('Request') %> | ||
<th width="25%"><%- @T('Created at') %> | ||
</thead> | ||
<tbody> | ||
<table class="settings-list settings-list--stretch"> | ||
<thead> | ||
<tr> | ||
<th width="10%"><%- @T('Direction') %> | ||
<th><%- @T('Request') %> | ||
<th width="25%"><%- @T('Created at') %> | ||
</thead> | ||
<tbody> | ||
<% for record in @records: %> | ||
<tr data-id="<%= record.id %>" class="js-record"> | ||
<td><%- @T(record.direction) %> | ||
<td><a href="#"><%= record.status %> <%= record.method %> <%= record.url %></a> | ||
<td><%- @humanTime(record.created_at) %> | ||
<tr data-id="<%= record.id %>" class="js-record"> | ||
<td><%- @T(record.direction) %> | ||
<td><a href="#"><%= record.status %> <%= record.method %> <%= record.url %></a> | ||
<td><%- @humanTime(record.created_at) %> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</tbody> | ||
</table> | ||
<% end %> | ||
</div> | ||
</div> |
8 changes: 8 additions & 0 deletions
8
app/assets/javascripts/app/views/widget/script_snipped.jst.eco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<hr> | ||
<h2><%- @T('Usage') %></h2> | ||
<% if @description: %> | ||
<% for item in @description: %> | ||
<p><%- @T(item...) %></p> | ||
<% end %> | ||
<% end %> | ||
<pre><code class="language-<%- @style %> js-code"><%- @content %></code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# 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)' | ||
|
||
# check if ticket with host is open | ||
customer = User.lookup(id: 1) | ||
|
||
# follow up detection by meta data | ||
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 = [] | ||
ticket_ids.each { |ticket_id| | ||
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 | ||
} | ||
|
||
# new ticket, set meta data | ||
title = "#{params[:host]} is #{params[:state]}" | ||
body = "EventID: #{params[:event_id]} | ||
Host: #{params[:host]} | ||
Service: #{params[:service]} | ||
State: #{params[:state]} | ||
Text: #{params[:text]} | ||
RemoteIP: #{request.remote_ip} | ||
UserAgent: #{request.env['HTTP_USER_AGENT']} | ||
" | ||
|
||
# add article | ||
if params[:state].present? && ticket_ids_found.present? | ||
ticket_ids_found.each { |ticket_id| | ||
ticket = Ticket.find_by(id: ticket_id) | ||
next if !ticket | ||
article = Ticket::Article.create!( | ||
ticket_id: ticket_id, | ||
type_id: Ticket::Article::Type.find_by(name: 'web').id, | ||
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id, | ||
body: body, | ||
subject: title, | ||
internal: false, | ||
) | ||
} | ||
if (!auto_close && params[:state].match(/#{state_recovery_match}/i)) || !params[:state].match(/#{state_recovery_match}/i) | ||
render json: { | ||
result: 'ticket already open, added note', | ||
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 | ||
state = Ticket::State.lookup(id: auto_close_state_id) | ||
ticket_ids_found.each { |ticket_id| | ||
ticket = Ticket.find_by(id: ticket_id) | ||
next if !ticket | ||
ticket.state_id = auto_close_state_id | ||
ticket.save! | ||
} | ||
render json: { | ||
result: "closed tickets with ids #{ticket_ids_found.join(',')}", | ||
ticket_ids: ticket_ids_found, | ||
} | ||
return | ||
end | ||
|
||
ticket = Ticket.create!( | ||
group_id: group_id, | ||
customer_id: customer.id, | ||
title: title, | ||
preferences: { | ||
check_mk: { | ||
host: params[:host], | ||
service: params[:service], | ||
}, | ||
} | ||
) | ||
article = Ticket::Article.create!( | ||
ticket_id: ticket.id, | ||
type_id: Ticket::Article::Type.find_by(name: 'web').id, | ||
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id, | ||
body: body, | ||
subject: title, | ||
internal: false, | ||
) | ||
|
||
render json: { | ||
result: "new ticket created (ticket id: #{ticket.id})", | ||
ticket_id: ticket.id, | ||
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 | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.