def check_heartbeat(): global sent_email heartbeats = [] with open("heartbeats.txt", "r") as infile: lines = infile.readlines() for line in lines: timestamp = line.strip().split(".")[0] date_str, time_str = timestamp.split("T") year_str, mon_str, day_str = date_str.split("-") year, mon, day = int(year_str), int(mon_str), int(day_str) hour_str, min_str, sec_str = time_str.split(":") hour, min, sec = int(hour_str), int(min_str), int(sec_str) heartbeat = datetime.datetime(year, mon, day, hour, min, sec) heartbeats.append(heartbeat) print "heartbeats len:", len(heartbeats) for index in range(1, len(heartbeats)): delta = (heartbeats[index] - heartbeats[index - 1]).seconds if delta < 250: print "heartbeat_interval:", str(delta / 60) + " min, ", str(delta % 60) + " sec" if delta < 60: if not sent_email: print "Sending email..." DLCLibs.send_mail( subject="heartbeat less than 60 sec...", body="heartbeat lessn than 60 sec... at {0}".format(cur_time), ) sent_email = True
with open(os.devnull, "w") as nullfile: global sent_email while True: print "\n" * 3 cur_time = time.strftime("%Y:%m:%d %H:%M:%S") print "current time: {0}".format(cur_time) ret = subprocess.call(["grep", search_str, "dlc_1.log"], stdout=nullfile) # if there is search string, ret = 0 if not ret: print "Found search str:{0}".format(search_str) # if email was not sent already, send now... if not sent_email: print "Sending email..." DLCLibs.send_mail(subject="wifi reset occured...", body="wifi reset happened at {0}".format(cur_time)) sent_email = True else: print "No trouble found" """ timestamp = datetime.datetime.now().time() if heartbeat_start < timestamp and heartbeat_end > timestamp: print 'Send heartbeat...' DLCLibs.send_mail(subject='wifi heartbeat', body = 'heartbeat') """ time.sleep(1) with open("heartbeats.txt", "w") as heartbeatfile: subprocess.call(["grep", "HeartBeat response received", "dlc_1.log"], stdout=heartbeatfile)