def sendAlertEmail(self, subject, msg): from xeniatools.utils import smtpClass smtp = smtpClass(self.smtpServer, self.emailUser, self.emailPwd) smtp.from_addr("%s@%s"%(self.emailUser,self.smtpServer)) smtp.rcpt_to(self.rcptList) smtp.subject(subject) smtp.message(msg) smtp.send()
emailServerDict = {} config = ConfigParser.RawConfigParser() config.read(options.templateFile) emailServer = config.items("EmailServer") for entry in emailServer: key = entry[0] emailServerDict[key] = entry[1] recipients = config.get("To", "Recipients") recipients = recipients.split(",") messageNfoDict = {} messageNfo = config.items("Email") for entry in messageNfo: key = entry[0] messageNfoDict[key] = entry[1] smtp = smtpClass(emailServerDict['server'], emailServerDict['from'], emailServerDict['pwd']) messageNfoDict['attachments'] = messageNfoDict['attachments'] % ( subsDict) fileAttachemnts = messageNfoDict['attachments'].split(",") for filePath in fileAttachemnts: filePath = filePath.replace('"', '') if (logger != None): logger.debug("Attaching file: %s" % (filePath)) smtp.attach(filePath) smtp.from_addr("%s@%s" % (emailServerDict['from'], emailServerDict['server'])) if (logger != None): logger.debug("Recipients %s" % (recipients)) smtp.rcpt_to(recipients)
"If set, this will send an email for any test result, not just one that does not pass percentage test." ) (options, args) = parser.parse_args() toList = [] if (options.emailList == None or len(options.emailList) == 0): print("No email addresses provided to send an alert to.") else: toList = options.emailList.split(',') sendEmail = False if (options.sendAnyResult): sendEmail = True subject = "[secoora_auto_alert]Free Disk Space " smtp = smtpClass(options.smtpServer, options.emailUser, options.emailPwd) smtp.from_addr("%s@%s" % (options.emailUser, options.smtpServer)) dirList = options.checkList.split(',') msg = "" testFail = False for entry in dirList: parts = entry.split(';') print("Testing directory: \"%s\", test percentage: %d" % (parts[0], int(parts[1]))) stats = os.statvfs(parts[0]) freespace = (stats.f_bavail * stats.f_frsize) / 1024 totaldiskspace = (stats.f_blocks * stats.f_frsize) / 1024 percentFree = (freespace / float(totaldiskspace)) * 100.0 if (len(msg)): msg += "\n\n"
def checkSensorActivity(xeniaDb, hoursToLookback, emailList, outFilename, emailUser, emailPwd): try: print("Opening file: %s" %(outFilename)) outFile = open(outFilename, "w") if(hoursToLookback != None): outFile.write("Sensor activity check for the past %d hours.\n" %(hoursToLookback)) else: outFile.write("Sensor activity check for the entire record set in multi_obs.\n") platNfo = recursivedefaultdict() sql = "SELECT sensor.row_id AS row_id, sensor.active AS active, \ obs_type.standard_name as obs_type_standard_name,\ platform.platform_handle as platform_handle\ FROM sensor\ LEFT JOIN m_type on sensor.m_type_id = m_type.row_id\ LEFT JOIN m_scalar_type on m_scalar_type.row_id = m_type.m_scalar_type_id\ LEFT JOIN obs_type on obs_type.row_id = m_scalar_type.obs_type_id\ LEFT JOIN platform on sensor.platform_id = platform.row_id\ WHERE platform.active=1 ORDER BY sensor.row_id ASC;" dbCursor = xeniaDb.dbConnection.executeQuery(sql) if(dbCursor != None): for row in dbCursor: sensorId = row['row_id'] platNfo[sensorId]['platform_handle'] = row['platform_handle'] platNfo[sensorId]['obsName'] = row['obs_type_standard_name'] platNfo[sensorId]['active'] = row['active'] dbCursor.close() else: msg = "Error: %s" %(xeniaDb.dbConnection.getErrorInfo()) print(msg) outFile.write(msg) sys.exit(-1) reActivatedPlatforms = {} where = "" if(hoursToLookback != None): where = "WHERE m_date >= now() - interval '%d hours'" %(hoursToLookback) #Get all the DISTINCT sensor ids, then we'll mark them as active. sql = "SELECT DISTINCT(sensor_id) AS sensor_id,platform_handle,\ obs_type.standard_name as obs_type_standard_name\ FROM multi_obs\ LEFT JOIN m_type on multi_obs.m_type_id = m_type.row_id\ LEFT JOIN m_scalar_type on m_scalar_type.row_id = m_type.m_scalar_type_id\ LEFT JOIN obs_type on obs_type.row_id = m_scalar_type.obs_type_id %s;" %(where) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if(dbCursor != None): for row in dbCursor: sensorId = row['sensor_id'] if(platNfo.has_key(sensorId)): active = platNfo[sensorId]['active'] if(active != 1): platform_handle = platNfo[sensorId]['platform_handle'] obsName = platNfo[sensorId]['obsName'] msg = 'Platform: %s Sensor: %s(%d) inactive sensor now reporting.'\ %(platform_handle,obsName,sensorId) print(msg) outFile.write("%s\n" %(msg)) del platNfo[sensorId] else: platformHandle = row['platform_handle'] print("Platform: %s Sensor ID: %d not present in current active platform/sensor list. Added to re-activate list." %(platformHandle,sensorId)) if(reActivatedPlatforms.has_key(platformHandle) == False): reActivatedPlatforms[platformHandle] = [] info = {} info['sensorId'] = sensorId info['obsName'] = row['obs_type_standard_name'] reActivatedPlatforms[platformHandle].append(info) dbCursor.close() #If we had sensors come alive for platforms that were inactive, we re-activate the platforms and the sensors. if(len(reActivatedPlatforms)): print("Preparing to re-active Platforms and Sensors that have become active.") outFile.write("Reactivating Platforms and Sensors that have become active.\n") for platform in reActivatedPlatforms: sql = "UPDATE platform SET active=1 WHERE platform_handle='%s'" %(platform) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if(dbCursor != None): msg = "Platform: %s" %(platform) print(msg + " set active to 1.") outFile.write("%s\n" %(msg)) xeniaDb.dbConnection.commit() dbCursor.close() #sensorList = reActivatedPlatforms[platform] for info in reActivatedPlatforms[platform]: sql = "UPDATE sensor SET active=1 WHERE row_id=%d" %(info['sensorId']) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if(dbCursor != None): msg = "Platform: %s Sensor: %s(%d)" %(platform, info['obsName'], info['sensorId']) print(msg + " set active to 1.") outFile.write("%s\n" %(msg)) xeniaDb.dbConnection.commit() dbCursor.close() else: print("Error: %s" %(xeniaDb.dbConnection.getErrorInfo())) else: print("Error: %s" %(xeniaDb.dbConnection.getErrorInfo())) if(len(platNfo)): outFile.write("The following details show platforms and sensors that are marked as active but did not report.\n") for sensorId in platNfo: msg = "Platform: %s Sensor: %s(%d)" %(platNfo[sensorId]['platform_handle'],platNfo[sensorId]['obsName'],sensorId) print(msg + " did not report observations.") outFile.write("%s\n" %(msg)) outFile.close() smtp = smtpClass("inlet.geol.sc.edu", emailUser, emailPwd) smtp.subject('[secoora_auto_alert] Check Sensor Results') smtp.message('Attached is the latest run results for the sensorTableUtils script.') smtp.from_addr('*****@*****.**') smtp.rcpt_to(emailList) smtp.attach(outFilename) smtp.send() else: print("Error: %s" %(xeniaDb.dbConnection.getErrorInfo())) sys.exit(-1) except Exception, e: import traceback print(traceback.print_exc())
def checkSensorActivity(xeniaDb, hoursToLookback, emailList, outFilename, emailUser, emailPwd): try: print("Opening file: %s" % (outFilename)) outFile = open(outFilename, "w") if (hoursToLookback != None): outFile.write("Sensor activity check for the past %d hours.\n" % (hoursToLookback)) else: outFile.write( "Sensor activity check for the entire record set in multi_obs.\n" ) platNfo = recursivedefaultdict() sql = "SELECT sensor.row_id AS row_id, sensor.active AS active, \ obs_type.standard_name as obs_type_standard_name,\ platform.platform_handle as platform_handle\ FROM sensor\ LEFT JOIN m_type on sensor.m_type_id = m_type.row_id\ LEFT JOIN m_scalar_type on m_scalar_type.row_id = m_type.m_scalar_type_id\ LEFT JOIN obs_type on obs_type.row_id = m_scalar_type.obs_type_id\ LEFT JOIN platform on sensor.platform_id = platform.row_id\ WHERE platform.active=1 ORDER BY sensor.row_id ASC;" dbCursor = xeniaDb.dbConnection.executeQuery(sql) if (dbCursor != None): for row in dbCursor: sensorId = row['row_id'] platNfo[sensorId]['platform_handle'] = row['platform_handle'] platNfo[sensorId]['obsName'] = row['obs_type_standard_name'] platNfo[sensorId]['active'] = row['active'] dbCursor.close() else: msg = "Error: %s" % (xeniaDb.dbConnection.getErrorInfo()) print(msg) outFile.write(msg) sys.exit(-1) reActivatedPlatforms = {} where = "" if (hoursToLookback != None): where = "WHERE m_date >= now() - interval '%d hours'" % ( hoursToLookback) #Get all the DISTINCT sensor ids, then we'll mark them as active. sql = "SELECT DISTINCT(sensor_id) AS sensor_id,platform_handle,\ obs_type.standard_name as obs_type_standard_name\ FROM multi_obs\ LEFT JOIN m_type on multi_obs.m_type_id = m_type.row_id\ LEFT JOIN m_scalar_type on m_scalar_type.row_id = m_type.m_scalar_type_id\ LEFT JOIN obs_type on obs_type.row_id = m_scalar_type.obs_type_id %s;" % ( where) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if (dbCursor != None): for row in dbCursor: sensorId = row['sensor_id'] if (platNfo.has_key(sensorId)): active = platNfo[sensorId]['active'] if (active != 1): platform_handle = platNfo[sensorId]['platform_handle'] obsName = platNfo[sensorId]['obsName'] msg = 'Platform: %s Sensor: %s(%d) inactive sensor now reporting.'\ %(platform_handle,obsName,sensorId) print(msg) outFile.write("%s\n" % (msg)) del platNfo[sensorId] else: platformHandle = row['platform_handle'] print( "Platform: %s Sensor ID: %d not present in current active platform/sensor list. Added to re-activate list." % (platformHandle, sensorId)) if (reActivatedPlatforms.has_key(platformHandle) == False): reActivatedPlatforms[platformHandle] = [] info = {} info['sensorId'] = sensorId info['obsName'] = row['obs_type_standard_name'] reActivatedPlatforms[platformHandle].append(info) dbCursor.close() #If we had sensors come alive for platforms that were inactive, we re-activate the platforms and the sensors. if (len(reActivatedPlatforms)): print( "Preparing to re-active Platforms and Sensors that have become active." ) outFile.write( "Reactivating Platforms and Sensors that have become active.\n" ) for platform in reActivatedPlatforms: sql = "UPDATE platform SET active=1 WHERE platform_handle='%s'" % ( platform) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if (dbCursor != None): msg = "Platform: %s" % (platform) print(msg + " set active to 1.") outFile.write("%s\n" % (msg)) xeniaDb.dbConnection.commit() dbCursor.close() #sensorList = reActivatedPlatforms[platform] for info in reActivatedPlatforms[platform]: sql = "UPDATE sensor SET active=1 WHERE row_id=%d" % ( info['sensorId']) dbCursor = xeniaDb.dbConnection.executeQuery(sql) if (dbCursor != None): msg = "Platform: %s Sensor: %s(%d)" % ( platform, info['obsName'], info['sensorId']) print(msg + " set active to 1.") outFile.write("%s\n" % (msg)) xeniaDb.dbConnection.commit() dbCursor.close() else: print("Error: %s" % (xeniaDb.dbConnection.getErrorInfo())) else: print("Error: %s" % (xeniaDb.dbConnection.getErrorInfo())) if (len(platNfo)): outFile.write( "The following details show platforms and sensors that are marked as active but did not report.\n" ) for sensorId in platNfo: msg = "Platform: %s Sensor: %s(%d)" % ( platNfo[sensorId]['platform_handle'], platNfo[sensorId]['obsName'], sensorId) print(msg + " did not report observations.") outFile.write("%s\n" % (msg)) outFile.close() smtp = smtpClass("inlet.geol.sc.edu", emailUser, emailPwd) smtp.subject('[secoora_auto_alert] Check Sensor Results') smtp.message( 'Attached is the latest run results for the sensorTableUtils script.' ) smtp.from_addr('*****@*****.**') smtp.rcpt_to(emailList) smtp.attach(outFilename) smtp.send() else: print("Error: %s" % (xeniaDb.dbConnection.getErrorInfo())) sys.exit(-1) except Exception, e: import traceback print(traceback.print_exc())
parser.add_option("-a", "--SendAnyResult", dest="sendAnyResult",action= 'store_true', help="If set, this will send an email for any test result, not just one that does not pass percentage test.") (options, args) = parser.parse_args() toList = [] if(options.emailList == None or len(options.emailList) == 0): print("No email addresses provided to send an alert to.") else: toList = options.emailList.split(',') sendEmail = False if(options.sendAnyResult): sendEmail = True subject = "[secoora_auto_alert]Free Disk Space " smtp = smtpClass(options.smtpServer, options.emailUser, options.emailPwd) smtp.from_addr("%s@%s"%(options.emailUser,options.smtpServer)) dirList = options.checkList.split(',') msg = "" testFail = False for entry in dirList: parts = entry.split(';') print("Testing directory: \"%s\", test percentage: %d" %(parts[0],int(parts[1]))) stats = os.statvfs(parts[0]) freespace = (stats.f_bavail * stats.f_frsize) / 1024 totaldiskspace = (stats.f_blocks * stats.f_frsize) / 1024 percentFree = (freespace / float(totaldiskspace)) * 100.0 if(len(msg)): msg += "\n\n" if(percentFree < int(parts[1])):
emailServerDict = {} config = ConfigParser.RawConfigParser() config.read(options.templateFile) emailServer = config.items("EmailServer") for entry in emailServer: key = entry[0] emailServerDict[key] = entry[1] recipients = config.get("To", "Recipients") recipients = recipients.split(",") messageNfoDict = {} messageNfo = config.items("Email") for entry in messageNfo: key = entry[0] messageNfoDict[key] = entry[1] smtp = smtpClass(emailServerDict['server'], emailServerDict['from'], emailServerDict['pwd']) messageNfoDict['attachments'] = messageNfoDict['attachments'] % (subsDict) fileAttachemnts = messageNfoDict['attachments'].split(",") for filePath in fileAttachemnts: filePath = filePath.replace('"', '') if(logger != None): logger.debug("Attaching file: %s" %(filePath)) smtp.attach(filePath) smtp.from_addr("%s@%s" % (emailServerDict['from'],emailServerDict['server'])) if(logger != None): logger.debug("Recipients %s" %(recipients)) smtp.rcpt_to(recipients) subject = messageNfoDict['subject'] % (subsDict)