This code seems to work. I added CPU idle percentage, just because I think it's useful to know how little idle processor you have left, so you know when you need to upgrade a server, or if you should run an intense process.

sub userMemCPUpercNidle { my @usernames = @_; my ($usercpu, $usermem, $cpuidle); # return values foreach my $username (@usernames){ my $top = `top -b -n 1 -u $username`; # gets top from the system $top =~ m/(\d+\.\d+)\%id/; # matches CPU idle time $cpuidle = $1; my @lines = split(/\n/, $top); # split each line into an array val +ue my ($cpucol,$memcol); foreach my $line (@lines){ if ($line =~ m/\%CPU/){ # checks if it's collumb headers my $colcount = 0; foreach my $col (split(' ', $line)){ if ($col eq '%CPU') { $cpucol = $colcount; # number of col that is the CPU val f +or process } elsif ($col eq '%MEM') { $memcol = $colcount; # number of col that is the mem val f +or process } if (defined($cpucol) && defined($memcol)){ last; } else { $colcount++; } } } else { if (!defined($cpucol) && !defined($memcol)){ $linecount++; } else { my @values = split(/\s+/, $line); $usercpu += $values[$cpucol]; # add process cpu percent to t +otal $usermem += $values[$memcol]; # add process memory percent t +o total } } } } return $usermem, $usercpu, $cpuidle; }

Edit: This code was mainly written just to get it working. I didn't try to optimize it or think about how to do it better, just thought how it might be possible and did it to get it working. After looking at it later I noticed at least one major mistake. I tried to add the capability to check multiple users when first writing it, but then forgot that I had while I was writing. Will update with the code I am working on.


In reply to Re: Percent of CPU/Mem Usage for User by geekt
in thread Percent of CPU/Mem Usage for User by geekt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.