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/metal/quality.py
#encoding=utf-8

import os
import sys
import re
import json
import time
import fcntl
import datetime
import traceback
import random

###only for debug
if __name__ == '__main__':
    try:
        sys.path.append(os.getcwd() + '/../../../comm/')
    except Exception, e:
        sys.stderr.write("Set Module Directory Failed")
        sys.exit(1)

import constant
from plugin_base import VmBaseCollector
from utils.metric_handler import MetricHandler
from cutils import CommUtils

if __name__ == '__main__':
    TOOL_PATH = "../../../tools"
    LOG_PATH = "../../../log"
else:
    TOOL_PATH = "../tools"
    LOG_PATH = "../log"

class QualityCollect:
    """
    Host quality information collection
    """
    def __init__(self, logger, frequency=60):
        self.logger = logger
        self.frequency = frequency 
        self.data = {
            'collectTime':"",
            'smi':-1
        }
        
    def getSmiInfo(self):
        log_file = '%s/.smi_info' % (LOG_PATH)
        count = 0
        smi_index = 0
        try:
            if os.path.exists(log_file):
                with open(log_file) as fd:
                    line = fd.readline()
                    arr = line.strip().split()
                    if 'Core' in arr and 'CPU' in arr and 'SMI' in arr:
                        smi_index = arr.index('SMI')

                    if smi_index != 0:
                        for line in fd.readlines():
                            arr = line.strip().split()
                            if len(arr) > smi_index:
                                if(arr[0] == '-') and (arr[1] == '-'):
                                    count = int(arr[smi_index])
            cmd = 'ps -ef | grep turbostat | grep -v grep | grep -v timeout | wc -l'
            output = CommUtils.ExecuteTimeoutCommand(cmd, 3)
            if(int(output) <= 0):
                cmd = 'timeout %ss %s/turbostat --quiet -i %s > %s &' % (self.frequency-1, TOOL_PATH, self.frequency-2, log_file)
                CommUtils.ExecuteTimeoutCommand(cmd, 3)
        except Exception, e:
            self.logger.error("%s", str(e))

        self.data['smi'] = count
    
    def doCollect(self):
        """
        Collect host quality information
        """
        self.data['collectTime'] = int(time.time())
        self.getSmiInfo()

    def getCollectData(self):
        return self.data

class QualityCollector(VmBaseCollector):
    def init(self):
        self.set_frequency(60)
        self._collector = QualityCollect(self.logger(), self.frequency())
        self._handle = MetricHandler()
        self._handle.namespace = 'qce/cvm'
        self._last_report_nws_time = 0

    def do_collect(self):
        self.logger().info("QualityCollector start to run")
        now = int(time.time())
        try:
            self._collector.doCollect()
        except Exception,e:
            self.logger().error(traceback.format_exc().replace('\n','\\n'))

        hostDataInfo = self._collector.getCollectData()
        if now - self._last_report_nws_time >= 60:
            self._handle.clean_metrics()
            dimension = {'vmip': self.get_vmip(),'vm_uuid': self.get_vm_uuid()}
            timestamp = hostDataInfo['collectTime']

            batch_metric = []
            
            metric = {'name': 'smi', 'value': hostDataInfo['smi'], 'unit': 'int'}
            batch_metric.append(metric)

            self._handle.add_batch_metric(dimensions = dimension, timestamp = timestamp, batch = batch_metric)

            if (len(self._handle.get_metrics()) > 0):
                data = {'sender':'nws_sender', 'datas': self._handle.pop_metrics()}
                self.put_data(data)
                self._last_report_nws_time = now

def main():
    collector = QualityCollector()
    while True:
        collector.collect()
        collector.dump_data()
        time.sleep(60)

if __name__ == '__main__':
    main()