-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_data_alert.py
executable file
·195 lines (170 loc) · 6.38 KB
/
no_data_alert.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Importamos smtplib
import smtplib
import MySQLdb
from lxml import etree
from datetime import *
from email.mime.text import MIMEText
MAIN_HOST = ''
REPL_HOST = ''
USER = ''
PASS = ''
DB_1 = ''
DB_2 = ''
DB_3 = ''
message = ''
msg = MIMEText("Contenido del e-mail a enviar")
recipients = ['[email protected]', '[email protected]']
msg['Subject'] = 'Alerta Base de Datos'
msg['From'] = '[email protected]'
msg['To'] = ", ".join(recipients)
print msg
#Comprobación de tablas
db = MySQLdb.connect(host = MAIN_HOST, user = USER, passwd = PASS, db = DB_1)
cursor = db.cursor()
#AMT
print 'Comprobando AMT en 87.'
sql = 'SELECT date FROM AMT WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM AMT ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'CdP: No entran datos en AMT - platform db [Ultimo dato: '+ str(result[0][0]) +']\n'
#CNR2
print 'Comprobando CNR2 en 87.'
sql = 'SELECT date FROM CNR2 WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM CNR2 ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'CdP: No entran datos en CNR2 - platform db [Ultimo dato: '+ str(result[0][0]) +']\n'
#SAM_825E
print 'Comprobando Radiómetros en 87.'
sql = 'SELECT date FROM SAM_825E WHERE date > DATE_SUB(NOW(), INTERVAL 7 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM SAM_825E ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'CdP: No entran datos en Radiómetros - platform db [Ultimo dato: '+ str(result[0][0]) +']\n'
#Vaisala
print 'Comprobando meteo en 87.'
sql = 'SELECT date FROM VaisalaWind WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM VaisalaWind ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'CdP: No entran datos en Meteorológicos - platform db [Ultimo dato: '+ str(result[0][0]) +']\n'
#PROPS
print 'Comprobando PROPS en 87.'
sql = 'SELECT date FROM PROPS WHERE date > DATE_SUB(NOW(), INTERVAL 9 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM PROPS ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'CdP: No entran datos en PROPS - platform db [Ultimo dato: '+ str(result[0][0]) +']\n'
##processed
db = MySQLdb.connect(host = MAIN_HOST, user = USER, passwd = PASS, db = DB_2)
cursor = db.cursor()
#AMT
print 'Comprobando AMT - processed en 87.'
sql = 'SELECT date FROM AMT WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
message = message + 'CdP: Fallo platform - processed en AMT\n'
#SAM_825E
print 'Comprobando Radiómetros - processed en 87.'
sql = 'SELECT date FROM SAM_825E WHERE date > DATE_SUB(NOW(), INTERVAL 8 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
message = message + 'CdP: Fallo platform - processed en Radiometros\n'
#PROPS
print 'Comprobando PROPS - processed en 87.'
sql = 'SELECT date FROM PROPS WHERE date > DATE_SUB(NOW(), INTERVAL 9 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
message = message + 'CdP: Fallo platform - processed en PROPS\n'
#Ficocianinas
print 'Comprobando AMT - processed en 87.'
sql = 'SELECT date FROM mFlu_blue WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR) and value > 12'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)>0:
message = message + 'CdP: ALERTA!! - Valor Ficocianinas > 12\n'
#Master - Slave
print 'Comprobando sincronización'
db = MySQLdb.connect(host = REPL_HOST, user = 'webuser', passwd = 'webuserpass', db = DB_2)
cursor = db.cursor()
sql = 'SELECT date FROM AMT WHERE date > DATE_SUB(NOW(), INTERVAL 6 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
message = message + 'CdP: Fallo master - slave\n'
db.close();
#Oxigeno Cogotas
#Comprobación de tablas
db = MySQLdb.connect(host = REPL_HOST, user = USER, passwd = PASS, db = DB_3)
cursor = db.cursor()
print 'Comprobando Oxígeno en Cogotas - Server: doriiie02'
sql = 'SELECT date FROM AMT WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR) and do < 4'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)>0:
message = message + 'Cogotas: ALERTA!! - Valor Oxígeno < 4\n'
#AMT
print 'Comprobando AMT en Cogotas'
sql = 'SELECT date FROM AMT WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM AMT ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'Cogotas: No entran datos en AMT - [Ultimo dato: '+ str(result[0][0]) +']\n'
#mFlu_blue
print 'Comprobando mFlu_blue en cogotas'
sql = 'SELECT date FROM mFlu_blue WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM mFlu_blue ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'Cogotas: No entran datos en Fluorímetro (blue) - [Ultimo dato: '+ str(result[0][0]) +']\n'
#mFlu_chl
print 'Comprobando mFlu_blue en cogotas'
sql = 'SELECT date FROM mFlu_chl WHERE date > DATE_SUB(NOW(), INTERVAL 4 HOUR)'
cursor.execute(sql)
result = cursor.fetchall()
if len(result)<1:
sql = 'SELECT date FROM mFlu_chl ORDER BY date DESC LIMIT 1'
cursor.execute(sql)
result = cursor.fetchall()
message = message + 'Cogotas: No entran datos en Fluorímetro (chl) - [Ultimo dato: '+ str(result[0][0]) +']\n'
# Enviamos si hay algun fallo
if len(message)>0:
# Autenticamos
mailServer = smtplib.SMTP('smtp.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login("[email protected]","Sorianos")
print 'Enviando correo electrónico'
msg = MIMEText("El sistema de monitorizacion CdP - Cogotas ha detectado los siguientes fallos: \n\n" + message)
mailServer.sendmail(msg["From"], recipients, msg.as_string())
else:
print 'Correo electrónico no enviado - Status OK'
mailServer.close()