HEX
Server: LiteSpeed
System: Linux kapuas.iixcp.rumahweb.net 5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64
User: mirz4654 (1666)
PHP: 8.1.33
Disabled: system,exec,escapeshellarg,escapeshellcmd,passthru,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,popen,pclose,dl,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setsid,posix_setuid,posix_setpgid,ini_alter,show_source,define_syslog_variables,symlink,syslog,openlog,openlog,closelog,ocinumcols,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dll,ftp,myshellexec,socket_bind,mail,posix_getwpuid
Upload Files
File: //opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/plugins/other/res_mem_collector.py
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from clcommon.utils import run_command
from lvestats.core.plugin import LveStatsPlugin

LVEPS_COMMAND = '/usr/sbin/lveps'


class ResMEMCollector(LveStatsPlugin):
    order = 1500  # should run after other collectors, but before analyzers
    timeout = 15
    """
    The goal of this plugin is to substitute physical memory readings from /proc/lve/list
    with RES data from /proc/* for each process within the LVE
    The data is extracted from lveps -p command

    """
    @staticmethod
    def parse_mem_per_lve(lines):
        """
        parse the output of lveps -p -n -o id:10,mem:15
        lveps returns memory in megabytes. The data for LVE is taken from /proc/lve/list
        the data for processes is taken from /proc/PID/... and is what we need.
        :param: lines list of lines, as outputed by lveps
        :return: dictionary with LVE id as keys, and memory in bytes as values
        """
        result = {}
        lve_id = None
        mem = 0
        for i in range(1, len(lines)):
            val = lines[i].split()
            if len(val) == 2:
                if lve_id is not None:
                    result[lve_id] = mem * 256  # the mem comes in MB, we need to convert it into num of 4k pages
                mem = 0
                lve_id = int(val[0])
            elif len(val) == 1:
                mem += float(val[0])
        if lve_id is not None:
            result[lve_id] = mem * 256  # the mem comes in MB, we need to convert it into num of 4k pages
        return result

    @staticmethod
    def get_mem_per_lve():
        """
        Get amount of memory processes inside LVE use
        :return: dictionary of LVE_ID <-> MEM
        """
        lveps_output = run_command([LVEPS_COMMAND, '-p', '-n', '-o', 'id:10,mem:15'])
        return ResMEMCollector.parse_mem_per_lve(lveps_output.split('\n'))

    def execute(self, lve_data):
        self.update_lve_data(lve_data, self.get_mem_per_lve())

    @staticmethod
    def update_lve_data(lve_data, pmem_dict):
        """
        updates lve_data['stats'] pmem values for all LVEs
        Only update LVEs already in lve_data['stats'], skip others

        :param lve_data: plugin's lve_data
        :param pmem_dict: dictionary of LVE id <-> memory used in bytes
        :return: updated stats structure in lve_data
        """
        stats = lve_data['stats']
        for lve_id in stats:
            stat = stats[lve_id]
            if lve_id in pmem_dict:
                limit = stat.lmemphy
                pmem = pmem_dict[lve_id]
                if limit and pmem > limit:
                    stat.memphy = limit
                else:
                    stat.memphy = pmem_dict[lve_id]
            else:
                stat.memphy = 0