forked from cms-tsg-fog/RateMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailAlert.py
executable file
·110 lines (98 loc) · 3.84 KB
/
mailAlert.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
#!/usr/bin/env python
import os
import smtplib
from email.mime.text import MIMEText
import time
import DatabaseParser
from datetime import datetime,timedelta
import sys
import subprocess
import pdb
sys.path.append('/nfshome0/hltpro/scripts')
emailList = ["[email protected]"]
#emailList = ["[email protected]", "[email protected]"]
#emailList = ["[email protected]"]
#emailList = ["[email protected]"]
def getLastRuns(h=24):
lastRun,isCol,isGood = DatabaseParser.GetLatestRunNumber()
curs = DatabaseParser.ConnectDB()
query ="""SELECT A.RUNNUMBER,B.STARTTIME, B.STOPTIME,B.TRIGGERS
FROM CMS_RUNINFO.RUNNUMBERTBL A, CMS_WBM.RUNSUMMARY B
WHERE A.RUNNUMBER=B.RUNNUMBER AND B.TRIGGERS>100 AND A.RUNNUMBER > %d-1000""" % (lastRun,)
#query = query +repr(datetime.now()+timedelta(days=-1))
curs.execute(query)
runs = []
past = datetime.now()+timedelta(hours=-h)
for r,starttime,stoptime,trig in curs.fetchall():
if not stoptime or stoptime > past:
runs.append((r,trig,stoptime))
return runs
def digest(hours,maxRate=35,printAll=False):
isBadRun=False
text=""
runs = getLastRuns(hours)
for run,nTrig,time in runs:
run,isCol,isGood = DatabaseParser.GetLatestRunNumber(run)
runParser = DatabaseParser.DatabaseParser()
runParser.RunNumber = run
runParser.ParseRunSetup()
#lumiRange = runParser.GetLSRange(0,99999,isCol)
expressRates = {}
if isCol:
expressRates = runParser.GetTriggerRatesByLS("ExpressOutput")
else:
expressRates = runParser.GetTriggerRatesByLS("HLTriggerFinalPath") #ExpressCosmicsOutput
ExpRate = 0
if len(expressRates.values())>0:
ExpRate = sum(expressRates.values())/len(expressRates.values())
#for ls in lumiRange:
# ExpRate+=expressRates.get(ls,0)
#ExpRate/=len(lumiRange)
if ExpRate > maxRate or printAll:
text=text+"%s Run %d: %d Triggers, Average Express Rate %0.1f Hz\n" %(str(time),run,nTrig,ExpRate,)
if ExpRate > maxRate:
isBadRun = True
try:
text = text+" >> Processed Runs: %d-%d\n" % (runs[0][0],runs[-1][0],)
except:
text = text+" >> No Runs in last %d hours" % (hours,)
return isBadRun,text
def sendMail(email,subject,to,fro,msgtxt):
msg = MIMEText(msgtxt)
msg['Subject'] = subject
msg['From'] = fro
msg['To'] = to
s = smtplib.SMTP('localhost')
#s.sendmail("[email protected]", email, msg.as_string())
s.sendmail(email, email, msg.as_string())
s.quit()
def sendAudio(text):
try:
server = "cmsdaqweb.cms"
port="50555"
pline = subprocess.Popen(["echo", "<alarm sender=\"HLT\" talk=\"HLT goes mad!\" sound=\"StravinskiDansesAsolecentes.wav\" >"+text+"</alarm>"], stdout=subprocess.PIPE)
pnc = subprocess.Popen( ["nc", "cmsdaqweb.cms", "50555"], stdin=pline.stdout, stdout=subprocess.PIPE )
playps = pnc.communicate()[0]
print playps
except:
print "Failed to send audio alarm:", text
def audioAlert():
try: sendAudio("PLEASE CHECK TRIGGER RATES")
except: print "failed audio alarm call..."
def mailAlert(text):
try:
for email in emailList:
sendMail(email,"[HLTRateMon] Trigger Rate Warning", "HLT", "HLT", text)
print "Mail sent to:", email
except:
print "Failed to send mail"
print text
if __name__=='__main__':
isBad,text = digest(1)
sendMail("[email protected]","[HLTRateMonDebug] Express Rate Digest","HLTDebug","HLTDebug",text)
sendMail("[email protected]","[HLTRateMonDebug] Express Rate Digest","HLTDebug","HLTDebug",text)
try:
if isBad:
for email in emailList.emailList: sendMail(email,"[HLTRateMon] Express Rate Digest","HLT","HLT",text)
except:
print text