def SvcDoRun(self): tools.recordLog("Service start") time2 = 0 timeCha = 0 #执行扫描的时间差 mingzhongtime = '' while self.run: tools.recordLog("Server status " + gl.getvalue('version') + ' is running at ' + tools.get_time_stamp()) tools.recordLog("这是服务器启动后的开始") #gl.setvalue('count','0') sleepsecond = configRead.readConfig('parameter', 'sleepsecond') runtime = configRead.readConfig('parameter', 'runtime') print(sleepsecond) print(runtime) tools.recordLog(str(sleepsecond)) tools.recordLog(str(runtime)) currentTime = tools.get_hour() tools.recordLog(str(currentTime)) #开始按计划扫描后,plan=0,以错过本次的周期扫描。当plan还=1时,按定时间隔扫描。 plan = 1 time1 = tools.get_hour() for i in runtime: tools.recordLog(str(tools.time_cmp(currentTime, i))) tools.recordLog(str(i)) print("计划扫描时间" + i) print('时间差1:' + str(timeCha)) if tools.time_cmp(currentTime, i) < (int(sleepsecond) + int(timeCha)): tools.recordLog("计划扫描命中:" + i) print("计划扫描命中:" + i) print("上次命中计划:" + mingzhongtime) if i != mingzhongtime: print('currentTime=' + currentTime) print("上次命中计划:" + mingzhongtime) ServerHealth.health(plan) # 1 参数为计划扫描 plan = 0 mingzhongtime = i if plan == 1: print("周期扫描命中" + currentTime) tools.recordLog("周期扫描命中" + currentTime) ServerHealth.health(0) # 0 参数为周期扫描 time2 = tools.get_hour() timeCha = tools.time_cmp(time1, time2) print('时间差2:' + str(timeCha)) time.sleep(int(sleepsecond))
def listenWrite(words, questionnum=10): global kid global speak kid.startTime = tools.get_hour() if questionnum > len(words): tools.recordLog("出题数目大于单词总量") print("出题数目大于单词总量,将使用最大单词量") inputPress = '' tools.recordLog('') tools.recordLog('全新答题开始:') for i in words: kid.question = kid.question + 1 if kid.question > questionnum: quitgame(words) while inputPress != 'Q': print("\n第" + str(kid.question) + "题,请根据发音输入答案:", end='') speak.Speak("第" + str(kid.question) + "题") #time.sleep(1) try: playsound.playsound(i['mp3'], True) except Exception as err: tools.recordLog(str(err)) tools.recordLog("打开MP3出错") print("打开MP3出错") sys.exit(0) inputPress = input() inputPress = inputPress.strip().upper() if inputPress == i['word'].upper(): print('回答正确!') break elif inputPress == 'A' or inputPress == '': time.sleep(1) continue elif inputPress == 'S': print("正确的单词:" + i['word']) print(i['word'] + " 的含义: " + i['mean']) kid.wronglist.append(i['word']) kid.alarmTime = kid.alarmTime + 1 time.sleep(1) continue elif inputPress == 'Q': quitgame(words) else: tools.recordLog("第" + str(kid.question) + "题 " + i['word'] + ' 回答错误:' + inputPress) print(inputPress.lower() + '是错误的 ! 请再听一次') kid.wronglist.append(i['word']) print("该单词的含义:" + i['mean']) kid.question = kid.question + 1 quitgame(words)
def quitgame(words): global kid global speak kid.endTime = tools.get_hour() kid.wronglist = list(set(kid.wronglist)) #去除重复单词 wrongTime = len(kid.wronglist) rightTime = kid.question - wrongTime if kid.question == 1: print('\n亲爱的,你还没开始答题呢。要坚持哦!') sys.exit(0) print('得分 = ' + str(round((rightTime - 1) / (kid.question - 1) * 100, 0))) print('\n总共出题:' + str(kid.question - 1)) print('答对题目:' + str(rightTime - 1)) print('答错题目:' + str(wrongTime)) print('提示次数:' + str(kid.alarmTime)) kid.spendMinute = round(tools.time_cmp(kid.endTime, kid.startTime) / 60, 0) kid.spendSecond = tools.time_cmp(kid.endTime, kid.startTime) % 60 print('用时:' + str(kid.spendMinute) + '分' + str(kid.spendSecond) + '秒') speak.Speak('你总共得分:' + str(round((rightTime - 1) / (kid.question - 1) * 100, 0)) + '分') if len(kid.wronglist) > 0: print('\n以下是答错的单词') speak.Speak('你答错了下面的单词:') pprint.pprint(kid.wronglist) time.sleep(5) print('\n') rewrite(kid.wronglist, words) else: if (kid.question - 1) > 0: print('恭喜你全部正确!\n') speak.Speak('恭喜你全部正确!') tools.recordLog('得分 = ' + str(round((rightTime - 1) / (kid.question - 1) * 100, 0))) tools.recordLog('总共出题:' + str(kid.question - 1)) tools.recordLog('答对题目:' + str(rightTime - 1)) tools.recordLog('答错题目:' + str(wrongTime)) tools.recordLog('提示次数:' + str(kid.alarmTime)) tools.recordLog('用时:' + str(kid.spendMinute) + '分' + str(kid.spendSecond) + '秒') tools.recordLog('以下是答错的单词') tools.recordLog(str(kid.wronglist)) print('\n练习完毕,请输入回车退出') while len(input()) < 1: sys.exit(0)
def timeCheck(sendgroup, mydict): #sendgroup = configRead.readConfig('dingding','sendgroup') runTime = sendgroup[mydict][5] print(runTime) currentTime = tools.get_hour() print(currentTime) #在允许的时间段内扫描 if (currentTime >= runTime[0]) and (currentTime <= runTime[1]): print(mydict + "时间命中") tools.recordLog(mydict + "时间命中") return True else: print(mydict + "时间没命中") tools.recordLog(mydict + "时间没有命中") tools.recordLog("runTime0" + runTime[0]) tools.recordLog("runTime1" + runTime[1]) print("runTime0" + runTime[0]) print("runTime1" + runTime[1]) return False
def getSyncSeconds(list): #conn = pymssql.connect('183.111.122.191', 'sa', 'VTTthL5oLgyVckWdbd2EFw==', 'YT') conn = pymssql.connect(list[1], list[2], list[3], list[4]) cursor = conn.cursor() sqltext = r""" SELECT top 1 [CreateTime] FROM [dbo].[LotteryHistoryNumCache] order by [CreateTime] desc """ timeChayi = '0' try: cursor.execute(sqltext, 'timeout=6') row = cursor.fetchone() while row: currentTime = tools.get_hour() print(currentTime) thisTime = row[0].strftime('%H:%M:%S') print(thisTime) timeChayi = tools.time_cmp(currentTime, thisTime) print(timeChayi) row = cursor.fetchone() except Exception as err: print(str(err)) timeChayi = 'Down' finally: cursor.close() conn.close() return timeChayi
time_stamp = "%s.%03d" % (data_head, data_secs) return time_stamp def get_hour(): ct = time.time() local_time = time.localtime(ct) hourtime = time.strftime("%H:%M:%S", local_time) return hourtime def time_cmp(first_time, second_time): if first_time < second_time: first_time, second_time = second_time, first_time return (datetime.datetime.strptime(first_time, "%H:%M:%S") - datetime.datetime.strptime(second_time, "%H:%M:%S")).seconds sleepsecond = configRead.readConfig('parameter', 'sleepsecond') runtime = configRead.readConfig('parameter', 'runtime') currentTime = tools.get_hour() tools.recordLog(str(tools.time_cmp(currentTime, i))) print('sleepsecond=' + str(sleepsecond)) print(runtime) for i in runtime: if tools.time_cmp(currentTime, i) < sleepsecond: print("运行时间到了") else: print("未到运行时间")
def health(plan=1): infomationList = getServerinfo() #过滤服务器状态报警 dingdingAlarmDict = getAlarm.getAlarmDict(infomationList) dingdingAlarmDict.setdefault('ECSHealth', '') #收集阿里服务器到期日报警 #获取当前时间 # escAlarmTime = configRead.readConfig('aliyun', 'AlarmTime') currentTime = tools.get_hour() print(currentTime) print(escAlarmTime) if ((currentTime >= escAlarmTime[0]) and (currentTime <= escAlarmTime[1])): print('ECS报警时段命中') print('报警时段:' + escAlarmTime[0] + escAlarmTime[1]) currentDay = tools.get_day() print(currentDay) print(gl.getvalue('escSendday')) tools.recordLog('ECS报警时段命中') tools.recordLog(currentDay) tools.recordLog(gl.getvalue('escSendday')) if currentDay != gl.getvalue('escSendday'): print('ECS收集,天不同') tools.recordLog('ECS收集信息,一天只能一次') try: ecsAlarmList = alarmecs.getAlarmEcs() print(ecsAlarmList) numAlarm = len(ecsAlarmList) print('有' + str(numAlarm) + '台ECS将要到期') tools.recordLog('有' + str(numAlarm) + '台ECS将要到期') except Exception as err: tools.recordLog("alarmecs.getAlarmEcs err : " + str(err)) print(str(err)) if numAlarm > 0: print('ecs报警了') tools.recordLog('ecs>0报警了') gl.setvalue('escSendday', currentDay) dingdingAlarmDict['ECSHealth'] = '有' + str( numAlarm) + '台ECS将要到期' ecslists = ecslist.getEcslist() ecsfile = configRead.readConfig('aliyun', 'ecsfile') tools.writelisttohtml(ecslists, ecsfile) #print('打印MSG 字典') #print(dingdingAlarmDict) DingDingRobot.sendAlarm(dingdingAlarmDict, plan) tools.recordLog(str(dingdingAlarmDict)) #报警数据上色,生成html infomationList1 = htmlAlarm.color(infomationList, 0) #上色 htmlfile = configRead.readConfig('parameter', 'webfile') tools.writelisttohtml(infomationList1, htmlfile) #生成html infomationList2 = htmlAlarm.color(infomationList, 1) #上色,带升级链接 htmlfile = configRead.readConfig('parameter', 'webupdatefile') tools.writelisttohtml(infomationList2, htmlfile) #生成html getsqlServer() tools.recordLog('同步数据获取完毕') logging.debug('End of program'.center(30, '-'))
def sendAlarm(msgDict, plan): #第一个body 是纯消息,不带链接 #body = {"msgtype": "text","text": {"content": "测试"},"at": {"atMobiles": ["18678966660"], "isAtAll": "false"}} body = { "msgtype": "link", "link": { "text": "群机器人是钉钉群的高级扩展功能。", "title": "服务器状态信息", "picUrl": "", "messageUrl": "http://47.91.157.89:999/" } } headers = {'content-type': "application/json"} sendgroup = configRead.readConfig('dingding', 'sendgroup') count = int(gl.getvalue('count')) + 1 gl.setvalue('count', str(count)) print('count=' + str(count)) tools.recordLog('count=' + str(count)) for mydict in sendgroup.keys(): # (count % int(sendgroup[mydict][4]))==0 是执行频率 ,例如运维群每5分钟通知一次,那sendgroup[mydict][4]=5就好 print(mydict + ' 频率: ' + sendgroup[mydict][4]) runTime = sendgroup[mydict][5] print(runTime) currentTime = tools.get_hour() print(currentTime) #在允许的时间段内扫描 if (currentTime >= runTime[0]) and (currentTime <= runTime[1]): print("时间命中") tools.recordLog("时间命中") else: print("时间没命中") tools.recordLog("时间没有命中") tools.recordLog("runTime0" + runTime[0]) tools.recordLog("runTime1" + runTime[1]) print("runTime0" + runTime[0]) print("runTime1" + runTime[1]) continue print(mydict + ' 尝试' + 'plan=' + str(plan)) print('count=' + str(count) + '余数:' + str((count % int(sendgroup[mydict][4])))) #以下判断,当plan=1时,无条件执行扫描 if ((sendgroup[mydict][0].upper() == 'ON') and ((count % int(sendgroup[mydict][4])) == 0)) or ( (sendgroup[mydict][0].upper() == 'ON') and (plan == 1)): print(mydict + ' 命中: ' + 'plan=' + str(plan)) tools.recordLog(mydict + ' 命中: ' + 'plan=' + str(plan)) tools.recordLog('count=' + str(count) + ' 命中' + str((count % int(sendgroup[mydict][4])))) if sendgroup[mydict][1].upper() == 'SERVERHEALTH': msg = msgDict['ServerHealth'] else: msg = msgDict['SeviceHealth'] msg = msgPlan(msg, plan, sendgroup[mydict][1]) url = sendgroup[mydict][2] body['link']["text"] = msg body['link']["messageUrl"] = sendgroup[mydict][3] response = requests.post(url, data=json.dumps(body), headers=headers) res = eval(response.text) # eval 转为字典,不能防注入 if (response.status_code == 200) and (res['errcode'] == 0): tools.recordLog('DingDingCode:' + mydict + ' ' + str(res['errcode']) + ':' + res['errmsg']) print(res['errmsg']) return 0 #发送成功 else: tools.recordLog('DingDingCode:' + mydict + ' ' + str(res['errcode']) + ':' + res['errmsg']) print(res['errmsg']) return int(res['errcode']) #未发送或发送失败