def find_free_mem(self):
        """
        查询空闲内存内存百分比
        :return:
        """

        cmd = " top -b -n1 | head -5 "
        lines = self.exe_cmd(cmd)
        freeMem = None
        totalMem = None
        cachedMem = None
        for line in lines:
            line = line.strip("\r\n")
            if line.startswith("Mem"):
                valList = line.split(",")
                totalMem = strcm.remove_space(valList[0]).split(" ")[1].rstrip("k")
                freeMem = valList[2].strip().split(" ")[0].rstrip("k")

            if line.startswith("Swap"):
                valList = line.split(",")
                cachedMem = strcm.remove_space(valList[3].strip()).split(" ")[0].rstrip("k")

        if freeMem is not None and cachedMem is not None and totalMem is not None:
            freeRate = (float(freeMem) + float(cachedMem)) * 100 / float(totalMem)
            return float("%.1f" % freeRate)

        return None
    def read_tomcat_dict(self, config_file):
        """
        读取Nginx配置文件
        :param config_file:配置文件路径
        :return: 配置字典
        """

        remote_file = self.sftp.open(config_file, 'r')
        config_data = {}
        # read any new lines from the file
        line = remote_file.readline()
        while line:
            line = line.strip('\r\n').strip()
            #
            if line.startswith("<Context "):
                # 去掉冗余空格
                line = strcm.remove_space(line)
                dataList = line.split(" ")
                for dataItem in dataList:
                    if dataItem.find("=") > 0:
                        propList = dataItem.split("=")
                        key = propList[0]
                        if key in TOMCAT_KEYS and key not in config_data:
                            config_data[key] = propList[1].strip("\"'")

            line = remote_file.readline()
        remote_file.close()

        return config_data
    def read_nginx_dict(self, config_file):
        """
        读取Nginx配置文件
        :param config_file:配置文件路径
        :return: 配置字典
        """

        remote_file = self.sftp.open(config_file, 'r')
        config_data = {}
        # read any new lines from the file
        line = remote_file.readline()
        while line:
            line = line.strip('\r\n').strip()
            # 排除注释行
            if not line.startswith("#"):
                # 去掉冗余空格
                line = strcm.remove_space(line)
                line = line.rstrip(";").rstrip(" main").rstrip(" crit")
                dataList = line.split(" ")
                if len(dataList) >= 2:
                    key = dataList[0]
                    if key in NGX_KEYS and key not in config_data:
                        config_data[key] = " ".join(dataList[1:])
            line = remote_file.readline()
        remote_file.close()

        return config_data
    def read_nginx_upstream(self, config_file):
        """
        读取Nginx配置文件中的upstream
        :param config_file:配置文件路径
        :return: 配置字典
        """

        remote_file = self.sftp.open(config_file, 'r')
        upstream_data = {}
        isStart = False
        upstreamKey = None
        serverList = []

        # read any new lines from the file
        line = remote_file.readline()
        while line:
            line = line.strip('\r\n').strip()
            # 排除注释行
            if not line.startswith("#"):
                # 去掉冗余空格
                line = strcm.remove_space(line)
                if not isStart:
                    # 查找定义开头部分
                    if line.startswith("upstream "):
                        isStart = True
                        dataList = line.split(" ")
                        upstreamKey = dataList[1].rstrip("{")
                        serverList = []
                else:
                    # 查找服务器
                    if line.startswith("server "):
                        dataList = line.split(" ")
                        serverList.append(dataList[1].rstrip(";"))
                    # 查找定义结尾
                    if line.startswith("}"):
                        upstream_data[upstreamKey] = ";".join(serverList)
                        isStart = False

            line = remote_file.readline()
        remote_file.close()

        return upstream_data
    def find_up_days(self):
        """
        查询启动天数
        :return:
        """

        cmd = " top -b -n1 | head -1 "
        lines = self.exe_cmd(cmd)
        for line in lines:
            line = line.strip("\r\n")
            if line.startswith("top "):
                if line.find("days") > 0:
                    valList = line.split(",")
                    days = strcm.remove_space(valList[0]).split(" ")[4]
                    # logcm.print_obj(days, "days")
                    return float(days)
                else:
                    return 1.0

        return None
    def find_used_space(self):
        """
        查询已用磁盘Space
        :return:
        """

        cmd = "df -h"
        lines = self.exe_cmd(cmd)
        for line in lines:
            line = line.strip("\r\n")
            # 去掉冗余空格
            line = strcm.remove_space(line)
            valList = line.split(" ")
            valLen = len(valList)
            # 根目录
            if valList[valLen - 1] == "/":
                usedSpace = valList[valLen - 2].rstrip("%")
                # logcm.print_obj(usedSpace, "usedSpace")
                return float(usedSpace)

        return None
    def read_nginx_proxy_pass(self, config_file):
        """
        读取Nginx配置文件中的upstream
        :param config_file:配置文件路径
        :return: 配置字典
        """

        remote_file = self.sftp.open(config_file, 'r')
        proxyPassList = []

        # read any new lines from the file
        line = remote_file.readline()
        while line:
            line = line.strip('\r\n').strip()
            # 排除注释行
            if not line.startswith("#"):
                # 去掉冗余空格
                line = strcm.remove_space(line)
                # 查找定义开头部分
                if line.startswith("proxy_pass "):
                    dataList = line.split(" ")
                    proxyPassStr = dataList[1].rstrip(";")
                    # 去掉http开头
                    proxyPassStr = proxyPassStr.replace("http://", "")
                    # 去掉相对路径部分
                    pos = proxyPassStr.find("/")
                    if pos > 0:
                        proxyPassStr = proxyPassStr[0:pos]
                    # 去掉$部分
                    pos = proxyPassStr.find("$")
                    if pos > 0:
                        proxyPassStr = proxyPassStr[0:pos]
                    # 避免重复数据
                    if proxyPassStr not in proxyPassList:
                        proxyPassList.append(proxyPassStr)

            line = remote_file.readline()
        remote_file.close()

        return proxyPassList