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 value 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 for process } elsif ($col eq '%MEM') { $memcol = $colcount; # number of col that is the mem val for 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 total $usermem += $values[$memcol]; # add process memory percent to total } } } } return $usermem, $usercpu, $cpuidle; }