伺服器1 xx.168.8x.77
#!/usr/bin/python
#! _*_ coding:utf-8 _*_
import os
import sys
import time
mongo_ip = '192.168.xx.77'
ports = ['x001','x002']
def port(ip,port):
response = os.popen("tcping %s %s|grep open|awk -F' ' '{print $4}'|cut -d'.' -f1" %(ip,port)).read().strip()
if response == 'open':
return response
else:
return 'port_fail'
def write_f(st):
with open('mongo_pd.txt', 'a') as file:
file.write(st)
file.close()
pdlist = []
for p in ports:
jg = port(mongo_ip,p)
pdlist.append(mongo_ip+':'+p+'-->'+jg+'\n')
unique_list = list(set(pdlist))
if os.path.exists('mongo_pd.txt'):
os.remove('mongo_pd.txt')
for din in unique_list:
if 'port_fail' in din:
write_f(din)
else:
write_f(din)
伺服器2 x92.168.8x.83
#!/usr/bin/python
#! _*_ coding:utf-8 _*_
import os
import sys
import time
sshuser = 'root'
sshpassword = '12xxxxx'
ip = '1xx.168.x4.77'
pd_str = 'port_fail'
def dingding(ding_msg):
os.system("/usr/bin/curl https://oapi.dingtalk.com/robot/send?access_token=xb5ccxxxxxxxbbf1d7xxxx084xxxxxxe9b9ff968xxxxxxxxxx -H \"Content-Type: application/json\" -d \"{'msgtype': 'text', 'at': {'atMobiles': ['13xxx20xxx'], 'isAtAll': false}, 'text': {'content': \'%s\'}}\" " %(ding_msg))
#pd_str = 'open'
mongo_8x77_2port_status = os.popen('sshpass -p %s ssh %s@%s -o StrictHostKeyChecking=no "cat /data/jk_mongodb/8483_jk_8477mongodb/mongo_pd.txt" |grep %s'%(sshpassword,sshuser,ip,pd_str)).read().strip()
#print(mongo_8x77_2port_status)
if 'port_fail' in mongo_8x77_2port_status:
ding_msg = 'Yearning-pro_xx專案mongodb服務程序埠%s已經故障,請注意檢視!!!'%(mongo_8x77_2port_status)
dingding(ding_msg)