-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathemail_notification_script.py
116 lines (98 loc) · 3.85 KB
/
email_notification_script.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 26 19:52:22 2021
@author: shlomi
"""
import click
from loguru import logger
from pathlib import Path
cwd=Path().cwd()
main_path = Path('/home/ziskin')
@click.command()
@click.option('--path', '-s', help='a full path to where the credential file is and the T02 report',
type=click.Path(exists=True), default=main_path)
def main_program(*args, **kwargs):
from pathlib import Path
path = Path(kwargs['path'])
email_alert_when_no_T02_files(path)
return
def format_df_to_string_with_breaks(df):
df = df.reset_index()
li = []
li.append([x for x in df.columns])
for i, row in df.iterrows():
li.append(row.tolist())
big_str = '\n'.join('{}' for _ in range(len(li))).format(*li)
return big_str
def email_alert_when_no_T02_files(path=cwd):
"""run this file daily and check last 6 hours of 'T02_file_count.csv',
if all empty (0) then send an email to Yuval"""
import pandas as pd
df = pd.read_csv(path / 'T02_file_count.csv')
if df.empty:
logger.warning('No files for at least the last 24 hours!')
msg = 'No T02 files for at least the last 24 hours from AXIS'
else:
df.set_index('dt', inplace=True)
if df.iloc[-6:]['no_files'].all():
logger.warning('No files for the last 6 hours!')
big_str = format_df_to_string_with_breaks(df)
msg = 'No T02 files for the last 6 hours from AXIS, see report below!'
msg ='\n'.join([msg,big_str])
else:
return df
# sender_email, passwd = read_gmail_creds(path)
rec_mails = ['[email protected]', '[email protected]']
# rec_mails = ['[email protected]']
subject = 'Geophysics1: AXIS TO2 lack of data'
for rec_mail in rec_mails:
send_gmail_using_shell(rec_mail, subject, msg)
# else:
# logger.info('No total lack of AXIS T02 files detected in the last 6 hours.')
return df
def read_gmail_creds(path=cwd, filename='.ariel.geophysics1.txt'):
with open(path/filename) as f:
mylist = f.read().splitlines()[0]
email = mylist.split(',')[0]
passwd = mylist.split(',')[1]
# print(email, passwd)
return email, passwd
# def send_gmail(sender_email, receiver_email, passwd, subject='', msg=''):
# import smtplib, ssl
# from email.mime.text import MIMEText
# from email.mime.multipart import MIMEMultipart
# # port = 465 # For SSL
# port = 465
# smtp_server = "smtp.gmail.com"
# # sender_email = sender_mail # Enter your address
# # receiver_email = rec_email # Enter receiver address
# # password = input("Type your password and press enter: ")
# # message = """\
# # Subject: Hi there
# #
# # This message is sent from Python."""
# message = MIMEMultipart("alternative")
# message["Subject"] = subject
# message["From"] = sender_email
# message["To"] = receiver_email
# part1 = MIMEText(msg, "plain")
# message.attach(part1)
# context = ssl.create_default_context()
# with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
# # server.ehlo() # Can be omitted
# # server.starttls(context=context)
# # server.ehlo() # Can be omitted
# server.login(sender_email, passwd)
# server.sendmail(sender_email, receiver_email, message.as_string())
# logger.info('email sent to {} from {}.'.format(receiver_email, sender_email))
# return
def send_gmail_using_shell(receiver_email, subject='', msg=''):
import subprocess
command = 'echo "{}" | mail -s "{}" {}'.format(msg, subject, receiver_email)
subprocess.call(command, shell=True)
logger.info('email sent to {}.'.format(receiver_email))
return
if __name__ == '__main__':
main_program()