-
Notifications
You must be signed in to change notification settings - Fork 48
/
check_ansible_log.py
46 lines (39 loc) · 1.1 KB
/
check_ansible_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Description:
# This script searches the current Ansible log for unreachable
# hosts and failed tasks.
#
# Please adjust function 'send_log' matching your environment.
#
# Author: Joerg Kastning -- joerg(PUNKT)kastning(AT)uni-bielefeld(PUNKT)de
# License: MIT
import glob
import os
import smtplib
from email.mime.text import MIMEText
def send_log(log):
f = open(log, 'rb')
msg = MIMEText(f.read())
f.close()
msg['Subject'] = 'Content of %s' % log
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
s = smtplib.SMTP('localhost')
s.sendmail('[email protected]', ['[email protected]'], msg.as_string())
s.quit()
def main():
list_of_files = glob.glob('/var/log/ansible/*.log')
latest_file = max(list_of_files, key=os.path.getctime)
theFile = open(latest_file, 'r')
FILE = theFile.readlines()
theFile.close()
eventlist = []
for line in FILE:
if ('unreachable' or 'failed') in line:
if not ('unreachable=0' and 'failed=0') in line:
send_log(latest_file)
break
if __name__ == "__main__":
main()