HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux WebLive 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //usr/local/qcloud/monitor/barad/plugin/collector/metal/host_proxy_starter.py
import sys, os
import time
from plugin_base import VmBaseCollector

PYTHON_PATH = '/usr/local/qcloud/monitor/python26/bin/python'
PROXY_CLIENT_PATH = '/usr/local/qcloud/monitor/barad/plugin/collector/utils/proxy_client.py'

class HostProxyStarter(VmBaseCollector):
    def init(self):
    	# set the schedule frequency to 10s
        self.set_frequency(10)
        #self.__log = open('../log/host_proxy.log', mode = 'a+')

    # The entry function for barad scheduler 
    def do_collect(self):        
        self.__log = open('../log/host_proxy.log', mode = 'a+')
        #self.__log.write("do a do_collect.\n")
        if self.is_metal() and not self.check_version():
            self.restart_proc()
        if self.is_metal() and not (self.proc_running() and self.check_iscsid()):
            self.startup_proc()
        if not self.check_health():
            self.__log.write("check_health failed,begin restart_proc.\n")
            self.__log.flush()
            self.restart_proc()
        self.__log.close()

    def check_health(self):
        cmd = 'chmod 755 ' + PROXY_CLIENT_PATH + ';'  + PYTHON_PATH + ' ' + PROXY_CLIENT_PATH + ' attach 1 2' 
        try:
            out = os.popen(cmd, 'r').readlines()
            for line in out:
                key = "DISCOVERY"
                if key in line:
                    return True
        except OSError:
            self.__log.write("check_health Error :%s\n" % e)
            self.__log.flush()
            return False
        return False


    def check_iscsid(self):
        cmd = 'ps aux | grep -v grep | grep -w iscsid'
        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError:
            return False
        if len(out) < 1:
            # open-iscsi is not running
            return False
        return True

    def check_version(self):
        conf_file = '../etc/host_proxy.conf'
        version_file = '/var/run/Tx-proxy.lock'
        if not (os.path.exists(version_file) and os.path.exists(conf_file)):
            return False
        with open(conf_file, 'r') as f1:
            ver = f1.read().strip()
        with open(version_file, 'r') as f2:
            if ver == f2.read().strip():
                return True
        return False

    # check environment
    def is_metal(self, vendor = "14e4", device_id = "d802", driver = "bnxt_en"):
        cmd = 'lspci -nn -vv | grep Ethernet | grep Broadcom | grep \'' + vendor + ':' + device_id + '\''
        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError:
            return False
        if len(out) < 1:
            return False

        cmd = 'ethtool -i eth1 | grep -w ' + driver
        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError:
            return False
        if len(out) < 1:
            return False

        return True


    # Check whether the host_proxy progress is running
    def proc_running(self, procname = 'Tx-proxy'):
        cmd = 'ps aux | grep -w ' + procname +  ' | grep -v \'grep\''

        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError as e:
            self.__log.write("Error :%s\n" % e)
            self.__log.flush()
            return False

        for line in out:
            if procname in line:
                return True

        return False

    # Start the host_proxy process
    def startup_proc(self,path = '../plugin/collector/utils/host_proxy.py'):
        cmd = '/usr/local/qcloud/monitor/python26/bin/python ' + path
        self.__log.write('%s, host_proxy_starter handle: %s\n' %(time.ctime(), cmd) )
        self.__log.flush()

        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError as e:
            self.__log.write("Error :%s\n" % e)
            self.__log.flush()
            return False

        return True

    def restart_proc(self,path = '../plugin/collector/utils/host_proxy.py'):
        cmd = "ps axu | grep Tx-proxy | grep -v grep | awk '{print $2}' | xargs kill"
        self.__log.write('%s, host_proxy_starter try to restart Tx-proxy: %s\n' %(time.ctime(), cmd) )
        self.__log.flush()

        try:
            out = os.popen(cmd, 'r').readlines()
        except OSError as e:
            self.__log.write("Error :%s\n" % e)
            self.__log.flush()
            return False

        return self.startup_proc()

# main
def main():
    starter = HostProxyStarter()
    starter.init()

    while 1:
        starter.do_collect()
        time.sleep(10)

    self.__log.close()

# main
if __name__ == "__main__":
    main()