import repository from arizona
[raven.git] / owl / client / scripts.d / processes
1 #!/usr/bin/python
2
3 import subprocess
4
5 def to_float(x):
6     try:
7         return float(x)
8     except:
9         return 0
10
11 def to_int(x):
12     try:
13         return int(x)
14     except:
15         return 0
16
17 ps_process = subprocess.Popen(["ps", "aex", "-o", "pid,%cpu,%mem,rss,sz,args"], stdout=subprocess.PIPE)
18 output = ps_process.communicate()[0]
19
20 # sample output
21 #   PID %CPU %MEM   RSS    SZ COMMAND
22 #  2087  0.0  0.0   360   436 /sbin/mingetty tty1
23
24 count = 0
25 total_mem = 0.0
26 total_cpu = 0.0
27 total_rss = 0
28 total_sz = 0
29
30 for line in output.split("\n")[1:]:
31     parts = line.split()
32     if len(parts)>5:
33         pid = parts[0]
34         cpu = parts[1]
35         mem = parts[2]
36         rss = parts[3]
37         sz = parts[4]
38         args = " ".join(parts[5:])
39
40         count += 1
41         total_cpu += to_float(cpu)
42         total_mem += to_float(mem)
43         total_rss += to_int(rss)
44         total_sz += to_int(sz)
45
46 print "[processes]"
47 print "count =", count
48 print "mempct =", int(total_mem)
49 print "cpupct =", int(total_cpu)
50 print "rss =", total_rss
51 print "sz =", total_sz
52