Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alerta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ansible
Plugins
alerta
Commits
d7193b19
Commit
d7193b19
authored
4 years ago
by
jurgenhaas
Browse files
Options
Downloads
Patches
Plain Diff
ansible-inventories/bitegra#36 Alerta callback plugin
parent
11683c36
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
alerta.py
+180
-0
180 additions, 0 deletions
alerta.py
with
180 additions
and
0 deletions
alerta.py
0 → 100644
+
180
−
0
View file @
d7193b19
#!/usr/bin/python3
from
__future__
import
(
absolute_import
,
division
,
print_function
)
__metaclass__
=
type
import
os
import
json
import
socket
import
uuid
from
subprocess
import
call
import
logging
from
ansible.plugins.callback
import
CallbackBase
class
CallbackModule
(
CallbackBase
):
"""
ansible alerta callback plugin
ansible.cfg:
callback_plugins = <path_to_callback_plugins_folder>
callback_whitelist = alerta
and put the plugin in <path_to_callback_plugins_folder>
This plugin makes use of the following required environment variables:
ALERTA_COMPANY : The inventory
ALERTA_WEBHOOK : The Alerta webhook
ALERTA_APIKEY : The Alerta API key
"""
CALLBACK_VERSION
=
2.0
CALLBACK_TYPE
=
'
aggregate
'
CALLBACK_NAME
=
'
alerta
'
CALLBACK_NEEDS_WHITELIST
=
True
playbook
=
''
def
__init__
(
self
):
super
(
CallbackModule
,
self
).
__init__
()
logging
.
basicConfig
(
level
=
logging
.
FATAL
)
self
.
logger
=
logging
.
getLogger
(
'
alerta.ansible
'
)
self
.
logger
.
setLevel
(
logging
.
FATAL
)
self
.
company
=
os
.
getenv
(
'
ALERTA_COMPANY
'
,
''
)
def
_log
(
self
,
severity
,
status
,
type
,
msg
,
extra
=
None
,
host
=
None
,
task
=
None
):
if
self
.
logger
.
isEnabledFor
(
severity
):
values
=
{
'
host
'
:
self
.
hostname
,
'
session
'
:
self
.
session
,
'
index
'
:
self
.
index
,
'
status
'
:
status
,
'
msg
'
:
msg
,
'
ansible_type
'
:
type
,
'
ansible_company
'
:
self
.
company
,
'
ansible_playbook
'
:
self
.
playbook
,
'
ansible_task
'
:
task
,
'
ansible_host
'
:
host
,
'
ansible_result
'
:
json
.
dumps
(
extra
),
}
cmd
=
[
'
/usr/bin/curl
'
,
'
-XPOST
'
,
'
%s/alert
'
%
os
.
getenv
(
'
ALERTA_WEBHOOK
'
,
''
),
'
-H
'
,
'
Authorization: Key %s
'
%
os
.
getenv
(
'
ALERTA_APIKEY
'
,
''
),
'
-H
'
,
'
Content-type: application/json
'
,
'
-d
'
,
json
.
dumps
(
values
)
]
call
(
cmd
)
##### PLAYBOOK callbacks #####
def
v2_playbook_on_start
(
self
,
playbook
):
self
.
playbook
=
playbook
.
_file_name
self
.
_log
(
logging
.
INFO
,
'
OK
'
,
'
start
'
,
'
Start playbook %s
'
%
self
.
playbook
)
def
v2_playbook_on_play_start
(
self
,
play
):
self
.
_log
(
logging
.
INFO
,
'
OK
'
,
'
start
'
,
'
Start play %s
'
%
play
.
get_name
().
strip
())
def
v2_playbook_on_task_start
(
self
,
task
,
is_conditional
):
super
(
CallbackModule
,
self
).
v2_playbook_on_task_start
(
task
,
is_conditional
)
def
v2_playbook_on_cleanup_task_start
(
self
,
task
):
super
(
CallbackModule
,
self
).
v2_playbook_on_cleanup_task_start
(
task
)
def
v2_playbook_on_notify
(
self
,
result
,
handler
):
super
(
CallbackModule
,
self
).
v2_playbook_on_notify
(
result
,
handler
)
def
v2_playbook_on_handler_task_start
(
self
,
task
):
super
(
CallbackModule
,
self
).
v2_playbook_on_handler_task_start
(
task
)
def
v2_playbook_on_stats
(
self
,
stats
):
summarize_stat
=
{}
for
host
in
stats
.
processed
.
keys
():
summarize_stat
[
host
]
=
stats
.
summarize
(
host
)
if
self
.
errors
==
0
:
status
=
"
OK
"
else
:
status
=
"
FAILED
"
self
.
_log
(
logging
.
INFO
,
status
,
'
finish
'
,
'
Finish playbook %s
'
%
self
.
playbook
,
extra
=
summarize_stat
)
def
v2_playbook_on_no_hosts_matched
(
self
):
super
(
CallbackModule
,
self
).
v2_playbook_on_no_hosts_matched
()
def
v2_playbook_on_no_hosts_remaining
(
self
):
super
(
CallbackModule
,
self
).
v2_playbook_on_no_hosts_remaining
()
def
v2_playbook_on_include
(
self
,
included_file
):
super
(
CallbackModule
,
self
).
v2_playbook_on_include
(
included_file
)
def
v2_playbook_on_import_for_host
(
self
,
result
,
imported_file
):
super
(
CallbackModule
,
self
).
v2_playbook_on_import_for_host
(
result
,
imported_file
)
def
v2_playbook_on_not_import_for_host
(
self
,
result
,
missing_file
):
super
(
CallbackModule
,
self
).
v2_playbook_on_not_import_for_host
(
result
,
missing_file
)
def
v2_playbook_on_vars_prompt
(
self
,
varname
,
private
=
True
,
prompt
=
None
,
encrypt
=
None
,
confirm
=
False
,
salt_size
=
None
,
salt
=
None
,
default
=
None
):
super
(
CallbackModule
,
self
).
v2_playbook_on_vars_prompt
(
varname
,
private
,
prompt
,
encrypt
,
confirm
,
salt_size
,
salt
,
default
)
##### RUNNER callbacks #####
def
v2_runner_on_ok
(
self
,
result
,
**
kwargs
):
task_name
=
str
(
result
.
_task
).
replace
(
'
TASK:
'
,
''
)
if
'
failed
'
in
result
.
_result
.
keys
()
and
result
.
_result
[
'
failed
'
]:
self
.
errors
+=
1
status
=
'
FAILED
'
elif
'
changed
'
in
result
.
_result
.
keys
()
and
result
.
_result
[
'
changed
'
]:
status
=
'
CHANGED
'
else
:
status
=
'
OK
'
self
.
_log
(
logging
.
INFO
,
status
,
'
task
'
,
'
Task %s
'
%
task_name
,
extra
=
result
.
_result
,
host
=
result
.
_host
.
name
,
task
=
task_name
)
def
v2_runner_on_failed
(
self
,
result
,
**
kwargs
):
result
.
_result
[
'
failed
'
]
=
True
self
.
v2_runner_on_ok
(
result
)
def
v2_runner_on_unreachable
(
self
,
result
):
self
.
_log
(
logging
.
ERROR
,
'
FATAL
'
,
'
host
'
,
'
Host %s unreachable
'
%
result
.
_host
.
name
,
extra
=
result
.
_result
,
host
=
result
.
_host
.
name
)
def
v2_runner_on_async_poll
(
self
,
result
):
super
(
CallbackModule
,
self
).
v2_runner_on_async_poll
(
result
)
def
v2_runner_on_async_ok
(
self
,
result
):
self
.
v2_runner_on_ok
(
result
)
def
v2_runner_on_async_failed
(
self
,
result
):
result
.
_result
[
'
failed
'
]
=
True
self
.
v2_runner_on_ok
(
result
)
def
v2_runner_on_skipped
(
self
,
result
):
super
(
CallbackModule
,
self
).
v2_runner_on_skipped
(
result
)
def
v2_runner_retry
(
self
,
result
):
super
(
CallbackModule
,
self
).
v2_runner_retry
(
result
)
def
v2_runner_item_on_ok
(
self
,
result
):
self
.
v2_runner_on_ok
(
result
)
def
v2_runner_item_on_failed
(
self
,
result
):
result
.
_result
[
'
failed
'
]
=
True
self
.
v2_runner_on_ok
(
result
)
def
v2_runner_item_on_skipped
(
self
,
result
):
super
(
CallbackModule
,
self
).
v2_runner_item_on_skipped
(
result
)
##### OTHER callbacks #####
def
v2_on_file_diff
(
self
,
result
):
super
(
CallbackModule
,
self
).
v2_on_file_diff
(
result
)
def
v2_on_any
(
self
,
*
args
,
**
kwargs
):
super
(
CallbackModule
,
self
).
v2_on_any
(
*
args
,
**
kwargs
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment