| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | # This munin plugin isolates processes by looking for the 'pid' file created |
|---|
| 4 | # by 'allmydata start', then extracts the amount of memory they consume (both |
|---|
| 5 | # VmSize and VmRSS) from /proc |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | import os, sys, re |
|---|
| 9 | |
|---|
| 10 | # for testing |
|---|
| 11 | # os.environ["nodememory_warner1"] = "run/warner1" |
|---|
| 12 | # os.environ["nodememory_warner2"] = "run/warner2" |
|---|
| 13 | |
|---|
| 14 | nodedirs = [] |
|---|
| 15 | for k,v in os.environ.items(): |
|---|
| 16 | if k.startswith("nodememory_"): |
|---|
| 17 | nodename = k[len("nodememory_"):] |
|---|
| 18 | nodedirs.append((nodename, v)) |
|---|
| 19 | nodedirs.sort(lambda a,b: cmp(a[0],b[0])) |
|---|
| 20 | |
|---|
| 21 | pids = {} |
|---|
| 22 | |
|---|
| 23 | for node,nodedir in nodedirs: |
|---|
| 24 | pidfile = os.path.join(nodedir, "twistd.pid") |
|---|
| 25 | if os.path.exists(pidfile): |
|---|
| 26 | pid = int(open(pidfile,"r").read()) |
|---|
| 27 | pids[node] = pid |
|---|
| 28 | |
|---|
| 29 | fields = ["VmSize", "VmRSS"] |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | if len(sys.argv) > 1: |
|---|
| 33 | if sys.argv[1] == "config": |
|---|
| 34 | configinfo = \ |
|---|
| 35 | """graph_title Memory Consumed by Nodes |
|---|
| 36 | graph_vlabel bytes |
|---|
| 37 | graph_category Tahoe |
|---|
| 38 | graph_info This graph shows the memory used by specific processes |
|---|
| 39 | """ |
|---|
| 40 | for nodename,nodedir in nodedirs: |
|---|
| 41 | for f in fields: |
|---|
| 42 | configinfo += "%s_%s.label %s used by %s\n" % (nodename, f, |
|---|
| 43 | f, nodename) |
|---|
| 44 | linetype = "LINE1" |
|---|
| 45 | if f == "VmSize": |
|---|
| 46 | linetype = "LINE2" |
|---|
| 47 | configinfo += "%s_%s.draw %s\n" % (nodename, f, linetype) |
|---|
| 48 | if f == "VmData": |
|---|
| 49 | configinfo += "%s_%s.graph no\n" % (nodename, f) |
|---|
| 50 | |
|---|
| 51 | print(configinfo) |
|---|
| 52 | sys.exit(0) |
|---|
| 53 | |
|---|
| 54 | nodestats = {} |
|---|
| 55 | for node,pid in pids.items(): |
|---|
| 56 | stats = {} |
|---|
| 57 | statusfile = "/proc/%s/status" % pid |
|---|
| 58 | if not os.path.exists(statusfile): |
|---|
| 59 | continue |
|---|
| 60 | for line in open(statusfile,"r").readlines(): |
|---|
| 61 | for f in fields: |
|---|
| 62 | if line.startswith(f + ":"): |
|---|
| 63 | m = re.search(r'(\d+)', line) |
|---|
| 64 | stats[f] = int(m.group(1)) |
|---|
| 65 | nodestats[node] = stats |
|---|
| 66 | |
|---|
| 67 | for node,stats in nodestats.items(): |
|---|
| 68 | for f,value in stats.items(): |
|---|
| 69 | # TODO: not sure if /proc/%d/status means 1000 or 1024 when it says |
|---|
| 70 | # 'kB' |
|---|
| 71 | print("%s_%s.value %d" % (node, f, 1024*value)) |
|---|