def collect_partitions(self): disksresult = {} disk_partitions = psutil.disk_partitions(all=False) for partition in disk_partitions: # Help for this part - Official example : https://github.com/giampaolo/psutil/blob/master/scripts/disk_usage.py if os.name == 'nt': if 'cdrom' in partition.opts or partition.fstype == '': # skip cd-rom drives with no disk in it; they may raise # ENOENT, pop-up a Windows GUI error for a non-ready # partition or just hang. continue try: disk_usage = psutil.disk_usage(partition.mountpoint) diskresult = {} for key in disk_usage._fields: diskresult[key] = getattr(disk_usage, key) diskresult['mountpoint'] = partition.mountpoint diskresult['fstype'] = partition.fstype # Bytes to mb diskresult['total'] = float("{0:.2f}".format( helpers.bytes_to_mb(diskresult['total']))) diskresult['used'] = float("{0:.2f}".format( helpers.bytes_to_mb(diskresult['used']))) diskresult['free'] = float("{0:.2f}".format( helpers.bytes_to_mb(diskresult['free']))) disksresult[partition.device] = diskresult # Build the results except: pass # Ignore exception on disk return disksresult
def collect_io(self): results = {} disksdata_io = psutil.disk_io_counters(perdisk=True) for key, values in disksdata_io.items(): disk_io = disksdata_io[key] results[key] = { 'device': key, 'read_count': disk_io.read_count, 'write_count': disk_io.write_count, 'read_mb': float("{0:.2f}".format(helpers.bytes_to_mb( disk_io.read_bytes))), 'write_mb': float("{0:.2f}".format(helpers.bytes_to_mb( disk_io.write_bytes))), 'read_time_sec': disk_io.read_time if float("{0:.2f}".format( helpers.ms_to_s(disk_io.read_time))) else None, 'write_time_sec': disk_io.write_time if float("{0:.2f}".format( helpers.ms_to_s(disk_io.write_time))) else None, } return results
def collect_overall_disk_usage(self): disk_usage = psutil.disk_usage('/') result = { 'total': (helpers.bytes_to_mb(disk_usage.total)), 'used': (helpers.bytes_to_mb(disk_usage.used)), 'free': (helpers.bytes_to_mb(disk_usage.free)), 'percent': disk_usage.percent, } return result
def collect(self): data = {} memory = psutil.virtual_memory() for key in memory._fields: data[key] = getattr(memory, key) if key != "percent": data[key] = float("{0:.2f}".format( helpers.bytes_to_mb(data[key]))) # Transform bytes to mb return data
def collect(self): data = {} swap = psutil.swap_memory() for key in swap._fields: data[key] = getattr(swap, key) if key != "percent": data[key] = float("{0:.2f}".format( helpers.bytes_to_mb(data[key]))) # Transform bytes to mb return data
def collect_network_addrs(self): network_stats = psutil.net_io_counters(pernic=True) # True -> return the same information for every physical disk installed net_interfaces = psutil.net_if_addrs() results = {} for key, values in net_interfaces.items(): net_stats = network_stats[key] results[key] = { 'interface': key, 'address': values[0].address, 'bytes_sent_mb': float("{0:.2f}".format(helpers.bytes_to_mb(net_stats.bytes_sent))), 'bytes_received_mb': float("{0:.2f}".format(helpers.bytes_to_mb(net_stats.bytes_recv))), 'packets_sent': net_stats.packets_sent, 'packets_received': net_stats.packets_recv, 'err_received': net_stats.errin, # total number of errors while receiving 'err_sent': net_stats.errout, # total number of errors while sending 'err_packets_sent': net_stats.dropin, #total number of incoming packets which were dropped 'err_packets_received': net_stats.dropout # total number of outgoing packets which were dropped (always 0 on macOS and BSD) } return results
def collect(self): processes = [] # Retrieves only processes that are running running_processes = [ (process.info) for process in psutil.process_iter(attrs=[ 'pid', 'ppid', 'name', 'username', 'exe', 'cmdline', 'cpu_percent', 'memory_percent', 'status' ]) if process.info['status'] == psutil.STATUS_RUNNING ][self.DEFAULT_NB_OF_RETURNED_RESULTS:] #Limit to 30 processes for process in running_processes: try: process['cmdline'] = ' '.join( process['cmdline']).strip() #join args if 'cpu_percent' in process and process[ 'cpu_percent'] is not None: process['cpu_percent'] = float("{0:.2f}".format( process['cpu_percent'])) # Init process memory usage if 'memory_percent' in process and process[ 'memory_percent'] is not None: # The RAM used by a process is recovered based on the total RAM available for the server where the agent is installed total_memory = psutil.virtual_memory().total process['memory_used_mb'] = float("{0:.2f}".format( helpers.bytes_to_mb(((total_memory / 100) * process['memory_percent'])))) process['memory_percent'] = float("{0:.2f}".format( process['memory_percent'])) except psutil.NoSuchProcess: # https://psutil.readthedocs.io/en/latest/#psutil.NoSuchProcess pass except psutil.AccessDenied: # https://psutil.readthedocs.io/en/latest/#psutil.AccessDenied pass except: #default exception pass else: processes.append(process) return processes