-
Notifications
You must be signed in to change notification settings - Fork 4
/
summary-report.py
executable file
·149 lines (127 loc) · 4.43 KB
/
summary-report.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
"""
Script to parse Rundeck execution log and
send a summary email with all job failures
This script uses pygtail to tail logs
Run this script as a cron at whatever interval you desire
(the mail text is written expecting 3 hour interval, you can edit that)
"""
#To the arguments handling
import sys
import getopt
import socket
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from itertools import repeat
from smtplib import SMTP
from pygtail import Pygtail
###############################################################################
# User editable variables BEGIN
###############################################################################
# Name of the Rundeck project for which you want reports, usually Production
PROJECT = 'Production'
# The data-from user for email, ie. what the recipient sees in an email client
SENDER = '[email protected]'
# Recipients for the summary email
RCV = ['[email protected]', '[email protected]']
# SMTP server to send mails from
SMTP_SERVER = 'mail.domain.com'
# SMTP authentication username:
# this can be different from the data-from specified above
SMTP_USERNAME = '[email protected]'
# SMTP authentication password
SMTP_PASSWORD = 'goodstrongpassword'
# This will be used in the job execution links in summary email,
# this can be IP or hostname
BASE_URL = 'https://rundeck.internal.domain.tld'
###############################################################################
# User editable variables END
###############################################################################
ERROR_MSGS = ('failed]', 'timedout]')
NBSP = ' '
JOB = '{{}} {}<a href="{{}}">{{}}</a> {} {{}}<br/>'.format(
' '.join(repeat(NBSP, 3)), ' '.join(repeat(NBSP, 9))).format
LINK = '{}/project/{}/execution/show/{{}}'.format(BASE_URL, PROJECT).format
LOG = '/var/log/rundeck/rundeck.executions.log'
msg = MIMEMultipart('alternative')
msg['From'] = SENDER
msg['To'] = ', '.join(RCV)
def addLog(log_message):
global loggerName
var = "{}-{}\n".format(str(datetime.now())[:-7],log_message)
with open("summary-report.log", "a") as myfile:
myfile.write(var)
return
addLog("Starting script")
try:
opts, args = getopt.getopt(sys.argv[1:],"hh:p:f:t:s:b:g:",["help="])
except getopt.GetoptError:
print 'Try to use the help with the following command: summary-report.py -h'
sys.exit(2)
for opt,arg in opts:
if opt == '-h':
print """summary-report.py has the following arguments options:
-p <production>
-t <recipients emails>
-s <smtp host/sender email>
-b <base url>
"""
sys.exit()
elif opt == "-p":
PROJECT = arg
elif opt == "-t":
rcv = arg
elif opt == "-s":
smtp_username = arg
elif opt == "-b":
BASE_URL = arg
elif opt == "-g":
try:
smtp_port = int(arg)
except ValueError:
print("Option '-g' needs an integer as argument.")
sys.exit()
LOG = "/var/log/rundeck/rundeck.executions.log"
fail_count = 0
msg = MIMEMultipart('alternative')
msg['From'] = smtp_username
msg['To'] = ", ".join(rcv)
jobs = ''
failed_jobs = []
for line in Pygtail(LOG):
status = line.split()[4]
# Look for jobs that failed after retries, or timed out
# This will exclude jobs that succeed after a retry
if any(err_msg in status for err_msg in ERROR_MSGS):
job_date = '{} {}'.format(line.split()[0].strip('['),
line.split()[1].strip(']').split(',')[0])
job_name = ' '.join(line.split()[7:]).split('"')[1].strip('-/')
execution_id = status.split(':')[0].strip('[')
failed_jobs.append(
JOB(job_date, LINK(execution_id), execution_id, job_name))
# Send mail only if there are failures;
# Remove this if conditional to send mail always
if fail_count > 0:
addLog("Total jobs failed are {}".format(fail_count))
msg['Subject'] = 'Rundeck summary: ' + str(fail_count) + ' jobs failed'
part1 = MIMEText(mail_text, 'plain')
part2 = MIMEText(mail_text, 'html')
msg.attach(part1)
msg.attach(part2)
port_list = [587, 465, 25]
flag = True
index=0
while flag:
try:
server = smtplib.SMTP(smtp_server, port_list[index])
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(sender, rcv, msg.as_string())
server.quit()
flag=False
except :
index+=1
else :
addLog("No job failed")
addLog("Quitting the script")