MOON
Server: Apache
System: Linux vps.erhabenn.com.br 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User: machen (1008)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: //opt/microsoft/omsagent/tst/modules/high_cpu_mem/check_slabmem.py
import os
import subprocess

from error_codes       import *
from errors            import error_info
from .check_cpu        import get_pkg_ver
from install.check_oms import comp_versions_ge

def check_strace():
    dne_errs = 0
    strace_errors = subprocess.Popen(['strace','-f','-e','trace=access','curl',"'https://www.google.com'"],\
                        stderr=subprocess.PIPE).communicate()[1]
    # count of doesnotexist errors
    for line in (strace_errors.decode('utf8').split('\n')):
        if (line.endswith('= -1 ENOENT (No such file or directory)')):
            dne_errs += 1
    return (dne_errs <= 300)


def check_nss_var(slabtop_10_pretty):
    try:
        nss_var = subprocess.check_output(['printenv','NSS_SDB_USE_CACHE'], universal_newlines=True)
        if (nss_var == 'yes\n'):
            error_info.append((slabtop_10_pretty,))
            return ERR_SLAB_BLOATED
        else:
            return ERR_SLAB_NSS

    # no variable named NSS_SDB_USE_CACHE
    except subprocess.CalledProcessError:
        return ERR_SLAB_NSS
                


def check_slab_memory():
    # no issues found
    if (check_strace()):
        return NO_ERROR

    # >300 DNE error messages called
    try:
        slabtop_output = subprocess.check_output(['slabtop','--once','--sort','c'],\
                            universal_newlines=True, stderr=subprocess.STDOUT)
        slabtop_lines = slabtop_output.split('\n')

        # get top 10 objects based on cache size
        # [ objs, active, use (%), obj size (K), slabs, obj/slab, cache size (K), name ]
        slabtop_10 = list(map((lambda x : x.split()), (slabtop_lines[7:17])))
        for slabtop_line in slabtop_10:

            # dentry in top 10
            if (slabtop_line[-1] == 'dentry'):
                nss_ver = get_pkg_ver('nss-softokn')
                if (comp_versions_ge(nss_ver, '3.14.3-12.el6')):
                    slabtop_10_pretty = slabtop_lines[6:17]
                    return check_nss_var(slabtop_10_pretty)
                else:
                    return ERR_SLAB_NSSSOFTOKN

        # dentry not in top 10
        return ERR_SLAB_BLOATED

    # errored in running slabtop
    except subprocess.CalledProcessError as e:
        if (e.output.endswith('Permission denied\n')):
            return ERR_SUDO_PERMS
        else:
            error_info.append((e.output,))
            return ERR_SLAB