Created
July 6, 2015 17:41
-
-
Save ipmb/aaf068b8aa5906c7949e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import time | |
import psutil | |
PREFIX = [ | |
'server', | |
socket.getfqdn().replace('.', '_'), | |
] | |
def _to_graphite(keys, value, prefix=None): | |
# copy PREFIX to avoid modifying the global | |
stat_list = prefix or list(PREFIX) | |
stat_list.extend(keys) | |
return '{} {} {}'.format('.'.join(stat_list), | |
value, | |
int(time.time())) | |
def output(*args, **kwargs): | |
print(_to_graphite(*args, **kwargs)) | |
def cpu(): | |
prefix = 'cpu' | |
for key, value in psutil.cpu_times()._asdict().items(): | |
output([prefix, key], value) | |
for cpu, times in enumerate(psutil.cpu_times(percpu=True)): | |
for key, value in times._asdict().items(): | |
output([prefix, str(cpu), key], value) | |
output([prefix, 'percent'], psutil.cpu_percent()) | |
for cpu, value in enumerate(psutil.cpu_percent(percpu=True)): | |
output([prefix, str(cpu), 'percent'], value) | |
output([prefix, 'count'], psutil.cpu_count()) | |
def memory(): | |
for key, value in psutil.virtual_memory()._asdict().items(): | |
output(['mem', key], value) | |
for key, value in psutil.swap_memory()._asdict().items(): | |
output(['swap', key], value) | |
def disk(): | |
prefix = 'disk' | |
for partition in psutil.disk_partitions(): | |
for key, value in psutil.disk_usage(partition.mountpoint)._asdict().items(): | |
output([prefix, partition.device.split('/')[-1], key], value) | |
for disk, counters in psutil.disk_io_counters(perdisk=True).items(): | |
for key, value in counters._asdict().items(): | |
output([prefix, disk, key], value) | |
def network(): | |
prefix = 'net' | |
for nic, counters in psutil.net_io_counters(pernic=True).items(): | |
for key, value in counters._asdict().items(): | |
output([prefix, nic, key], value) | |
def misc(): | |
output(['uptime'], psutil.boot_time()) | |
output(['users'], len(psutil.users())) | |
output(['processes'], len(psutil.pids())) | |
if __name__ == '__main__': | |
cpu() | |
memory() | |
disk() | |
network() | |
misc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment