start_check.py 3.6 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# coding: utf-8
# OceanBase Deploy.
# Copyright (C) 2021 OceanBase
#
# This file is part of OceanBase Deploy.
#
# OceanBase Deploy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OceanBase Deploy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OceanBase Deploy.  If not, see <https://www.gnu.org/licenses/>.


from __future__ import absolute_import, division, print_function

R
Rongfeng Fu 已提交
23 24
from _errno import EC_CONFIG_CONFLICT_PORT

O
oceanbase-admin 已提交
25 26

stdio = None
R
Rongfeng Fu 已提交
27
success = True
O
oceanbase-admin 已提交
28 29 30 31


def get_port_socket_inode(client, port):
    port = hex(port)[2:].zfill(4).upper()
R
Rongfeng Fu 已提交
32
    cmd = "bash -c 'cat /proc/net/{tcp,udp}' | awk -F' ' '{print $2,$10}' | grep '00000000:%s' | awk -F' ' '{print $2}' | uniq" % port
O
oceanbase-admin 已提交
33 34 35 36 37 38 39
    res = client.execute_command(cmd)
    if not res or not res.stdout.strip():
        return False
    stdio.verbose(res.stdout)
    return res.stdout.strip().split('\n')


R
Rongfeng Fu 已提交
40 41 42 43 44 45 46 47
def start_check(plugin_context, strict_check=False, *args, **kwargs):
    def alert(*arg, **kwargs):
        global success
        if strict_check:
            success = False
            stdio.error(*arg, **kwargs)
        else:
            stdio.warn(*arg, **kwargs)
R
Rongfeng Fu 已提交
48 49 50 51 52 53 54
    def error(*arg, **kwargs):
        global success
        if plugin_context.dev_mode:
            stdio.warn(*arg, **kwargs)
        else:
            success = False
            stdio.error(*arg, **kwargs)
R
Rongfeng Fu 已提交
55 56 57 58
    def critical(*arg, **kwargs):
        global success
        success = False
        stdio.error(*arg, **kwargs)
O
oceanbase-admin 已提交
59 60 61 62 63
    global stdio
    cluster_config = plugin_context.cluster_config
    clients = plugin_context.clients
    stdio = plugin_context.stdio
    servers_port = {}
R
Rongfeng Fu 已提交
64
    stdio.start_loading('Check before start obproxy')
O
oceanbase-admin 已提交
65 66 67
    for server in cluster_config.servers:
        ip = server.ip
        client = clients[server]
R
Rongfeng Fu 已提交
68 69 70 71 72 73
        server_config = cluster_config.get_server_conf(server)
        port = int(server_config["listen_port"])
        prometheus_port = int(server_config["prometheus_listen_port"])
        remote_pid_path = "%s/run/obproxy-%s-%s.pid" % (server_config['home_path'], server.ip, server_config["listen_port"])
        remote_pid = client.execute_command("cat %s" % remote_pid_path).stdout.strip()
        if remote_pid:
R
Rongfeng Fu 已提交
74
            if client.execute_command('ls /proc/%s/fd' % remote_pid):
R
Rongfeng Fu 已提交
75 76
                continue
        
O
oceanbase-admin 已提交
77 78 79 80 81 82 83
        if ip not in servers_port:
            servers_port[ip] = {}
        ports = servers_port[ip]
        server_config = cluster_config.get_server_conf_with_default(server)
        stdio.verbose('%s port check' % server)
        for key in ['listen_port', 'prometheus_listen_port']:
            port = int(server_config[key])
R
Rongfeng Fu 已提交
84
            alert_f = alert if key == 'prometheus_listen_port' else critical
O
oceanbase-admin 已提交
85
            if port in ports:
R
Rongfeng Fu 已提交
86
                alert_f(EC_CONFIG_CONFLICT_PORT.format(server1=server, port=port, server2=ports[port]['server'], key=ports[port]['key']))
O
oceanbase-admin 已提交
87 88 89 90 91 92
                continue
            ports[port] = {
                'server': server,
                'key': key
            }
            if get_port_socket_inode(client, port):
R
Rongfeng Fu 已提交
93
                alert_f('%s:%s port is already used' % (ip, port))
R
Rongfeng Fu 已提交
94
                
O
oceanbase-admin 已提交
95
    if success:
R
Rongfeng Fu 已提交
96 97 98 99
        stdio.stop_loading('succeed')
        plugin_context.return_true()
    else:
        stdio.stop_loading('fail')