0

We've recently restructured part of our web hosting setup; splitting a series of applications over a number of users on a single (virtual) machine, to better be able to spot resource usage problems and the like.

At this juncture, however, the problem is that while if we have a candidate for troublesome behaviour this can be confirmed readily using top -u, a method for getting a top-like display of resource usage grouped by user has thus far been elusive.

Any suggestions?

Edit:

To elaborate:

I don't want to see each and every process line (there are quite a few; up to 15 per user in extreme cases); I want to see one line per user; with the cumulative resource usage for that user, and one such line for every user.

  • You could easily script something in perl, or your favorite text-parsing language, that would take the output of **top -b** and show you the per-user totals. – Flimzy Jun 29 '11 at 08:57

1 Answers1

1

Alright... because I was bored, and it seemed like a fun challenge, I wrote this script which I think will do what you want. I have tested its basic functionality to work, and it is intended to be flexible, so you could tell top to output only the fields you care about, and it ought to adapt properly. But it undoubtedly does not handle every case of formatting output from every version of top.

per-user-top

Sample output, from my own workstation:

      USER       VIRT        RES        SHR       %CPU       %MEM      TIME+      procs
       ntp        41m        580        444          0          0    0:02.93          1
  haldaemo        69m         3m         2m          0        0.1    0:01.85          2
   jonhall        12g       803m       230m          4       13.3  134:24.09         47
  postgres       471m         1m        788          0          0    0:15.24          5
   postfix       117m         7m         6m          0        0.1    0:00.02          3
     statd        14m         16          8          0          0    0:00.00          1
      root         2g       473m        18m          0        7.7  108:18.75        126
     avahi        66m        784        628          0          0    0:00.15          2
   dovecot        12m         96         48          0          0    0:00.00          1
    daemon        26m         56         32          0          0    0:00.00          2
  messageb        23m         1m        812          0          0    0:01.74          1
Flimzy
  • 2,454
  • 18
  • 26
  • That is indeed a nice script; thanks. :) But it is interacting rather, … oddly with `watch`. (Run it yourself as `watch -t -n 1 put.pl` to see what I mean.) It's as if it diffs intermediate results, or something? Or is that something in the script? I couldn't immediately find anything to that effect, but… – Williham Totland Jun 29 '11 at 10:49
  • That is strange. I've never used watch. I'll see if I have time to play with that tonight. – Flimzy Jun 29 '11 at 23:05
  • Also note that my script is not perfectly accurate--probably no big deal for your use cases. But it will suffer from lots of rounding errors, since it takes the rounded values from top, then rounds them again. For greater accuracy, you'd probably have to read from /proc directly. – Flimzy Jun 29 '11 at 23:06