#!/usr/bin/python#importamos librerias requeridasimport atomimport gdata.calendarimport gdata.calendar.serviceimport randomimport timeimport smtplibimport mathimport commandsimport loggingfrom email.mime.text import MIMEText#variables globalesLOGFILE = "checkServer.log"cpu_mail_level = 3cpu_sms_level = 5mem_mail_level = 2.5mem_sms_level = 0.5email = "******@gmail.com"email_password = "*******"#funcionesdef check_cpu(): fObj = open("/proc/loadavg","r") fData = fObj.readline() fObj.close() cpu_status = math.ceil(float(fData.split()[0])) return cpu_statusdef check_mem(): status,mTotal = commands.getstatusoutput("free -m | grep Mem | cut -f11 -d' '") status,mFree = commands.getstatusoutput("free -m | grep Mem | cut -f26 -d' '") mFree = math.ceil((float(mFree)*100)/float(mTotal)) return mFreedef is_saturated(): logging.basicConfig(filename=LOGFILE,level=logging.DEBUG,format="%(asctime)s - %(name)s::%(levelname)s %(message)s") cpu_level = check_cpu() mem_level = check_mem() print "Nivel de CPU: %s -- Nivel de Mem: %s" % (cpu_level,mem_level) print "Nivel mail CPU: %s -- Nivel sms CPU: %s" % (cpu_mail_level,cpu_sms_level) print "Nivel mail Mem: %s -- Nivel sms Mem: %s" % (mem_mail_level,mem_sms_level) if( (cpu_level >= cpu_mail_level) and (cpu_level < cpu_sms_level) ): sendmail(cpu_level,"CPU") elif(cpu_level >= cpu_sms_level): sendsms(cpu_level,"CPU") sendmail(cpu_level,"CPU") else: print "CPU funcionando correctamente." logging.debug("CPU: OK") if( (mem_level <= mem_mail_level) and (mem_level > mem_sms_level) ): sendmail(mem_level,"Memoria") elif(mem_level <= mem_sms_level): sendsms(mem_level,"Memoria") sendmail(mem_level,"Memoria") else: print "Memoria funcionando correctamente." logging.debug("Memory: OK")def sendmail(nstatus,by): sFrom = email sTo = email headers = ["From: "+sFrom,"Subject: Alerta PROLIANTG6 saturado.","To: "+sTo,"MIME-Version: 1.0","Content-Type: text/html"] headers = "\r\n".join(headers) msg = "El servidor PROLIANTG6 se encuentra al nivel %s de saturacion de %s." % (nstatus,by) sObj = smtplib.SMTP("smtp.gmail.com",587) sObj.ehlo() sObj.starttls() sObj.ehlo() sObj.login(sFrom,email_password) try: sObj.sendmail(sFrom,sTo,headers+"\r\n\r\n"+msg) except: print "No se pudo enviar el correo." else: print "Se envio el correo correctamente." logging.debug(by+" Email sent.") sObj.close()def sendsms(nstatus,by): cs = gdata.calendar.service.CalendarService() cs.email = email cs.password = email_password cs.source = "GoogleSMSCalender_" + str(random.randint(0, 10000)) cs.ProgrammaticLogin() event = gdata.calendar.CalendarEventEntry() event.title = atom.Title(text="Alerta PROLIANTG6 saturado.") texto = "El servidor PROLIANTG6 se encuentra al nivel %s de saturacion de %s." % (nstatus,by) event.content = atom.Content(text=texto) event.where.append(gdata.calendar.Where(value_string="Repuestos Gualsan")) start_time = time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime(time.time() + 2 * 60)) when = gdata.calendar.When(start_time=start_time, end_time=start_time) reminder = gdata.calendar.Reminder(minutes=1, extension_attributes={"method":"sms"}) when.reminder.append(reminder) event.when.append(when) try: new_event = cs.InsertEvent(event, "/calendar/feeds/default/private/full") except: print "No se pudo enviar el sms." else: print "SMS enviado correctamente." logging.debug(by+" SMS sent.")if __name__ == "__main__": is_saturated()