HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux WebLive 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //usr/local/qcloud/monitor/barad/plugin/collector/vm/barad_health.py
import sys
import time
import os

if __name__ == "__main__":
    try:
        sys.path.append(os.getcwd() + '/../')
        sys.path.append(os.getcwd() + '/../../base/')
        sys.path.append(os.getcwd() + '/../../../comm/')
    except Exception as e:
        sys.stderr.write("Set Module Directory Failed")
        sys.exit(1)
        
from utils.metric_handler import MetricHandler
from phealth import PluginHealth
from plugin_base import VmBaseCollector

class baradInfo(object):
    def __init__(self, logger):
        self.logger = logger
        self.plugin = PluginHealth()

    def do_collect(self):
        barad_map = {}
        try:
            #barad_map["cpu_usage"] = self.plugin.get_cpu_percent()
            barad_mem = self.plugin.get_rss_memory() / 1024.0
            barad_map["memory_used"] = "%.3f" % barad_mem
        except Exception as e:
            self.logger.info("failed to collect barad_agent component, error: %s" % e)
        return barad_map
    
    
class PluginCollector(VmBaseCollector):
    def init(self):
        self.set_frequency(60)
        self.handler = MetricHandler()
        self.handler.namespace = 'qce/cvm'
        self.handler.dimensions = ['vm_uuid']
        self._barad_collector = baradInfo(self.logger())
    
    
    def do_collect(self):
        vm_uuid = self.get_vm_uuid()
        barad_data = self._barad_collector.do_collect()
        batch_metric = []
        #metric = {"name": "barad_cpu_usage", "value": barad_data.get("cpu_usage", 0.0), "unit": "%"}
        #batch_metric.append(metric)
        metric = {"name": "barad_mem_used", "value": barad_data.get("memory_used", 0.0), "unit": "MB"}
        batch_metric.append(metric)
        dimensions = {'vm_uuid': vm_uuid}
        self.handler.add_batch_metric(batch=batch_metric, dimensions=dimensions)
        if (len( self.handler.get_metrics()) > 0) :
            data = {'sender':'nws_sender', 'datas': self.handler.pop_metrics()}
            self.put_data(data)
            
def main():
    collector = PluginCollector()
    while True:
        beg = time.time()
        collector.collect()
        collector.dump_data()
        time.sleep(10)

if __name__ == "__main__":
    main()