def ftp_shlo_log(): global lf lf = None print("下載SHLO LOCK LOG FILE...") USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp36a_dba') ftp = FTP("100.1.1.6", USER_ID, PASSWORD) org_path = "DSA1:[MIS.CPS]" ftp.cwd(org_path) ls_file = ['SHLO_60E.TXT', 'SHLO_76A.TXT'] for fn in ls_file: filename = fn + ";1" try: lf = open(fn, "w") ftp.retrlines("RETR " + filename, writeline) lf.close() print("下載SHLO LOCK LOG FILE,完畢...") except: print("Err: 下載SHLO LOCK LOG FILE,失敗...") print(sys.exc_info()) ftp.quit()
def CHK_HSM_STAT(): print("Executing " + os.path.basename(__file__) + "...") USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp76a_crm') #連RDB conn = pyodbc.connect(r'DSN=RDBPCM60;UID=' + USER_ID + ';PWD=' + PASSWORD) sql_str = "select STATUS_CODE, STATUS_EXPLAIN from pcmb204m where ename = 'HSM'" try: cursor = conn.cursor() cursor.execute(sql_str) for row in cursor: #print(row) stat_code = row[0] #狀態(1運轉0停機) stat_desc = row[1].strip() #狀態描述 print("CHK_HSM_STAT: 檢查目前HSM產線狀態為:" + stat_desc) #print(stat_code) except Exception as e: stat_code = 99 #出現任何錯誤,狀態碼設定為99 print("Err: HSM產線運作狀況讀取失敗.") print(e.args) #關閉資料庫連線 conn.close() return stat_code
def DEL_36A_SHLO_LOG(): global log_file print("刪除CPS 36A上的SHOW LOCK LOG檔案...") str_dt = str(datetime.datetime.now()) str_dt = parser.parse(str_dt).strftime("%Y%m%d%H%M%S") timeout = 20 USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp36a_cps') host_ip = '100.1.1.6' try: tn = telnetlib.Telnet(host_ip) except Exception as e: f = open('SYS_SHLO_LOG.TXT', 'a') f.write('Err on SYS_SHLO create a telnet link(' + host_ip + ').\n') f.write('funtion: DEL_36A_SHLO_LOG\n') f.write('time:' + str_dt + '\n') f.write(e.args) f.write('\n\n') f.close() return #for debug #tn.set_debuglevel(1) tn.read_until(b"Username: "******"\r") tn.read_until(b"Password: "******"\r") #waiting for prompt tn.read_until(b"[MIS.CPS]", timeout) tn.write(b'\r') log_file = "SHLO_60E.TXT" str_cmd = "DEL " + log_file + ";* /NOCON\r" print(str_cmd) tn.read_until(b"[MIS.CPS]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format log_file = "SHLO_76A.TXT" str_cmd = "DEL " + log_file + ";* /NOCON\r" print(str_cmd) tn.read_until(b"[MIS.CPS]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format tn.read_until(b"[MIS.CPS]", timeout) tn.write(b"logout\r") time.sleep(1) #close connection tn.close() print("刪除36A SHOW LOCK LOG檔案,完畢...\n\n\n")
def rd_hps_log(): global file host_ip = '100.1.1.2' #AXP76A str_dt = str(datetime.datetime.now()) str_dt = parser.parse(str_dt).strftime("%Y%m%d") timeout = 20 tot_diff_minu = 0 try: tn = telnetlib.Telnet(host_ip) except Exception as e: file.write('Err on rd_hps_log create a telnet link(' + host_ip + ').\n') file.write('funtion: rd_hps_log\n') file.write('time:' + str_dt + '\n') file.write(str(e.args)) file.write('\n\n') return 99 #tn.set_debuglevel(1) USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp76a_hps') tn.read_until(b"Username: "******"\r") tn.read_until(b"Password: "******"\r") #waiting for prompt tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(b'\r') str_cmd = "TYPE SCH" + str_dt + ".LOG /tail=10\r" #str_cmd = "TYPE SCH20180401.LOG /tail=10\r" print(str_cmd) tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format date_fmt = "%Y/%m/%d-%H:%M:%S" cnt = 0 log_last_dt = "" while True: try: line = tn.read_until(b"\r", timeout) # Check for new line and CR line_str = line.decode("ASCII") print(line_str) except Exception as e: line_str = "" print("The return string is decoding error.") print("The origin string =>" + str(line)) print(str(e.args)) file.write("The return string is decoding error.\n") file.write("The origin string =>" + str(line) + "\n") file.write(str(e.args)) file.write('\n\n') if (b"[MIS.HPS.LOG]" ) in line: # If last read line is the prompt, end loop break if len(line_str.strip()) > 16: log_last_dt = line_str[2:21] #print("Substring =>" + log_last_dt) str_dt = str(datetime.datetime.now()) curr_dt = parser.parse(str_dt).strftime(date_fmt) print("log_last_dt='" + log_last_dt + "'") print("curr_dt='" + curr_dt + "'") file.write("log_last_dt='" + log_last_dt + "'\n") file.write("curr_dt='" + curr_dt + "'\n") try: a = datetime.datetime.strptime(log_last_dt, date_fmt) #LOG紀錄最後日期時間 b = datetime.datetime.strptime(curr_dt, date_fmt) #系統目前日期時間 delta = b - a tot_diff_minu = delta.days * 1440 + delta.seconds / 60 #時間差(分鐘) print("tot_diff_minu=" + str(tot_diff_minu)) file.write("tot_diff_minu=" + str(tot_diff_minu) + "\n") except Exception as e: print("TYPE 指令異常,略過本次檢查...") file.write("TYPE 指令異常,略過本次檢查...\n") file.write('Err on rd_hps_log()\n') file.write('time:' + curr_dt + '\n') file.write(str(e.args)) file.write('\n\n') return 99 tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(b"logout\r") time.sleep(1) #close connection tn.close() return tot_diff_minu print("rd_hps_log,完畢...\n\n\n")
def rest_hsm_sch(): global file host_ip = '100.1.1.2' #AXP76A str_dt = str(datetime.datetime.now()) str_dt = parser.parse(str_dt).strftime("%Y%m%d") timeout = 20 recnt = 0 try: tn = telnetlib.Telnet(host_ip) except Exception as e: file.write('Err on rest_hsm_sch create a telnet link(' + host_ip + ').\n') file.write('funtion: rest_hsm_sch\n') file.write('time:' + str_dt + '\n') file.write(str(e.args)) file.write('\n\n') return USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp76a_hps') tn.read_until(b"Username: "******"\r") tn.read_until(b"Password: "******"\r") #waiting for prompt tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(b'\r') # 建立資料庫連線 conn = sqlite3.connect('yusco.db') # 讀取當天重啟次數 sqlstr = "select RECONNECT_CNT from HSMW_PLAN_STAT " sqlstr = sqlstr + "where " sqlstr = sqlstr + "CHK_DATE='" + str_dt + "' " #print(sqlstr) cursor = conn.execute(sqlstr) result = cursor.fetchone() if result is not None: recnt = result[0] else: sqlstr = "insert into HSMW_PLAN_STAT (CHK_DATE, RECONNECT_CNT) values (" sqlstr += "'" + str_dt + "',0)" try: conn.execute(sqlstr) conn.commit() except sqlite3.Error as er: print("insert HSMW_PLAN_STAT RECONNECT_CNT er=" + er.args[0] + "\n") print(str_dt + " RECONNECT_CNT資料新增異常...Rollback!\n") file.write("insert HSMW_PLAN_STAT RECONNECT_CNT er=" + er.args[0] + "\n") file.write(str_dt + " RECONNECT_CNT資料新增異常...Rollback!\n") conn.execute("rollback") if recnt < 3: str_cmd = "STOP SCH\r" print(str_cmd) file.write(str_cmd + "\n") tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format time.sleep(5) str_cmd = "SV\r" print(str_cmd) file.write(str_cmd + "\n") tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format # 更新當天重啟次數 recnt += 1 sqlstr = "update HSMW_PLAN_STAT set RECONNECT_CNT = " + str( recnt) + " " sqlstr += "where CHK_DATE='" + str_dt + "'" try: conn.execute(sqlstr) conn.commit() except sqlite3.Error as er: str_dt = str(datetime.datetime.now()) print("update HSMW_PLAN_STAT RECONNECT_CNT er=" + er.args[0] + "\n") print(str_dt + " RECONNECT_CNT=" + str(recnt) + "資料更新異常...Rollback!\n") file.write("update HSMW_PLAN_STAT RECONNECT_CNT er=" + er.args[0] + "\n") file.write(str_dt + " RECONNECT_CNT=" + str(recnt) + "資料更新異常...Rollback!\n") conn.execute("rollback") else: print("$$$ 當天重啟次數已達三次,不再進行重啟. $$$\n") file.write("$$$ 當天重啟次數已達三次,不再進行重啟. $$$\n") tn.read_until(b"[MIS.HPS.LOG]", timeout) tn.write(b"logout\r") time.sleep(1) #close connection tn.close() # 資料庫連線關閉 conn.close()
def SYS_SHLO(arg_host): global log_file print("執行" + arg_host + "系統LOCK檢查作業...\n") str_dt = str(datetime.datetime.now()) str_dt = parser.parse(str_dt).strftime("%Y%m%d%H%M%S") if arg_host == "AXP76A": host_ip = '100.1.1.2' log_file = 'SHLO_76A.TXT' else: host_ip = '100.1.1.3' log_file = 'SHLO_60E.TXT' timeout = 20 log_timeout = 180 try: tn = telnetlib.Telnet(host_ip) except Exception as e: f = open('SYS_SHLO_LOG.TXT', 'a') f.write('Err on SYS_SHLO create a telnet link(' + host_ip + ').\n') f.write('funtion: SYS_SHLO\n') f.write('time:' + str_dt + '\n') f.write(e.args) f.write('\n\n') f.close() return #for debug #tn.set_debuglevel(1) USER_ID, PASSWORD = GET_LO_ID.GET_LOGIN_ID('axp76a_mgr') tn.read_until(b"Username: "******"\r") tn.read_until(b"Password: "******"\r") #waiting for prompt tn.read_until(b"[MIS.MGR]", timeout) tn.write(b'\r') str_cmd = "DEL " + log_file + ";* /NOCON\r" print(str_cmd) tn.read_until(b"[MIS.MGR]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format str_cmd = "SHLO /OUT=" + log_file + "\r" print(str_cmd) tn.read_until(b"[MIS.MGR]", timeout) tn.write(str_cmd.encode()) # string convert to bytes format cps36a_id, cps36a_pwd = GET_LO_ID.GET_LOGIN_ID('axp36a_cps') str_cmd = 'copy ' + log_file + ' axp36a"' + cps36a_id + ' ' + cps36a_pwd + '"::sys$login:*.* \r' #print(str_cmd) tn.read_until(b"[MIS.MGR]", log_timeout) tn.write(str_cmd.encode()) # string convert to bytes format tn.read_until(b"[MIS.MGR]", timeout) tn.write(b"logout\r") time.sleep(1) #close connection tn.close() print(arg_host + "系統LOCK檢查作業結束,等待下次執行...\n\n\n")