python 遠端巡檢多臺伺服器ogg狀態資訊
阿新 • • 發佈:2020-10-12
因為工作中對ogg資料一致性,實時性要求不高,同步異常時有充足修復時間,未配置ogg異常監控,但仍需定期檢查執行狀態,因此寫了這個小指令碼,在Windows上一鍵執行即可。
1 #!/usr/bin/env python 2 # -*-coding: utf-8 -*- 3 # @Time:2020/7/13 9:47 4 # @Author: WSN 5 6 import paramiko 7 import re 8 from hostlist import * 9 10 ''' 11 hostlist config example: 12 hostlist=[{'host': 'x.x.x.x', 'port': '22', 'user': 'xxxx', 'pwd': 'xxxxx', 'oggdir': '/ogg'}]13 ''' 14 15 16 class SshOgg: 17 def __init__(self, host, port, username, pwd): 18 self.host = host 19 self.port = port 20 self.username = username 21 self.pwd = pwd 22 self.ssh = paramiko.SSHClient() 23 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())24 self.ssh.connect(self.host, self.port, self.username, self.pwd) 25 26 def check_ogg_status(self, oggdir): 27 cmd = '''su - oracle -c \'echo "info all" |%s/./ggsci\'''' % oggdir 28 stdin, stdout, stderr = self.ssh.exec_command(cmd) 29 for i in stdout.readlines():30 if re.search(r'REPLICAT', i): 31 print(i) 32 33 def closed(self): 34 self.ssh.close() 35 36 37 if __name__ == '__main__': 38 for con_info in hostlist: 39 host = con_info['host'] 40 port = con_info['port'] 41 username = con_info['user'] 42 pwd = con_info['pwd'] 43 oggdir = con_info['oggdir'] 44 ssh = SshOgg(host, port, username, pwd) 45 print('-------%s上ogg程序執行狀態資訊-------' % host) 46 ssh.check_ogg_status(oggdir) 47 ssh.closed()