def collect(self): """ Collect network interface stats. """ # Initialize results results = {} if os.access(self.PROC, os.R_OK): # Open File file = open(self.PROC) # Build Regular Expression greed = '' if self.config['greedy'].lower() == 'true' : greed = '[0-9]+' exp = '^(?:\s*)((?:%s)%s):(?:\s*)(?P<rx_bytes>\d+)(?:\s*)(?P<rx_packets>\w+)(?:\s*)(?P<rx_errors>\d+)(?:\s*)(?P<rx_drop>\d+)(?:\s*)(?P<rx_fifo>\d+)(?:\s*)(?P<rx_frame>\d+)(?:\s*)(?P<rx_compressed>\d+)(?:\s*)(?P<rx_multicast>\d+)(?:\s*)(?P<tx_bytes>\d+)(?:\s*)(?P<tx_packets>\w+)(?:\s*)(?P<tx_errors>\d+)(?:\s*)(?P<tx_drop>\d+)(?:\s*)(?P<tx_fifo>\d+)(?:\s*)(?P<tx_frame>\d+)(?:\s*)(?P<tx_compressed>\d+)(?:\s*)(?P<tx_multicast>\d+)(?:.*)$' % (( '|'.join(self.config['interfaces']) ), greed) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() elif psutil: network_stats = psutil.network_io_counters(True) for device in network_stats.keys(): results[device] = {} results[device]['rx_bytes'] = network_stats[device].bytes_recv results[device]['tx_bytes'] = network_stats[device].bytes_sent results[device]['rx_packets'] = network_stats[device].packets_recv results[device]['tx_packets'] = network_stats[device].packets_sent for device in results: stats = results[device] for s,v in stats.items(): # Get Metric Name metric_name = '.'.join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), self.MAX_VALUES[s]) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value = metric_value, unit = 'byte') for u in self.config['byte_unit']: # Public Converted Metric self.publish(metric_name.replace('bytes', u), convertor.get(unit = u)) else: # Publish Metric Derivative self.publish(metric_name, metric_value) return None
def collect(self): """ Collect network interface stats. """ # Initialize results results = {} if os.access(self.PROC, os.R_OK): # Open File file = open(self.PROC) # Build Regular Expression greed = "0-9" if self.config["greedy"].lower == "true" else "" exp = ( "^(?:\s*)([%s%s]+):(?:\s*)(?P<rx_bytes>\d+)(?:\s*)(?P<rx_packets>\w+)(?:\s*)(?P<rx_errors>\d+)(?:\s*)(?P<rx_drop>\d+)(?:\s*)(?P<rx_fifo>\d+)(?:\s*)(?P<rx_frame>\d+)(?:\s*)(?P<rx_compressed>\d+)(?:\s*)(?P<rx_multicast>\d+)(?:\s*)(?P<tx_bytes>\d+)(?:\s*)(?P<tx_packets>\w+)(?:\s*)(?P<tx_errors>\d+)(?:\s*)(?P<tx_drop>\d+)(?:\s*)(?P<tx_fifo>\d+)(?:\s*)(?P<tx_frame>\d+)(?:\s*)(?P<tx_compressed>\d+)(?:\s*)(?P<tx_multicast>\d+)(?:.*)$" % (("|".join(self.config["interfaces"])), greed) ) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() elif psutil: network_stats = psutil.network_io_counters(True) for device in network_stats.keys(): results[device] = {} results[device]["rx_bytes"] = network_stats[device].bytes_recv results[device]["tx_bytes"] = network_stats[device].bytes_sent results[device]["rx_packets"] = network_stats[device].packets_recv results[device]["tx_packets"] = network_stats[device].packets_sent for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = ".".join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), self.MAX_VALUES[s]) # Convert rx_bytes and tx_bytes if s == "rx_bytes" or s == "tx_bytes": convertor = diamond.convertor.binary(value=metric_value, unit="byte") for u in self.config["byte_unit"].split(): # Public Converted Metric self.publish(metric_name.replace("bytes", u), convertor.get(unit=u)) else: # Publish Metric Derivative self.publish(metric_name, metric_value) return None
def collect(self): """ Collect network interface stats. """ if not os.access(self.PROC, os.R_OK): return None # Initialize results results = {} # Open File file = open(self.PROC) # Build Regular Expression exp = '^(?:\s*)([%s0-9]+):(?:\s*)(?P<rx_bytes>\d+)(?:\s*)(?P<rx_packets>\w+)(?:\s*)(?P<rx_errors>\d+)(?:\s*)(?P<rx_drop>\d+)(?:\s*)(?P<rx_fifo>\d+)(?:\s*)(?P<rx_frame>\d+)(?:\s*)(?P<rx_compressed>\d+)(?:\s*)(?P<rx_multicast>\d+)(?:\s*)(?P<tx_bytes>\d+)(?:\s*)(?P<tx_packets>\w+)(?:\s*)(?P<tx_errors>\d+)(?:\s*)(?P<tx_drop>\d+)(?:\s*)(?P<tx_fifo>\d+)(?:\s*)(?P<tx_frame>\d+)(?:\s*)(?P<tx_compressed>\d+)(?:\s*)(?P<tx_multicast>\d+)(?:.*)$' % ( '|'.join(self.config['interfaces'])) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = '.'.join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), self.MAX_VALUES[s]) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value=metric_value, unit='byte') for u in self.config['byte_unit'].split(): # Public Converted Metric self.publish(metric_name.replace('bytes', u), convertor.get(unit=u)) else: # Publish Metric Derivative self.publish(metric_name, metric_value)
def collect(self): """ Collect network interface stats. """ if not os.access(self.PROC, os.R_OK): return None # Initialize results results = {} # Open File file = open(self.PROC) # Build Regular Expression exp = '^(?:\s*)([%s0-9]+):(?:\s*)(?P<rx_bytes>\d+)(?:\s*)(?P<rx_packets>\w+)(?:\s*)(?P<rx_errors>\d+)(?:\s*)(?P<rx_drop>\d+)(?:\s*)(?P<rx_fifo>\d+)(?:\s*)(?P<rx_frame>\d+)(?:\s*)(?P<rx_compressed>\d+)(?:\s*)(?P<rx_multicast>\d+)(?:\s*)(?P<tx_bytes>\d+)(?:\s*)(?P<tx_packets>\w+)(?:\s*)(?P<tx_errors>\d+)(?:\s*)(?P<tx_drop>\d+)(?:\s*)(?P<tx_fifo>\d+)(?:\s*)(?P<tx_frame>\d+)(?:\s*)(?P<tx_compressed>\d+)(?:\s*)(?P<tx_multicast>\d+)(?:.*)$' % ( '|'.join(self.config['interfaces']) ) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() for device in results: stats = results[device] for s,v in stats.items(): # Get Metric Name metric_name = '.'.join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), self.MAX_VALUES[s]) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value = metric_value, unit = 'byte') for u in self.config['byte_unit'].split(): # Public Converted Metric self.publish(metric_name.replace('bytes', u), convertor.get(unit = u)) else: # Publish Metric Derivative self.publish(metric_name, metric_value)
def collect(self): """ Collect network interface stats. """ # Initialize results results = {} greed = '' if str_to_bool(self.config['greedy']): greed = '\S*' if os.access(self.PROC, os.R_OK): # Open File file = open(self.PROC) # Build Regular Expression exp = ( ('^(?:\s*)((?:%s)%s):(?:\s*)' + '(?P<rx_bytes>\d+)(?:\s*)' + '(?P<rx_packets>\w+)(?:\s*)' + '(?P<rx_errors>\d+)(?:\s*)' + '(?P<rx_drop>\d+)(?:\s*)' + '(?P<rx_fifo>\d+)(?:\s*)' + '(?P<rx_frame>\d+)(?:\s*)' + '(?P<rx_compressed>\d+)(?:\s*)' + '(?P<rx_multicast>\d+)(?:\s*)' + '(?P<tx_bytes>\d+)(?:\s*)' + '(?P<tx_packets>\w+)(?:\s*)' + '(?P<tx_errors>\d+)(?:\s*)' + '(?P<tx_drop>\d+)(?:\s*)' + '(?P<tx_fifo>\d+)(?:\s*)' + '(?P<tx_colls>\d+)(?:\s*)' + '(?P<tx_carrier>\d+)(?:\s*)' + '(?P<tx_compressed>\d+)(?:.*)$') % (('|'.join(self.config['interfaces'])), greed)) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() else: if not psutil: self.log.error('Unable to import psutil') self.log.error('No network metrics retrieved') return None network_stats = psutil.net_io_counters(pernic=True) exp = ('^(?:\s*)((?:%s)%s)' % (('|'.join(self.config['interfaces'])), greed)) reg = re.compile(exp) # Match Interfaces for device in network_stats.keys(): match = reg.match(device) if match: network_stat = network_stats[device] results[device] = {} results[device]['rx_bytes'] = network_stat.bytes_recv results[device]['tx_bytes'] = network_stat.bytes_sent results[device]['rx_packets'] = network_stat.packets_recv results[device]['tx_packets'] = network_stat.packets_sent for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = '.'.join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), diamond.collector.MAX_COUNTER) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value=metric_value, unit='byte') for u in self.config['byte_unit']: # Public Converted Metric self.publish(metric_name.replace('bytes', u), convertor.get(unit=u), 2) else: # Publish Metric Derivative self.publish(metric_name, metric_value) return None
def collect(self): """ Collect network interface stats. """ # Initialize results results = {} if os.access(self.PROC, os.R_OK): # Open File with open(self.PROC) as file: # Build Regular Expression greed = '' if self.config['greedy'].lower() == 'true': greed = '\S*' exp = ('^(?:\s*)((?:%s)%s):(?:\s*)' + '(?P<rx_bytes>\d+)(?:\s*)' + '(?P<rx_packets>\w+)(?:\s*)' + '(?P<rx_errors>\d+)(?:\s*)' + '(?P<rx_drop>\d+)(?:\s*)' + '(?P<rx_fifo>\d+)(?:\s*)' + '(?P<rx_frame>\d+)(?:\s*)' + '(?P<rx_compressed>\d+)(?:\s*)' + '(?P<rx_multicast>\d+)(?:\s*)' + '(?P<tx_bytes>\d+)(?:\s*)' + '(?P<tx_packets>\w+)(?:\s*)' + '(?P<tx_errors>\d+)(?:\s*)' + '(?P<tx_drop>\d+)(?:\s*)' + '(?P<tx_fifo>\d+)(?:\s*)' + '(?P<tx_colls>\d+)(?:\s*)' + '(?P<tx_carrier>\d+)(?:\s*)' + '(?P<tx_compressed>\d+)(?:.*)$') % ( ('|'.join(self.config['interfaces'])), greed) try: reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() except Exception as e: self.log.error( "Failed to parse file: {0!s}".format(e) ) else: if not psutil: self.log.error('Unable to import psutil') self.log.error('No network metrics retrieved') return None network_stats = psutil.network_io_counters(True) for device in network_stats.keys(): network_stat = network_stats[device] results[device] = {} results[device]['rx_bytes'] = network_stat.bytes_recv results[device]['tx_bytes'] = network_stat.bytes_sent results[device]['rx_packets'] = network_stat.packets_recv results[device]['tx_packets'] = network_stat.packets_sent for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = '.'.join(['net', s]) # Get Metric Value metric_value = long(v) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value=metric_value, unit='byte') for u in self.config['byte_unit']: # Public Converted Metric self.dimensions = { 'iface': device } self.publish_cumulative_counter(metric_name.replace('bytes', u), convertor.get(unit=u), precision=2) else: # Publish Metric Derivative self.dimensions = { 'iface': device } self.publish_cumulative_counter(metric_name, metric_value) return None
def collect(self): """ Collect network interface stats. """ # Initialize results results = {} greed = '' if str_to_bool(self.config['greedy']): greed = '\S*' if os.access(self.PROC, os.R_OK): # Open File file = open(self.PROC) # Build Regular Expression exp = (('^(?:\s*)((?:%s)%s):(?:\s*)' + '(?P<rx_bytes>\d+)(?:\s*)' + '(?P<rx_packets>\w+)(?:\s*)' + '(?P<rx_errors>\d+)(?:\s*)' + '(?P<rx_drop>\d+)(?:\s*)' + '(?P<rx_fifo>\d+)(?:\s*)' + '(?P<rx_frame>\d+)(?:\s*)' + '(?P<rx_compressed>\d+)(?:\s*)' + '(?P<rx_multicast>\d+)(?:\s*)' + '(?P<tx_bytes>\d+)(?:\s*)' + '(?P<tx_packets>\w+)(?:\s*)' + '(?P<tx_errors>\d+)(?:\s*)' + '(?P<tx_drop>\d+)(?:\s*)' + '(?P<tx_fifo>\d+)(?:\s*)' + '(?P<tx_colls>\d+)(?:\s*)' + '(?P<tx_carrier>\d+)(?:\s*)' + '(?P<tx_compressed>\d+)(?:.*)$') % (('|'.join(self.config['interfaces'])), greed)) reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() # Close File file.close() else: if not psutil: self.log.error('Unable to import psutil') self.log.error('No network metrics retrieved') return None network_stats = psutil.net_io_counters(pernic=True) exp = ('^(?:\s*)((?:%s)%s)' % (('|'.join(self.config['interfaces'])), greed)) reg = re.compile(exp) # Match Interfaces for device in network_stats.keys(): match = reg.match(device) if match: network_stat = network_stats[device] results[device] = {} results[device]['rx_bytes'] = network_stat.bytes_recv results[device]['tx_bytes'] = network_stat.bytes_sent results[device]['rx_packets'] = network_stat.packets_recv results[device]['tx_packets'] = network_stat.packets_sent for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = '.'.join([device, s]) # Get Metric Value metric_value = self.derivative(metric_name, long(v), diamond.collector.MAX_COUNTER) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value=metric_value, unit='byte') for u in self.config['byte_unit']: # Public Converted Metric self.publish(metric_name.replace('bytes', u), convertor.get(unit=u), 2) else: # Publish Metric Derivative self.publish(metric_name, metric_value) return None
def collect(self): """ Collect network interface stats. """ # Initialize results results = {} if os.access(self.PROC, os.R_OK): # Open File with open(self.PROC) as file: # Build Regular Expression greed = '' if self.config['greedy'].lower() == 'true': greed = '\S*' exp = ( '^(?:\s*)((?:%s)%s):(?:\s*)' + '(?P<rx_bytes>\d+)(?:\s*)' + '(?P<rx_packets>\w+)(?:\s*)' + '(?P<rx_errors>\d+)(?:\s*)' + '(?P<rx_drop>\d+)(?:\s*)' + '(?P<rx_fifo>\d+)(?:\s*)' + '(?P<rx_frame>\d+)(?:\s*)' + '(?P<rx_compressed>\d+)(?:\s*)' + '(?P<rx_multicast>\d+)(?:\s*)' + '(?P<tx_bytes>\d+)(?:\s*)' + '(?P<tx_packets>\w+)(?:\s*)' + '(?P<tx_errors>\d+)(?:\s*)' + '(?P<tx_drop>\d+)(?:\s*)' + '(?P<tx_fifo>\d+)(?:\s*)' + '(?P<tx_colls>\d+)(?:\s*)' + '(?P<tx_carrier>\d+)(?:\s*)' + '(?P<tx_compressed>\d+)(?:.*)$') % ( ('|'.join(self.config['interfaces'])), greed) try: reg = re.compile(exp) # Match Interfaces for line in file: match = reg.match(line) if match: device = match.group(1) results[device] = match.groupdict() except Exception as e: self.log.error("Failed to parse file: {0!s}".format(e)) else: if not psutil: self.log.error('Unable to import psutil') self.log.error('No network metrics retrieved') return None network_stats = psutil.network_io_counters(True) for device in network_stats.keys(): network_stat = network_stats[device] results[device] = {} results[device]['rx_bytes'] = network_stat.bytes_recv results[device]['tx_bytes'] = network_stat.bytes_sent results[device]['rx_packets'] = network_stat.packets_recv results[device]['tx_packets'] = network_stat.packets_sent for device in results: stats = results[device] for s, v in stats.items(): # Get Metric Name metric_name = '.'.join(['net', s]) # Get Metric Value metric_value = long(v) # Convert rx_bytes and tx_bytes if s == 'rx_bytes' or s == 'tx_bytes': convertor = diamond.convertor.binary(value=metric_value, unit='byte') for u in self.config['byte_unit']: # Public Converted Metric self.dimensions = {'iface': device} self.publish_cumulative_counter(metric_name.replace( 'bytes', u), convertor.get(unit=u), precision=2) else: # Publish Metric Derivative self.dimensions = {'iface': device} self.publish_cumulative_counter(metric_name, metric_value) return None