forked from kausrini/Mini-ScienceDMZ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendStatus.py
More file actions
110 lines (91 loc) · 5.24 KB
/
sendStatus.py
File metadata and controls
110 lines (91 loc) · 5.24 KB
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
#######################################################################################################
# Author : Advait M #
# #
# This file sends device status via email. Please import the email_config.py and secrets.py file #
# in order to scucessfully send the device status. #
# #
# Please check the email_config.py file for more information. #
# #
#######################################################################################################
# Import smtplib for the actual sending function
from smtplib import SMTP_SSL as SMTP
import sys
import difflib
import shutil
import os
import ntpath
from email_config import sender,receiver,smtp_server,path_for_logs,path_for_backup,path_for_diff
# Uncomment this and enter the path for secrets.py file which we will pull the username and password from.
from secrets import username,password
# Python email packages used.
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import Encoders
from email.mime.application import MIMEApplication
try:
# Function takes 4 arguments. Sender's email id, receiver's email id, the smtp server that you'll be using and the path to the log file. These parameters should be defined in the email_config file. Customize these to meet your needs.
def sendDeviceStatus(sender_email_id,receiver_email_id,smtp_server_name,log_path):
print "sending email"
# Create the email message.
msg = MIMEMultipart()
msg['Subject'] = 'Device status'
msg['From'] = sender_email_id
msg['To'] = ", ".join(receiver_email_id)
msg.preamble = 'This is the latest device log. Please inspect to check device status'
part = MIMEBase('application', "octet-stream")
try:
for p in log_path:
attachment = MIMEApplication(open(p, "r").read(), _subtype="txt")
attachment.add_header('Content-Disposition', 'attachment' ,filename=ntpath.basename(p))
msg.attach(attachment)
except:
print "Failed to attach files"
try:
conn = SMTP(smtp_server_name)
except:
print "Check your email relay"
try:
conn.login(username,password)
except:
print "Auhtentication problem. Please check your credentials in the secrets.py file"
conn.sendmail(sender,receiver,msg.as_string())
conn.quit()
def prepare_logs(log_path_list,backup_file_path_list,diff_file_path_list):
final_paths = []
for i in range(0,len(log_path_list)):
# Check if backup log file exists. This branch will only be executed once.
if not os.path.isfile(backup_file_path_list[i]):
# Create a copy of the log file to calculate difference later if the file does not exist
backup_log_file = file(ntpath.basename(backup_file_path_list[i]),"wb")
shutil.copyfile(log_path_list[i],backup_file_path_list[i])
backup_log_file.close()
# Send email with the device logs.
#sendDeviceStatus(sender,receiver,smtp_server,path_for_logs)
final_paths.append(log_path_list[i])
else:
# Clear the old diff file.
if os.path.isfile(diff_file_path_list[i]):
os.remove(diff_file_path_list[i])
# Create a new file with latest diff.
changes = file(ntpath.basename(diff_file_path_list[i]),"wb")
changed_contents = difflib.unified_diff(open(backup_file_path_list[i]).readlines(),open(log_path_list[i]).readlines(),n=0)
changes.writelines(changed_contents)
changes.close()
# Send status email only if there are changes in the logs.
if os.stat(diff_file_path_list[i]).st_size != 0:
#sendDeviceStatus(sender,receiver,smtp_server,path_for_diff) # Logs have changed, send update!
final_paths.append(diff_file_path_list[i])
# Remove the old backup file and update it with latest logs"
os.remove(backup_file_path_list[i])
backup_log_file = file(ntpath.basename(backup_file_path_list[i]),"wb")
shutil.copyfile(log_path_list[i],backup_file_path_list[i])
backup_log_file.close()
return final_paths
rslt = prepare_logs(path_for_logs,path_for_backup,path_for_diff)
if rslt:
sendDeviceStatus(sender,receiver,smtp_server,rslt)
else:
print "No change since last status, email not sent!"
except:
print " Oops! Something went wrong! Make sure your email_config.py and secrets.py files are correct!"