Skip to content

Regex fix for edge cases #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gemini2463
Copy link

The script tries to match lines with this regex in the read_logs function:
($user, $cpu, $memory, $command) = $l =~ m{^(\w+)\s+\d+\s+(\d{1,2}.\d)\s+(\d{1,2}.\d).\d{1,2}:\d{2}\s+(.)$};

Problems:
(\w+) only matches word characters, but user:20 in ps pads usernames with spaces.
The %CPU field could be >99, so (\d{1,2}.\d) will not match '100' or higher. It expects a decimal like 2.3.
The ps output for %CPU is integer (not decimal) for large values.
The line starts with the process name, plus spaces.

Fixed with:
$l =~ s/^\s+//; # remove leading spaces
my @fields = split /\s+/, $l, 11;
my ($user, $pid, $cpu, $memory, $vsz, $rss, $tty, $stat, $start, $time, $command) = @fields;

…tion:

($user, $cpu, $memory, $command) = $l =~ m{^(\w+)\s+\d+\s+(\d{1,2}\.\d)\s+(\d{1,2}\.\d).*\d{1,2}:\d{2}\s+(.*)$};

Problems:
(\w+) only matches word characters, but user:20 in ps pads usernames with spaces.
The %CPU field could be >99, so (\d{1,2}\.\d) will not match '100' or higher. It expects a decimal like 2.3.
The ps output for %CPU is integer (not decimal) for large values.
The line starts with the process name, plus spaces.

Fixed with:
$l =~ s/^\s+//; # remove leading spaces
my @fields = split /\s+/, $l, 11;
my ($user, $pid, $cpu, $memory, $vsz, $rss, $tty, $stat, $start, $time, $command) = @fields;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant