Last active
July 12, 2018 07:53
-
-
Save kepsic/7a7b3f2be92942b585615765df650ec4 to your computer and use it in GitHub Desktop.
FlexBi import monitor script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import MySQLdb | |
import datetime | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
current_date = datetime.datetime.now() | |
sender_email = "flexbi@email.cc" | |
notify_emails = "some@email.cc" | |
delay_hours = 24 | |
def send_notify(smtp_rcpt=notify_emails,smtp_from=sender_email,smtp_subject='ZVV FlexBI Error with account %s', **kwargs): | |
# Create message container - the correct MIME type is multipart/alternative. | |
msg = MIMEMultipart('alternative') | |
msg['Subject'] = smtp_subject % (kwargs['account_id']) | |
msg['From'] = smtp_from | |
msg['To'] = smtp_rcpt | |
# Create the body of the message (a plain-text and an HTML version). | |
text = "Hi!\n FlexBi have a problem with account %s! \n Last successful import was %s! \n" % (kwargs['account_id'],kwargs['last_import_at']) | |
html = """\ | |
<html> | |
<head></head> | |
<body> | |
<p>Hi!<br> | |
How are you?<br> | |
We have problem with account %s<br> | |
Last import was more than 24h ago at <strong style="background-color:Tomato;">%s</strong> | |
</p> | |
</body> | |
</html> | |
""" % (kwargs['account_id'],kwargs['last_import_at']) | |
# Record the MIME types of both parts - text/plain and text/html. | |
part1 = MIMEText(text, 'plain') | |
part2 = MIMEText(html, 'html') | |
# Attach parts into message container. | |
# According to RFC 2046, the last part of a multipart message, in this case | |
# the HTML message, is best and preferred. | |
msg.attach(part1) | |
msg.attach(part2) | |
# Send the message via local SMTP server. | |
s = smtplib.SMTP('localhost') | |
# sendmail function takes 3 arguments: sender's address, recipient's address | |
# and message to send - here it is sent as one string. | |
s.sendmail(smtp_from, smtp_rcpt,msg.as_string()) | |
s.quit() | |
def main(): | |
# Open database connection | |
db = MySQLdb.connect("localhost","flexmon","SQLPASSWORD","flexbi_private" ) | |
# prepare a cursor object using cursor() method | |
cursor = db.cursor() | |
# execute SQL query using execute() method. | |
cursor.execute("select account_id,last_import_at, error_message,error_at,error_counter,application_type from source_applications") | |
# Fetch a single row using fetchone() method. | |
rows = cursor.fetchall() | |
for account_id,last_import_at, error_message,error_at,error_counter,application_type in rows: | |
diff = current_date - last_import_at | |
days, seconds = diff.days, diff.seconds | |
diff_hours = days * 24 + seconds // 3600 | |
diff_minutes = (seconds % 3600) // 60 | |
diff_seconds = seconds % 60 | |
#send notify if last import is older than delay_hours | |
if diff_hours > delay_hours: | |
send_notify(smtp_rcpt=notify_emails,account_id=account_id,last_import_at=last_import_at) | |
print account_id,last_import_at,error_message, error_at, error_counter, application_type | |
# disconnect from server | |
db.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment