<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import threading
import time
import re
import copy

descriptors = list()
Desc_Skel   = {}
_Worker_Thread = None
_Lock = threading.Lock() # synchronization lock

Debug = False

def dprint(f, *v):
    if Debug:
        print >>sys.stderr, "DEBUG: "+f % v

class UpdateTrafficThread(threading.Thread):

    __slots__ = ( 'proc_file' )

    def __init__(self, params):
        threading.Thread.__init__(self)
        self.running       = False
        self.shuttingdown  = False
        self.refresh_rate = 10
        if "refresh_rate" in params:
            self.refresh_rate = int(params["refresh_rate"])

        self.target_device = params["target_device"]
        self.metric       = {}

        self.proc_file = "/proc/net/dev"
        self.stats_tab = {
            "recv_bytes"  : 0,
            "recv_pkts"   : 1,
            "recv_errs"   : 2,
            "recv_drops"  : 3,
            "trans_bytes" : 8,
            "trans_pkts"  : 9,
            "trans_errs"  : 10,
            "trans_drops" : 11,
            }
        self.stats      = {}
        self.stats_prev = {}

    def shutdown(self):
        self.shuttingdown = True
        if not self.running:
            return
        self.join()

    def run(self):
        self.running = True

        while not self.shuttingdown:
            _Lock.acquire()
            self.update_metric()
            _Lock.release()
            time.sleep(self.refresh_rate)

        self.running = False

    def update_metric(self):
        f = open(self.proc_file, "r")
        for l in f:
            a = l.split(":")
            dev = a[0].lstrip()
            if dev != self.target_device: continue

            dprint("%s %s", dev, ">>update_metric");
            self.stats = {}
            _stats = a[1].split()
            for name, index in self.stats_tab.iteritems():
                self.stats[name+'_'+self.target_device] = int(_stats[index])
            self.stats["time"] = time.time()
            dprint("%s", self.stats)

            if "time" in self.stats_prev:
                dprint("%s: %d = %d - %d", "DO DIFF", self.stats["time"]-self.stats_prev["time"], self.stats["time"], self.stats_prev["time"])
                d = self.stats["time"] - self.stats_prev["time"]
                for name, cur in self.stats.iteritems():
                    self.metric[name] = float(cur - self.stats_prev[name])/d

            self.stats_prev = self.stats.copy()
            break

        return

    def metric_of(self, name):
        val = 0
        if name in self.metric:
            _Lock.acquire()
            val = self.metric[name]
            _Lock.release()
        return val



def metric_init(params):
    global descriptors, Desc_Skel, _Worker_Thread, Debug

    print '[multi_traffic] Received the following parameters'
    print params

    Desc_Skel = {
        'name'        : 'XXX',
        'call_back'   : metric_of,
        'time_max'    : 60,
        'value_type'  : 'float',
        'format'      : '%.3f',
        'units'       : 'XXX',
        'slope'       : 'both',
        'description' : 'XXX',
        'groups'      : 'Network Interface Traffic',
        }

    if "refresh_rate" not in params:
        params["refresh_rate"] = 10
    if "debug" in params:
        Debug = params["debug"]
    dprint("%s", "Debug mode on")
    
    define_device = False
    for para in params.keys():
         if para.startswith('target_device'): define_device = True
    if not define_device:<commit_msg>Rename group<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import threading
import time
import re
import copy

descriptors = list()
Desc_Skel   = {}
_Worker_Thread = None
_Lock = threading.Lock() # synchronization lock

Debug = False

def dprint(f, *v):
    if Debug:
        print >>sys.stderr, "DEBUG: "+f % v

class UpdateTrafficThread(threading.Thread):

    __slots__ = ( 'proc_file' )

    def __init__(self, params):
        threading.Thread.__init__(self)
        self.running       = False
        self.shuttingdown  = False
        self.refresh_rate = 10
        if "refresh_rate" in params:
            self.refresh_rate = int(params["refresh_rate"])

        self.target_device = params["target_device"]
        self.metric       = {}

        self.proc_file = "/proc/net/dev"
        self.stats_tab = {
            "recv_bytes"  : 0,
            "recv_pkts"   : 1,
            "recv_errs"   : 2,
            "recv_drops"  : 3,
            "trans_bytes" : 8,
            "trans_pkts"  : 9,
            "trans_errs"  : 10,
            "trans_drops" : 11,
            }
        self.stats      = {}
        self.stats_prev = {}

    def shutdown(self):
        self.shuttingdown = True
        if not self.running:
            return
        self.join()

    def run(self):
        self.running = True

        while not self.shuttingdown:
            _Lock.acquire()
            self.update_metric()
            _Lock.release()
            time.sleep(self.refresh_rate)

        self.running = False

    def update_metric(self):
        f = open(self.proc_file, "r")
        for l in f:
            a = l.split(":")
            dev = a[0].lstrip()
            if dev != self.target_device: continue

            dprint("%s %s", dev, ">>update_metric");
            self.stats = {}
            _stats = a[1].split()
            for name, index in self.stats_tab.iteritems():
                self.stats[name+'_'+self.target_device] = int(_stats[index])
            self.stats["time"] = time.time()
            dprint("%s", self.stats)

            if "time" in self.stats_prev:
                dprint("%s: %d = %d - %d", "DO DIFF", self.stats["time"]-self.stats_prev["time"], self.stats["time"], self.stats_prev["time"])
                d = self.stats["time"] - self.stats_prev["time"]
                for name, cur in self.stats.iteritems():
                    self.metric[name] = float(cur - self.stats_prev[name])/d

            self.stats_prev = self.stats.copy()
            break

        return

    def metric_of(self, name):
        val = 0
        if name in self.metric:
            _Lock.acquire()
            val = self.metric[name]
            _Lock.release()
        return val



def metric_init(params):
    global descriptors, Desc_Skel, _Worker_Thread, Debug

    print '[multi_traffic] Received the following parameters'
    print params

    Desc_Skel = {
        'name'        : 'XXX',
        'call_back'   : metric_of,
        'time_max'    : 60,
        'value_type'  : 'float',
        'format'      : '%.3f',
        'units'       : 'XXX',
        'slope'       : 'both',
        'description' : 'XXX',
        'groups'      : 'network_interface_traffic',
        }

    if "refresh_rate" not in params:
        params["refresh_rate"] = 10
    if "debug" in params:
        Debug = params["debug"]
    dprint("%s", "Debug mode on")
    
    define_device = False
    for para in params.keys():
         if para.startswith('target_device'): define_device = True
    if not define_device:
{'fraction_non_alphanumeric': 0.12754600365219834, 'fraction_numerical': 0.0058997050147492625, 'mean_word_length': 1.7426810477657935, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4033433', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2169, 'n_words': 492}