Skip to content

Instantly share code, notes, and snippets.

@gadamc
Created June 4, 2011 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadamc/1007853 to your computer and use it in GitHub Desktop.
Save gadamc/1007853 to your computer and use it in GitHub Desktop.
scripts to monitor muon veto high voltage
#!/bin/bash
MuonVetoHVHome=/home/edelweiss/Programme/HV-cmd
export PYTHONSTARTUP=/etc/pythonstart
PYTHONPATH=/usr/local/bin/python
cd $MuonVetoHVHome && $PYTHONPATH ./muonvetohvlog.py
sleep 30
cd $MuonVetoHVHome && $PYTHONPATH ./muonvetohvemailwarning.py
#!/usr/bin/env python
from couchdbkit import Server, Database
import copy, os, sys, json, smtplib
def getWarnDocsNeedAttention():
vr = db.view('muonveto/hvlogwarning_notify', include_docs=True, reduce = False)
docs = list()
for row in vr:
docs.append(row['doc'])
return docs
def getPreviousWarnDoc():
vr = db.view('muonveto/hvlogwarning', descending = True, reduce = False)
for row in vr:
doc = db.get(row['id'])
if doc.has_key('notificationstatus'):
if doc['notificationstatus'] != 'none':
return doc
return None
def compare(docnew, docold):
'''
Compares the two HV warning documents, sends out a notification email if necessary
and returns the value of the notificationstatus that should be set in the docnew
'''
# print 'newdoc\n'
# print json.dumps(docnew, sort_keys = True, indent = 2)
if docold == None:
#print 'old doc is none'
emailwarning(docnew, docold)
return 'emailsent'
# print '\n olddoc\n'
# print json.dumps(docold, sort_keys = True, indent = 2)
if len(docnew['values']) != len(docold['values']):
#print 'size of values array has changed'
emailwarning(docnew, docold)
return 'emailsent'
chanlistnew = list()
chanlistold = list()
for i in range(len(docnew['values'])):
chanlistnew.append(docnew['values'][i]['channel'])
chanlistold.append(docold['values'][i]['channel'])
chanlistnew.sort()
chanlistold.sort()
set1 = set(chanlistnew)
set2 = set(chanlistold)
set3 = set1.symmetric_difference(set2)
if len(set3) > 0:
#print 'there is a different set of channels in the values array'
emailwarning(docnew, docold)
return 'emailsent'
return 'no email'
def emailwarning(doc, docold):
inhalt = '\n\nThe script muonvethvlog.py detected a change in the WARNING status of the muonvetoHV.\n\nSee the HV Warning Doc in the DB: ' + doc['_id'] + '\n\n'
inhalt += 'http://xxx.xxx.xxx.xxx:5984/_utils/document.html?' + db.dbname + '/' + doc['_id'] + '\n\n'
inhalt += 'The following channels are reported in the latest warning file.\n\n'
inhalt += ''.join([s.rjust(12) for s in \
('Channel', 'Actual', 'Demand', 'Saved', 'Backup')]) + '\n'
for item in doc['values']:
line = (item['channel'], item['actual'], item['demand'], item['saved'], item['backup'])
inhalt += ''.join([str(s).rjust(12) for s in line])+'\n'
if docold != None:
inhalt += '\n\nThe following channels were reported in the previous warning file.\n\n'
inhalt += ''.join([str(s).rjust(12) for s in \
('Channel', 'Actual', 'Demand', 'Saved', 'Backup')]) + '\n'
for item in docold['values']:
line = (item['channel'], item['actual'], item['demand'], item['saved'], item['backup'])
inhalt += ''.join([str(s).rjust(12) for s in line]) + '\n'
absender = 'noreply'
adressat = emailrecipients
betreff = 'Muon Veto High Voltage Warning'
zeit = 'time()'
text = 'From: '+absender+'\n'
text += 'To:' + ", ".join(dbf['sendEmailto']) + '\n'
text += 'Subject: '+betreff+'\n'
text = text + inhalt
# print text
server = smtplib.SMTP('localhost')
server.sendmail(absender,dbf['sendEmailto'],text)
server.quit()
def main(*args):
global s
global db
s = Server("http://xxx.xxx.xxx.xxx:5984")
db = s.get_or_create_db("muonhv")
global dbf
dbf = db['muonvetohvlog_configuration']
global emailrecipients
emailrecipients = '"' +str(dbf['sendEmailto'][0]) + '"'
for i in range(1, len(dbf['sendEmailto'])):
emailrecipients += ',"' + str(dbf['sendEmailto'][i]) + '"'
warndocs = getWarnDocsNeedAttention()
for doc in warndocs:
lastwarndoc = getPreviousWarnDoc()
doc['notificationstatus'] = compare(doc, lastwarndoc)
doc['_rev'] = db.get_rev(doc['_id'])
db.save_doc(doc)
if __name__ == '__main__':
main(*sys.argv[1:])
#!/usr/bin/env python
from couchdbkit import Server, Database, BulkSaveError
from time import strftime, time, sleep
import sys, os, subprocess, shlex, smtplib, copy, re
def formatHvValue(value):
if (isinstance(value,str)):
# #see if this string is really an int or a float
if 'Error' in value:
return value
else:
return int(value)
# if it is a int save it as int
return value
def getVoltages():
command = '/home/edelweiss/Programme/HV-cmd/hv-lecroy -c "R E C0DO256"' # reads the lecroy module
saved = open(str(dbf['actualHVfile']) , "r") #reads the actual .hvv file
savedvals = dict()
for li in saved:
if li[0:3] == 'chn':
savedvals[str(formatHvValue(li[4:7]))] = formatHvValue(li[9:])
saved.close()
args = shlex.split(command)
args[2] = '"R E C0DO256"'
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
listOfChans = list()
listOfChansEr = list()
for line in p.stdout:
if line[0] == 'C':
chandoc = dict()
chandoc['channel'] = formatHvValue(line[1:5])
chandoc['demand'] = formatHvValue(line[5:15])
chandoc['backup'] = formatHvValue(line[15:23])
chandoc['actual'] = formatHvValue(line[23:])
chandoc['saved'] = savedvals[str(chandoc['channel'])]
listOfChans.append(chandoc)
try:
if abs(chandoc['saved'] - chandoc['demand']) > int(dbf['dHV_saved_demand']) or \
abs(chandoc['demand'] - chandoc['actual']) > int(dbf['dHV_demand_actual']):
# print chandoc, 'Error'
listOfChansEr.append(chandoc)
except:
print 'there appears an error while comparing the saved values with the actual values'
theReturn = list()
theReturn.append(listOfChans)
theReturn.append(listOfChansEr)
return theReturn
def isThisRunning( process_name ): # tests if another program is running the lecroy module
ps = subprocess.Popen("ps -eaf", shell=True, stdout=subprocess.PIPE)
ps.wait()
output = ps.stdout.read()
if re.search(process_name, output) != None:
print 'found', process_name
ps.stdout.close()
return True
ps.stdout.close()
return False
return output
def main(*args):
try: # check if it is possible to connect to the CouchDB
s = Server("http://xxx.xxx.xxx.xxx:5984")
db = s.get_or_create_db("muonhv")
global dbf # is used by some other functions
dbf = db['muonvetohvlog_configuration']
except:
print 'can not connect to the Couch db'
return None
for i in dbf['proc_to_exit']:
print i
if isThisRunning(i) == True:
# if another program is running it quits
print 'found processing quiting'
sys.exit(-1)
doc = dict()
#creates the doc for the CouchDB
doc['_id'] = 'muonvetohighvoltage_' + str(time())
doc['type'] = 'muonvetohvlog'
doc['content'] = 'HV measurements of the LeCroy module'
doc['author'] = 'Demian Hartmann'
datedoc = dict()
datedoc['year'] = int(strftime("%Y"))
datedoc['month'] = int(strftime("%m"))
datedoc['day'] = int(strftime("%d"))
datedoc['hour'] = int(strftime("%H"))
datedoc['minute'] = int(strftime("%M"))
datedoc['second'] = int(strftime("%S"))
datedoc['unixtime'] = time()
doc['date_valid'] = datedoc
edoc = copy.deepcopy(doc)
volts = getVoltages()
if volts is None:
return None
#creates the warning doc
doc['values'] = volts[0]
edoc['values'] = volts[1]
edoc['type'] = 'muonvetohvwarninglogfile'
edoc['content'] = 'warnings of the HV LeCroy module, indicating a changed hv value'
edoc['_id'] = doc['_id'] + '.warningfile'
edoc['notificationstatus'] = 'none'
db.save_doc(doc)
# test size of error doc and then save
if len(edoc['values']) > 0:
db.save_doc(edoc)
else:
print 'no errors found'
if __name__=='__main__':
main(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment