Last active
September 19, 2018 14:52
-
-
Save gmlp/7db05c278ae25001a66974c875f400d7 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
import psutil | |
import statsd | |
import time | |
import argparse | |
from multiprocessing import Pool | |
PARSER = argparse.ArgumentParser() | |
PARSER.add_argument("-p", "--port", | |
type=int, | |
help="Port, default [8125]", default="8125") | |
PARSER.add_argument("-s", "--server", | |
help="Server IP, 127.0.0.1 by default", | |
default="127.0.0.1") | |
PARSER.add_argument("-i", "--interval", | |
help="Interval time, 10 by default", | |
default="10") | |
PARSER.add_argument("-n", "--hostname", | |
help="hostname, localhost by default", | |
default="localhost") | |
PARSER.add_argument("-t", "--processes", | |
help=" max processes, 4 by default", | |
default="4") | |
ARGS = PARSER.parse_args() | |
PORT = ARGS.port | |
SERVER = ARGS.server | |
INTERVAL = int(ARGS.interval) | |
HOSTNAME = ARGS.hostname | |
PROCESSES = int(ARGS.processes) | |
client = statsd.StatsClient(SERVER, PORT, | |
prefix='hostname.{}.system.memory'.format(HOSTNAME)) | |
def get_memory(pid): | |
ps = psutil.Process(pid) | |
with ps.oneshot(): | |
mem = ps.memory_info() | |
client.gauge('pid.{}.rss'.format(pid), mem.rss) | |
client.gauge('pid.{}.vms'.format(pid), mem.vms) | |
client.gauge('pid.{}.shared'.format(pid), mem.shared) | |
client.gauge('pid.{}.text'.format(pid), mem.text) | |
client.gauge('pid.{}.lib'.format(pid), mem.lib) | |
client.gauge('pid.{}.data'.format(pid), mem.data) | |
client.gauge('pid.{}.dirty'.format(pid), mem.dirty) | |
if __name__ == "__main__": | |
pool = Pool(processes=PROCESSES) | |
while True: | |
pool.map(get_memory, psutil.pids()) | |
time.sleep(INTERVAL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment