You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.2 KiB
105 lines
4.2 KiB
6 years ago
|
#!/usr/bin/env python
|
||
|
# -*- coding:utf-8 -*-
|
||
|
|
||
|
'''
|
||
5 years ago
|
1, yum install pip
|
||
6 years ago
|
yum -y install python-pip
|
||
|
|
||
5 years ago
|
2, pip install kazoo
|
||
|
pip install kazoo
|
||
6 years ago
|
|
||
5 years ago
|
or
|
||
|
|
||
|
3, conda install kazoo
|
||
|
conda install -c conda-forge kazoo
|
||
|
|
||
|
run script and parameter description:
|
||
6 years ago
|
nohup python -u monitor_server.py /data1_1T/escheduler 192.168.xx.xx:2181,192.168.xx.xx:2181,192.168.xx.xx:2181 /escheduler/masters /escheduler/workers> monitor_server.log 2>&1 &
|
||
5 years ago
|
the parameters are as follows:
|
||
|
/data1_1T/escheduler : the value comes from the installPath in install.sh
|
||
|
192.168.xx.xx:2181,192.168.xx.xx:2181,192.168.xx.xx:2181 : the value comes from zkQuorum in install.sh
|
||
|
the value comes from zkWorkers in install.sh
|
||
|
/escheduler/masters : the value comes from zkMasters in install.sh
|
||
|
/escheduler/workers : the value comes from zkWorkers in install.sh
|
||
6 years ago
|
'''
|
||
6 years ago
|
import sys
|
||
6 years ago
|
import socket
|
||
|
import os
|
||
|
import sched
|
||
|
import time
|
||
|
from datetime import datetime
|
||
|
from kazoo.client import KazooClient
|
||
|
|
||
|
schedule = sched.scheduler(time.time, time.sleep)
|
||
|
|
||
|
class ZkClient:
|
||
|
def __init__(self):
|
||
5 years ago
|
# hosts configuration zk address cluster
|
||
6 years ago
|
self.zk = KazooClient(hosts=zookeepers)
|
||
|
self.zk.start()
|
||
6 years ago
|
|
||
5 years ago
|
# read configuration files and assemble them into a dictionary
|
||
6 years ago
|
def read_file(self,path):
|
||
|
with open(path, 'r') as f:
|
||
|
dict = {}
|
||
|
for line in f.readlines():
|
||
|
arr = line.strip().split('=')
|
||
|
if (len(arr) == 2):
|
||
|
dict[arr[0]] = arr[1]
|
||
|
return dict
|
||
|
|
||
5 years ago
|
# get the ip address according to hostname
|
||
6 years ago
|
def get_ip_by_hostname(self,hostname):
|
||
|
return socket.gethostbyname(hostname)
|
||
|
|
||
5 years ago
|
# restart server
|
||
6 years ago
|
def restart_server(self,inc):
|
||
6 years ago
|
config_dict = self.read_file(install_path + '/conf/config/run_config.conf')
|
||
6 years ago
|
|
||
|
master_list = config_dict.get('masters').split(',')
|
||
6 years ago
|
print master_list
|
||
6 years ago
|
master_list = list(map(lambda item : self.get_ip_by_hostname(item),master_list))
|
||
|
|
||
|
worker_list = config_dict.get('workers').split(',')
|
||
6 years ago
|
print worker_list
|
||
6 years ago
|
worker_list = list(map(lambda item: self.get_ip_by_hostname(item), worker_list))
|
||
|
|
||
6 years ago
|
if (self.zk.exists(masters_zk_path)):
|
||
6 years ago
|
zk_master_list = []
|
||
6 years ago
|
zk_master_nodes = self.zk.get_children(masters_zk_path)
|
||
6 years ago
|
for zk_master_node in zk_master_nodes:
|
||
|
zk_master_list.append(zk_master_node.split('_')[0])
|
||
|
restart_master_list = list(set(master_list) - set(zk_master_list))
|
||
|
if (len(restart_master_list) != 0):
|
||
|
for master in restart_master_list:
|
||
5 years ago
|
print("master " + self.get_ip_by_hostname(master) + " server has down")
|
||
6 years ago
|
os.system('ssh ' + self.get_ip_by_hostname(master) + ' sh ' + install_path + '/bin/escheduler-daemon.sh start master-server')
|
||
6 years ago
|
|
||
6 years ago
|
if (self.zk.exists(workers_zk_path)):
|
||
6 years ago
|
zk_worker_list = []
|
||
6 years ago
|
zk_worker_nodes = self.zk.get_children(workers_zk_path)
|
||
6 years ago
|
for zk_worker_node in zk_worker_nodes:
|
||
|
zk_worker_list.append(zk_worker_node.split('_')[0])
|
||
|
restart_worker_list = list(set(worker_list) - set(zk_worker_list))
|
||
|
if (len(restart_worker_list) != 0):
|
||
|
for worker in restart_worker_list:
|
||
5 years ago
|
print("worker " + self.get_ip_by_hostname(worker) + " server has down")
|
||
6 years ago
|
os.system('ssh ' + self.get_ip_by_hostname(worker) + ' sh ' + install_path + '/bin/escheduler-daemon.sh start worker-server')
|
||
6 years ago
|
|
||
|
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||
|
schedule.enter(inc, 0, self.restart_server, (inc,))
|
||
5 years ago
|
# default parameter 60s
|
||
6 years ago
|
def main(self,inc=60):
|
||
5 years ago
|
# the enter four parameters are: interval event, priority (sequence for simultaneous execution of two events arriving at the same time), function triggered by the call,
|
||
|
# the argument to the trigger function (tuple form)
|
||
6 years ago
|
schedule.enter(0, 0, self.restart_server, (inc,))
|
||
|
schedule.run()
|
||
|
if __name__ == '__main__':
|
||
6 years ago
|
if (len(sys.argv) < 4):
|
||
|
print('please input install_path,zookeepers,masters_zk_path and worker_zk_path')
|
||
|
install_path = sys.argv[1]
|
||
|
zookeepers = sys.argv[2]
|
||
|
masters_zk_path = sys.argv[3]
|
||
|
workers_zk_path = sys.argv[4]
|
||
6 years ago
|
zkClient = ZkClient()
|
||
5 years ago
|
zkClient.main(300)
|