Skip to content

Instantly share code, notes, and snippets.

@turicas
Created June 7, 2012 19:46
Show Gist options
  • Save turicas/2891134 to your computer and use it in GitHub Desktop.
Save turicas/2891134 to your computer and use it in GitHub Desktop.
Monitoring information about host/OS and some processes (used at PyPLN's broker)
#!/usr/bin/env python
# coding: utf-8
import socket
from time import time
import psutil
def get_outgoing_ip((host, port)):
"""Connect to remote host/port, return local IP used by OS"""
raw_socket = socket.socket(socket.AF_INET)
raw_socket.connect((host, port))
data = raw_socket.getsockname()
raw_socket.close()
return data[0]
def get_host_info():
"""Return a ``dict`` with system's information
`Example of its output <https://gist.github.com/gists/2891134>`_
"""
memory_usage = psutil.phymem_usage()
cached_memory = psutil.cached_phymem()
buffered_memory = psutil.phymem_buffers()
real_used = memory_usage.used - buffered_memory - cached_memory
real_free = memory_usage.total - real_used
percent = 100 * (float(memory_usage.used) / memory_usage.total)
real_percent = 100 * (float(real_used) / memory_usage.total)
virtual_used = psutil.used_virtmem()
virtual_free = psutil.avail_virtmem()
virtual_total = virtual_used + virtual_free
info_per_nic = psutil.network_io_counters(pernic=True)
network_info = {}
for key, value in info_per_nic.iteritems():
network_info[key] = {'bytes sent': value.bytes_sent,
'bytes received': value.bytes_recv,
'packets sent': value.packets_sent,
'packets received': value.packets_recv,}
partitions = psutil.disk_partitions()
storage_info = {}
for partition in partitions:
disk_usage = psutil.disk_usage(partition.mountpoint)
storage_info[partition.device] = {'mount point': partition.mountpoint,
'file system': partition.fstype,
'total bytes': disk_usage.total,
'total used bytes': disk_usage.used,
'total free bytes': disk_usage.free,
'percent used': disk_usage.percent,}
return {'memory': {'free': memory_usage.free,
'total': memory_usage.total,
'used': memory_usage.used,
'cached': cached_memory,
'buffers': buffered_memory,
'real used': real_used,
'real free': real_free,
'percent': percent,
'real percent': real_percent,
'total virtual': virtual_total,
'used virtual': virtual_used,
'free virtual': virtual_free,},
'cpu': {'number of cpus': psutil.NUM_CPUS,
'cpu percent': psutil.cpu_percent(),},
'network': {'interfaces': network_info,},
'storage': storage_info,
'uptime': time() - psutil.BOOT_TIME,}
def get_process_info(process_id):
"""Return CPU and memory information for a given PID"""
try:
process = psutil.Process(process_id)
except psutil.error.NoSuchProcess:
return None
memory_info = process.get_memory_info()
return {'cpu percent': process.get_cpu_percent(),
'resident memory': memory_info.rss,
'virtual memory': memory_info.vms,
'pid': process.pid,
'started at': process.create_time,}
if __name__ == '__main__':
from pprint import pprint
from time import time
from os import getpid
host_info = get_host_info()
host_info['network']['cluster ip'] = get_outgoing_ip(('localhost', 80))
broker_info = get_process_info(getpid())
broker_info['type'] = 'broker'
broker_info['active workers'] = 4
processes = [broker_info]
for job in range(broker_info['active workers']):
worker_info = get_process_info(getpid())
if worker_info is not None:
worker_info['worker'] = 'the worker is a lie'
worker_info['document id'] = '...'
worker_info['type'] = 'worker'
processes.append(worker_info)
data = {'host': host_info, 'processes': processes, 'timestamp': time()}
pprint(data)
{'host': {'cpu': {'cpu percent': 4.9, 'number of cpus': 4},
'memory': {'buffers': 214372352L,
'cached': 919220224,
'free': 1369661440L,
'free virtual': 0,
'percent': 65.21955293723627,
'real free': 2503254016L,
'real percent': 36.433711831634305,
'real used': 1434767360L,
'total': 3938021376L,
'total virtual': 0,
'used': 2568359936L,
'used virtual': 0},
'network': {'cluster ip': '127.0.0.1',
'interfaces': {'eth0': {'bytes received': 171472224,
'bytes sent': 12556068,
'packets received': 1527830,
'packets sent': 61180},
'eth1': {'bytes received': 74551186,
'bytes sent': 171524,
'packets received': 524231,
'packets sent': 1130},
'lo': {'bytes received': 483510,
'bytes sent': 483510,
'packets received': 4373,
'packets sent': 4373},
'teredo': {'bytes received': 0,
'bytes sent': 0,
'packets received': 0,
'packets sent': 0}}},
'storage': {'/dev/sda3': {'file system': 'ext4',
'mount point': '/',
'percent used': 70.8,
'total bytes': 399561412608,
'total free bytes': 96656392192,
'total used bytes': 282904956928}},
'uptime': 17135.324898004532},
'processes': [{'active workers': 4,
'cpu percent': 0.0,
'pid': 5505,
'resident memory': 7438336,
'started at': 1339540467.82,
'type': 'broker',
'virtual memory': 46899200},
{'cpu percent': 0.0,
'document id': '...',
'pid': 5505,
'resident memory': 7467008,
'started at': 1339540467.82,
'type': 'worker',
'virtual memory': 46899200,
'worker': 'the worker is a lie'},
{'cpu percent': 0.0,
'document id': '...',
'pid': 5505,
'resident memory': 7471104,
'started at': 1339540467.82,
'type': 'worker',
'virtual memory': 46899200,
'worker': 'the worker is a lie'},
{'cpu percent': 0.0,
'document id': '...',
'pid': 5505,
'resident memory': 7471104,
'started at': 1339540467.82,
'type': 'worker',
'virtual memory': 46899200,
'worker': 'the worker is a lie'},
{'cpu percent': 0.0,
'document id': '...',
'pid': 5505,
'resident memory': 7471104,
'started at': 1339540467.82,
'type': 'worker',
'virtual memory': 46899200,
'worker': 'the worker is a lie'}],
'timestamp': 1339540468.831705}
@fccoelho
Copy link

fccoelho commented Jun 7, 2012

@turicas: we are missing a time stamp...

@turicas
Copy link
Author

turicas commented Jun 8, 2012

@fccoelho: fixed.

@fccoelho
Copy link

@turicas
Copy link
Author

turicas commented Jun 12, 2012

@fccoelho, thanks! Done. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment