Exemple #1
0
def CheckSection(section, isSetting=False):
    """
    function: check the section parameters status
    input: section, isSetting
    output: NA
    """

    global configFile
    dirName = os.path.dirname(os.path.realpath(__file__))
    configFile = "%s/../gspylib/etc/conf/check_list.conf" % dirName

    # get the parameter and value about section from configuration file
    if (section == '/etc/security/limits.conf'):
        checkList = ['open files', 'pipe size']
        commParameterList = DefaultValue.getConfigFilePara(
            configFile, section, checkList)
    else:
        commParameterList = DefaultValue.getConfigFilePara(configFile, section)

    # checking or setting the parameter what in the commParameterList
    checkSpecifiedItems(section, commParameterList, isSetting)
def checkNetWorkMTU():
    """
    function: gs_check check NetWork card MTU parameters
    input: NA
    output: int
    """
    try:
        # Init cluster info
        DbClusterInfo = dbClusterInfo()
        DbClusterInfo.initFromStaticConfig(g_opts.user)
        localHost = DefaultValue.GetHostIpOrName()
        nodeIp = None
        for dbnode in DbClusterInfo.dbNodes:
            if (dbnode.name == localHost):
                nodeIp = dbnode.backIps[0]
                break
        networkCardNum = DefaultValue.CheckNetWorkBonding(nodeIp, False)
        # check NetWork card MTU parameters
        valueStr = DefaultValue.checkNetWorkMTU(nodeIp, False)
        if (not str(valueStr).isdigit()):
            g_logger.log("Abnormal reason: Failed to obtain network"
                         " card MTU value." + " Error: \n%s" % valueStr)
            return 1
        netParameter = DefaultValue.getConfigFilePara(configFile,
                                                      '/sbin/ifconfig')
        if (int(valueStr) != int(g_opts.mtuValue)):
            g_logger.log("        Abnormal: network '%s' 'mtu' value[%s:%s]"
                         " is different from the other node [%s:%s]"
                         % (networkCardNum, localHost, valueStr,
                            g_opts.hostname, g_opts.mtuValue))
            return 1
        elif (int(valueStr) != int(netParameter["mtu"])):
            g_logger.log("        Warning reason: variable 'MTU' RealValue "
                         "'%s' ExpectedValue '%s'." % (valueStr,
                                                       netParameter["mtu"]))
            return 2
        else:
            return 0

    except Exception as e:
        g_logger.log("        Abnormal reason: Failed to obtain the"
                     " networkCard parameter [MTU]. Error: \n        %s"
                     % str(e))
        return 1
def checkSysctlParameter(kernelParameter, isSet):
    """
    function: check and set the OS parameters
    input: kernelParameter: OS parameters list will be check and set
           isSet: the flag, when it is only True then will set OS parameters
    output: NA
    """
    setParameterList = {}
    patchlevel = ""

    # get the suggest parameters and updata kernelParameter
    suggestParameterList = DefaultValue.getConfigFilePara(
        configFile, 'SUGGEST:%s' % actioItemMap["Check_SysCtl_Parameter"][0])
    kernelParameter.update(suggestParameterList)

    # check the OS parameters
    if ("fs.aio-max-nr" in kernelParameter):
        g_logger.log("        Warning reason: Checking or setting the"
                     " parameter 'fs.aio-max-nr' should be use "
                     "'gs_checkos -i A10' or 'gs_checkos -i B4'.")
        kernelParameter.pop("fs.aio-max-nr")
    # Get OS version
    distname, version = g_Platform.dist()[0:2]
    if (distname == "SuSE" and version == "11"):
        cmd = "grep -i 'PATCHLEVEL' /etc/SuSE-release  |" \
              " awk -F '=' '{print $2}'"
        (status, output) = subprocess.getstatusoutput(cmd)
        if (status == 0 and output != ""):
            patchlevel = output.strip()
    # Gs_check get NetWork card MTU
    if (g_opts.action == "osKernelParameterCheck"):
        checkrestult = checkNetWorkMTU()
        if checkrestult != 0:
            resultList.append(checkrestult)

    for key in kernelParameter:
        # The SuSE 11 SP1 operating system
        # does not have vm.extfrag_threshold parameter, skip check
        if (patchlevel == "1" and key == "vm.extfrag_threshold"):
            continue
        sysFile = "/proc/sys/%s" % key.replace('.', '/')
        # High version of linux no longer supports tcp_tw_recycle
        if not os.path.exists(
                sysFile) and key == "net.ipv4.tcp_tw_recycle":
            continue
        if (DefaultValue.checkDockerEnv() and key in docker_no_need_check):
            continue
        # The parameter sctpchecksumerrors check method is independent
        if (key == "sctpchecksumerrors"):
            cmd = "cat /proc/net/sctp/snmp | grep SctpChecksumErrors" \
                  " | awk '{print $2}'"
        else:
            cmd = "cat %s" % ("/proc/sys/%s" % key.replace('.', '/'))
        (status, output) = subprocess.getstatusoutput(cmd)
        if (status == 0):
            if (key == "vm.min_free_kbytes"
                    and output.split() != kernelParameter[key].split()):
                expected_min = float(kernelParameter[key].split()[0]) * 0.9
                expected_max = float(kernelParameter[key].split()[0]) * 1.1
                if (int(output.split()[0]) > expected_max
                        or int(output.split()[0]) < expected_min):
                    resultList.append(1)
                    g_logger.log("        Abnormal reason: variable '%s'"
                                 " RealValue '%s' ExpectedValue '%s'."
                                 % (key, output, kernelParameter[key]))
                    setParameterList[key] = kernelParameter[key]
            elif (key == "net.ipv4.ip_local_port_range"
                  and output.split() != kernelParameter[key].split()):
                expected_min = int(kernelParameter[key].split()[0])
                expected_max = int(kernelParameter[key].split()[1])
                if (int(output.split()[0]) < expected_min
                        or int(output.split()[1]) > expected_max):
                    resultList.append(2)
                    g_logger.log("        Warning reason: variable '%s'"
                                 " RealValue '%s' ExpectedValue '%s'."
                                 % (key, output, kernelParameter[key]))
            elif (output.split() != kernelParameter[key].split() and
                  key not in list(suggestParameterList.keys())):
                resultList.append(1)
                g_logger.log("        Abnormal reason: variable '%s'"
                             " RealValue '%s' ExpectedValue '%s'."
                             % (key, output, kernelParameter[key]))
                setParameterList[key] = kernelParameter[key]
            elif output.split() != kernelParameter[key].split():
                if (key == "vm.overcommit_ratio"):
                    cmd = "cat /proc/sys/vm/overcommit_memory"
                    (status, value) = subprocess.getstatusoutput(cmd)
                    if (status == 0 and value == "0"):
                        continue
                resultList.append(2)
                g_logger.log("        Warning reason: variable '%s' RealValue"
                             " '%s' ExpectedValue '%s'."
                             % (key, output, kernelParameter[key]))
        else:
            resultList.append(1)
            g_logger.log("        Abnormal reason: Failed to obtain the OS "
                         "kernel parameter [%s]. Error: \n        %s"
                         % (key, output))
            setParameterList[key] = kernelParameter[key]

    if (1 in resultList and 'Check' in g_opts.action):
        g_logger.log("        %s failed." % g_opts.action)
    elif (2 in resultList and 'Check' in g_opts.action):
        g_logger.log("        %s warning." % g_opts.action)
    else:
        g_logger.log("        All values about system control"
                     " parameters are correct: Normal")

    # set the OS parameters
    if isSet:
        setOSParameter(setParameterList, patchlevel)