Skip to content

Comments

Regex fix for edge cases#5

Open
gemini2463 wants to merge 1 commit intoCpanelInc:masterfrom
gemini2463:master
Open

Regex fix for edge cases#5
gemini2463 wants to merge 1 commit intoCpanelInc:masterfrom
gemini2463:master

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