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/rdma_net.py
import sys
import os, time

sys.path.append(os.getcwd() + '/../../../comm/')
import constant
from plugin_base import VmBaseCollector
from utils.metric_handler import MetricHandler


class NetworkCollect:
    def __init__(self):
        self.isrdma = self.check_rdma_host()

    def check_rdma_host(self):
        file_path = '/etc/host_bond_info.conf'
        if not os.path.exists(file_path):
            return False
        with open(file_path) as fd:
            res = fd.readlines()
            for line in res:
                if line.find('rdma') > -1:
                    return True

    def get_network_info(self):
        net_buffer = {}
        with open("/proc/net/dev") as net_file:
            contents = net_file.readlines()
            for line in contents:
                if line.find(":") < 0:
                    continue
                card_name = line.split(":")[0].strip()
                if card_name == 'bond0':
                    line_fields = line.split(":")[1].lstrip().split()
                    net_buffer[card_name] = {
                        "InBytes": int(line_fields[0]),
                        "InPackets": int(line_fields[1]),
                        "OutBytes": int(line_fields[8]),
                        "OutPackets": int(line_fields[9])
                    }
                    break
        return net_buffer


def test_network_collect():
    collector = NetworkCollect()
    net_buffer = collector.get_network_info()
    print 'bond0 : ', net_buffer.get('bond0')


class NetCollector(VmBaseCollector):
    def init(self):
        self.set_frequency(10)
        self.collector = NetworkCollect()
        self.handler = MetricHandler()
        self.last_net_info = {}
        self.handler.namespace = 'qce/cvm'
        self.uuid= self.get_vm_uuid()
        self.last_report_time = 0

    def do_collect(self):
        if self.collector.isrdma is False:
            return
        current_time = int(time.time())
        net_info = self.collector.get_network_info()
        vm_uuid = self.get_vm_uuid()
        dimensions = {'vm_uuid': self.uuid}
        batch_metric = []
        for key in self.last_net_info.keys():
            delta_time = current_time - self.last_report_time
            in_traffic = float(net_info[key]["InBytes"] - self.last_net_info[key]["InBytes"]) / delta_time
            out_traffic = float(net_info[key]["OutBytes"] - self.last_net_info[key]["OutBytes"]) / delta_time
            in_packets = float(net_info[key]["InPackets"] - self.last_net_info[key]["InPackets"]) / delta_time
            out_packets = float(net_info[key]["OutPackets"] - self.last_net_info[key]["OutPackets"]) / delta_time
            #only report bond0 net stat
            if key == 'bond0':
                metric = {'name': 'rdma_inpkg', 'value': in_packets, 'unit': 'pps'}
                batch_metric.append(metric)
                metric = {'name': 'rdma_outpkg', 'value': out_packets, 'unit': 'pps'}
                batch_metric.append(metric)
                metric = {'name': 'rdma_intraffic', 'value': in_traffic, 'unit': 'Bps'}
                batch_metric.append(metric)
                metric = {'name': 'rdma_outtraffic', 'value': out_traffic, 'unit': 'Bps'}
                batch_metric.append(metric)

        self.last_net_info = net_info
        self.last_report_time = current_time
        self.handler.add_batch_metric(batch=batch_metric, dimensions=dimensions, timestamp=current_time)
        if (len(self.handler.get_metrics()) > 0):
            data = {'sender': 'nws_sender', 'datas': self.handler.pop_metrics()}
            self.put_data(data)


def main():
    collector = NetCollector()
    while 1:
        collector.collect()
        collector.dump_data()
        time.sleep(3)


if __name__ == '__main__':
    main()