Package pypln :: Package utils :: Module monitoring
[hide private]

Source Code for Module pypln.utils.monitoring

 1  #!/usr/bin/env python 
 2  # coding: utf-8 
 3   
 4  import socket 
 5  from time import time 
 6  import psutil 
 7   
 8   
9 -def get_outgoing_ip((host, port)):
10 raw_socket = socket.socket(socket.AF_INET) 11 raw_socket.connect((host, port)) 12 data = raw_socket.getsockname() 13 raw_socket.close() 14 return data
15
16 -def get_host_info():
17 memory_usage = psutil.phymem_usage() 18 cached_memory = psutil.cached_phymem() 19 buffered_memory = psutil.phymem_buffers() 20 real_used = memory_usage.used - buffered_memory - cached_memory 21 real_free = memory_usage.total - real_used 22 percent = 100 * (float(memory_usage.used) / memory_usage.total) 23 real_percent = 100 * (float(real_used) / memory_usage.total) 24 virtual_used = psutil.used_virtmem() 25 virtual_free = psutil.avail_virtmem() 26 virtual_total = virtual_used + virtual_free 27 info_per_nic = psutil.network_io_counters(pernic=True) 28 network_info = {} 29 for key, value in info_per_nic.iteritems(): 30 network_info[key] = {'bytes sent': value.bytes_sent, 31 'bytes received': value.bytes_recv, 32 'packets sent': value.packets_sent, 33 'packets received': value.packets_recv,} 34 partitions = psutil.disk_partitions() 35 storage_info = {} 36 for partition in partitions: 37 disk_usage = psutil.disk_usage(partition.mountpoint) 38 storage_info[partition.device] = {'mount point': partition.mountpoint, 39 'file system': partition.fstype, 40 'total bytes': disk_usage.total, 41 'total used bytes': disk_usage.used, 42 'total free bytes': disk_usage.free, 43 'percent used': disk_usage.percent,} 44 return {'memory': {'free': memory_usage.free, 45 'total': memory_usage.total, 46 'used': memory_usage.used, 47 'cached': cached_memory, 48 'buffers': buffered_memory, 49 'real used': real_used, 50 'real free': real_free, 51 'percent': percent, 52 'real percent': real_percent, 53 'total virtual': virtual_total, 54 'used virtual': virtual_used, 55 'free virtual': virtual_free,}, 56 'cpu': {'number of cpus': psutil.NUM_CPUS, 57 'cpu percent': psutil.cpu_percent(),}, 58 'network': {'interfaces': network_info,}, 59 'storage': storage_info, 60 'uptime': time() - psutil.BOOT_TIME,}
61
62 -def get_process_info(process_id):
63 try: 64 process = psutil.Process(process_id) 65 except psutil.error.NoSuchProcess: 66 return None 67 memory_info = process.get_memory_info() 68 return {'cpu percent': process.get_cpu_percent(), 69 'resident memory': memory_info.rss, 70 'virtual memory': memory_info.vms, 71 'pid': process.pid, 72 'started at': process.create_time,}
73