def update(self): """Update network stats using the input method. Stats is a list of dict (one dict per interface) """ # Init new stats stats = self.get_init_value() if self.input_method == 'local': # Update stats using the standard system lib # Grab network interface stat using the psutil net_io_counter method try: netiocounters = psutil.net_io_counters(pernic=True) except UnicodeDecodeError as e: logger.debug('Can not get network interface counters ({})'.format(e)) return self.stats # Grab interface's status (issue #765) # Grab interface's speed (issue #718) netstatus = {} try: netstatus = psutil.net_if_stats() except OSError as e: # see psutil #797/glances #1106 logger.debug('Can not get network interface status ({})'.format(e)) # Previous network interface stats are stored in the network_old variable if not hasattr(self, 'network_old'): # First call, we init the network_old var try: self.network_old = netiocounters except (IOError, UnboundLocalError): pass return self.stats # By storing time data we enable Rx/s and Tx/s calculations in the # XML/RPC API, which would otherwise be overly difficult work # for users of the API time_since_update = getTimeSinceLastUpdate('net') # Loop over interfaces network_new = netiocounters for net in network_new: # Do not take hidden interface into account # or KeyError: 'eth0' when interface is not connected #1348 if self.is_hide(net) or net not in netstatus: continue try: cumulative_rx = network_new[net].bytes_recv cumulative_tx = network_new[net].bytes_sent cumulative_cx = cumulative_rx + cumulative_tx rx = cumulative_rx - self.network_old[net].bytes_recv tx = cumulative_tx - self.network_old[net].bytes_sent cx = rx + tx netstat = {'interface_name': n(net), 'time_since_update': time_since_update, 'cumulative_rx': cumulative_rx, 'rx': rx, 'cumulative_tx': cumulative_tx, 'tx': tx, 'cumulative_cx': cumulative_cx, 'cx': cx, # Interface status 'is_up': netstatus[net].isup, # Interface speed in Mbps, convert it to bps # Can be always 0 on some OSes 'speed': netstatus[net].speed * 1048576, # Set the key for the dict 'key': self.get_key() } except KeyError: continue else: # Append the interface stats to the list stats.append(netstat) # Save stats to compute next bitrate self.network_old = network_new elif self.input_method == 'snmp': # Update stats using SNMP # SNMP bulk command to get all network interface in one shot try: netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) except KeyError: netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid['default'], bulk=True) # Previous network interface stats are stored in the network_old variable if not hasattr(self, 'network_old'): # First call, we init the network_old var try: self.network_old = netiocounters except (IOError, UnboundLocalError): pass else: # See description in the 'local' block time_since_update = getTimeSinceLastUpdate('net') # Loop over interfaces network_new = netiocounters for net in network_new: # Do not take hidden interface into account if self.is_hide(net): continue try: # Windows: a tips is needed to convert HEX to TXT # http://blogs.technet.com/b/networking/archive/2009/12/18/how-to-query-the-list-of-network-interfaces-using-snmp-via-the-ifdescr-counter.aspx if self.short_system_name == 'windows': try: interface_name = str(base64.b16decode(net[2:-2].upper())) except TypeError: interface_name = net else: interface_name = net cumulative_rx = float(network_new[net]['cumulative_rx']) cumulative_tx = float(network_new[net]['cumulative_tx']) cumulative_cx = cumulative_rx + cumulative_tx rx = cumulative_rx - float(self.network_old[net]['cumulative_rx']) tx = cumulative_tx - float(self.network_old[net]['cumulative_tx']) cx = rx + tx netstat = { 'interface_name': interface_name, 'time_since_update': time_since_update, 'cumulative_rx': cumulative_rx, 'rx': rx, 'cumulative_tx': cumulative_tx, 'tx': tx, 'cumulative_cx': cumulative_cx, 'cx': cx} except KeyError: continue else: netstat['key'] = self.get_key() stats.append(netstat) # Save stats to compute next bitrate self.network_old = network_new # Update the stats self.stats = stats return self.stats
def update(self): """Update disk I/O stats using the input method.""" # Init new stats stats = self.get_init_value() if self.input_method == 'local': # Update stats using the standard system lib # Grab the stat using the psutil disk_io_counters method # read_count: number of reads # write_count: number of writes # read_bytes: number of bytes read # write_bytes: number of bytes written # read_time: time spent reading from disk (in milliseconds) # write_time: time spent writing to disk (in milliseconds) try: diskiocounters = psutil.disk_io_counters(perdisk=True) except Exception: return stats # Previous disk IO stats are stored in the diskio_old variable if not hasattr(self, 'diskio_old'): # First call, we init the diskio_old var try: self.diskio_old = diskiocounters except (IOError, UnboundLocalError): pass else: # By storing time data we enable Rx/s and Tx/s calculations in the # XML/RPC API, which would otherwise be overly difficult work # for users of the API time_since_update = getTimeSinceLastUpdate('disk') diskio_new = diskiocounters for disk in diskio_new: # By default, RamFS is not displayed (issue #714) if self.args is not None and not self.args.diskio_show_ramfs and disk.startswith('ram'): continue # Do not take hide disk into account if self.is_hide(disk): continue # Compute count and bit rate try: read_count = (diskio_new[disk].read_count - self.diskio_old[disk].read_count) write_count = (diskio_new[disk].write_count - self.diskio_old[disk].write_count) read_bytes = (diskio_new[disk].read_bytes - self.diskio_old[disk].read_bytes) write_bytes = (diskio_new[disk].write_bytes - self.diskio_old[disk].write_bytes) diskstat = { 'time_since_update': time_since_update, 'disk_name': n(disk), 'read_count': read_count, 'write_count': write_count, 'read_bytes': read_bytes, 'write_bytes': write_bytes} # Add alias if exist (define in the configuration file) if self.has_alias(disk) is not None: diskstat['alias'] = self.has_alias(disk) except KeyError: continue else: diskstat['key'] = self.get_key() stats.append(diskstat) # Save stats to compute next bitrate self.diskio_old = diskio_new elif self.input_method == 'snmp': # Update stats using SNMP # No standard way for the moment... pass # Update the stats self.stats = stats return self.stats