-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_Send_Monthly_Report.sh
executable file
·173 lines (100 loc) · 7.02 KB
/
2_Send_Monthly_Report.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python
#**************************************************************************************************************************************************************
# Script Version: Version 5.0
# Owner: M Abdul Azeez Muqthar (06121Z)
# Program name: 2_Send_Monthly_Report
# Release Date: 23-Apr-2019
# Credits: Source languages used - Python, Html, Bash
# Version 5.0 updates: Process has been simplified ,html output is generated and report file is attached.
#
# Input: From Service Now files
# Output: Mail Report, Monthly_Report.xlsx and Email
#
#**************************************************************************************************************************************************************
#
#
#************************************************************* Please refrain from editing this file **********************************************************
import pandas as pd
import numpy as np
import glob
import subprocess
import os
import sys
from datetime import date, timedelta, datetime
to_address = "elijosep@in.ibm.com, vp.shetty@in.ibm.com" # To Address
cc_address = "" # CC Address Enter your email address in between the quotes
date = datetime.today().date()
start_date = (date.replace(day=1) - timedelta(days =1)).replace(day=1)
subject = 'Monthly analysis of Incidents /Change requests/Service Requests for Month of ' + start_date.strftime('%B')
incident_columns = sorted([u'Number', u'Priority', u'Affected Date', u'Company', u'State', u'Resolved', u'Category', u'Assignment group', u'Assigned to', u'Resolved by', u'Work notes', u'Short description', u'Breached Resolution Time', u'Type'])
columns = ['Number' , 'Short description','Priority', 'Category', 'Affected Date', 'Company', 'State', 'Assigned to', 'Resolved by', 'Work notes']
sr_columns = columns[:]
sr_columns.remove('Category')
change_columns = sorted([u'Number', u'Company', u'Short description', u'Approval', u'Type', u'State', u'Planned start date', u'Planned end date', u'Assigned to'])
incident_files_count = int(subprocess.check_output('ls ./Input/|grep incident*|wc -l', shell=True).strip('\n'))
change_files_count = int(subprocess.check_output('ls ./Input/|grep change*|wc -l', shell=True).strip('\n'))
os.system('cd ./Input/; c=0;for f in incident*.xlsx; do ((c++)); mv "$f" incident\("$c"\).xlsx ; done > /dev/null 2>&1')
os.system('cd ./Input/; c=0;for f in change*.xlsx; do ((c++)); mv "$f" change\("$c"\).xlsx ; done > /dev/null 2>&1')
duplicate_incidents = os.popen('cd Input; [ `diff -q incident\(1\).xlsx incident\(2\).xlsx|wc -l` = 0 ] || [ `diff -q incident\(2\).xlsx incident\(3\).xlsx|wc -l` = 0 ] || [ `diff -q incident\(1\).xlsx incident\(3\).xlsx|wc -l` = 0 ] && echo True || echo False').read().strip('\n')
duplicate_changes = os.popen('cd Input; [ `diff -q change\(1\).xlsx change\(2\).xlsx|wc -l` = 0 ] || [ `diff -q change\(2\).xlsx change\(3\).xlsx|wc -l` = 0 ] || [ `diff -q change\(1\).xlsx change\(3\).xlsx|wc -l` = 0 ] && echo True || echo False').read().strip('\n')
if (incident_files_count != 3):
print('Terminated: Download incident files of all three regions')
elif (change_files_count != 3):
print('Terminated: Download change files of all three regions')
elif (duplicate_incidents == 'True'):
print('Terminated: Duplicate Incidents found')
elif (duplicate_changes == 'True'):
print('Terminated: Duplicate Changes found')
else:
incident_data = pd.DataFrame()
for f, region in zip(glob.glob('./Input/incident*'), ['AP', 'EU', 'NA']):
df = pd.read_excel(f)
if (sorted(df.columns) != incident_columns):
sys.exit('Fields ' + str(list(set(incident_columns).difference(df.columns))[:]) + ' not found in ' + region + ' region')
incident_data = incident_data.append(df, ignore_index=True)
o = incident_data.select_dtypes(include=['object']).columns.tolist()
incident_data[o] = incident_data[o].fillna('')
for col in o:
incident_data[col] = incident_data[col].apply(lambda x: x.encode('ascii','ignore'))
incident_writer = pd.ExcelWriter('./Input/Incidents.xlsx', engine='xlsxwriter')
incident_data.to_excel(incident_writer, sheet_name='Sheet1', index = False)
incident_writer.save()
os.system('rm -rf ./Input/incident*')
change_data = pd.DataFrame()
for f, region in zip(glob.glob('./Input/change*'), ['AP', 'EU', 'NA']):
df = pd.read_excel(f)
if (sorted(df.columns) != change_columns):
sys.exit('Fields ' + str(list(set(change_columns).difference(df.columns))[:]) + ' not found in ' + region + ' region')
change_data = change_data.append(df, ignore_index=True)
o = change_data.select_dtypes(include=['object']).columns.tolist()
change_data[o] = change_data[o].fillna('')
for col in o:
change_data[col] = change_data[col].apply(lambda x: x.encode('ascii','ignore'))
change_writer = pd.ExcelWriter('./Input/Changes.xlsx', engine='xlsxwriter')
change_data.sort_values(by = 'Planned start date', inplace= True)
change_data.to_excel(change_writer, sheet_name='Sheet1', index = False)
change_writer.save()
os.system('rm -rf ./Input/change*')
incident_data['Work notes'].fillna('', inplace=True)
incident_data['Work notes'] = incident_data['Work notes'].apply(lambda x: x.split('\n\n')[0])
incident_data['Work notes'] = incident_data['Work notes'].apply(lambda x: x.replace('\n', '<br/>').replace('(Work notes)', '<br/>'))
incident_data['Affected Only Date'] = incident_data['Affected Date'].apply(lambda x: x.date())
f = open('./Output/Mail_Report.html', 'w+')
f.write("<html>\n<head>\n<title>Monthly Report</title>\n</head>\n<body>")
f.close()
f = open('./Output/Mail_Report.html', 'a+')
f.write('<p>Hi,</p>\n')
f.write('<br/>\nMonthly Report has been generated.<br/>\n<br/>\n Kindly check the attachment section\n')
f.write('\n<br/><br/><br/>\n*** This is an automatically generated email, please do not reply ***')
f.write('</body></html>')
f.close()
report_writer = pd.ExcelWriter('./Output/Monthly_Report_'+ start_date.strftime('%B') +'.xlsx', engine='xlsxwriter')
incident_data['Work notes'] = incident_data['Work notes'].apply(lambda x: x.replace('<br/>', ' '))
incident_data[incident_data['Type'] == 'Incident'][columns].sort_values(by = 'Affected Date', ascending = False).to_excel(report_writer, sheet_name= 'Incidents', index = False)
incident_data[incident_data['Type'] == 'Service Request'][sr_columns].sort_values(by = 'Affected Date', ascending = False).to_excel(report_writer, sheet_name= 'Service Requests', index = False)
change_data.to_excel(report_writer, sheet_name= 'Changes', index = False)
report_writer.save()
os.system("if [ `ps aux|grep postfix|wc -l` -lt 4 ]; then sudo start postfix; fi")
os.system("gio open ./Output/Mail_Report.html")
os.system("""echo 'Do you want to mail it? [Y/N]'; read answer; if [ "$answer" != "${answer#[Yy]}" ] ;then cat ./Output/Mail_Report.html |/usr/local/bin/mutt -e 'set content_type=text/html' -s '""" + subject +"' -a ./Output/Monthly_Report* -c '"+ cc_address + "' -- " + to_address + ";echo 'Mail Sent'; else echo 'Mail not sent'; fi")
os.system("rm -rf ./Input/Incidents.xlsx ./Input/Changes.xlsx; sleep 3")