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
34ed87a8
Commit
34ed87a8
authored
4 years ago
by
jurgenhaas
Browse files
Options
Downloads
Patches
Plain Diff
ansible-inventories/bitegra#36 Use Python requests instead of curl, also cleanup script
parent
d7193b19
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
+28
-108
28 additions, 108 deletions
alerta.py
with
28 additions
and
108 deletions
alerta.py
+
28
−
108
View file @
34ed87a8
...
...
@@ -3,13 +3,11 @@
from
__future__
import
(
absolute_import
,
division
,
print_function
)
__metaclass__
=
type
import
logging
import
os
import
json
import
requests
import
socket
import
uuid
from
subprocess
import
call
import
logging
from
ansible.plugins.callback
import
CallbackBase
...
...
@@ -39,142 +37,64 @@ class CallbackModule(CallbackBase):
logging
.
basicConfig
(
level
=
logging
.
FATAL
)
self
.
logger
=
logging
.
getLogger
(
'
alerta.ansible
'
)
self
.
logger
.
setLevel
(
logging
.
FATAL
)
self
.
hostname
=
socket
.
gethostname
()
self
.
company
=
os
.
getenv
(
'
ALERTA_COMPANY
'
,
''
)
self
.
session
=
str
(
uuid
.
uuid1
())
self
.
playbook
=
''
def
_log
(
self
,
severity
,
status
,
type
,
msg
,
extra
=
None
,
host
=
None
,
task
=
None
):
def
_log
(
self
,
severity
,
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
),
'
environment
'
:
self
.
company
,
'
resource
'
:
self
.
session
,
'
event
'
:
'
Exception
'
,
'
tags
'
:
[
'
environment:%s
'
%
self
.
company
],
'
group
'
:
'
Application
'
,
'
severity
'
:
'
critical
'
,
'
service
'
:
[
'
www.example.com
'
],
'
text
'
:
'
%s - %s
'
%
(
self
.
playbook
,
task
),
'
value
'
:
''
,
'
rawData
'
:
''
,
}
headers
=
{
'
Authorization
'
:
'
Key %s
'
%
os
.
getenv
(
'
ALERTA_APIKEY
'
,
''
),
'
Content-type
'
:
'
application/json
'
}
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
)
requests
.
post
(
'
%s/alert
'
%
os
.
getenv
(
'
ALERTA_WEBHOOK
'
,
''
),
json
=
values
,
headers
=
headers
)
##### 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
'
severity
=
logging
.
FATAL
elif
'
changed
'
in
result
.
_result
.
keys
()
and
result
.
_result
[
'
changed
'
]:
s
tatus
=
'
CHANGED
'
s
everity
=
logging
.
INFO
else
:
status
=
'
OK
'
self
.
_log
(
logging
.
INFO
,
status
,
'
task
'
,
'
Task %s
'
%
task_name
,
extra
=
result
.
_result
,
host
=
result
.
_host
.
name
,
task
=
task_name
)
severity
=
logging
.
DEBUG
self
.
_log
(
severity
,
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
)
self
.
_log
(
logging
.
FATAL
)
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
)
self
.
v2_runner_on_failed
(
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
)
self
.
v2_runner_on_failed
(
result
)
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