Esempio n. 1
0
    def is_master_process(self, uwsgi_server_port='8002'):
        """
        判断自身是否为uwsgi 中的mater 进程

        :param param1: this is a first param
        :param param2: this is a second param
        :returns: this is a description of what is returned
        :raises keyError: raises an exception
        @author: jhuang
        @time:9/26/2018
        """

        import os

        self.self_pid = str(os.getpid())
        if is_linux_system():
            self.uwsgi_master_pid = str(exec_shell(
                """netstat -anp|grep %s|awk '{printf $7}'|cut -d/ -f1""" % (uwsgi_server_port))['msg'])
            self.self_ppid = str(os.getppid())
        else:
            self.self_ppid = '-1'
            self.uwsgi_master_pid = '0'

        logger.debug('uwsgi_master_pid:{uwsgi_master_pid},self_ppid:{self_ppid},self_pid:{self_pid}'.format(
            uwsgi_master_pid=self.uwsgi_master_pid,
            self_ppid=self.self_ppid, self_pid=self.self_pid))
        if self.uwsgi_master_pid == self.self_ppid:
            return True
        else:
            return False
Esempio n. 2
0
def get_host_all_ip():
    """
       获取主机全部IP地址

       :param param1: this is a first param
       :param param2: this is a second param
       :returns: ip list
       :raises keyError: raises an exception
       @author: jhuang
       @time:05/02/2018
    """

    ip_list = []

    if is_linux_system():
        ip_dic = exec_shell(
            "/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:'")
        ip_list = str(ip_dic['msg']).split('\n')
    else:
        hostname = socket.gethostname()
        addrs = socket.getaddrinfo(hostname, None)
        for item in addrs:
            if item[0] == 2:
                ip_list.append(item[4][0])
    return ip_list
Esempio n. 3
0
def get_netspeed(eth=''):
    """
    |##desc: 网卡网速获取
    |##:param: None
    |##:return: None
    |##@author: jhuang
    |##@time:2017-07-20
    """
    RX = 0
    TX = 0
    if is_linux_system() is True:
        # return RX, TX
        if eth == '':
            eth = psutil.net_io_counters(pernic=True).keys()[1]

        ret = exec_shell('/sbin/ifconfig %s | grep bytes' % (eth))
        X1 = re.findall('bytes.(\d+)', ret['msg'])

        sleep(1)

        ret = exec_shell('/sbin/ifconfig %s | grep bytes' % (eth))
        X2 = re.findall('bytes.(\d+)', ret['msg'])
        print X2
        if len(X2) > 0:
            # RX 为下行流量 TX 为上行流量
            RX = covert_bytes(int(X2[0]) - int(X1[0]))
            TX = covert_bytes(int(X2[1]) - int(X1[1]))
        print RX, RX
    return RX, TX
Esempio n. 4
0
def unzip(zip_file_path, unzip_to_dir):
    """
    | ##@函数目的: 解压zip文件到指定目录
    | ##@参数说明:zipfilename为zip文件路径,unziptodir为解压文件后的文件目录
    | ##@返回值:无
    | ##@开发人:jhuang
    | ##@函数逻辑:
    """
    if not os.path.isfile(zip_file_path):
        logger.error('待解压文件不存在:%s' % (zip_file_path))
        return False
    logger.debug( zip_file_path + '->' + unzip_to_dir)
    ret = exec_shell('unzip')
    if 'Usage' in str(ret['msg']):
        logger.debug('存在unzip命令,使用unzip命令解压...')
        if is_linux_system():
            exec_shell('unzip -o %s -d %s > %s_unzip.log 2>&1' % (zip_file_path, unzip_to_dir, zip_file_path))
        else:
            exec_shell('unzip -o %s -d %s  > %s_unzip.log' % (zip_file_path, unzip_to_dir, zip_file_path))
    else:
        if not os.path.exists(unzip_to_dir):
            os.mkdir(unzip_to_dir)
        zfobj = zipfile.ZipFile(zip_file_path)
        for name in zfobj.namelist():
            name = name.replace('\\', '/')
            if name.endswith('/'):
                p = os.path.join(unzip_to_dir, name[:-1])
                if os.path.exists(p):
                    # 如果文件夹存在,就删除之:避免有新更新无法复制
                    shutil.rmtree(p)
                os.mkdir(p)
            else:
                ext_filename = os.path.join(unzip_to_dir, name)
                ext_dir = os.path.dirname(ext_filename)
                if not os.path.exists(ext_dir):
                    os.mkdir(ext_dir, 0777)
                outfile = open(ext_filename, 'wb')
                outfile.write(zfobj.read(name))
                outfile.close()
Esempio n. 5
0
def ping_test(host, ping_count='2'):
    """
    ping

    :param param1: this is a first param
    :param param2: this is a second param
    :returns: True 主机Ping 通
    :raises keyError: raises an exception
    @author: jhuang
    @time:01/03/2018
    """
    import subprocess
    with open(os.devnull, "wb") as limbo:
        parm_count = '-n'
        if is_linux_system():
            parm_count = '-c'
        result = subprocess.Popen(["ping", parm_count, ping_count, host], stdout=limbo, stderr=limbo).wait()
        if result:
            print host, "inactive"
            return False
        else:
            print host, "active"
            return True