| 6919 |
kshitij.so |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
Alerts regarding inventory update.
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
import sys
|
|
|
8 |
import MySQLdb
|
|
|
9 |
import optparse
|
|
|
10 |
import time
|
|
|
11 |
import datetime
|
|
|
12 |
import smtplib
|
|
|
13 |
from email.mime.text import MIMEText
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
parser = optparse.OptionParser()
|
|
|
17 |
usage = "Usage: %prog -H [DB Address]"
|
|
|
18 |
parser.add_option("-H", "--host", dest="host",default="",type="string",help="Database Address",metavar="HOST")
|
|
|
19 |
(options, args) = parser.parse_args()
|
|
|
20 |
if ( not(options.host)):
|
|
|
21 |
print "Fatal: Required arguments are missing!"
|
|
|
22 |
print "Use: -h / --help to get help."
|
|
|
23 |
sys.exit(1)
|
|
|
24 |
db = MySQLdb.connect(options.host,"root","shop2020","inventory" )
|
|
|
25 |
now = datetime.datetime.now()
|
|
|
26 |
cursor = db.cursor()
|
|
|
27 |
MESSAGE=[]
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
def getUpdate(warehouseName,warehouseId,checkInterval):
|
|
|
31 |
|
|
|
32 |
try:
|
|
|
33 |
print "Warehouse Name:",warehouseName
|
|
|
34 |
print "Warehouse Id:",warehouseId
|
|
|
35 |
print "Time Period:",checkInterval
|
|
|
36 |
print str(now)
|
|
|
37 |
sql = '''SELECT lastCheckedOn FROM warehouse where id=%d''' % \
|
|
|
38 |
(warehouseId)
|
|
|
39 |
cursor.execute(sql)
|
|
|
40 |
data = cursor.fetchone()
|
|
|
41 |
print "Data [0]:",data[0]
|
|
|
42 |
if (data[0] is None):
|
|
|
43 |
print "Inside None if"
|
|
|
44 |
mailText = 'Last Checked On is NULL for '+warehouseName+" Id = "+str(warehouseId)+"\n\n"
|
|
|
45 |
print mailText
|
|
|
46 |
MESSAGE.append(mailText)
|
|
|
47 |
else:
|
|
|
48 |
diffMinutes =int((time.mktime(now.timetuple()) - time.mktime(data[0].timetuple()))/60)
|
|
|
49 |
print "Diff",diffMinutes
|
|
|
50 |
if ( diffMinutes > checkInterval):
|
|
|
51 |
print "Diffcheck"
|
|
|
52 |
mailText = "Warehouse "+warehouseName+" Id = "+str(warehouseId)+" was last checked "+str(diffMinutes)+" minutes ago while its interval is "+str(checkInterval)+" minutes"+"\n\n"
|
|
|
53 |
print mailText
|
|
|
54 |
MESSAGE.append(mailText)
|
|
|
55 |
print "checking timediff"
|
|
|
56 |
|
|
|
57 |
except:
|
|
|
58 |
"MYSQL Exception"
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
def checkWarehouseHoliday(warehouseHolidays):
|
|
|
62 |
currentDay=str(now.weekday())
|
|
|
63 |
print "Current Day",currentDay
|
|
|
64 |
print "WarehouseHoliday",warehouseHolidays
|
|
|
65 |
if currentDay in warehouseHolidays:
|
|
|
66 |
return True
|
|
|
67 |
else:
|
|
|
68 |
return False
|
|
|
69 |
|
|
|
70 |
def sendMail():
|
|
|
71 |
|
|
|
72 |
print MESSAGE
|
|
|
73 |
msg = ''.join(MESSAGE)
|
|
|
74 |
smtpServer = smtplib.SMTP('localhost')
|
|
|
75 |
smtpServer.set_debuglevel(1)
|
|
|
76 |
msg = MIMEText(msg)
|
|
|
77 |
sender = 'inventory-monitor@shop2020.in'
|
|
|
78 |
recipients = ['kshitij.sood@shop2020.in', 'amar.kumar@shop2020.in']
|
|
|
79 |
msg['Subject'] = "Warehouse Inventory Monitor"
|
|
|
80 |
msg['From'] = sender
|
|
|
81 |
msg['To'] = ", ".join(recipients)
|
|
|
82 |
|
|
|
83 |
try:
|
|
|
84 |
smtpServer.sendmail(sender, recipients, msg.as_string())
|
|
|
85 |
print "Successfully sent email"
|
|
|
86 |
|
|
|
87 |
except:
|
|
|
88 |
print "Error: unable to send email"
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
def main():
|
|
|
92 |
try:
|
|
|
93 |
sql = '''select * from warehouseMonitor'''
|
|
|
94 |
cursor.execute(sql)
|
|
|
95 |
listWarehouse = cursor.fetchall()
|
|
|
96 |
for warehouse in listWarehouse:
|
|
|
97 |
if ( checkWarehouseHoliday(str(warehouse[3])) ):
|
|
|
98 |
continue
|
|
|
99 |
if ( warehouse[4]==1 ):
|
|
|
100 |
getUpdate(warehouse[1],warehouse[2],warehouse[5])
|
|
|
101 |
if (MESSAGE[0] is not None):
|
|
|
102 |
sendMail()
|
|
|
103 |
|
|
|
104 |
except:
|
|
|
105 |
"MYSQL Exception"
|
|
|
106 |
|
|
|
107 |
finally:
|
|
|
108 |
print "Closing db connection"
|
|
|
109 |
db.close()
|
|
|
110 |
|
|
|
111 |
if __name__ == "__main__":
|
|
|
112 |
main()
|