#!/usr/bin/python import smtplib import mimetypes import email import email.mime.application import time import glob import MySQLdb import os import sys path = '/opt/sensiplicity' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/SS-L1.conf') # MySQL Connection db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() def hostname_sql(): host = "TempberryPi" cursor.execute("SELECT value FROM sensors_system WHERE name = 'server_hostname'") row = cursor.fetchone() if row: host = row[0] return host hostname = hostname_sql() # Create a text/plain message msg = email.mime.Multipart.MIMEMultipart() msg['Subject'] = ''+ hostname +' (SS-L1) Logger Error!' msg['From'] = data.send_address msg['To'] = data.recv_address # find the path of a sensor directory that starts with 28 i = 1 devicelist = glob.glob('/sys/bus/w1/devices/*/w1_slave') send_out_email = "no" all_sensor_data = '' def temp_status(): # Select qSQL with id=4. server_temperature = "Fahrenheit" cursor.execute("SELECT value FROM sensors_system WHERE name = 'server_temperature'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: server_temperature = row[0] return server_temperature server_temperature = temp_status() def email_status(): # Select qSQL with id=4. smtp_state = "on" cursor.execute("SELECT value FROM sensors_system WHERE name = 'smtp_state'") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: smtp_state = row[0] return smtp_state smtp_state = email_status() if (smtp_state == "off"): sys.exit() for sensor in devicelist: sensor_file = sensor sensor_id = sensor_file.split('/') # Select qSQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"'") row = cursor.fetchone() if row: if (row[4] == "on" and row[5] == "yes" and (row[10] != "" and row[11] != "") or (row[12] != "" and row[13] != "")): sensor_data = '' # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. tfile = open(sensor_file) # Read all of the text in the file. text = tfile.read() # Close the file now that the text has been read. tfile.close() # Split the text with new lines (\n) and select the second line. secondline = text.split("\n")[1] # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). temperaturedata = secondline.split(" ")[9] # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. temperature = float(temperaturedata[2:]) # Put the decimal point in the right place and display it. ctemperature = temperature / 1000 ftemperature = (temperature/1000 + 0) * 9/5 + 32 main_temp = ctemperature if (server_temperature == 'Fahrenheit'): main_temp = ftemperature name = row[1] if (row[3] != ''): name = row[3] low = row[10] if (row[12] != ''): low = row[12] high = row[11] if (row[13] != ''): high = row[13] if main_temp > float(high): sensor_data = "==========================================================================\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "= The sensor is located at %s" % sensor + "/w1_slave\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + '= The sensor "'+ name +'" is OVER TEMP!!!\n' sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "= Current Tempurature is: "+ str(main_temp) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= Low Tempurature Setting: "+ str(low) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= High Tempurature Setting: "+ str(high) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "==========================================================================\n" sensor_data = sensor_data + " \n" send_out_email = "yes" elif main_temp < float(low): sensor_data = "==========================================================================\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "= The sensor is located at %s" % sensor + "/w1_slave\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + '= The sensor "'+ name +'" is UNDER TEMP!!!\n' sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "= Current Tempurature is: "+ str(main_temp) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= Low Tempurature Setting: "+ str(low) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= High Tempurature Setting: "+ str(high) +" "+ str(server_temperature)+"\n" sensor_data = sensor_data + "= \n" sensor_data = sensor_data + "==========================================================================\n" sensor_data = sensor_data + " \n" send_out_email = "yes" all_sensor_data = sensor_data + all_sensor_data print all_sensor_data if send_out_email == "yes": # The main body is just another attachment #body_text = """There is an error on the TempberryPi (SS-L1) Logger.\n\n""" + all_sensor_data body_text = all_sensor_data #body = email.mime.Text.MIMEText("""Pi""") body = email.mime.Text.MIMEText(body_text) msg.attach(body) # send via Gmail server # port 25 packets to be port 587 and it is trashing port 587 packets. # So, I use the default port 25, but I authenticate. s = smtplib.SMTP(data.smtp_server + ':' + data.smtp_port) s.starttls() s.login(data.smtp_login,data.smtp_pass) s.sendmail(data.recv_address,[data.recv_address], msg.as_string()) s.quit() os.system("/bin/rm ./c")